I intend to define a JNDI propery of the type java.util.Properties
in the WildFly application server to read it from my application.
As described in the an older WildFly documentation one can create a global binding of the type object-factory. In the example is an optional environment
tag to hold multiple key/value pairs. That would map exactly my desire to get a java.util.Properties
when reading a JNDI resource.
The question is weather there is already an implementiation of javax.naming.spi.ObjectFactory
to create a java.util.Properties
object out of an object-factory binding or do I need to implement it myself and install it as a separate module (like it is described at mastertheboss.com/... )?
Ok, I just implemented it mysef.
package com.myorg.wildfly.objectfactory;
...
public class WildFlyPropertiesObjectFactory implements ObjectFactory
{
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment) throws Exception
{
Properties p = new properties();
if(environment !=null)
{
Set<?> entrySet = environment.keySet();
for(Object o : entrySet)
{
p.put(o, environment.get(o));
}
}
return p;
}
}
Clean&Build this single class into a JAR file wildfly-properties-objetfactory.jar
Add a module via jboss-cli.sh
module add --name=my.jndi.propertyreader --resource=wildfly-properties-objetfactory.jar --dependencies=javax.api
Create a naming binding via jboss-cli.sh
/subsystem=nameing/binding=java\:my_super_cool_jndi_name:add(binding-type=object-factory, module=my.jndi.propertyreader, class=com.myorg.wildfly.objectfactory.WildFlyPropertiesObjectFactory, environment=[key1=value1, key2=value2])
And in my application I can access it in an CDI bean via
public void test() {
Properties p = (Properties) new InitialContext().lookup("java:my_super_cool_jndi_name");
System.out.printf("Properties: %s%n", p);
}
Or as shown in the mentioned link inject it as a direct dependency @Resource(lookup = "java:my_super_cool_jndi_name") Properties p;
Works as a charm.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.