KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > container > AbsFactory


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: AbsFactory.java 1121 2006-09-27 08:51:06Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.container;
27
28 import java.lang.reflect.Method JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import javax.naming.Context JavaDoc;
33 import javax.naming.NamingException JavaDoc;
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 /**
52  * Abstract factory which implements common and defaults methods.<br>
53  * It should be extended by Bean factories.
54  * @param <PoolType> the type of bean instance.
55  * @author Florent Benoit
56  */

57 public abstract class AbsFactory<PoolType extends EasyBeansBean> implements Factory<PoolType, Long JavaDoc> {
58
59     /**
60      * Logger.
61      */

62     private static JLog logger = JLogFactory.getLog(AbsFactory.class);
63
64     /**
65      * Name of the class of the managed bean.
66      */

67     private String JavaDoc className = null;
68
69     /**
70      * Container that created this factory.
71      */

72     private EZBContainer container = null;
73
74     /**
75      * Pool that manage beans instance.
76      */

77     private Pool<PoolType, Long JavaDoc> pool = null;
78
79     /**
80      * Class used to build bean's instance.
81      */

82     private Class JavaDoc<PoolType> beanClass = null;
83
84     /**
85      * Context for java: lookups.
86      */

87     private Context JavaDoc javaContext = null;
88
89     /**
90      * Reference on the naming manager.
91      */

92     private static NamingManager namingManager = null;
93
94     /**
95      * List of external Resources injectors.
96      */

97     private List JavaDoc<ResourceInjector> injectors = null;
98
99     /**
100      * Keep a direct reference to the method so that we don't need to compute
101      * each time the method object to invoke.<br>
102      * http://java.sun.com/j2se/1.5.0/docs/guide/rmi/spec/rmi-stubs24.html
103      */

104     private Map JavaDoc<Long JavaDoc, Method JavaDoc> hashes = null;
105
106     /**
107      * Builds a new factory with a given name and its container.
108      * @param className name of this factory (name of class that is managed)
109      * @param container the root component of this factory.
110      * @throws FactoryException if class can't be loaded.
111      */

112     @SuppressWarnings JavaDoc("unchecked")
113     public AbsFactory(final String JavaDoc className, final EZBContainer container) throws FactoryException {
114         this.className = className;
115         this.container = container;
116         Class JavaDoc clazz = null;
117         try {
118             clazz = getContainer().getClassLoader().loadClass(getClassName());
119         } catch (ClassNotFoundException JavaDoc 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 JavaDoc e) {
128             throw new FactoryException("Cannot get instance of the naming manager", e);
129         }
130
131         this.injectors = container.getConfiguration().getInjectors();
132     }
133
134     /**
135      * Callback called when object is gonna be removed.
136      * @param instance that is being removed from the pool.
137      */

138     public void remove(final PoolType instance) {
139         ClassLoader JavaDoc oldClassLoader = Thread.currentThread().getContextClassLoader();
140         Thread.currentThread().setContextClassLoader(getContainer().getClassLoader());
141         try {
142             // call callbacks
143
instance.preDestroyEasyBeansLifeCycle();
144         } catch (Exception JavaDoc e) {
145             logger.error("Could not complete preDestroy method on instance", e);
146         } finally {
147             Thread.currentThread().setContextClassLoader(oldClassLoader);
148         }
149     }
150
151     /**
152      * Injects Resources into the Bean.
153      * @param instance The Bean instance to be injected.
154      * @throws PoolException if resources cannot be injected.
155      */

156     protected void injectResources(final PoolType instance) throws PoolException {
157
158         // call external resources injectors
159
for (ResourceInjector injector : injectors) {
160             try {
161                 injector.preEasyBeansInject(instance);
162             } catch (Throwable JavaDoc t) {
163                 // Protection from malicious code
164
logger.error("preEasyBeansInject() for {0} failed", injector.getClass().getName(), t);
165             }
166         }
167
168         // call dependency injection
169
try {
170             instance.injectedByEasyBeans();
171         } catch (EasyBeansInjectionException e) {
172             throw new PoolException("Cannot inject resources in the created bean", e);
173         }
174
175         // call external resources injectors
176
for (ResourceInjector injector : injectors) {
177             try {
178                 injector.postEasyBeansInject(instance);
179             } catch (Throwable JavaDoc t) {
180                 // Protection from malicious code
181
logger.error("postEasyBeansInject() for {0} failed", injector.getClass().getName(), t);
182             }
183         }
184     }
185
186     /**
187      * Gets the computed hashes.
188      * @return computed hashes
189      */

190     protected Map JavaDoc<Long JavaDoc, Method JavaDoc> getHashes() {
191         return hashes;
192     }
193
194     /**
195      * Sets the hashes for the current bean class.
196      * @param hashes method hashes computed as RMI hashes
197      */

198     protected void setHashes(final Map JavaDoc<Long JavaDoc, Method JavaDoc> hashes) {
199         this.hashes = hashes;
200     }
201
202     /**
203      * Gets the java: context.
204      * @return java: context.
205      */

206     public Context JavaDoc getJavaContext() {
207         return javaContext;
208     }
209
210     /**
211      * Sets the java: context.
212      * @param javaContext the java: context.
213      */

214     public void setJavaContext(final Context JavaDoc javaContext) {
215         // Can be only set once
216
if (this.javaContext != null) {
217             throw new IllegalStateException JavaDoc("The javaContext can only be set once. Already set !");
218         }
219         this.javaContext = javaContext;
220     }
221
222     /**
223      * Gets the bean's class.
224      * @return bean class used to instantiate beans.
225      */

226     public Class JavaDoc<PoolType> getBeanClass() {
227         return beanClass;
228     }
229
230     /**
231      * Sets the bean class that will be used to build bean's instance.
232      * @param beanClass the instance of the bean class name
233      */

234     protected void setBeanClass(final Class JavaDoc<PoolType> beanClass) {
235         this.beanClass = beanClass;
236     }
237
238     /**
239      * Sets the pool used by this factory.
240      * @param pool the pool which managed bean instances
241      */

242     protected void setPool(final Pool<PoolType, Long JavaDoc> pool) {
243         this.pool = pool;
244     }
245
246     /**
247      * Gets the container used by this factory.
248      * @return container of this factory
249      */

250     public EZBContainer getContainer() {
251         return container;
252     }
253
254     /**
255      * Gets the className used by this factory.
256      * @return classname that will be instantiated to build bean instance.
257      */

258     public String JavaDoc getClassName() {
259         return className;
260     }
261
262     /**
263      * Gets the reference on the naming manager.
264      * @return the reference on the naming manager.
265      */

266     protected static NamingManager getNamingManager() {
267         return namingManager;
268     }
269
270
271     /**
272      * Gets the pool used by this factory.
273      * @return pool.
274      */

275     public Pool<PoolType, Long JavaDoc> getPool() {
276         return pool;
277     }
278
279     /**
280      * A request comes to the bean factory and needs to be handled.<br>
281      * A response is done which contains the answer.
282      * @param request the EJB request.
283      * @return a response that have been processed by the factory.
284      */

285     public abstract EJBResponse rpcInvoke(final EJBRequest request);
286
287
288     /**
289      * Init the factory.
290      * @throws FactoryException if the initialization fails.
291      */

292     public void init() throws FactoryException {
293
294     }
295
296 }
297
Popular Tags