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.

Sunday, March 8, 2015

Override CMake in Cygwin

As of this writing, the CMake bundle with Cygwin is on version 3.1.2. This would be a bad news to me as I can't generate visual studio project using this tool. When I was issued a command cmake --help, this will shows a list of supported platform. Whereas the latest version of this writing would be 3.2.0-rc2. It has far more lists of supported platform.

Available generators on version 3.1.2.
Generators

The following generators are available on this platform:
  Unix Makefiles              = Generates standard UNIX makefiles.
  Ninja                       = Generates build.ninja files (experimental).
  CodeBlocks - Ninja          = Generates CodeBlocks project files.
  CodeBlocks - Unix Makefiles = Generates CodeBlocks project files.
  CodeLite - Ninja            = Generates CodeLite project files.
  CodeLite - Unix Makefiles   = Generates CodeLite project files.
  Eclipse CDT4 - Ninja        = Generates Eclipse CDT 4.0 project files.
  Eclipse CDT4 - Unix Makefiles = Generates Eclipse CDT 4.0 project files.
  KDevelop3                   = Generates KDevelop 3 project files.
  KDevelop3 - Unix Makefiles  = Generates KDevelop 3 project files.
  Kate - Ninja                = Generates Kate project files.
  Kate - Unix Makefiles       = Generates Kate project files.
  Sublime Text 2 - Ninja      = Generates Sublime Text 2 project files.
  Sublime Text 2 - Unix Makefiles = Generates Sublime Text 2 project files.
Available generators on version 3.2.0-rc2
Generators

The following generators are available on this platform:
  Visual Studio 6             = Generates Visual Studio 6 project files.
  Visual Studio 7             = Generates Visual Studio .NET 2002 project files.
  Visual Studio 10 2010       = Generates Visual Studio 10 (VS 2010) project files.
  Visual Studio 11 2012       = Generates Visual Studio 11 (VS 2012) project files.
  Visual Studio 12 2013       = Generates Visual Studio 12 (VS 2013) project files.
  Visual Studio 14 2015       = Generates Visual Studio 14 (VS 2015) project files.
  Visual Studio 7 .NET 2003   = Generates Visual Studio .NET 2003 project files.
  Visual Studio 8 2005        = Generates Visual Studio 8 2005 project files.
  Visual Studio 9 2008        = Generates Visual Studio 9 2008 project files.
  Borland Makefiles           = Generates Borland makefiles.
  NMake Makefiles             = Generates NMake makefiles.
  NMake Makefiles JOM         = Generates JOM makefiles.
  MSYS Makefiles              = Generates MSYS makefiles.
  MinGW Makefiles             = Generates a make file for use with mingw32-make.
  Unix Makefiles              = Generates standard UNIX makefiles.
  Ninja                       = Generates build.ninja files (experimental).
  Watcom WMake                = Generates Watcom WMake makefiles.
  CodeBlocks - MinGW Makefiles= Generates CodeBlocks project files.
  CodeBlocks - NMake Makefiles= Generates CodeBlocks project files.
  CodeBlocks - Ninja          = Generates CodeBlocks project files.
  CodeBlocks - Unix Makefiles = Generates CodeBlocks project files.
  CodeLite - MinGW Makefiles  = Generates CodeLite project files.
  CodeLite - NMake Makefiles  = Generates CodeLite project files.
  CodeLite - Ninja            = Generates CodeLite project files.
  CodeLite - Unix Makefiles   = Generates CodeLite project files.
  Eclipse CDT4 - MinGW Makefiles = Generates Eclipse CDT 4.0 project files.
  Eclipse CDT4 - NMake Makefiles = Generates Eclipse CDT 4.0 project files.
  Eclipse CDT4 - Ninja        = Generates Eclipse CDT 4.0 project files.
  Eclipse CDT4 - Unix Makefiles = Generates Eclipse CDT 4.0 project files.
  Kate - MinGW Makefiles      = Generates Kate project files.
  Kate - NMake Makefiles      = Generates Kate project files.
  Kate - Ninja                = Generates Kate project files.
  Kate - Unix Makefiles       = Generates Kate project files.
  Sublime Text 2 - MinGW Makefiles = Generates Sublime Text 2 project files.
  Sublime Text 2 - NMake Makefiles = Generates Sublime Text 2 project files.
  Sublime Text 2 - Ninja      = Generates Sublime Text 2 project files.
  Sublime Text 2 - Unix Makefiles = Generates Sublime Text 2 project files.
