+

Search Tips   |   Advanced Search

Use JNDI binding for dynamic values from the server configuration files

We can bind a reference for dynamic values into the default Java Naming and Directory Interface (JNDI) namespace from the server configuration files using the jndiReferenceEntry element on the Liberty profile.

The default JNDI namespace is available in the Liberty profile to provide bindings to miscellaneous objects that are required by applications. Based on the features that are enabled in the server, we can bind a predetermined set of objects to the default JNDI namespace. Additionally, we can bind a reference to an object factory, which dynamically determines the value that it returns. We can also use this object factory to return custom object types to an application.

For more information about the JNDI naming, see Naming.

  1. Add the jndi-1.0 Liberty feature to the server.xml file.
    <featureManager>
       <feature>jndi-1.0</feature>
    </featureManager>

  2. Create an ObjectFactory class that returns a programmatically defined value.
    import javax.naming.spi.ObjectFactory;
    
    public class MyObjectFactory implements ObjectFactory {
      @Override
      public Object getObjectInstance(Object o, Name n, Context c, Hashtable<?, ?> envmt) throws Exception { 
        Properties p = new Properties();
        p.put("abc", 123);
        return p; 
      }
    }    

  3. Include the ObjectFactory in a library element in the server.xml file:
    <library id="objectFactoryLib"> 
       <fileset dir="${server.config.dir}/lib" includes="factory.jar"/>  
    </library>

  4. Declare the factory in a jndiObjectFactory element in the server.xml file and reference the previously declared library.
    <jndiObjectFactory id="objectFactory" libraryRef="objectFactoryLib"                      
             className="com.ibm.example.factory.MyObjectFactory"/>

    We can also declare the type of object that the factory returns. The type is returned by the javax.naming.Context.list() method.

    <jndiObjectFactory id="objectFactory" libraryRef="objectFactoryLib"                      
             className="com.ibm.example.factory.MyObjectFactory"                      
             objectClassName="java.util.Properties"/>

  5. Declare the entry in a jndiReferenceEntry element in the server.xml file and reference the previously declared factory.

      <jndiReferenceEntry id="refEntry" jndiName="ref/entry" factoryRef="objectFactory"/>

  6. To declare more properties for the jndiReferenceEntry element in the server.xml file:
    <jndiReferenceEntry id="refEntry" jndiName="ref/entry" factoryRef="objectFactory"> 
       <properties abc="123"/>  
    </jndiReferenceEntry>

    These additional properties are represented as javax.naming.StringRefAddr on the javax.naming.Reference that is passed to the factory:

    import javax.naming.spi.ObjectFactory; 
    
    public class MyObjectFactory implements ObjectFactory { 
      @Override 
      public Object getObjectInstance(Object o, Name n, Context c, Hashtable<?, ?> envmt) throws Exception {
        Properties p = new Properties();           
        Reference ref = (Reference) o;
        RefAddr refAddr = ref.get("abc"); 
        p.put("abc", refAddr == null ? 123 : refAddr.getContent()); 
        return p; 
      }   
    }

  7. We can inject the resulting object from an application using a resource environment reference:
    @Resource(name="ref/entry")
    private Properties properties;


Parent topic: Deploy applications to the Liberty profile

Tasks:

  • Use JNDI binding for constants from the server configuration files