How to register JNDI resources for webapps in WSO2 Application Server

WSO2 Application Server uses Embedded Tomcat as its underlying servlet container. This gives the advantage of inheriting all the features supported by Apache Tomcat. With this support WSO2 Application Server brings together the webapp hosting facility along with web-services hosting facility which comes form from Apache Axis2.

Supporting JNDI InitialContext implementation is one of the requirement for a Java EE Application Server. From 4.5.0 release onwards, WSO2 Application Server gets this support by inheriting the JNDI implementation of Tomcat.

In this post we will look into the details of how to register JNDI resources for webapps and how to access them in WSO2 Application Server 4.5.0 onwards.

First, we have to register JNDI resources for webapps context. Then only we can lookup for them. Registering JNDI resources can be done in two ways. They can be either per webapp or global.

If it is per webapp, then they can be registered by placing them in the context.xml file of the webapp it self. These resources will only be available to that particular webapp context only. Because each webapp’s context is isolated from each other.

Eg –

<Resource name="jdbc/TestDataSource" auth="Container"
          type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
          maxActive="100" maxIdle="30" maxWait="10000"
          url="jdbc:mysql://localhost:3306/test_db"
          username="root" password="root"/>

Other properties for these resources should go inside each respective Resource tags. We can define any number of resources as above but please make sure those are within tag. In the above example I have defined a Resoure used to define a database and other properties related to that database.

If it is global, you have place them under <GlobalNamingResources> tag in tomcat’s server server configuration file (catalina-server.xml in WSO2 AS). Then they can be referenced in webapp via linking them in the context.xml file of webapp. Globan resources are visible to all webapps deployed in the tomcat.

Eg –

<ResourceLink name="jdbc/TestDataSource"
         global="jdbc/TestDataSource"
         type="javax.sql.DataSource"/>

You have to make sure those classes used to define resource-type are in the classpath.

Finally have a reference for those resources in the web.xml of the webapp.

Eg –

<resource-ref>
 <description>Your Description</description>
 <res-ref-name>jdbc/TestDataSource</res-ref-name>
 <res-type>javax.sql.DataSource</res-type>
 <res-auth>Container</res-auth>
</resource-ref>

Now we are done with defining JNDI resources. Next lets look at how to lookup them from a webapp.

Context initCtx = new InitialContext();
SelectorContext selectorContext = 
   new SelectorContext((Hashtable) initCtx.getEnvironment(), false);
Context envCtx = (Context) selectorContext.lookup("java:comp/env");

Here the SelectorContext is the Tomcat JNDI Context implementation. First using the IntialContext environment we have to build the Catalina selector context, and then we can lookup from that.

The we can look up for the resource which we defined earlier.

DataSource ds = (DataSource) envCtx.lookup("jdbc/TestDataSource");

Thats all. Now we have the JDBC DataSource at hand, form this we can do the related operations.

Sample to Test

I have added a sample in here. This demonstrates the configurations and accessing resources defined as Global Naming Resources.  Following are the steps on how to run this.

1. Build the bean and factory implementation found in /factroy. This is based on bean custom fictory implementation mentioned in here.
2. Copy the compiled jar file in /target directory to {WSO2_Application_server_HOME}/repository/components/lib/
3. Add the following global namaing reosurce entry to {WSO2_Application_Server_HOME}/repository/conf/tomcat/catalina-server.xml

<GlobalNamingResources>
    <Resource name="bean/MyBeanFactory" auth="Container" type="org.kishanthan.sample.javabean.bean.MyBean"
    factory="org.kishanthan.sample.javabean.factory.MyBeanFactory" bar="456" foo="987"/>
</GlobalNamingResources>

4. Start the Application Server.
5. Build the sample. This will generate the war file in target directory. The Global Resource in referenced in this webapp via <ResourceLink> as follows:

<Context debug="5" reloadable="true" crossContext="true">
   <ResourceLink name="bean/MyBeanFactory" global="bean/MyBeanFactory" auth="Container"
   type="org.kishanthan.sample.javabean.bean.MyBean"/>
</Context>

6. Deploy the webapp in Applcaition Server and access it.

If you have trouble building this sample, I also have added the built distribution of both factory and the webapp in here.

When accessing the bean servlet, you will see that the defined values for “foo” and “bar” will get printed.

About kishanthan

I’m currently working as a Software Engineer at WSO2, an open source software company. I hold an Engineering degree, majoring in Computer Science & Engineering field, from University of Moratuwa, Sri Lanka.
This entry was posted in How to, Java, Tomcat, WSO2 and tagged , , , , , , . Bookmark the permalink.

4 Responses to How to register JNDI resources for webapps in WSO2 Application Server

  1. Peter Meyer says:

    Hi Kishanthan,

    the following config works in a normal Tomcat, but not in WSO2:
    GlobalNamingResources
    Resource factory=”org.demo.jndi.URLResourceFactory” name=”url/ZoneConfiguration”
    type=”java.net.URL” url=”file:///c:/temp/local.properties”/
    /GlobalNamingResources

    Is this a limitation?

    Thanks
    Peter

  2. kishanthan says:

    Hi Peter,
    I assume that you have defined the resource as GlobalNamingResources in $WSO2AS_HOME/repository/conf/tomcat/catalina-server.xml under the “<Server>” configuration and you are trying to access GlobalResources in your webapp via the ResourceLink option in your webapp’s context.xml file.
    What is the error you getting here? The GlobalNamingResources option works as expected in the 5.0.0 release of WSO2 AS.

  3. Pingback: Access Cabon Data Sources within webapps in WSO2 Application Server | Kishanthan's Blog

  4. kishanthan says:

    Hi Peter,

    You can refer the “Sample to Test” section on the above post. I have included a sample which is based in custom resource factory [2] implementation and global naming resources. You can also place your factory impl jar under $CARBON_HOME/lib/endorsed. I have tested that as-well.

    Kishanthan.
    [1] http://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html#Adding_Custom_Resource_Factories

Leave a reply to Peter Meyer Cancel reply