JBoss.orgCommunity Documentation

Chapter 66. JNDI naming

66.1. Prerequisites
66.2. How it works
66.2.1. JNDI System property initialization
66.2.2. JNDI reference binding
66.3. Configuration examples
66.4. Recommendations for Application Developers
66.5. InitialContextInitializer API

We need to configure JNDI environment properties and Reference binding with the eXo container standard mechanism.

The Naming service covers:

Make sure you understand the Java Naming and Directory InterfaceTM (JNDI) concepts before using this service.

The InitialContextInitializer configuration example:

  <component>
    <type>org.exoplatform.services.naming.InitialContextInitializer</type>
    <init-params>
      <value-param>.
        <name>bindings-store-path</name>.
        <value>bind-references.xml</value>.
      </value-param>.
      <value-param> 
        <name>overload-context-factory</name> 
        <value>true</value> 
      </value-param>
      <properties-param>
        <name>default-properties</name>
        <description>Default initial context properties</description>
        <property name="java.naming.factory.initial" value="org.exoplatform.services.naming.SimpleContextFactory"/>
      </properties-param>
      <properties-param>
        <name>mandatory-properties</name>
        <description>Mandatory initial context properties</description>
        <property name="java.naming.provider.url" value="rmi://localhost:9999"/>
      </properties-param>
    </init-params>
  </component>

where

binding-store-path is file path which stores binded datasources in runtime

overload-context-factory allows to overload the default initial context factory by a context factory that is ExoContainer aware and that is able to delegate to the original initial context factory if it detects that it is not in the eXo scope. By default the feature is disabled since it is only required on AS that don't share the objects by default like tomcat but unlike JBoss AS

The BindReferencePlugin component plugin configuration example (for JDBC datasource):

  <component-plugins> 
    <component-plugin> 
      <name>bind.datasource</name>
      <set-method>addPlugin</set-method>
      <type>org.exoplatform.services.naming.BindReferencePlugin</type>
      <init-params>
        <value-param>
          <name>bind-name</name>
          <value>jdbcjcr</value>
        </value-param>
        <value-param>
          <name>class-name</name>
          <value>javax.sql.DataSource</value>
        </value-param>  
        <value-param>
          <name>factory</name>
          <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
        </value-param>
        <properties-param>
          <name>ref-addresses</name>
          <description>ref-addresses</description>
          <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
          <property name="url" value="jdbc:hsqldb:file:target/temp/data/portal"/>
          <property name="username" value="sa"/>
          <property name="password" value=""/>
        </properties-param>     
      </init-params>    
  </component-plugin>

InitialContextInitalizer also provides feature of references binding in runtime. References have bind in runtime will be persisted and automatically rebinded on a next system start.

Service provides methods for binding reference.

     public void bind(String bindName, 
                      String className, 
                      String factory, 
                      String factoryLocation, 
                      Map<String, String> refAddr) 
                 throws NamingException, FileNotFoundException, XMLStreamException;

Example of usage:

      // obtain InitialContextInitializer instance from ExoContainer (e.g. PortalContainer)
      InitialContextInitializer initContext = (InitialContextInitializer)container.getComponentInstanceOfType(InitialContextInitializer.class);
  
      Map<String, String> refAddr = new HashMap<String, String>();
      refAddr.put("driverClassName", "oracle.jdbc.OracleDriver");
      refAddr.put("url", "jdbc:oracle:thin:@oraclehost:1521:orcl");
      refAddr.put("username", "exouser");
      refAddr.put("password", "exopassword");

      initContext.bind("jdbcexco", "javax.sql.DataSource", "org.apache.commons.dbcp.BasicDataSourceFactory", null, refAddr);

      // try to get just bound DataSource
      DataSource ds = (DataSource)new InitialContext().lookup("jdbcexo");