Search code examples
jakarta-eewebspherejndi

How to access namespace bindings on Java EE using XML?


I added a name space binding on IBM WebSphere 7.0 using the following info:

  • Binding type = String
  • Binding identifier = test
  • Name in name space relative to lookup name prefix 'cell/nodes/DummyNode01/servers/server1/': = url1
  • String value = <some string>

I was able to access the String value on my web application's code using the code below:

Context initial_ctx;
initial_ctx = new InitialContext();
String value = (String) initial_ctx.lookup("url1");

Now I want to put the String value in XML instead of accessing it via code. How do I do I access the name space bindings of WebSphere in XML? Can I declare it inside the context-param tag?

Thanks!


Solution

  • A couple of notes.

    First, the string name of the NSB (JNDI string) will vary on WebSphere depending on the scope you set. "server" scope as you identified in your post lets you reference this string value by its friendly name ("url1" in your example) as-is. However, watch out in a clustered environment - you'll have to define this NSB on every server instance where you probably want the same value for all cluster member appservers. In this case, define the NSB at cell level and in your code (or in a moment, your Spring xml) use jndi name "cell/persistent/url1."

    Since NSBs in WebSphere are just strings in the naming service (JNDI), you can use the Spring JndiFactoryBean:

    <bean id="myUrl1NameSpaceBinding" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="url1"/> <!-- cell/persistent/url1 in a cluster-wide shared namespace binding -->
        <property name="cache" value="true"/>
        <property name="resourceRef" value="false"/>
        <property name="lookupOnStartup" value="false"/>
        <property name="expectedType" value="java.lang.String"/>
    </bean>
    

    There is a shorthand flavor too:

    <jee:jndi-lookup />
    

    See also: Spring XML docs

    Hope this helps,

    ScottH