1 18 package org.apache.activemq.jndi; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 import javax.naming.spi.ObjectFactory ; 24 import javax.naming.Name ; 25 import javax.naming.Context ; 26 import javax.naming.Reference ; 27 import javax.naming.StringRefAddr ; 28 import javax.naming.NamingException ; 29 import java.util.Hashtable ; 30 import java.util.Properties ; 31 import java.util.Enumeration ; 32 33 37 public class JNDIReferenceFactory implements ObjectFactory { 38 39 static Log log = LogFactory.getLog(JNDIReferenceFactory.class); 40 41 53 public Object getObjectInstance(Object object, Name name, Context nameCtx, Hashtable environment) throws Exception { 54 Object result = null; 55 if (object instanceof Reference ) { 56 Reference reference = (Reference ) object; 57 58 if (log.isTraceEnabled()) { 59 log.trace("Getting instance of " + reference.getClassName()); 60 } 61 62 Class theClass = loadClass(this, reference.getClassName()); 63 if (JNDIStorableInterface.class.isAssignableFrom(theClass)) { 64 65 JNDIStorableInterface store = (JNDIStorableInterface) theClass.newInstance(); 66 Properties properties = new Properties (); 67 for (Enumeration iter = reference.getAll(); iter.hasMoreElements();) { 68 69 StringRefAddr addr = (StringRefAddr ) iter.nextElement(); 70 properties.put(addr.getType(), (addr.getContent() == null) ? "" : addr.getContent()); 71 72 } 73 store.setProperties(properties); 74 result = store; 75 } 76 } 77 else { 78 log.error("Object " + object + " is not a reference - cannot load"); 79 throw new RuntimeException ("Object " + object + " is not a reference"); 80 } 81 return result; 82 } 83 84 92 93 public static Reference createReference(String instanceClassName, JNDIStorableInterface po) throws NamingException { 94 if (log.isTraceEnabled()) { 95 log.trace("Creating reference: " + instanceClassName + "," + po); 96 } 97 Reference result = new Reference (instanceClassName, JNDIReferenceFactory.class.getName(), null); 98 try { 99 Properties props = po.getProperties(); 100 for (Enumeration iter = props.propertyNames(); iter.hasMoreElements();) { 101 String key = (String ) iter.nextElement(); 102 String value = props.getProperty(key); 103 javax.naming.StringRefAddr addr = new javax.naming.StringRefAddr (key, value); 104 result.add(addr); 105 } 106 } 107 catch (Exception e) { 108 log.error(e.getMessage(), e); 109 throw new NamingException (e.getMessage()); 110 } 111 return result; 112 } 113 114 122 123 public static Class loadClass(Object thisObj, String className) throws ClassNotFoundException { 124 ClassLoader loader = thisObj.getClass().getClassLoader(); 126 Class theClass; 127 if (loader != null) { 128 theClass = loader.loadClass(className); 129 } 130 else { 131 theClass = Class.forName(className); 134 } 135 return theClass; 136 } 137 138 } 139 | Popular Tags |