Sunday, July 28, 2013

Wrong configuration in hibernate.chg.xml

This is very bad, do you know what will happened when you miss configure the hibernate configuration as the code shown below?
    username

    password


Here is the error I get when trying to establish a connection:
org.hibernate.exception.GenericJDBCException: Cannot open connection
 at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
 at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
 at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
 at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
 at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
 at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
 at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:160)
 at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:81)
 at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1473)
 at org.huahsin.dao.LoginDao.getUserByUserId(LoginDao.java:36)
 at org.huahsin.web.LoginAction.(LoginAction.java:69)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
 at java.lang.Class.newInstance0(Class.java:355)
 at java.lang.Class.newInstance(Class.java:308)
 at com.ibm.ws.jsf.config.annotation.WebSphereAnnotationLifecycleProvider.newInstance(WebSphereAnnotationLifecycleProvider.java:47)
 at org.apache.myfaces.config.ManagedBeanBuilder.buildManagedBean(ManagedBeanBuilder.java:162)
 at org.apache.myfaces.el.unified.resolver.ManagedBeanResolver.createManagedBean(ManagedBeanResolver.java:303)
 at org.apache.myfaces.el.unified.resolver.ManagedBeanResolver.getValue(ManagedBeanResolver.java:266)
 at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:55)
 at org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:142)
 at org.apache.myfaces.el.VariableResolverImpl.resolveVariable(VariableResolverImpl.java:65)
 at org.apache.myfaces.el.convert.VariableResolverToELResolver.getValue(VariableResolverToELResolver.java:116)
 at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:55)
 at org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:142)
 at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:71)
 at org.apache.el.parser.AstValue.getTarget(AstValue.java:96)
 at org.apache.el.parser.AstValue.setValue(AstValue.java:200)
 at org.apache.el.ValueExpressionImpl.setValue(ValueExpressionImpl.java:425)
 at org.apache.myfaces.el.convert.ValueExpressionToValueBinding.setValue(ValueExpressionToValueBinding.java:125)
 at org.apache.myfaces.custom.updateactionlistener.UpdateActionListener.processAction(UpdateActionListener.java:154)
 at javax.faces.event.ActionEvent.processListener(ActionEvent.java:51)
 at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:344)
 at javax.faces.component.UICommand.broadcast(UICommand.java:103)
 at javax.faces.component.UIData.broadcast(UIData.java:757)
 at javax.faces.component.UIViewRoot._broadcastAll(UIViewRoot.java:937)
 at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:271)
 at javax.faces.component.UIViewRoot._process(UIViewRoot.java:1249)
 at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:675)
 at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:34)
 at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:171)
 at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
 at javax.faces.webapp.FacesServlet.service(FacesServlet.java:189)
 at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1221)
 at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:757)
 at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:440)
 at com.ibm.ws.webcontainer.filter.WebAppFilterChain.invokeTarget(WebAppFilterChain.java:125)
 at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:92)
 at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:97)
 at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:192)
 at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:89)
 at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:144)
 at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:192)
 at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:89)
 at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:939)
 at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1036)
 at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:81)
 at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:930)
 at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost$1.run(DynamicVirtualHost.java:253)
 at com.ibm.ws.http.dispatcher.internal.channel.HttpDispatcherLink$TaskWrapper.run(HttpDispatcherLink.java:457)
 at com.ibm.ws.threading.internal.Worker.executeWork(Worker.java:398)
 at com.ibm.ws.threading.internal.Worker.run(Worker.java:380)
 at java.lang.Thread.run(Thread.java:662)
Caused by: java.sql.SQLException: Incorrect password or user com.informix.asf.IfxASFRemoteException: Kok.Hoe.Loh@128.230.11.83 is not known on the database server.
 at com.informix.jdbc.IfxSqliConnect.(IfxSqliConnect.java:1195)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
 at com.informix.jdbc.IfxDriver.connect(IfxDriver.java:254)
 at java.sql.DriverManager.getConnection(DriverManager.java:582)
 at java.sql.DriverManager.getConnection(DriverManager.java:154)
 at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133)
 at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
 ... 60 more
Caused by: com.informix.asf.IfxASFRemoteException: Kok.Hoe.Loh@128.230.11.83
 at com.informix.asf.Connection.recvConnectionResponse(Connection.java:688)
 at com.informix.asf.Connection.establishConnection(Connection.java:1613)
 at com.informix.asf.Connection.(Connection.java:347)
 at com.informix.jdbc.IfxSqliConnect.(IfxSqliConnect.java:1055)
 ... 69


