1 25 26 package org.objectweb.easybeans.container; 27 28 import java.lang.reflect.Method ; 29 import java.util.List ; 30 import java.util.Map ; 31 32 import javax.naming.Context ; 33 import javax.naming.NamingException ; 34 35 import org.objectweb.easybeans.api.EZBContainer; 36 import org.objectweb.easybeans.api.EZBPermissionManager; 37 import org.objectweb.easybeans.api.Factory; 38 import org.objectweb.easybeans.api.FactoryException; 39 import org.objectweb.easybeans.api.bean.EasyBeansBean; 40 import org.objectweb.easybeans.api.injection.EasyBeansInjectionException; 41 import org.objectweb.easybeans.api.injection.ResourceInjector; 42 import org.objectweb.easybeans.api.pool.Pool; 43 import org.objectweb.easybeans.api.pool.PoolException; 44 import org.objectweb.easybeans.log.JLog; 45 import org.objectweb.easybeans.log.JLogFactory; 46 import org.objectweb.easybeans.naming.NamingManager; 47 import org.objectweb.easybeans.rpc.api.EJBRequest; 48 import org.objectweb.easybeans.rpc.api.EJBResponse; 49 import org.objectweb.easybeans.rpc.util.Hash; 50 51 57 public abstract class AbsFactory<PoolType extends EasyBeansBean> implements Factory<PoolType, Long > { 58 59 62 private static JLog logger = JLogFactory.getLog(AbsFactory.class); 63 64 67 private String className = null; 68 69 72 private EZBContainer container = null; 73 74 77 private Pool<PoolType, Long > pool = null; 78 79 82 private Class <PoolType> beanClass = null; 83 84 87 private Context javaContext = null; 88 89 92 private static NamingManager namingManager = null; 93 94 97 private List <ResourceInjector> injectors = null; 98 99 104 private Map <Long , Method > hashes = null; 105 106 112 @SuppressWarnings ("unchecked") 113 public AbsFactory(final String className, final EZBContainer container) throws FactoryException { 114 this.className = className; 115 this.container = container; 116 Class clazz = null; 117 try { 118 clazz = getContainer().getClassLoader().loadClass(getClassName()); 119 } catch (ClassNotFoundException e) { 120 throw new FactoryException("Cannot load the class for class name '" + getClassName() + "'", e); 121 } 122 setBeanClass(clazz); 123 setHashes(Hash.hashClass(clazz)); 124 125 try { 126 namingManager = NamingManager.getInstance(); 127 } catch (NamingException e) { 128 throw new FactoryException("Cannot get instance of the naming manager", e); 129 } 130 131 this.injectors = container.getConfiguration().getInjectors(); 132 } 133 134 138 public void remove(final PoolType instance) { 139 ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader(); 140 Thread.currentThread().setContextClassLoader(getContainer().getClassLoader()); 141 try { 142 instance.preDestroyEasyBeansLifeCycle(); 144 } catch (Exception e) { 145 logger.error("Could not complete preDestroy method on instance", e); 146 } finally { 147 Thread.currentThread().setContextClassLoader(oldClassLoader); 148 } 149 } 150 151 156 protected void injectResources(final PoolType instance) throws PoolException { 157 158 for (ResourceInjector injector : injectors) { 160 try { 161 injector.preEasyBeansInject(instance); 162 } catch (Throwable t) { 163 logger.error("preEasyBeansInject() for {0} failed", injector.getClass().getName(), t); 165 } 166 } 167 168 try { 170 instance.injectedByEasyBeans(); 171 } catch (EasyBeansInjectionException e) { 172 throw new PoolException("Cannot inject resources in the created bean", e); 173 } 174 175 for (ResourceInjector injector : injectors) { 177 try { 178 injector.postEasyBeansInject(instance); 179 } catch (Throwable t) { 180 logger.error("postEasyBeansInject() for {0} failed", injector.getClass().getName(), t); 182 } 183 } 184 } 185 186 190 protected Map <Long , Method > getHashes() { 191 return hashes; 192 } 193 194 198 protected void setHashes(final Map <Long , Method > hashes) { 199 this.hashes = hashes; 200 } 201 202 206 public Context getJavaContext() { 207 return javaContext; 208 } 209 210 214 public void setJavaContext(final Context javaContext) { 215 if (this.javaContext != null) { 217 throw new IllegalStateException ("The javaContext can only be set once. Already set !"); 218 } 219 this.javaContext = javaContext; 220 } 221 222 226 public Class <PoolType> getBeanClass() { 227 return beanClass; 228 } 229 230 234 protected void setBeanClass(final Class <PoolType> beanClass) { 235 this.beanClass = beanClass; 236 } 237 238 242 protected void setPool(final Pool<PoolType, Long > pool) { 243 this.pool = pool; 244 } 245 246 250 public EZBContainer getContainer() { 251 return container; 252 } 253 254 258 public String getClassName() { 259 return className; 260 } 261 262 266 protected static NamingManager getNamingManager() { 267 return namingManager; 268 } 269 270 271 275 public Pool<PoolType, Long > getPool() { 276 return pool; 277 } 278 279 285 public abstract EJBResponse rpcInvoke(final EJBRequest request); 286 287 288 292 public void init() throws FactoryException { 293 294 } 295 296 } 297 | Popular Tags |