How could I upgrade the CMake in Cygwin? Unfortunately, this can't be done since the latest version I got is the one I had already installed. Try thinking out of the box, download the latest CMake in separate path, then override the Cygwin path with a new CMake path in environment variable, this is how I do it:
  1. Declare CMAKE_PATH pointing to the new CMake file system.
  2. Put this path before CYGWIN_PATH in PATH variable. 
This is how it looks like in PATH variable:

%CMAKE_PATH%\bin;%CYGWIN_PATH%\bin

With this change, I can only use it in DOS command because Cygwin will also reference to version 3.1.2.

Sunday, March 1, 2015

How do CMake determine static or dynamic Boost library?

I was trying to integrate Boost filesystem and system module into CMake in the project, but interestingly, I found out that find_package were not functioning properly:

find_package(Boost 1.54.0 COMPONENTS filesystem system REQUIRED)

I just spot this symptom in this morning. When I am generating a project file from Cmake with this command cmake -G “xxx”, I hit an error as mention below:

CMake Error at D:/tool/cmake-3.2.0-rc1-win32-x86/share/cmake-3.2/Modules/FindBoost.cmake:1182 (message):
  Unable to find the requested Boost libraries.

  Boost version: 1.54.0

  Boost include path: C:/Boost/include/boost-1_54

  Could not find the following Boost libraries:

          boost_filesystem
          boost_system

No Boost libraries were found. You may need to set BOOST_LIBRARYDIR to the directory containing Boost libraries or BOOST_ROOT to the location of Boost.

When I have the debug mode turned on, like this set(Boost_DEBUG 1), I have the following piece captured:
-- [ D:/tool/cmake-3.2.0-rc1-win32-x86/share/cmake-3.2/Modules/FindBoost.cmake:890 ] _boost_LIBRARY_SEARCH_DIRS = C:/Boost/lib;C:/Boost/stage/lib;C:/Boost/include/boost-1_54/lib;C:/Boost/include/boost-1_54/../lib;C:/Boost/include/boost-1_54/stage/lib;PATHS;C:/boost/lib;C:/boost;/sw/local/lib
-- [ D:/tool/cmake-3.2.0-rc1-win32-x86/share/cmake-3.2/Modules/FindBoost.cmake:1001 ] Searching for FILESYSTEM_LIBRARY_RELEASE: boost_filesystem-vc110-mt-1_54;boost_filesystem-vc110-mt;boost_filesystem-mt-1_54;boost_filesystem-mt;boost_filesystem
-- [ D:/tool/cmake-3.2.0-rc1-win32-x86/share/cmake-3.2/Modules/FindBoost.cmake:1037 ] Searching for FILESYSTEM_LIBRARY_DEBUG: boost_filesystem-vc110-mt-gd-1_54;
boost_filesystem-vc110-mt-gd;boost_filesystem-mt-gd-1_54;boost_filesystem-mt-gd;boost_filesystem-mt;boost_filesystem
-- [ D:/tool/cmake-3.2.0-rc1-win32-x86/share/cmake-3.2/Modules/FindBoost.cmake:1001 ] Searching for SYSTEM_LIBRARY_RELEASE: boost_system-vc110-mt-1_54;boost_sys
tem-vc110-mt;boost_system-mt-1_54;boost_system-mt;boost_system
-- [ D:/tool/cmake-3.2.0-rc1-win32-x86/share/cmake-3.2/Modules/FindBoost.cmake:1037 ] Searching for SYSTEM_LIBRARY_DEBUG: boost_system-vc110-mt-gd-1_54;boost_sy
stem-vc110-mt-gd;boost_system-mt-gd-1_54;boost_system-mt-gd;boost_system-mt;boost_system
-- [ D:/tool/cmake-3.2.0-rc1-win32-x86/share/cmake-3.2/Modules/FindBoost.cmake:1088 ] Boost_FOUND = 1

...
CMake was searching for boost_xxx but I had only libboost_xxx installed in my system. I was wondering how could I direct CMake to take libboost_xxx instead of boost_xxx? Thanks to open source that could allow me to dive into the source code to see what is happening actually. From the clue given above, there is a piece of logic in FindBoost.cmake governing CMake whether a boost library prefix with “lib” or without “lib” should be taken. Here is the piece:
...