Guess what is wrong with the code shown above? I was so surprise that I have miss type the hibernate.connection.username to hibernate.username and also hibernate.connection.password to hibernate.password.


Unable to find data source in Liberty Profile server

Recently the data source at my local server have create so much trouble to me. I'm using WebSphere Liberty Profile server in my development. In order to ease my work on deployment into production server with minimum changes as possible, I created a data source at my local server. But the data source doesn't seem function properly. I have the following code that grabs the data source from the server and establishes a connection to database server.
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

datasourceJndi="jdbc/informixDB";
Context jndiCntx = new InitialContext();
           
DataSource ds = (javax.sql.DataSource)jndiCntx.lookup(datasourceJndi);
            
//set the connection.
super.con = ds.getConnection();

The code was not working and following stack trace is being thrown.
java.sql.SQLNonTransientException: J2CA8030E: Unable to find jdbcDriverRef com.ibm.ws.jdbc.jdbcDriver_gen_d3507f1a-ecba-4697-9861-d2fe2c14d742 for dataSource jdbc/informixDB.

 at org.huahsin.util.DaoManager.getDaoConnection(DaoManager.java:56)
 at org.huahsin.util.DaoManager.setSql(DaoManager.java:65)
 at org.huahsin.dao.MenuItemDao.findMenuByUserId(MenuItemDao.java:43)
 at org.huahsin.web.MenuBean.getMenuList(MenuBean.java:32)
 at org.huahsin.LoginBean.doLogin(LoginBean.java:243)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.apache.el.parser.AstValue.invoke(AstValue.java:266)
 at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
 at org.apache.jasper.el.JspMethodExpression.invoke(JspMethodExpression.java:70)
 at javax.faces.component._MethodExpressionToMethodBinding.invoke(_MethodExpressionToMethodBinding.java:88)
 at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:100)
 at javax.faces.component.UICommand.broadcast(UICommand.java:120)
 at javax.faces.component.UIViewRoot._broadcastAll(UIViewRoot.java:937)
 at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:271)
 at javax.faces.component.UIViewRoot._process(UIViewRoot.java:1249)
 at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:675)
 at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:34)
 at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:171)
 at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
 at javax.faces.webapp.FacesServlet.service(FacesServlet.java:189)
 at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1221)
 at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:757)
 at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:440)
 at com.ibm.ws.webcontainer.filter.WebAppFilterChain.invokeTarget(WebAppFilterChain.java:125)
 at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:92)
 at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:97)
 at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:192)
 at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:89)
 at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:144)
 at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:192)
 at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:89)
 at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:939)
 at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1036)
 at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:81)
 at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:930)
 at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost$1.run(DynamicVirtualHost.java:253)
 at com.ibm.ws.http.dispatcher.internal.channel.HttpDispatcherLink$TaskWrapper.run(HttpDispatcherLink.java:457)
 at com.ibm.ws.threading.internal.Worker.executeWork(Worker.java:398)
 at com.ibm.ws.threading.internal.Worker.run(Worker.java:380)
 at java.lang.Thread.run(Thread.java:662)

It was very hard for me to analyze this problem because there are not much people know there is a lightweight WebSphere Application Server ever exists in this world. And since this is new, the support usually is very limited. I have run out of clues on this issue. The temporary solution for this is to switch back to JDBC connection just for my local development
Class.forName("com.informix.jdbc.IfxDriver");
super.con = DriverManager.getConnection("jdbc:informix-sqli://xxx.xxx.xx.xx:2020/dbname:INFORMIXSERVER=online", "username", "password");

Syntax error when making a foreign key in MySQL

In DERBY, I create a table which have reference on another table with following query:
CREATE TABLE authority (
 username VARCHAR(10) NOT NULL,
 authority VARCHAR(10) NOT NULL,
 foreign key(username) references users(username)
);

DERBY have no problem with this query but in MySQL there is a syntax error in foreign key. If this going to execute in MySQL, changes need to be done as follow:
CREATE TABLE authority (
 username VARCHAR(10) NOT NULL,
 authority VARCHAR(10) NOT NULL,
 constraint fk_username foreign key(username) references users(username)
);

Sunday, July 21, 2013

Beware of in web.xml

