How to install SVN on linux

You have to have root permission

switch to root

sudo su root

then

yum install subversion
Tagged with: , ,
Posted in Linux

How to find external IP address of your linux box

curl http://ipecho.net/plain
Tagged with: , ,
Posted in Linux

Missing Spring Framework Web MVC Library (Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/mvc])

I ran into this following error when trying to load a sprint context file

Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/mvc]

and I had the following in my configuration.

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:task="http://www.springframework.org/schema/task" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:util="http://www.springframework.org/schema/util"

	xsi:schemaLocation="

http://www.springframework.org/schema/beans


http://www.springframework.org/schema/beans/spring-beans-3.0.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-3.0.xsd


http://www.springframework.org/schema/task


http://www.springframework.org/schema/task/spring-task-3.0.xsd

		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

....

	<mvc:annotation-driven />
...

it means I am missing the spring web mvc library.
adding the following dependency solved the problem

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>org.springframework.web.servlet</artifactId>
			<version>${springVersion}</version>
		</dependency>
Tagged with: ,
Posted in Spring Framework

Missing Spring Framework Plugin Declaration

I ran into an problem when loading spring web application, specifically, it complained about processing a spring bean xml file.

cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element ‘plugin:list’.

I have the following my spring context file.

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:plugin="http://www.springframework.org/schema/plugin"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd 
		   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
       	   http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
       	   http://www.springframework.org/schema/plugin http://www.springframework.org/schema/plugin/spring-plugin.xsd">
...
 <plugin:list id="remoteProductProviders" class="xxx.xxx.xxx.RemoteProductProvider" />
...

adding spring framework plugin dependency in the pom file solved the problem.

		<dependency>
			<groupId>org.springframework.plugin</groupId>
			<artifactId>spring-plugin-core</artifactId>
			<version>0.8.0.RELEASE</version>
		</dependency>
Tagged with: ,
Posted in Tomcat

Add Maven Dependent Libraries to Deployment Assembly in Eclipse

When you create a maven web project and start to add maven dependencies, the dependent jars are not assembled and deployed to your local tomcat server. Here is a simple step to add these libraries so every time you add dependencies to pom, they are deployed.
right click the project, and bring up the properties -> Deployment Assembly

eclipse-assemply

Click add

assemply-add

Then select Java Build Path Entries -> Maven Dependencies -> Finish

Tagged with: , , ,
Posted in Tools

Eclipse Luna SVN Console Empty

I have experienced an issue with eclipse luna, when I update or commit some changes, the svn console is empty, so I have no easy way to know what updates I got to my workspace or what commits I have made.
Looks like there is a setting, windows->preferences->Team->svn->Console, and by default the “show console automatically” is off (never). You need to change the option to “On Output”, and then you should see all the information in svn console

eclipse-svn

Tagged with: , ,
Posted in Eclipse

Eclipse tomcat java.lang.OutOfMemoryError PermGen space

I am sure you have experienced this problem here. I just came across this when trying to run a complicated web application in eclipse. If you google this error, most of the posts out there direct you to the eclipse.ini file. But increasing the perm max memory on eclipse did not help me. This issue was more a tomcat run time issue than eclipse issue, so modifying the launch arguments fixed the issue for me.

Let’s make it a full trouble shooting resolution by including the eclipse.ini file change here as well, because in some scenarios, this update helps.

1. eclipse.ini update

-startup
plugins/org.eclipse.equinox.launcher_1.3.0.v20140415-2008.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.200.v20140603-1326
-product
org.eclipse.epp.package.jee.product
--launcher.defaultAction
openFile
--launcher.XXMaxPermSize
4096M
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
4096m
--launcher.defaultAction
openFile
--launcher.appendVmargs
-vmargs
-Dosgi.requiredJavaVersion=1.6
-Xms40m
-Xmx4096m

2. update the launch arguments of tomcat server in eclipse by double clicking the server instance, and clicking opening launch configuration, and tabbing to “Arguments”, and append the following

-XX:PermSize=1024m -XX:MaxPermSize=1024m

eclipse-tomcat

Tagged with: , , ,
Posted in Eclipse, Tomcat

MYSQL conditional statement example (case when then)

MYSQL has very powerful set of control flow/conditional features, and case/when/then is one of these.
Assume that you need a table to store a generic value, with different value types, and for the sake of demo, let’s keep it simple. You have 3 types of values to store in this generic_table
type 1, string
type 2, integer
type 3, datetime