set(Boost_LIB_PREFIX "")
if ( WIN32 AND Boost_USE_STATIC_LIBS AND NOT CYGWIN )
  set(Boost_LIB_PREFIX "lib")
endif()

...
Since I'm doing this in DOS prompt, WIN32 and NOT CYGWIN are returning true, but Boost_USE_STATIC_LIBS were returning false. This reminds me that I have something in my code:
...

set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
set(Boost_ALL_DYN_LINK ON)

...
Clearly, I should ON the Boost_USE_STATIC_LIBS to resolve this problem. And this is interesting to know that a boost static library is prefixed with “lib”.

Tuesday, February 24, 2015

CMake unable to read Boost's version.hpp

Ever wonder how Boost works together with CMake? I have no knowledge prior to this setup, thus this is my initial code in CMakeLists.txt.
cmake_minimum_required(VERSION 2.8.4)
project("Project A")

set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
set(Boost_ALL_DYN_LINK ON)

find_package(Boost 1.54.0 COMPONENTS filesystem system REQUIRED)

if(Boost_FOUND)
 message(STATUS "Boost library were found")
 
 include_directories(${Boost_INCLUDE_DIRS})
 add_executable(Project_A source/main.cpp)
 target_link_libraries(syncFile ${Boost_LIBRARIES})
else()
 message(STATUS "No Boost library were found")
endif()
As of this writing, I'm using Boost 1.54 and CMake 3.1.2. Anyhow, there is an error when generating the Makefile. Here is the error:
CMake Error at /usr/share/cmake-3.1.2/Modules/FindBoost.cmake:685 (file):
  file STRINGS file "/cygdrive/d/visual studio
  2012/projectA/C:/boost/include/boost-1_54/boost/version.hpp" cannot be
  read.
Call Stack (most recent call first):
  CMakeLists.txt:19 (find_package)


-- Could NOT find Boost
-- No Boost library were found
-- Configuring incomplete, errors occurred!
I have been trying on cygwin, and DOS prompt, it is somewhat weird that the error was happening in FindBoost.cmake. This is the code snippet on the piece that throw an error:
# Set Boost_FOUND based only on header location and version.
# It will be updated below for component libraries.
if(Boost_INCLUDE_DIR)
  if(Boost_DEBUG)
    message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
                   "location of version.hpp: ${Boost_INCLUDE_DIR}/boost/version.hpp")
  endif()

  # Extract Boost_VERSION and Boost_LIB_VERSION from version.hpp
  set(Boost_VERSION 0)
  set(Boost_LIB_VERSION "")
  file(STRINGS "${Boost_INCLUDE_DIR}/boost/version.hpp" _boost_VERSION_HPP_CONTENTS REGEX "#define BOOST_(LIB_)?VERSION ")
A quick check on ${Boost_INCLUDE_DIR}, it shows C:/boost/include/boost-1_54, but I don't know why there is a default path and why ${Boost_INCLUDE_DIR} appended to that default path. I have tried the workaround on BOOST_ROOT as described here, and here, and also try the workaround on CMAKE_PREFIX_PATH as describe here but none of them work. I also found this link were quite useful, but it doesn't work on find_package command.

I'm running out of clues on this problem. Since the default path was bundled together with CMake as one package, I choose to override Boost_INCLUDE_DIR instead of shake him off away.
  ...

  set(Boost_INCLUDE_DIR /cygdrive/c/boost/include/boost-1_54)
But this isn't a perfect solution as it wouldn't work on Linux since the Boost path is referring to Windows. I'm still searching a solution for this.

Sunday, February 15, 2015

undefined reference of copy_file

./source/main.o: In function `boost::filesystem::copy_file(boost::filesystem::path const&, boost::filesystem::path const&)':
/home/kokhoe/tool/boost_1_54_0/boost/filesystem/operations.hpp:384: undefined reference to `boost::filesystem::detail::copy_file(boost::filesystem::path const&, boost::filesystem::path const&, boost::filesystem::copy_option, boost::system::error_code*)'
collect2: error: ld returned 1 exit status
make: *** [syncFile] Error 1
I got this error when I have the following piece executed:
path source = file_A;
path dest = path_B;
copy_file(source, dest);
Luckily, there is already a programmer had the problem solved (read here), what he did is to put this code before #include <boost/filesystem.hpp>.

#define BOOST_NO_CXX11_SCOPED_ENUMS

Do take note also path_B must be a file name rather than a path name.