I have a Spring Security project sharing across two servers, one is a normal Tomcat, and another one with SSL configured. At one point, I have this code configured in web.xml:
    
        
            SpringSecurity3
            /*
        
        
            CONFIDENTIAL
        
    
The above code gives an instruction that only HTTPS is allowed to connect to the application. If I’m deploying the project into the Tomcat without SSL configure, I will hit the HTTP 404 error. This was quite annoying every time I deploy the code into 2 different servers where I need to comment and un-comment and then comment it back again and again. Until I found there is a solution to overcome this issue which is by configures this in Server’s web.xml. I am referring to the server project right inside the Eclipse workspace, not the physical Tomcat installation directory.

According to forum, if <transport-guarantee>CONFIDENTIAL</transport-guarantee> is omit, this will indicate that the application can be connect using HTTPS and HTTP. Otherwise Tomcat will automatically route to HTTPS.

DNS cause my LibreOfice compilation failed.

I manage to resolve the connection error happened on last week, that was due to my local Internet Service Provider DNS problem. I have the problem resolved by switching to Google DNS. To do this, amend the file located at /etc/resolvconf/resolv.conf.d/head, add nameserver 8.8.8.8 and nameserver 8.8.4.4 at the end of the file. Please note that is only applicable to Ubuntu 12.10, never add DNS in /etc/network/interfaces as I been told that the path has already obsolete after to version 12.

Sunday, July 14, 2013

Unlucky day to compile LibreOffice

I'm having a bad day in this morning, because I hit following error when I'm compiling LibreOffice.
cd /home/kokhoe/workspace/LibreOffice/core/src/tmp && wget --progress=dot:mega -Q 0 -P "." -l 0 -nd -nH -N http://dev-www.libreoffice.org/src/libcdr-0.0.14.tar.bz2 2>&1 | tee -a /home/kokhoe/workspace/LibreOffice/core/src/fetch.log && [ $PIPESTATUS -eq 0 ] && SUM=`md5sum libcdr-0.0.14.tar.bz2 | sed "s/ .*//"` && if test "$SUM" != "d88f9b94df880d2c05be943b000ca112"; then echo ERROR: expected checksum for libcdr-0.0.14.tar.bz2 is d88f9b94df880d2c05be943b000ca112 2>&1 | tee -a /home/kokhoe/workspace/LibreOffice/core/src/fetch.log; false; fi && mv libcdr-0.0.14.tar.bz2 ../ 
--2013-07-14 10:09:33--  http://dev-www.libreoffice.org/src/libcdr-0.0.14.tar.bz2
Resolving dev-www.libreoffice.org (dev-www.libreoffice.org)... 32.1.6.124, 2001:67c:2178:7::70
Connecting to dev-www.libreoffice.org (dev-www.libreoffice.org)|32.1.6.124|:80... failed: Connection timed out.
Connecting to dev-www.libreoffice.org (dev-www.libreoffice.org)|2001:67c:2178:7::70|:80... failed: Network is unreachable.
make: *** [/home/kokhoe/workspace/LibreOffice/core/workdir/unxlngx6.pro/download] Error 1
It seem there are some problem with connecting to the URL, dev-www.libreoffice.org. I make a try to connect the URL in browser but failed. I then issue the make command again and this time I got timed out error as shown below:
--2013-07-14 10:15:37--  (try: 3)  http://dev-www.libreoffice.org/src/libcdr-0.0.14.tar.bz2
Connecting to dev-www.libreoffice.org (dev-www.libreoffice.org)|32.1.6.124|:80... failed: Connection timed out.
Retrying
My initial assumption was this incidence could be a temporary issue. The server might be under maintenance due to the reason being today is Sunday. Without further waiting, I immediately sent out an email to developer list request for help.

When I check back the email at night, unfortunately no one was reply me. Helpless night.

Configuring HTTPS causes redirect loop in Spring Security

My application now have authentication module guarding its usage from anonymous user. But sometime I do felt that authentication module doesn’t really showing a great help in securing my web application especially the technology in nowadays is greatly improve, hacking is just as easy as 1, 2, 3. Thus I was thinking to integrate HTTPS protocol into my application to make it more secure. Sound great!! According to the documentation, by adding this code > requires-channel=”true” into the configuration as shown below, will force the application to use HTTPS protocol.
    <http auto-config="true">
        <intercept-url access="ROLE_ADMIN" pattern="/landingpage*"/>
        <intercept-url access="IS_AUTHENTICATED_ANONYMOUSLY" pattern="/login*" requires-channel="https"/>
  
        <form-login authentication-failure-url="/loginfailed" default-target-url="/landingpage" login-page="/login"/>
        
        <session-management invalid-session-url="/sessionTimeout">
            <concurrency-control error-if-maximum-exceeded="true" max-sessions="1"/>
        </session-management>

        <logout invalidate-session="false" logout-success-url="/login"/>
    
