users@glassfish.java.net

Re: How to do simple JNDI String bindings?

From: <glassfish_at_javadesktop.org>
Date: Fri, 27 Feb 2009 15:29:20 PST

The way we do it is to register a JNDI binding to a URLResourceFactory. Then the URL points to an external property file. Within that is the string/value pair.

Then in our code to access it, it looks something like this:

                        URL url = (java.net.URL) new InitialContext().lookup("url/MyPropFile");
                        Properties p = new Properties();
                        p.load(new FileInputStream(url.getFile()));

                        propertyValue = p.getProperty("connectionUrl");

However, I agree that for a Single String property, it would be nice to have the ability to bind a String to JNDI tree directly.

Also, bind a URL instead of a Properties object because things like XML docs, http URLs, etc can then all use this URLResourceFactory.

public class URLResourceFactory implements ObjectFactory {

        /**
         * @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 {
                Reference ref = (Reference) obj;
                RefAddr addr = ref.get("url");
                if (addr == null) {
                        throw new NamingException("Missing parameter with key 'url'");
                }
                
                return new URL((String) addr.getContent());
        }

}
[Message sent by forum member 'athrawn17' (athrawn17)]

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