1. design and create this table

CREATE TABLE `generic_value` (
  `type` int(11) DEFAULT NULL,
  `string_value` varchar(45) DEFAULT NULL,
  `int_value` int(11) DEFAULT NULL,
  `time_value` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$

2. insert some sample values

insert into generic_value(type, string_value) values (1, "hello world");
insert into generic_value(type, int_value) values (2, 9);
insert into generic_value(type, time_value) values (3, "20140726")

3. use the mysql conditional to select the corresponding value out based on the type

select type,
case type
	when 1 then string_value
	when 2 then int_value
	when 3 then time_value
end as value
from generic_value
Tagged with: , , , , ,
Posted in database, MyBatis

How to remove trovi

The nasty virus can hijack your browsers, and changed your home page, new tab page, search engines, and bring up the annoying popups…
I have found one effect solution. Give it a try. It worked for me.

Go to www.bleepingcomputer.com
tab download, and search adware
download and run AdwCleaner
run the scan and clean, and complete them by rebooting your PC.

reset your browsers
1 firefox
help -> troubleshooting information -> reset firefox

firefox-reset

2. chrome
settings -> advanced settings -> reset browser settings

chrome-reset

3. IE
tools -> internet options -> advanced -> Reset

ie-reset

Good luck

Tagged with: , , , ,
Posted in Tools

Simple Example of Mybatis JAVA Maven Implementation 8 – Customized Type Handler

In the following example, we will show how to write a customized type handler. The type handler is a mybatis feature to allow you to intercept and update the information before the information is committed to the database, and/or after the information is retrieved from database but before it is being used by your application.

One example I am showing here, is that assume you maintain the user login information in database, and you want to encrypt the password before writing to the database and you want to decrypt it after retrieving from database.

1. create a user login table

delimiter $$

CREATE TABLE `user` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(45) NOT NULL,
  `password` varchar(45) NOT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1$$


2. create a user bean class

package com.springriver.example.mybatis.bean;

public class User {
	private int userId;
	private String userName;
	private String password;
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	
}

3. create the mapper interfaces and the manager class
UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.springriver.example.mybatis.mapper.UserMapper">


<select id="selectUserById" parameterType="int" resultType="User">
	select user_id as userId, user_name as userName, password from user where user_id = #{id}
</select>

<insert id="insertUser" parameterType="User" useGeneratedKeys="true" keyProperty="userId">
	insert into user(user_name, password) values (#{userName}, #{password})
</insert>



</mapper>

UserMapper.java

package com.springriver.example.mybatis.mapper;

import com.springriver.example.mybatis.bean.User;

public interface UserMapper {
	public User selectUserById(Integer id);
	public int insertUser(User user);
}

UserManager.java

package com.springriver.example.mybatis.util;

import com.springriver.example.mybatis.bean.User;
import com.springriver.example.mybatis.mapper.UserMapper;


public class UserManager {
	public static User selectUserById(final int id){
		return (User)new DatabaseOperation() {
			
			@Override
			public Object performAction() {
				// TODO Auto-generated method stub
				return executeMapperFunction(UserMapper.class, "selectUserById", id);
			}
		}.invoke();
	}
	
	public static int insertUser(final User user){
		return (Integer)new DatabaseOperation(true) {
			
			@Override
			public Object performAction() {
				// TODO Auto-generated method stub
				executeMapperFunction(UserMapper.class, "insertUser", user);
				return user.getUserId();
			}
		}.invoke();
	}
}

4. update the database-config.xml to include the new mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<typeAliases>
		<typeAlias type="com.springriver.example.mybatis.bean.Category" alias="Category"/>
		<typeAlias type="com.springriver.example.mybatis.bean.Product" alias="Product"/>	
		<typeAlias type="com.springriver.example.mybatis.bean.User" alias="User"/>	
	</typeAliases>
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="${driver}" />
				<property name="url" value="${url}" />
				<property name="username" value="${username}" />
				<property name="password" value="${password}" />
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<mapper resource="com/springriver/example/mybatis/mapper/CategoryMapper.xml" />
		<mapper resource="com/springriver/example/mybatis/mapper/ProductMapper.xml" />
		<mapper resource="com/springriver/example/mybatis/mapper/UserMapper.xml" />
	</mappers>
</configuration>

5. write a junit testing class to test the UserManager functions

package com.springriver.example.mybatis;

import org.junit.Test;

import com.springriver.example.mybatis.bean.User;
import com.springriver.example.mybatis.util.UserManager;

public class UserManagerTest {
	@Test
	public void testUser(){
		User user = new User();
		user.setUserName("jsmith");
		user.setPassword("donkey");
		int insertedUserId = UserManager.insertUser(user);
		User selectedUser = UserManager.selectUserById(insertedUserId);
		assert(selectedUser != null);
		assert(selectedUser.getUserName().equals(user.getUserName()));
	}
	
	
}

6. query the database table User to see the information you inserted.

result

you can see that the password is persisted to database as clear text.
Let’s write use type handler to encrypt the password before persisting to database and decrypt it after retrieving from database

7. write a util class to do AES encryption/decryption. refer to my tutorial on this topic.

package com.springriver.example.mybatis.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.springframework.core.io.ClassPathResource;

public class EncryptionUtil {
	private static SecretKeySpec skeySpec;
	
	static {	
		try {			
			ClassPathResource res = new ClassPathResource("key.key");
			if(res != null){
				File file = res.getFile();
				FileInputStream input = new FileInputStream(file);
				byte[] in = new byte[(int)file.length()];
				input.read(in);
				skeySpec = new SecretKeySpec(in, "AES");
				input.close();
			}
		}catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	public static byte[] encrypt(String input) 
			throws GeneralSecurityException, NoSuchPaddingException{
	       Cipher cipher = Cipher.getInstance("AES");

	       cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
	       return cipher.doFinal(input.getBytes());
		
	}
	
	
	public static String decrypt(byte[] input) throws GeneralSecurityException, NoSuchPaddingException{
		Cipher cipher = Cipher.getInstance("AES");
		cipher.init(Cipher.DECRYPT_MODE, skeySpec);
		return new String(cipher.doFinal(input));
	}
	
	

}

8. write a type handler

package com.springriver.example.mybatis.handler;

import java.security.GeneralSecurityException;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.crypto.NoSuchPaddingException;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import com.springriver.example.mybatis.util.EncryptionUtil;

public class PasswordTypeHandler implements TypeHandler<String> {

	public String getResult(ResultSet arg0, String arg1) throws SQLException {
		// TODO Auto-generated method stub
		String retValue = null;
		try {
			retValue = EncryptionUtil.decrypt(arg0.getBytes(arg1));
		} catch (NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (GeneralSecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return retValue;
	}

	public String getResult(ResultSet arg0, int arg1) throws SQLException {
		String retValue = null;
		try {
			retValue = EncryptionUtil.decrypt(arg0.getBytes(arg1));
		} catch (NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (GeneralSecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return retValue;
	}

	public String getResult(CallableStatement arg0, int arg1) throws SQLException {
		String retValue = null;
		try {
			retValue = EncryptionUtil.decrypt(arg0.getBytes(arg1));
		} catch (NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (GeneralSecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return retValue;
	}

	public void setParameter(PreparedStatement arg0, int arg1, String arg2,
			JdbcType arg3) throws SQLException {
	byte[] password = null;
		
		if (arg2 != null)
			try {
				password = EncryptionUtil.encrypt(arg2) ;
			} catch (NoSuchPaddingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (GeneralSecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		arg0.setBytes(arg1, password);
		
	}

}

9. update UserMapper.xml to apply the new TypeHandler to password field

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.springriver.example.mybatis.mapper.UserMapper">


<select id="selectUserById" parameterType="int" resultMap="UserMap">
	select user_id as userId, user_name as userName, password from user where user_id = #{id}
</select>

<insert id="insertUser" parameterType="User" useGeneratedKeys="true" keyProperty="userId">
	insert into user(user_name, password) values (#{userName}, 
		#{password,jdbcType=BINARY,javaType=java.lang.String,
		typeHandler=com.springriver.example.mybatis.handler.PasswordTypeHandler})
</insert>

<resultMap type="User" id="UserMap">
<result property="password" column="password" javaType="java.lang.String" jdbcType="BINARY" 
	typeHandler="com.springriver.example.mybatis.handler.PasswordTypeHandler"/>

</resultMap>


</mapper>

10 run the UserManagerTest again, and query the user table

result-after-encryption

you can see the password is stored as encrypted text in database.

download the source
springriver.mybatis.simple8

Tagged with: , , ,
Posted in MyBatis