Hi,

Many times we want to create some customer JNDI names which will hold some string objects to perform some application specific functionality. In general terms we can say “We want to bind some strings to the JBoss AS6 server’s JNDI tree.” so in order to do that we have the following two options

Option-1).

According to J2EE Specifications we can write the following kind of tag inside our application’s “WEB-INF/web.xml” file:

<?xml version='1.0' encoding='UTF-8'?>
<web-app>
  <env-entry>
    <env-entry-name>TestJNDIKey</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>Some_Application_Specific_JNDIData</env-entry-value>
  </env-entry>
</web-app>

Once your web.xml file has the above kind of entry then you can use the following kind of Code snippet inside your application code (like servlet or jsp) to get the JNDI entry’s bind value.

   InitialContext ctx = new InitialContext();
   Context envCtx = (Context) ctx.lookup("java:comp/env");
   String jndiData = (String) envCtx.lookup("TestJNDIKey");

Option-2).

Open the “jboss-services.xml” file from the following location “$JBOSS_HOME/server/$PROFILE/conf” directory and then add the following kind of <mbean> declaration using “org.jboss.naming.JNDIBindingServiceMgr”

<mbean code="org.jboss.naming.JNDIBindingServiceMgr" name="jboss.testing:name=test">
       <attribute name="BindingsConfig" serialDataType="jbxb">
         <jndi:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
                        xmlns:jndi="urn:jboss:jndi-binding-service:1.0"
                        xs:schemaLocation="urn:jboss:jndi-binding-service resource:jndi-binding-service_1_0.xsd">
             <jndi:binding name="TestJNDIKey">
                 <jndi:value trim="true">Hello, SomeValue!</jndi:value>
             </jndi:binding>
         </jndi:bindings>
        </attribute>
</mbean>

Option-3).

If you don;t want to edit the “jboss-services.xml” file provided by JBoss AS6 then in that case you can create your own Service file with some name like “test-service.xml” inside your “/WEB-INF” directory.
NOTE: the file name must end with “*-service.xml”

OR

Create a file with some name like “test-service.xml” and then place it inside your “$PROFILE/deploy” directory, so that as soon as you will start your JBoss AS6 instance the Service will be activated and the JNDI name will be binded in the JNDI tree of your JBoss.

So you can add the following MBean declaration somewhere in the file Either inside “/server/
/conf/jboss-service.xml” Or add the following declaration inside “/WEB-INF/test-service.xml” file:

<?xml version="1.0" encoding="UTF-8"?>
<server>
<mbean code="org.jboss.naming.JNDIBindingServiceMgr" name="jboss.tests:name=example1">
       <attribute name="BindingsConfig" serialDataType="jbxb">
         <jndi:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
                        xmlns:jndi="urn:jboss:jndi-binding-service:1.0"
                        xs:schemaLocation="urn:jboss:jndi-binding-service resource:jndi-binding-service_1_0.xsd">
             <jndi:binding name="TestJNDIKey">
                 <jndi:value trim="true">Hello, SomeValue!</jndi:value>
             </jndi:binding>
         </jndi:bindings>
        </attribute>
</mbean>
</server>

Step2). Now restart your JBoss AS6 profile like

./run.sh -c default_Test -b 10.10.10.10

Above means you are starting the JBoss profile “default_Test” on Listen address 10.10.10.10.

Step3). You can use the the following client to access the JNDI name remotely/locally.

import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
public class ExClient
   {
       public static void main(String args[]) throws Exception
        {
          Properties env = new Properties();
          env.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.jboss.naming.NamingContextFactory");
          env.setProperty(Context.PROVIDER_URL,"10.10.10.10:1099");
          Context ctx = new InitialContext(env);
          System.out.println("nntCreated InitialContext, env=" + env);
          Object data = ctx.lookup("TestJNDIKey");
          System.out.println("nntlookup(MyJNDIName): " + (String)data);
         }
   }

.
.
Thanks
Middleware Magic Team

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.