KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > container > session > SessionFactory


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: SessionFactory.java 1133 2006-10-04 14:30:41Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.container.session;
27
28 import org.objectweb.easybeans.api.EZBContainer;
29 import org.objectweb.easybeans.api.FactoryException;
30 import org.objectweb.easybeans.api.bean.EasyBeansSB;
31 import org.objectweb.easybeans.api.bean.info.IBeanInfo;
32 import org.objectweb.easybeans.api.container.EZBSessionContext;
33 import org.objectweb.easybeans.api.pool.PoolException;
34 import org.objectweb.easybeans.container.AbsFactory;
35 import org.objectweb.easybeans.container.info.SessionBeanInfo;
36 import org.objectweb.easybeans.log.JLog;
37 import org.objectweb.easybeans.log.JLogFactory;
38 import org.objectweb.easybeans.rpc.api.EJBRequest;
39 import org.objectweb.easybeans.rpc.api.EJBResponse;
40
41 /**
42  * This class manages the session bean and its creation/lifecycle.
43  * @param <PoolType> the type of bean instance.
44  * @author Florent Benoit
45  */

46 public abstract class SessionFactory<PoolType extends EasyBeansSB<PoolType>> extends AbsFactory<PoolType> {
47
48     /**
49      * Logger.
50      */

51     private static JLog logger = JLogFactory.getLog(SessionFactory.class);
52
53     /**
54      * Session Desc (deployment info).
55      */

56     private SessionBeanInfo sessionBeanInfo = null;
57
58     /**
59      * Builds a new factory with a given name and its container.
60      * @param className name of this factory (name of class that is managed)
61      * @param container the root component of this factory.
62      * @throws FactoryException if class can't be loaded.
63      */

64     public SessionFactory(final String JavaDoc className, final EZBContainer container) throws FactoryException {
65         super(className, container);
66     }
67
68     /**
69      * Stops the factory.
70      */

71     public void stop() {
72         // remove pool
73
try {
74             getPool().stop();
75         } catch (PoolException e) {
76             logger.error("Problem when stopping the factory", e);
77         }
78
79     }
80
81     /**
82      * @return information of the current bean.
83      */

84     public IBeanInfo getBeanInfo() {
85         return sessionBeanInfo;
86     }
87
88     /**
89      * @return information of the current bean.
90      */

91     public SessionBeanInfo getSessionBeanInfo() {
92         return sessionBeanInfo;
93     }
94
95     /**
96      * Sets the information object for a session bean.
97      * @param sessionBeanInfo information on the bean.
98      */

99     public void setSessionBeanInfo(final SessionBeanInfo sessionBeanInfo) {
100         this.sessionBeanInfo = sessionBeanInfo;
101     }
102
103     /**
104      * Gets a new ID or a null value.
105      * @param beanId given id.
106      * @return new id
107      */

108     protected abstract Long JavaDoc getId(final Long JavaDoc beanId);
109
110     /**
111      * Creates an instance with the given hint.
112      * @param clue a clue given by the Pool. Could be null.
113      * @throws PoolException if instance cannot be created.
114      * @return the created instance.
115      */

116     public PoolType create(final Long JavaDoc clue) throws PoolException {
117         PoolType instance = null;
118         ClassLoader JavaDoc oldClassLoader = Thread.currentThread().getContextClassLoader();
119         Thread.currentThread().setContextClassLoader(getContainer().getClassLoader());
120         try {
121             try {
122                 instance = getBeanClass().newInstance();
123             } catch (InstantiationException JavaDoc e) {
124                 throw new PoolException("Cannot create a new instance", e);
125             } catch (IllegalAccessException JavaDoc e) {
126                 throw new PoolException("Cannot create a new instance", e);
127             } catch (Exception JavaDoc e) {
128                 throw new PoolException("Cannot create a new instance", e);
129             }
130         } finally {
131             Thread.currentThread().setContextClassLoader(oldClassLoader);
132         }
133
134         // Set the factory
135
instance.setEasyBeansFactory(this);
136
137         // Init the session Context
138
EZBSessionContext<PoolType> sessionContext = new EasyBeansSessionContext<PoolType>(instance);
139         instance.setEasyBeansContext(sessionContext);
140
141         oldClassLoader = Thread.currentThread().getContextClassLoader();
142         Thread.currentThread().setContextClassLoader(getContainer().getClassLoader());
143         try {
144             // Call injection
145
injectResources(instance);
146
147             // post construct callback
148
instance.postConstructEasyBeansLifeCycle();
149         } finally {
150             Thread.currentThread().setContextClassLoader(oldClassLoader);
151         }
152
153         return instance;
154     }
155
156     /**
157      * A request comes to the bean factory and needs to be handled.<br>
158      * A response is done which contains the answer.
159      * @param request the EJB request.
160      * @return a response that have been processed by the factory.
161      */

162     @Override JavaDoc
163     public EJBResponse rpcInvoke(final EJBRequest request) {
164         // get hash of the request
165
long hash = request.getMethodHash();
166
167         // Get Args (use context classloader for the Serialization)
168
Object JavaDoc[] args = null;
169         ClassLoader JavaDoc oldClassLoader = Thread.currentThread().getContextClassLoader();
170         Thread.currentThread().setContextClassLoader(getContainer().getClassLoader());
171         try {
172             args = request.getMethodArgs();
173         } finally {
174             Thread.currentThread().setContextClassLoader(oldClassLoader);
175         }
176
177         // call the method
178
return localCall(hash, args, request.getBeanId());
179
180     }
181
182     /**
183      * Gets a bean for the given id.
184      * @param beanId id of the expected bean.
185      * @return a Stateless bean.
186      * @throws IllegalArgumentException if bean is not found.
187      */

188     protected abstract PoolType getBean(final Long JavaDoc beanId) throws IllegalArgumentException JavaDoc;
189
190
191     /**
192      * Do a local call on a method of this factory.
193      * @param hash the hash of the method to execute.
194      * @param methodArgs the arguments of the method
195      * @param beanId the id of the bean that we want (stateful).
196      * @return response container new id (if any) and value.
197      */

198     public abstract EJBResponse localCall(final long hash, final Object JavaDoc[] methodArgs, final Long JavaDoc beanId);
199
200 }
201
Popular Tags