WebLogic JNDI Custom Resource Configuration
I have been using WebLogic for a couple years now. While it is better than Oracle Application Server, it is missing some very important functionality. Today’s complaint is the lacking of a way to add custom JNDI resources. This was discussed at stackoverflow: Custom resource in JNDI on different application servers. I am currently using Oracle WebLogic 10g (10.3.5), so my solution may not be relevant in future versions.
I was very frustrated that other application servers add the functionality, but not WebLogic. The best solution I found was this: weblogic-jndi-startup. I tried it and it works, but it has some limitations. The objects added must have a constructor that accepts a single String parameter. What I want is to add Properties and perhaps LDAP connections (similar to JDBC connections).
#weblogic-jndi-custom-resource-configuration
I drew from Roger’s code and made this project: weblogic-jndi-custom-resource-configuration. It provides a couple separate Initializers: StringInitializer
, PropertiesInitializer
, and LdapDirContextInitializer
. Installation and configuration instructions can be found at weblogic-jndi-custom-resource-configuration. Essentially, you copy the jar to the domain classpath of WebLogic and add Initializers as Startup Classes.
##StringInitializer
StringInitializer
will load a java.lang.String instance into JNDI at a specified location.
###Configuration
Field | Value |
---|---|
Name | server-node |
Class Name | com.idmworks.weblogic.jndiconfiguration.StringInitializer |
Arguments | server/node=Test |
Failure is Fatal | unticked |
Run Before Application Deployments | unticked |
Run Before Application Activations | ticked |
The magic happens with the arguments: server/node=Test
. StringInitializer
will insert the value, “Test”, at the JNDI location server/node
.
###Usage
####Retrieving String from JNDI
final InitialContext initialContext = new InitialContext();
final String node = (String) initialContext.lookup("server/node");
In previous example, the String
instance is available just as any other object in JNDI.
##PropertiesInitializer
PropertiesInitializer
will load a java.util.Properties
instance (based on a properties file) into JNDI at a specified location.
###Configuration
Field | Value |
---|---|
Name | myapp-properties |
Class Name | com.idmworks.weblogic.jndiconfiguration.PropertiesInitializer |
Arguments | properties/myapp=/path/to/myapp.properties |
Failure is Fatal | unticked |
Run Before Application Deployments | unticked |
Run Before Application Activations | ticked |
As before, we focus on the arguments: properties/myapp=/path/to/myapp.properties
. PropertiesInitializer
will create an instance of java.util.Properties
from the properties found at /path/to/myapp.properties
. It next places it at the JNDI location properties/myapp
.
###Usage
####Retrieving Properties from JNDI
final InitialContext initialContext = new InitialContext();
final Properties myappProperties = (Properties) initialContext.lookup("properties/myapp");
In previous example, the Properties
instance is available just as any other object in JNDI.
##LdapDirContextInitializer
LdapDirContextInitializer
will load a java.util.Properties
instance (based on a properties file).
It adds into JNDI a javax.naming.Reference
with a javax.naming.spi.ObjectFactory
that will create a DirContext
.
The created DirContext
from the factory will be configured by the specified properties file.
###Configuration
Field | Value |
---|---|
Name | ldap-test |
Class Name | com.idmworks.weblogic.jndiconfiguration.LdapDirContextInitializer |
Arguments | ldap/test=/path/to/ldap.properties |
Failure is Fatal | unticked |
Run Before Application Deployments | unticked |
Run Before Application Activations | ticked |
####/path/to/ldap.properties
java.naming.provider.url=ldap://localhost:389/dc=home
#The following lines could be uncommented if the LDAP Connection requires authentication
#java.naming.security.principal=cn=user,dc=home
#java.naming.security.credentials=password
The arguments: ldap/test=/path/to/ldap.properties
work similar to PropertiesInitializer
.
LdapDirContextInitializer
will create an instance of java.util.Properties
from the properties found at /path/to/ldap.properties
.
What is added at the JNDI location ldap/test
is a javax.naming.Reference
.
The reference is configured with a javax.naming.spi.ObjectFactory
that will generate a new DirContext
with the properties specified at ``/path/to/ldap.properties.
Currently, the properties is also stored in JNDI at
ldap/test__properties`.
###Usage
####Retrieving LDAP Connection from JNDI
final InitialContext initialContext = new InitialContext();
final DirContext ldapContext = (DirContext) initialContext.lookup("ldap/test");
In previous example, the DirContext
instance is available just as any other object in JNDI.
Each time initialContext.lookup("ldap/test")
is called, a new
DirContext` is created, so it is the responsibility of the caller to close the connection.
##References