You can bind a reference for dynamic values into the default Java™ Naming and Directory Interface (JNDI) namespace from the server
configuration files by using the jndiReferenceEntry
element in Liberty.
About this task
The default JNDI namespace is available in Liberty to provide bindings to miscellaneous
objects that are required by applications. Based on the features that are enabled in your server,
you can bind a predetermined set of objects to the default JNDI namespace. Additionally, you can
bind a reference to an object factory, which dynamically determines the value that it returns. You
can also use this object factory to return custom object types to an application.
For more information about the JNDI naming, see Naming.
Procedure
-
Add the
jndi-1.0
Liberty feature to the
server.xml file.
<featureManager>
<feature>jndi-1.0</feature>
</featureManager>
- 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;
}
}
- 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>
- 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"/>
You 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"/>
- 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"/>
- 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;
}
}
-
You can inject the resulting object to an application by using a resource environment
reference:
@Resource(name="ref/entry")
private Properties properties;