users@glassfish.java.net

Re: Glassfish v2 - JNDI - String or Properties or Bean

From: <glassfish_at_javadesktop.org>
Date: Mon, 25 May 2009 09:47:54 PDT

You can have a factory like the class below (packaged as a .jar) available to GlassFish (eg; GF_INSTALL_DIR/lib)

Configure a custom-resource (eg: jndi-name=custom/double) with factory-class as PrimitivesFactory and type as java.lang.Double, with two properties

type="double"
value="22.1"

Now, your application can do (new InitialContext().)lookup("custom/double") which will return 22.1




public class PrimitivesFactory implements Serializable, ObjectFactory {

    public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
        Reference ref = (Reference)obj;

        Enumeration<RefAddr> refAddrs = ref.getAll();
        String type = null;
        String value = null;
        while(refAddrs.hasMoreElements()){
            RefAddr addr = refAddrs.nextElement();
            String propName = addr.getType();

            if (propName.equalsIgnoreCase("type")) {
            type = (String)addr.getContent();
            }

            if(propName.equalsIgnoreCase("value")){
            value = (String)addr.getContent();
            }
        }

        if(type != null && value != null){
            type = type.toUpperCase();
            if(type.equals("INT") || type.equals("INTEGER")){
                return Integer.valueOf(value);
            } else if (type.equals("LONG")){
                return Long.valueOf(value);
            } else if(type.equals("DOUBLE")){
                return Double.valueOf(value);
            } else if(type.equals("FLOAT") ){
                return Float.valueOf(value);
            } else if(type.equals("CHAR") || type.equals("CHARACTER")){
                return value.charAt(0);
            } else if(type.equals("SHORT")){
                return Short.valueOf(value);
            } else if(type.equals("BYTE")){
                return Byte.valueOf(value);
            } else if(type.equals("BOOLEAN")){
                return Boolean.valueOf(value);
            } else if(type.equals("STRING")){
                return value;
            }
        }
        return null;
    }
}
[Message sent by forum member 'jr158900' (jr158900)]

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