GenerationJava.com's Simple-JNDI

Simple-JNDI is intended to solve two problems. The first is that of finding a container independent way of opening a database connection, the second is to find a good way of specifying application configurations.

In a J2EE container, you can use JNDI as a java.sql.Connection factory. This code however is not usable outside of a J2EE container as you cannot guarrentee you have a JNDI implementation available, and to be honest, you don't want to start some other server to make your simple script run. Simple-JNDI is not a server, it is merely an API, however it looks exactly the same as the JNDI client code used in a J2EE container.

Applications need configuration. A servlets container uses the web.xml files to configure, while a J2EE container uses a different system, for example, JBoss has a jboss.jcml configuration file which sets a system up via JMX managed beans. An application could however use Simple-JNDI for parameter values.

Using Simple-JNDI

Using Simple-JNDI is the same as using JNDI itself. So this is not covered here, however the examples do give the basic gist.

Installing Simple-JNDI

Installing Simple-JNDI is as simple as adding the simple-jndi jar, jdbc 2.0 jar and the commons-lang jar to your classpath.

Setting up Simple-JNDI

This is where all the work goes in a Simple-JNDI installation. Firstly you need a jndi.properties file, which somehow needs to go into your classpath. This jndi.properties needs one manadatory value:

java.naming.factory.initial=com.generationjava.jndi.PropertiesFactory

This property is a part of the jndi specification, java.naming.factory.initial. This should be set equal to com.generationjava.jndi.PropertiesFactory.

There are two optional, simple-jndi specific parameters.

The first, com.generationjava.jndi.root, is the location of your simple-jndi root, which is the location in which simple-jndi looks for values when code asks for them. The following code block details a few examples with explanatory comments.

# absolute directory, using the default file protocol

com.generationjava.jndi.root=/home/hen/gj/simple-jndi/config/



# relative directory, using the default file protocol

com.generationjava.jndi.root=config/



# specified file protocol with an absolute directory

com.generationjava.jndi.root=file:///home/hen/gj/simple-jndi/config/



# specified file protocol with a relative directory

com.generationjava.jndi.root=file://config/



# classpath protocol with a package 'config'

com.generationjava.jndi.root=classpath://config

If no com.generationjava.jndi.root is specified, then a classpath root is chosen, with no package.

The second optional parameter is the delimiter used to separate elements in a lookup value. This allows code to get closer to pretending to be another JNDI implementation, such as DNS or LDAP.

# DNS/Java like delimiters

com.generationjava.jndi.delimiter=.



# LDAP/XPath like delimiters

com.generationjava.jndi.delimiter=/

If no com.generationjava.jndi.delimiter is specified, then a '.' (dot) is chosen.