Thursday, May 28, 2015

Little trick on handling Maven failed to transfer error...

Transfer error again...
This is so frustrating every time I receive this error: Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4 from http://maven.repository.redhat.com/techpreview/all was cached in the local repository, resolution will not be reattempted until the update interval of jboss-ga-plugin-repository has elapsed or updates are forced. Original error: Could not transfer artifact org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4 from/to jboss-ga-plugin-repository (http://maven.repository.redhat.com/techpreview/all): The operation was cancelled.
I have seen this error for so many times, and usually my action plan was:
  1. Refresh and clean the project.
  2. Force update Maven dependencies to the project.
And usually the result were still the same. There is a little nice trick could be done for this error, I got this trick from my senior:
  1. Open command prompt.
  2. Change directory to .m2 repository.
  3. Fire this command for /r %i in (*.lastUpdated) do del %i and wait until it complete.
  4. Force update Maven dependencies to the project.
This should cure the problem.

Saturday, May 23, 2015

Arquillian is looking for com/opera/core/systems/OperaDriver

On last week when I was unit testing with Arquillian, I got this error causing my unit test failed.
java.lang.NoClassDefFoundError: com/opera/core/systems/OperaDriver
 at java.lang.Class.getDeclaredMethods0(Native Method)
 at java.lang.Class.privateGetDeclaredMethods(Class.java:2575)
 at java.lang.Class.getDeclaredMethods(Class.java:1857)
 at org.jboss.arquillian.core.impl.Reflections.getObserverMethods(Reflections.java:52)
 at org.jboss.arquillian.core.impl.ExtensionImpl.of(ExtensionImpl.java:51)
 at org.jboss.arquillian.core.impl.ManagerImpl.inject(ManagerImpl.java:198)
 at org.jboss.arquillian.core.impl.InjectorImpl.inject(InjectorImpl.java:58)
 at org.jboss.arquillian.core.impl.loadable.ServiceRegistryLoader.createServiceInstance(ServiceRegistryLoader.java:108)
 at org.jboss.arquillian.core.impl.loadable.ServiceRegistryLoader.all(ServiceRegistryLoader.java:55)
 at org.jboss.arquillian.drone.impl.DroneRegistrar.registerConfigurators(DroneRegistrar.java:84)
 at org.jboss.arquillian.drone.impl.DroneRegistrar.register(DroneRegistrar.java:77)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:622)
 at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
 at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)
 at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)
 at org.jboss.arquillian.test.impl.TestContextHandler.createSuiteContext(TestContextHandler.java:60)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:622)
 at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
 at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
 at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:135)
 at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:115)
 at org.jboss.arquillian.test.impl.EventTestRunnerAdaptor.beforeSuite(EventTestRunnerAdaptor.java:68)
 at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:97)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: com.opera.core.systems.OperaDriver
 at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
 ... 35 more
While spotting the error on the Maven configuration, I found out that grahene-webdriver and arquillian-drone-webdriver were both declare in POM.
        <dependency>
         <groupid>org.jboss.arquillian.graphene</groupid>
         <artifactid>graphene-webdriver</artifactid>
         <version>2.0.0.Alpha1</version>
         <type>pom</type>
         <scope>test</scope>
        </dependency>
        
        <dependency>
         <groupid>org.jboss.arquillian.graphene</groupid>
         <artifactid>graphene-webdriver-drone</artifactid>
         <version>2.0.0.Alpha3</version>
         <scope>test</scope>
        </dependency>
        
        <dependency>
         <groupid>org.jboss.arquillian.extension</groupid>
         <artifactid>arquillian-drone-webdriver</artifactid>
         <scope>test</scope>
        </dependency>
        
        <dependency>
         <groupid>org.jboss.arquillian.extension</groupid>
         <artifactid>arquillian-drone-webdriver-depchain</artifactid>
         <type>pom</type>
         <scope>test</scope>
        </dependency>
Why both web driver were declare? That was my first question come into my mind. Is that necessary? Is there any difference? As according to the Arquillian guide, I'm suppose to have graphene-webdriver with version 2.0.3.Final. And the other 3 dependencies are not needed. This has my problem resolved.