Unfortunately the connection were refuse. The connection refuse I mention here is referring to page not found or HTTP 404 being shown on the page. This configuration will not going to work until I discover it few days later of the research. In order to enable HTTPS, the server first must have SSL port enable. Unfortunately Tomcat was disabled by default. To enable SSL port, there are tons of tutorials article on the Internet.

Almost there! The last step is to configure port mapping in the above Spring Security to complete the whole thing. Add following code inside <http>:
    <http auto-config="true">
        ....
        ....
        <port-mappings>
            <port-mapping http="8084" https="8443"/>
        </port-mappings>
    </http>

Missing port mapping in the configuration may cause following error.

The webpage at http://localhost:8084/WebApp3/landingpage has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer. Error code:
ERR_TOO_MANY_REDIRECTS
In conclusion, if the application require to enforce HTTPS rule: 1) Ensure server has SSL port enable. 2) Port mapping has been configure in Spring configuration. 3) Intercept URL to HTTPS channel in Spring configuration.

ClassCastException when subtype casting

    Object[] objects = new Object[10];
    String[] strings = (String[]) objects;
We all know that all objects in JAVA are drive from Object class. Does it true? If this statement is true then why the code shown above throw a ClassCastException in run-time? Now consider following code.
    String[] strings = new String[10];
    Object[] objects = (Object[]) strings;
     
    objects[0] = new String("simple Text");
    
    System.out.println(objects[0]);
The code compiles and execute successfully without error. And the content was print out at the end of the execution. Don't you think this is interesting? This code has prove that Object is holding a String in memory. I spent quite some time searching on this and I found this:
Java is a strongly typed language, and that means you can only cast an object to a type it extends from (either a superclass or an interface). – StackOverflow.com

Wednesday, July 3, 2013

What is EL Expression Unbalanced?

Here I got a web page developed using JSF 2.0. Anyhow I am not able to render the page correctly and following error shown on the page:

/thepage.xhtml @12,44 value="#{theBean.contactKey)" EL Expression Unbalanced: ... #{theBean.contactKey)

What is this error means? It took quite some time for me to understand this error message until I found out there is something not so right on the eval-expression construct, #{theBean.contactKey). There are 2 types of construct so far I read in the EL Expression book, either #{expr} or ${expr}. Thus there is no such thing #{expr).

ANT unable to change JDK path

ANT is depends on JAVA_HOME environment variable to determined which JDK version should be use during compilation. Somehow my situation was even though I have update from existing JDK1.5 JAVA_HOME to a new JDK1.6 JAVA_HOME, ANT still using JDK1.5 during the compilation.

Until I found out there is a better way to configure JAVA_HOME for ANT in Eclipse rather than configure it through environment variable, which is, in Eclipse IDE > Run Menu > External Tools Configuration > JRE tab > choose Run in same JRE as workspace option. I choose this option because whenever I change the compiler in the project, the ANT compiler will change accordingly.

Monday, July 1, 2013

Exclude test code being pack into WAR

Here is another stupid putting the test code right under the src directory. I am not sure what is the purpose of doing this since we know that test code should not be deploy into production server, thus I have to exclude it from the main code. I mean not to delete the test code but to exclude them from being package into WAR. This is how I do it:
    
        
        ....
    

SQLException: Field 'id' doesn't have a default

By using the code below in hibernate configuration file will automatically assign a value into ID column without assigning it manually.
    
        
            
        
        ...
        ...
    
Anyhow it does not work as expected and following rubbish being thrown.
    Caused by: java.sql.SQLException: Field 'id' doesn't have a default value
     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)
     at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
     at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1169)
     at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:693)
     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1404)
     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1318)
     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1303)
     at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:93)
     at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:56)
     ... 22 more
What is wrong with my code? According to my colleague, there is nothing wrong in my code, just the column ID in ORDERS table is not declare as auto increment. Thus by alter the table column to auto increment will solve the problem.