Abandoned

Note: This blog has been abandoned, any future updates will be found here: https://phillipgreenii.github.io/

Wednesday, March 28, 2012

WebLogic JNDI Custom Resource Configuration

Note: This blog has been abandoned, any future updates will be found here: https://phillipgreenii.github.io/specifying-projectstage-in-jndi-with/

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: http://code.google.com/p/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: https://bitbucket.org/phillip_green_idmworks/weblogic-jndi-custom-resource-configuration/. It provides a couple separate Initializers: StringInitializer,PropertiesInitializer, and LdapDirContextInitializer. Installation and configuration instructions can be found at https://bitbucket.org/phillip_green_idmworks/weblogic-jndi-custom-resource-configuration/overview. 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

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

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

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

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

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

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 code>/path/to/ldap.properties. Currently, the properties is also stored in JNDI at ldap/test__properties.

Usage

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.

No comments:

Post a Comment