Saturday, May 16, 2015

Now I have Tomcat with Arquillian

Beginning mid of April until now, I have already spent almost 27 hours on the alien tool, Arquillian. The major problem I'm facing with this tool is the setup, the setup guide didn't mention clearly which Arquillian version should correlated with JBoss version. End up I spent my whole day fighting on the version issue. As of today, I am still failing to make it work with JBoss. This has greatly jeopardize my progress.

This is what make me failed during the setup for JBoss.
        <dependency>
         <groupid>org.jboss.spec</groupid>
         <artifactid>jboss-javaee-6.0</artifactid>
         <version>3.0.2.Final</version>
         <type>pom</type>
         <scope>provided</scope>
        </dependency>
        <dependency>
         <groupid>org.jboss.as</groupid>
         <artifactid>jboss-as-arquillian-container-managed</artifactid>
         <scope>test</scope>
        </dependency>
And this is what I have for Arquillian:
        <dependency>
            <groupid>org.jboss.arquillian.junit</groupid>
            <artifactid>arquillian-junit-container</artifactid>
            <scope>test</scope>
        <dependency>
            <groupid>org.jboss.arquillian.protocol</groupid>
            <artifactid>arquillian-protocol-servlet</artifactid>
            <scope>test</scope>
        </dependency>
        <dependency>
         <groupid>org.jboss.arquillian.graphene</groupid>
         <artifactid>graphene-webdriver-drone</artifactid>
         <version>2.0.0.Alpha3</version>
         <scope>test</scope>
        </dependency>
        <dependency>
         <groupid>org.jboss.arquillian.extension</groupid>
         <artifactid>arquillian-drone-webdriver</artifactid>
         <scope>test</scope>
        </dependency>
        <dependency>
         <groupid>org.jboss.arquillian.extension</groupid>
         <artifactid>arquillian-drone-webdriver-depchain</artifactid>
         <type>pom</type>
         <scope>test</scope>
        </dependency>
So far I only manage to make it work on Tomcat, and this is what I have for Tomcat:
        <dependency>
         <groupId>org.jboss.arquillian.container</groupId>
         <artifactId>arquillian-tomcat-embedded-7</artifactId>
         <version>1.0.0.CR4</version>
         <scope>test</scope>
        </dependency>
        <dependency>
         <groupId>org.apache.tomcat.embed</groupId>
         <artifactId>tomcat-embed-core</artifactId>
         <version>7.0.42</version>
         <scope>test</scope>
        </dependency>
        <dependency>
         <groupId>org.apache.tomcat.embed</groupId>
         <artifactId>tomcat-embed-jasper</artifactId>
         <version>7.0.42</version>
         <scope>test</scope>
        </dependency>
        <dependency>
         <groupId>org.apache.tomcat.embed</groupId>
         <artifactId>tomcat-embed-logging-juli</artifactId>
         <version>7.0.42</version>
         <scope>test</scope>
        </dependency>
        <dependency>
         <groupId>org.eclipse.jdt.core.compiler</groupId>
         <artifactId>ecj</artifactId>
         <version>4.2.2</version>
         <scope>test</scope>
        </dependency>
When running on Tomcat, there is additional stuff is needed which is not mentioned in Arquillian documentation, but I found it in the forum mention that I must have org.jboss.weld.environment.servlet.Listener in web.xml. First the following library is require:
        <dependency>
         <groupId>org.jboss.weld.servlet</groupId>
         <artifactId>weld-servlet</artifactId>
         <version>2.0.3.Final</version>
         <scope>test</scope>
        </dependency>
And then create a new web.xml in src/test/resources with following content:
<web-app id="WebApp_ID" version="3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 <display-name>WebEngineering3</display-name>
 <welcome-file-list>
  <welcome-file>index.xhtml</welcome-file>
 </welcome-file-list>
 
 <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>
 
 <listener>
  <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
 </listener>
</web-app>

Friday, May 1, 2015

Arquillian throws IllegalArgumentException during packaging with ShrinkWrap

Just come across Arquillian and find it so cool that I am able to perform unit test on front end without the 'Selenium way'. The way the Selenium did was I have to leave my computer alone while the test is running otherwise the test result will not accurate.

