Carbon JNDI Resources
Java Naming and Directory Interface (JNDI) is a Java Application Programming Interface (API) providing naming and directory functionality for Java software clients to discover and look up data and objects via a name. Carbon maintains platform level JNDI resources such as Carbon DataSources, Transaction Manager etc. Applications deployed in WSO2 Application Server (such as, web applications, Axis2 services etc.) can access them using an InitialContext
object.
Example
Context initCtx = new InitialContext(); DataSource ds = (DataSource) initialContext.lookup("jndi_name");
Carbon JNDI resources inherit the multi tenant behavior from the Carbon platform. As a result, JNDI resources registered at tenant level can only be accessed by that particular tenant.
JNDI resources for web apps
WSO2 Applications Server deploys the web applications on embedded Tomcat server. As a result, the JNDI support provided by the Tomcat server is available for the web application. In addition to Tomcat level JNDI resources, web applications can lookup Carbon JNDI resources as mentioned above.
Registering a Tomcat JNDI resource
For more details on Tomcat JNDI resources, go to http://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html
Webapp level registering
To register a JNDI resource on webapp level, place the JNDI resource in the context.xml
file of the webapp. Since each webapp’s context is isolated from each other, this ensures that the resources will be available to the webapp context only.
Example
<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"/>
The above example defines a database resource and its properties. You can define any number of resources. Properties of a resource should go inside its <Resource
> element.
Global registering
Place resources under the <GlobalNamingResources
> element in Tomcat's server configuration file (<PRODUCT_HOME>repository/conf/tomcat/catalina-server.xml
in WSO2 products).
<GlobalNamingResources> <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"/> </GlobalNamingResources>
Then, the resources can be referenced by linking them in the webapp's context.xml
file. Global resources are visible to all webapps deployed in the server.
Example
<ResourceLink name="jdbc/TestDataSource" global="jdbc/TestDataSource" type="javax.sql.DataSource"/>
Global naming resources are available for all the tenants. Web applications deployed in any tenant can access them by referencing them inside the web applications.
Accessing JNDI resources from web applications
Application Server 5.2.0 allows web applications to access both Tomcat and Carbon JNDI resources using an InitialContext
object. However, Tomcat level JNDI resources get the precedence over Carbon level JNDI resources. As a result, when a lookup call is made using an InitialContext
object, it first looks up in the Tomcat JNDI resources of that web app. If there are no resources bound for the given name in the Tomcat JNDI context of that web app, then it will look up in the Carbon JNDI context. If there is a resource bound for that name in the Carbon context, then it will return that resource.
In web applications Carbon JNDI resources are available only for lookup calls. That means methods such as listBindings()
will return only the list of the Tomcat level JNDI resources.
Accessing Tomcat JNDI resources
Application Server 5.2.0 allows web applications to access Tomcat JNDI resources in the same manner as a native Tomcat server.
Example
Context initCtx = new InitialContext(); DataSource ds = (DataSource) initialContext.lookup("java:comp/env/jdbc/TestDataSource");
Or
Context initCtx = new InitialContext(environment); Context context = (Context) initCtx.lookup("java:comp/env"); DataSource ds = (DataSource) context.lookup("jdbc/TestDataSource");