JBoss.orgCommunity Documentation

Chapter 20. JNDI naming

20.1. Prerequisites
20.2. How it works
20.2.1. JNDI System property initialization
20.2.2. JNDI reference binding
20.3. Configuration examples
20.4. Recommendations for Application Developers
20.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>
      <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>

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. Java temp directory is used to persist references in bind-references.xml file.

Service provide 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");