Tuesday, April 21, 2015

Java Server Faces can't live without javax.faces.webapp.FacesServlet

Just a simple little web page developed using Primefaces but it doesn't show up. How simple it is? See below:
<h:body>
   <h:form>
      <h:outputlabel value="First value: "/>
      <p:inputtext id="first"/>
   </h:form>
</h:body>
Why the UI component doesn't render it well is actually I left out the following piece in web.xml:
<servlet>
   <servlet-name>Faces Servlet</servlet-name>
   <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
   <servlet-name>Faces Servlet</servlet-name>
   <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
My final word is the above piece is very important. Java Server Faces can't live without that piece.

Sunday, April 19, 2015

Error during statement execution (file: '/import.sql')

When I create a Maven project using artifact ID jboss-javaee6-webapp-archetype version 7.1.3.CR8, there are a bunch code are already crated for you. My personal habit is whenever a project is first created, I will make a deploy to ensure my server is up and running properly. Somehow I got the following message complaining something was went wrong:
11:30:50,752 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (MSC service thread 1-8) HHH000231: Schema export unsuccessful: org.hibernate.tool.hbm2ddl.ImportScriptException: Error during statement execution (file: '/import.sql'): insert into Member (id, name, email, phone_number) values (0, 'John Smith', 'john.smith@mailinator.com', '2125551212')
 at org.hibernate.tool.hbm2ddl.SchemaExport.importScript(SchemaExport.java:452) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
 at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:379) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
 at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:305) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
 at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:294) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
 at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:452) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
 at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
 at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:84) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
 at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
 at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:889) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
 at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
 at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.createContainerEntityManagerFactory(PersistenceUnitServiceImpl.java:162) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
 at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.start(PersistenceUnitServiceImpl.java:85) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
 at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
 at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146) [rt.jar:1.6.0_34]
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.6.0_34]
 at java.lang.Thread.run(Thread.java:701) [rt.jar:1.6.0_34]
Caused by: org.h2.jdbc.JdbcSQLException: Table "MEMBER" not found; SQL statement:
insert into Member (id, name, email, phone_number) values (0, 'John Smith', 'john.smith@mailinator.com', '2125551212') [42102-161]
 at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
 at org.h2.message.DbException.get(DbException.java:169)
 at org.h2.message.DbException.get(DbException.java:146)
 at org.h2.command.Parser.readTableOrView(Parser.java:4749)
 at org.h2.command.Parser.readTableOrView(Parser.java:4727)
 at org.h2.command.Parser.parseInsert(Parser.java:949)
 at org.h2.command.Parser.parsePrepared(Parser.java:375)
 at org.h2.command.Parser.parse(Parser.java:279)
 at org.h2.command.Parser.parse(Parser.java:251)
 at org.h2.command.Parser.prepareCommand(Parser.java:217)
 at org.h2.engine.Session.prepareLocal(Session.java:415)
 at org.h2.engine.Session.prepareCommand(Session.java:364)
 at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1119)
 at org.h2.jdbc.JdbcStatement.executeUpdateInternal(JdbcStatement.java:121)
 at org.h2.jdbc.JdbcStatement.executeUpdate(JdbcStatement.java:110)
 at org.jboss.jca.adapters.jdbc.WrappedStatement.executeUpdate(WrappedStatement.java:371)
 at org.hibernate.tool.hbm2ddl.DatabaseExporter.export(DatabaseExporter.java:64) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
 at org.hibernate.tool.hbm2ddl.SchemaExport.importScript(SchemaExport.java:447) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
 ... 16 more
I was wondering since when I got such an import.sql get invoked in my code? I was searching in the whole project code and I didn't find any. Later I found out this blog, Hibernate has a mechanism to load SQL script on startup.
You can execute an SQL script during the SessionFactory creation right after the database schema generation to import data in a fresh database. You just need to add a file named import.sql in your classpath root and set either create or create-dropas your hibernate.hbm2ddl.auto property.
Got it, that file did really exists in the classpath. To make my life simple, I would just rename import.sql to other name.

Sunday, April 12, 2015

Operation failed with status WAITING

My first learning objective on EJB is to establish a connection to the server and then return a value, say 5063, to the client. Thus, I have the following piece on server side:
@Remote
public interface AuthenticationRemote {

 public int status();
}


@Stateless
@LocalBean
public class Authentication implements AuthenticationRemote {

    /**
     * Default constructor. 
     */
    public Authentication() {
    }

 @Override
 public int status() {
  return 5063;
 }

}
The project name for the pieces above, ejb2, will be the context path used by a client to identify the remote path of a specific method call. When the piece is deployed to the server, following log would be seen:
java:global/ejb2/Authentication!org.huahsin.AuthenticationRemote
java:app/ejb2/Authentication!org.huahsin.AuthenticationRemote
java:module/Authentication!org.huahsin.AuthenticationRemote
java:jboss/exported/ejb2/Authentication!org.huahsin.AuthenticationRemote
java:global/ejb2/Authentication!org.huahsin.Authentication
java:app/ejb2/Authentication!org.huahsin.Authentication
java:module/Authentication!org.huahsin.Authentication
On the client side, as in following piece, is a separate project which will establish a connection to the above piece:
public class EjbClient {

 public static void main(String args[]) throws NamingException {
  Properties props = new Properties();
  props.put("java.naming.factory.url.pkgs", "org.jboss.ejb.client.naming");
  props.put("java.naming.factory.initial", "org.jboss.naming.remote.client.InitialContextFactory");
  props.put("java.naming.provider.url", "remote://127.0.0.1:4447");
  props.put("jboss.naming.client.ejb.context", "true");
  props.put("jboss.naming.client.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT","false");
  
  InitialContext context = new InitialContext(props);
  
  String appName = "";
  String moduleName = "ejb2";
  String distinctName = "";
  String beanName = Authentication.class.getSimpleName();
  String interfaceName = AuthenticationRemote.class.getName();
  String name = "ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + interfaceName;
  
  AuthenticationRemote bean = (AuthenticationRemote) context.lookup(name);
  int result = bean.status();
 }
}
I was so unlucky that I was failed to establish a connection to server. End I up I get this:
Exception in thread "main" javax.naming.NamingException: Failed to create remoting connection [Root exception is java.lang.RuntimeException: Operation failed with status WAITING]
 at org.jboss.naming.remote.client.ClientUtil.namingException(ClientUtil.java:36)
 at org.jboss.naming.remote.client.InitialContextFactory.getInitialContext(InitialContextFactory.java:121)
 at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:684)
 at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
 at javax.naming.InitialContext.init(InitialContext.java:242)
 at javax.naming.InitialContext.(InitialContext.java:216)
 at org.huahsin68.EjbClient.main(EjbClient.java:21)
Caused by: java.lang.RuntimeException: Operation failed with status WAITING
 at org.jboss.naming.remote.protocol.IoFutureHelper.get(IoFutureHelper.java:89)
 at org.jboss.naming.remote.client.NamingStoreCache.getRemoteNamingStore(NamingStoreCache.java:56)
 at org.jboss.naming.remote.client.InitialContextFactory.getOrCreateCachedNamingStore(InitialContextFactory.java:166)
 at org.jboss.naming.remote.client.InitialContextFactory.getOrCreateNamingStore(InitialContextFactory.java:139)
 at org.jboss.naming.remote.client.InitialContextFactory.getInitialContext(InitialContextFactory.java:104)
 ... 5 more
But later I found out I forgot to turn on my server. Finally, the result variable is returning the value of 5063.