Now with this creature, I am worry free. In 'Arquillian way', I'm no longer have the web deploy to the server and have the server up and running before the test run. All I have to do were done in programming, this will include package the content in war file and get it deploy to server. It wasn't an Ant build, it was using ShrinkWrap.

Assuming I have my classes put inside the package org.huahsin68, the first step in unit test was the war packaging. It was done with the piece shows below:
   @Deployment(testable=false)
   public static WebArchive createDeployments() {
      WebArchive war = ShrinkWrap.create(WebArchive.class, "ThePackage.war");
      war.setWebXML(new File(WEBAPP_SRC, "WEB-INF/web.xml"));
      war.addPackage(java.lang.Package.getPackage("org.huahsin"));
      war.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
      war.addAsWebResource(new File(WEBAPP_SRC, "index.xhtml"));

      return war;
   }
Somehow there is an error with that piece when the test were run. Following stacktrace tells the whole story.
java.lang.RuntimeException: Could not invoke deployment method: public static org.jboss.shrinkwrap.api.spec.WebArchive org.huahsin68.IndexBeanTest.createDeployment()
 at org.jboss.arquillian.container.test.impl.client.deployment.AnnotationDeploymentScenarioGenerator.invoke(AnnotationDeploymentScenarioGenerator.java:177)
 at org.jboss.arquillian.container.test.impl.client.deployment.AnnotationDeploymentScenarioGenerator.generateDeployment(AnnotationDeploymentScenarioGenerator.java:99)
 at org.jboss.arquillian.container.test.impl.client.deployment.AnnotationDeploymentScenarioGenerator.generate(AnnotationDeploymentScenarioGenerator.java:62)
 at org.jboss.arquillian.container.test.impl.client.deployment.DeploymentGenerator.generateDeployment(DeploymentGenerator.java:79)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:622)
 at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
 at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)
 at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)
 at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:135)
 at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:115)
 at org.jboss.arquillian.core.impl.EventImpl.fire(EventImpl.java:67)
 at org.jboss.arquillian.container.test.impl.client.ContainerEventController.execute(ContainerEventController.java:100)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:622)
 at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
 at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)
 at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)
 at org.jboss.arquillian.test.impl.TestContextHandler.createClassContext(TestContextHandler.java:75)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:622)
 at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
 at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
 at org.jboss.arquillian.test.impl.TestContextHandler.createSuiteContext(TestContextHandler.java:60)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:622)
 at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
 at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
 at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:135)
 at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:115)
 at org.jboss.arquillian.test.impl.EventTestRunnerAdaptor.beforeClass(EventTestRunnerAdaptor.java:80)
 at org.jboss.arquillian.junit.Arquillian$2.evaluate(Arquillian.java:182)
 at org.jboss.arquillian.junit.Arquillian.multiExecute(Arquillian.java:314)
 at org.jboss.arquillian.junit.Arquillian.access$100(Arquillian.java:46)
 at org.jboss.arquillian.junit.Arquillian$3.evaluate(Arquillian.java:199)
 at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
 at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:147)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.reflect.InvocationTargetException
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:622)
 at org.jboss.arquillian.container.test.impl.client.deployment.AnnotationDeploymentScenarioGenerator.invoke(AnnotationDeploymentScenarioGenerator.java:173)
 ... 50 more
Caused by: java.lang.IllegalArgumentException: Pack must be specified
 at org.jboss.shrinkwrap.impl.base.Validate.notNull(Validate.java:43)
 at org.jboss.shrinkwrap.impl.base.container.ContainerBase.addPackage(ContainerBase.java:1363)
 at org.huahsin68.IndexBeanTest.createDeployment(IndexBeanTest.java:42)
 ... 55 more
There is a lazy way to resolve this issue, just pick any of the class and retrieve the package name from that class. Thus I replace the code:

war.addPackage(java.lang.Package.getPackage("org.huahsin"));

with this:

war.addPackage(anyClass.class.getPackage());

But this is not the clean way because if the class will going to remove in one day and this could lead to a disaster.

Good luck!

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.