KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > container > session > stateless > StatelessSessionFactory


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

25
26 package org.objectweb.easybeans.container.session.stateless;
27
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30
31 import javax.ejb.ApplicationException JavaDoc;
32 import javax.ejb.NoSuchEJBException JavaDoc;
33
34 import org.objectweb.easybeans.api.EZBContainer;
35 import org.objectweb.easybeans.api.FactoryException;
36 import org.objectweb.easybeans.api.bean.EasyBeansSLSB;
37 import org.objectweb.easybeans.api.pool.PoolException;
38 import org.objectweb.easybeans.container.session.SessionFactory;
39 import org.objectweb.easybeans.pool.JPool;
40 import org.objectweb.easybeans.pool.PoolEntryStatistics;
41 import org.objectweb.easybeans.pool.PoolFactory;
42 import org.objectweb.easybeans.rpc.JEJBResponse;
43 import org.objectweb.easybeans.rpc.api.EJBResponse;
44 import org.objectweb.easybeans.rpc.api.RPCException;
45
46 /**
47  * This class manages the stateless session bean and its creation/lifecycle.
48  * @author Florent Benoit
49  */

50 public class StatelessSessionFactory extends SessionFactory<EasyBeansSLSB> implements
51         PoolFactory<EasyBeansSLSB, Long JavaDoc> {
52
53     /**
54      * Builds a new factory with a given name and its container.
55      * @param className name of this factory (name of class that is managed)
56      * @param container the root component of this factory.
57      * @throws FactoryException if class can't be loaded.
58      */

59     public StatelessSessionFactory(final String JavaDoc className, final EZBContainer container) throws FactoryException {
60         super(className, container);
61         setPool(new JPool<EasyBeansSLSB, Long JavaDoc>(this));
62     }
63
64     /**
65      * Checks if the given object with the given clue is matching.
66      * @param object given object against which the check should be done.
67      * @param clue the object used as clue to check the matching.
68      * @return true if it is matching, else false.
69      */

70     public boolean isMatching(final EasyBeansSLSB object, final Long JavaDoc clue) {
71         // all instances are matching.
72
// But we shouldn't go here
73
return true;
74     }
75
76     /**
77      * Validate an instance by giving some statistics.
78      * @param object the instance to validate
79      * @param stats some statistics to help in the validating process.
80      * @return true if the element is valid, else false.
81      */

82     public boolean validate(final EasyBeansSLSB object, final PoolEntryStatistics stats) {
83         return true;
84     }
85
86     /**
87      * Gets a new ID or a null value.
88      * @param beanId given id.
89      * @return new id
90      */

91     @Override JavaDoc
92     protected Long JavaDoc getId(final Long JavaDoc beanId) {
93         return null;
94     }
95
96     /**
97      * Gets a bean for the given id.
98      * @param beanId id of the expected bean.
99      * @return a Stateless bean.
100      * @throws IllegalArgumentException if bean is not found.
101      */

102     @Override JavaDoc
103     protected EasyBeansSLSB getBean(final Long JavaDoc beanId) throws IllegalArgumentException JavaDoc {
104         try {
105             return getPool().get();
106         } catch (PoolException e) {
107             throw new IllegalArgumentException JavaDoc("Cannot get element in the pool", e);
108         }
109     }
110
111     /**
112      * Do a local call on a method of this factory.
113      * @param hash the hash of the method to execute.
114      * @param methodArgs the arguments of the method
115      * @param beanId the id of the bean that we want (stateful).
116      * @return response container new id (if any) and value.
117      */

118     @Override JavaDoc
119     public EJBResponse localCall(final long hash, final Object JavaDoc[] methodArgs, final Long JavaDoc beanId) {
120
121         // build EJB Response and set the id
122
EJBResponse ejbResponse = new JEJBResponse();
123
124         EasyBeansSLSB bean = null;
125         try {
126             bean = getBean(null);
127         } catch (IllegalArgumentException JavaDoc e) {
128             ejbResponse.setRPCException(new RPCException("Cannot get element in the pool", e));
129             return ejbResponse;
130         } catch (NoSuchEJBException JavaDoc e) {
131             ejbResponse.setRPCException(new RPCException("Bean has been removed", e));
132             return ejbResponse;
133         }
134
135         Method JavaDoc m = getHashes().get(Long.valueOf(hash));
136
137         if (m == null) {
138             ejbResponse.setRPCException(new RPCException("Cannot find method called on the bean '" + getClassName() + "'."));
139             return ejbResponse;
140         }
141
142         Object JavaDoc value = null;
143
144         // set ClassLoader
145
ClassLoader JavaDoc oldClassLoader = Thread.currentThread().getContextClassLoader();
146         Thread.currentThread().setContextClassLoader(getContainer().getClassLoader());
147
148         try {
149             value = m.invoke(bean, methodArgs);
150         } catch (IllegalArgumentException JavaDoc e) {
151             ejbResponse.setRPCException(new RPCException(e));
152         } catch (IllegalAccessException JavaDoc e) {
153             ejbResponse.setRPCException(new RPCException(e));
154         } catch (InvocationTargetException JavaDoc e) {
155             Throwable JavaDoc cause = e.getCause();
156             RPCException rpcException = new RPCException(cause);
157             // ApplicationException ?
158
ApplicationException JavaDoc applicationException = getBeanInfo().getApplicationExceptions().get(cause.getClass().getName());
159             if (applicationException != null) {
160                 rpcException.setApplicationException();
161             }
162             ejbResponse.setRPCException(rpcException);
163         } finally {
164             Thread.currentThread().setContextClassLoader(oldClassLoader);
165             // push back into the pool
166
try {
167                 getPool().release(bean);
168             } catch (PoolException e) {
169                 ejbResponse.setRPCException(new RPCException("cannot release bean", e));
170             }
171
172         }
173         ejbResponse.setValue(value);
174         return ejbResponse;
175
176     }
177
178 }
179
Popular Tags