Monday, May 28, 2012

MySQL dump and restore

#create dump file
mysqldump -u root -p db_test1 > test.db

#load dump file to database
mysql -u root -p db_test2 < test.db

if an error thrown :
ERROR 2006 (HY000) at line XX: MySQL server has gone away

find and uncomment/add with a value like below or higher :
max_allowed_packet = 16M 
located in my.ini(windows) or /etc/my.cnf(linux) and restart mysql server.

that error is usually because of the large dump file.

reference : http://icesquare.com/wordpress/error-2006-hy000-at-line-172-mysql-server-has-gone-away/

Wednesday, May 23, 2012

Error : Could not get the value for parameter encoding for plugin execution default-resources ...

This error happened when my friend tried to load a spring maven based project in her STS 2.9.1 IDE causing the dependencies jars were not able to be loaded. The error log was:
"
Could not get the value for parameter encoding for plugin execution default-resources Plugin org.apache.maven.plugins:maven-resources-plugin:2.4.3 or one of its dependencies could not be resolved: Failure to transfer org.apache.maven.doxia:Doxia-sink-api:jar:1.0-alpha-7 from http://repo1.maven.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interv..."


Tried to Google but no answer found. Finally we got the answer. It was due to an older version of STS 2.5.x already installed in the machine. But not sure how they are related since I didn't investigate more and we just used the older IDE to run the project and start to work.

I believe this can easily be fixed by removing the related folders from the maven local repository.

Tuesday, May 22, 2012

Producer Extends, Consumer Super (PECS)

I always remember-and-forget about this Java Generics concept. So I will put it here as easy reference lookup for me.

public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp)

question : why is it Comparator<? super T> instead of Comparator<? extends T> ?

answer:

Josh Bloch's mnemonic PECS is useful here. It stands for:

Producer extends, Consumer super

This means that when a parameterized type being passed to a method will produce instances of T (they will be retrieved from it in some way), ? extends T should be used, since any instance of a subclass of T is also a T.

When a parameterized type being passed to a method will consume instances of T (they will be passed to it to do something), ? super T should be used because an instance of T can legally be passed to any method that accepts some supertype of T. A Comparator<Number> could be used on a Collection<Integer>, for example. ? extends T would not work, because a Comparator<Integer> could not operate on a Collection<Number>.

Edit: To clarify a little more on get/put (produce/consume):
public T something();
The above is a method that produces T.

public void something(T t);
The above is a method that consumes T.

"Producer extends, Consumer super" applies to how the method a parameterized object is being passed to is going to be using that object. In the case of Collections.max(), items will be retrieved from the Collection, so it is a producer. Those items will be passed as arguments to the method on Comparator, so it is a consumer.

reference : http://stackoverflow.com/questions/2248390/java-generics-collections-max-signature-and-comparator/2248503#2248503

Wednesday, May 16, 2012

Prevent HQL Injection Via Named Parameter

  
hibernateTemplate.find("from User where username ='" + name + "'");
hibernateTemplate.findByNamedParam("from User where username=:userName", "userName" , name);
Both statements will return the same result. The difference is the 2nd statement is immune to HQL injection while the 1st one is not.
This is easily verified by using the well-known injection code ' or '1'='1
Addtional testing using other injection codes would be better to get some confidence.
 Reference : https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet

Friday, May 11, 2012

Spring Mobile + Spring Security + Hibernate + MySQL

Based on my readings on this, this and this, I came out with a testing project. Jquery Mobile is also used as the basic layout for mobile site. There is one issue however. Unlike gmail application where I can use a same session when switching between mobile site and the normal site after I log in(by using Chrome UA Spoofer to change between mobile user agent and normal browser agent), this is not happening in this project. It treats mobile site and normal site as 2 different sessions. Maybe I will study more about this in the future since I just learn about Spring.

 Project downloadable here.

Tuesday, May 8, 2012

What is the Use of ContextLoaderListener in Spring ?

in web.xml:

 
 org.springframework.web.context.ContextLoaderListener
 



 applicationContext.xml

This is used for bootstrap the startup – shutdown the spring application.we can define the configuration details and load it during the application startup and save it as context so we can use that configuration in overall application .

For Example :

Database Configuration

Context is always one for an application. There is no need to do configuration for ContextLoaderListener in Spring 3.0.Application will successfully run without to define it. But If we need to define some configuration into the context and that should be loaded during application start so we have to defined it in deployment descriptor

source : http://www.aoiblog.com/spring-contextloaderlistener/

* Testing in Spring 3.1 , adding ContextLoaderListener to the deployment descriptor(web.xml) without context-param and param-value, cause java.io.FileNotFoundException of the applicationContext.xml. So they must be added together.