users@glassfish.java.net

Re: How to do simple JNDI String bindings?

From: <glassfish_at_javadesktop.org>
Date: Wed, 19 Nov 2008 02:05:05 PST

If you want one String, you probably will soon need more ;-)

Here is a factory class for Properties, in which we store all of our custom configuration. You place this class somewhere on your Glassfish server classpath.

public class JNDIPropertiesFactory implements ObjectFactory {

        /* (non-Javadoc)
         * @see javax.naming.spi.ObjectFactory#getObjectInstance(java.lang.Object, javax.naming.Name, javax.naming.Context, java.util.Hashtable)
         */
        public Object getObjectInstance(Object obj,
                                                                        Name name,
                                                                        Context nameCtx,
                                                                        Hashtable<?, ?> environment) throws Exception {
                if (!(obj instanceof Reference)) {
                        return null;
                }

        Properties config = new Properties();

        Reference ref = (Reference)obj;
        for (Enumeration<RefAddr> en = ref.getAll(); en.hasMoreElements(); ) {
                RefAddr ra = en.nextElement();
                config.put(ra.getType(), String.valueOf(ra.getContent()));
        }

        return config;
        }
}

You can then add name/value pairs to this using Resources/JNDI/Custom Resources - "Resource Type" is java.util.Properties, "Factory Class" is JNDIPropertiesFactory.

In your EJB you can get at those settings using :

Properties pros = new InitialContext().lookup("jndi/name");

Hope that helps, Ed
[Message sent by forum member 'edrandall' (edrandall)]

http://forums.java.net/jive/thread.jspa?messageID=317547