1 17 18 19 package org.apache.naming.factory; 20 21 import java.util.Hashtable ; 22 import javax.naming.Name ; 23 import javax.naming.Context ; 24 import javax.naming.InitialContext ; 25 import javax.naming.NamingException ; 26 import javax.naming.Reference ; 27 import javax.naming.RefAddr ; 28 import javax.naming.spi.ObjectFactory ; 29 import org.apache.naming.EjbRef; 30 31 37 38 public class EjbFactory 39 implements ObjectFactory { 40 41 42 44 45 47 48 50 51 53 54 56 57 62 public Object getObjectInstance(Object obj, Name name, Context nameCtx, 63 Hashtable environment) 64 throws Exception { 65 66 if (obj instanceof EjbRef) { 67 Reference ref = (Reference ) obj; 68 69 RefAddr linkRefAddr = ref.get(EjbRef.LINK); 71 if (linkRefAddr != null) { 72 String ejbLink = linkRefAddr.getContent().toString(); 74 Object beanObj = (new InitialContext ()).lookup(ejbLink); 75 101 return beanObj; 102 } 103 104 ObjectFactory factory = null; 105 RefAddr factoryRefAddr = ref.get(Constants.FACTORY); 106 if (factoryRefAddr != null) { 107 String factoryClassName = 109 factoryRefAddr.getContent().toString(); 110 ClassLoader tcl = 112 Thread.currentThread().getContextClassLoader(); 113 Class factoryClass = null; 114 if (tcl != null) { 115 try { 116 factoryClass = tcl.loadClass(factoryClassName); 117 } catch(ClassNotFoundException e) { 118 NamingException ex = new NamingException 119 ("Could not load resource factory class"); 120 ex.initCause(e); 121 throw ex; 122 } 123 } else { 124 try { 125 factoryClass = Class.forName(factoryClassName); 126 } catch(ClassNotFoundException e) { 127 NamingException ex = new NamingException 128 ("Could not load resource factory class"); 129 ex.initCause(e); 130 throw ex; 131 } 132 } 133 if (factoryClass != null) { 134 try { 135 factory = (ObjectFactory ) factoryClass.newInstance(); 136 } catch(Throwable t) { 137 } 138 } 139 } else { 140 String javaxEjbFactoryClassName = 141 System.getProperty("javax.ejb.Factory", 142 Constants.OPENEJB_EJB_FACTORY); 143 try { 144 factory = (ObjectFactory ) 145 Class.forName(javaxEjbFactoryClassName).newInstance(); 146 } catch(Throwable t) { 147 if (t instanceof NamingException ) 148 throw (NamingException ) t; 149 NamingException ex = new NamingException 150 ("Could not create resource factory instance"); 151 ex.initCause(t); 152 throw ex; 153 } 154 } 155 156 if (factory != null) { 157 return factory.getObjectInstance 158 (obj, name, nameCtx, environment); 159 } else { 160 throw new NamingException 161 ("Cannot create resource instance"); 162 } 163 164 } 165 166 return null; 167 168 } 169 170 171 } 172 173 | Popular Tags |