KickJava   Java API By Example, From Geeks To Geeks.

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


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$
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.container;
27
28
29 import static javax.ejb.TransactionManagementType.BEAN JavaDoc;
30 import static javax.ejb.TransactionManagementType.CONTAINER JavaDoc;
31
32 import java.io.Serializable JavaDoc;
33 import java.lang.reflect.InvocationTargetException JavaDoc;
34 import java.lang.reflect.Method JavaDoc;
35 import java.security.Identity JavaDoc;
36 import java.security.Principal JavaDoc;
37 import java.util.Collection JavaDoc;
38 import java.util.Date JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.Properties JavaDoc;
42
43 import javax.ejb.EJBContext JavaDoc;
44 import javax.ejb.EJBException JavaDoc;
45 import javax.ejb.EJBHome JavaDoc;
46 import javax.ejb.EJBLocalHome JavaDoc;
47 import javax.ejb.Timer JavaDoc;
48 import javax.ejb.TimerService JavaDoc;
49 import javax.ejb.TransactionManagementType JavaDoc;
50 import javax.naming.InitialContext JavaDoc;
51 import javax.naming.NamingException JavaDoc;
52 import javax.transaction.Status JavaDoc;
53 import javax.transaction.SystemException JavaDoc;
54 import javax.transaction.TransactionManager JavaDoc;
55 import javax.transaction.UserTransaction JavaDoc;
56
57 import org.objectweb.easybeans.api.Factory;
58 import org.objectweb.easybeans.api.bean.EasyBeansBean;
59 import org.objectweb.easybeans.api.container.EZBEJBContext;
60 import org.objectweb.easybeans.log.JLog;
61 import org.objectweb.easybeans.log.JLogFactory;
62 import org.objectweb.easybeans.security.propagation.context.SecurityCurrent;
63 import org.objectweb.easybeans.transaction.JTransactionManager;
64
65 /**
66  * Class implementing the EJBContext interface.
67  * It is extended for Session context or MessageDriven Context
68  * @param <BeanType> The type of bean managed.
69  * @author Florent Benoit
70   */

71 public class EasyBeansEJBContext<BeanType extends EasyBeansBean> implements EZBEJBContext<BeanType>, EJBContext JavaDoc {
72
73     /**
74      * Logger.
75      */

76     private JLog logger = JLogFactory.getLog(EasyBeansEJBContext.class);
77
78     /**
79      * java:comp/env prefix.
80      */

81     private static final String JavaDoc JAVA_COMP_ENV = "java:comp/env/";
82
83     /**
84      * Reference to the transaction manager.
85      */

86     private TransactionManager JavaDoc transactionManager = null;
87
88     /**
89      * Type of transaction.
90      */

91     private TransactionManagementType JavaDoc transactionManagementType = null;
92
93     /**
94      * Bean is using run-as ?
95      */

96     private boolean runAsBean = false;
97
98     /**
99      * Link to the factory.
100      */

101     private Factory easyBeansFactory = null;
102
103     /**
104      * Builds a default EJB Context implementation.
105      * @param easyBeansFactory used to get the transaction management type.
106      */

107     public EasyBeansEJBContext(final Factory easyBeansFactory) {
108         this.easyBeansFactory = easyBeansFactory;
109         this.transactionManagementType = easyBeansFactory.getBeanInfo().getTransactionManagementType();
110         this.runAsBean = easyBeansFactory.getBeanInfo().getSecurityInfo().getRunAsRole() != null;
111
112         this.transactionManager = JTransactionManager.getTransactionManager();
113     }
114
115     /**
116      * Obtain the enterprise bean's remote home interface.
117      * @return The enterprise bean's remote home interface.
118      * @throws IllegalStateException if the enterprise bean does not have a
119      * remote home interface.
120      */

121     public EJBHome JavaDoc getEJBHome() throws IllegalStateException JavaDoc {
122         throw new IllegalStateException JavaDoc("No Home");
123     }
124
125     /**
126      * Obtain the enterprise bean's local home interface.
127      * @return The enterprise bean's local home interface.
128      * @throws IllegalStateException - if the enterprise bean does not have a
129      * local home interface.
130      */

131     public EJBLocalHome JavaDoc getEJBLocalHome() throws IllegalStateException JavaDoc {
132         throw new IllegalStateException JavaDoc("No Local Home");
133     }
134
135     /**
136      * Use the JNDI naming context java:comp/env to access enterprise bean's
137      * environment. Obtain the enterprise bean's environment properties. Note:
138      * If the enterprise bean has no environment properties this method returns
139      * an empty java.util.Properties object. This method never returns null.
140      * @return The environment properties for the enterprise bean.
141      */

142     @Deprecated JavaDoc
143     public Properties JavaDoc getEnvironment() {
144         throw new UnsupportedOperationException JavaDoc();
145     }
146
147     /**
148      * Use Principal getCallerPrincipal() instead. Obtain the
149      * java.security.Identity of the caller. This method is deprecated in EJB
150      * 1.1. The Container is allowed to return alway null from this method. The
151      * enterprise bean should use the getCallerPrincipal method instead.
152      * @return The Identity object that identifies the caller.
153      */

154     @Deprecated JavaDoc
155     public Identity JavaDoc getCallerIdentity() {
156         throw new UnsupportedOperationException JavaDoc();
157     }
158
159     /**
160      * Obtain the java.security.Principal that identifies the caller.
161      * @return The Principal object that identifies the caller. This method
162      * never returns null.
163      */

164     public Principal JavaDoc getCallerPrincipal() {
165         return SecurityCurrent.getCurrent().getSecurityContext().getCallerPrincipal(runAsBean);
166     }
167
168     /**
169      * Use boolean isCallerInRole(String roleName) instead. Test if the caller
170      * has a given role. This method is deprecated in EJB 1.1. The enterprise
171      * bean should use the isCallerInRole(String roleName) method instead.
172      * @param role The java.security.Identity of the role to be tested.
173      * @return True if the caller has the specified role.
174      */

175     @Deprecated JavaDoc
176     public boolean isCallerInRole(final Identity JavaDoc role) {
177         throw new UnsupportedOperationException JavaDoc();
178     }
179
180     /**
181      * Test if the caller has a given security role.
182      * @param roleName The name of the security role. The role must be one of
183      * the security roles that is defined in the deployment descriptor.
184      * @return True if the caller has the specified role.
185      */

186     public boolean isCallerInRole(final String JavaDoc roleName) {
187
188         // Get list of declared roles for this bean.
189

190         boolean foundItem= false;
191         List JavaDoc<String JavaDoc> declaredRoles = easyBeansFactory.getBeanInfo().getSecurityInfo().getDeclaredRoles();
192         if (declaredRoles == null || !declaredRoles.contains(roleName)) {
193             logger.debug("No security-role with role name ''{0}'' was declared for the bean ''{1}''", roleName, easyBeansFactory
194                     .getBeanInfo().getName());
195             return false;
196         }
197
198         // Check with JACC manager
199
boolean inRole = getBean().getEasyBeansFactory().getContainer().getPermissionManager().isCallerInRole(easyBeansFactory
200                 .getBeanInfo().getName(), roleName, runAsBean);
201
202         return inRole;
203     }
204
205     /**
206      * Obtain the transaction demarcation interface. Only enterprise beans with
207      * bean-managed transactions are allowed to to use the UserTransaction
208      * interface. As entity beans must always use container-managed
209      * transactions, only session beans with bean-managed transactions are
210      * allowed to invoke this method.
211      * @return The UserTransaction interface that the enterprise bean instance
212      * can use for transaction demarcation.
213      * @throws java.lang.IllegalStateException - The Container throws the
214      * exception if the instance is not allowed to use the
215      * UserTransaction interface (i.e. the instance is of a bean with
216      * container-managed transactions).
217      */

218     public UserTransaction JavaDoc getUserTransaction() throws IllegalStateException JavaDoc {
219         if (transactionManagementType == CONTAINER) {
220             throw new IllegalStateException JavaDoc("This bean is not allowed to use getUserTransaction() "
221                     + " method as it is in ContainerManagedTransaction");
222         }
223         return (UserTransaction JavaDoc) transactionManager;
224     }
225
226     /**
227      * Mark the current transaction for rollback. The transaction will become
228      * permanently marked for rollback. A transaction marked for rollback can
229      * never commit. Only enterprise beans with container-managed transactions
230      * are allowed to use this method.
231      * @throws java.lang.IllegalStateException - The Container throws the
232      * exception if the instance is not allowed to use this method (i.e.
233      * the instance is of a bean with bean-managed transactions).
234      */

235     public void setRollbackOnly() throws IllegalStateException JavaDoc {
236         if (transactionManagementType == BEAN) {
237             throw new IllegalStateException JavaDoc("This bean is not allowed to use setRollbackOnly() "
238                     + " method as it is in BeanManagedTransaction");
239         }
240
241         // Check if there is a transaction, as it is mandatory
242
try {
243             if (transactionManager.getTransaction() == null) {
244                 throw new IllegalStateException JavaDoc("Cannot use setRollbackOnly() outside transaction");
245             }
246         } catch (SystemException JavaDoc e) {
247             throw new IllegalStateException JavaDoc("Cannot get transaction on transaction manager", e);
248         }
249
250
251         try {
252             transactionManager.setRollbackOnly();
253         } catch (SystemException JavaDoc e) {
254             throw new RuntimeException JavaDoc("setRollbackOnly() raised an unexpected exception:", e);
255         }
256     }
257
258     /**
259      * Test if the transaction has been marked for rollback only. An enterprise
260      * bean instance can use this operation, for example, to test after an
261      * exception has been caught, whether it is fruitless to continue
262      * computation on behalf of the current transaction. Only enterprise beans
263      * with container-managed transactions are allowed to use this method.
264      * @return True if the current transaction is marked for rollback, false
265      * otherwise.
266      * @throws java.lang.IllegalStateException - The Container throws the
267      * exception if the instance is not allowed to use this method (i.e.
268      * the instance is of a bean with bean-managed transactions).
269      */

270     public boolean getRollbackOnly() throws IllegalStateException JavaDoc {
271         if (transactionManagementType == BEAN) {
272             throw new IllegalStateException JavaDoc("This bean is not allowed to use getRollbackOnly() "
273                     + " method as it is in BeanManagedTransaction");
274         }
275         try {
276             switch (transactionManager.getStatus()) {
277                 case Status.STATUS_MARKED_ROLLBACK:
278                 case Status.STATUS_ROLLING_BACK:
279                     return true;
280                 case Status.STATUS_ACTIVE:
281                 case Status.STATUS_COMMITTING:
282                 case Status.STATUS_PREPARED:
283                 case Status.STATUS_PREPARING:
284                     return false;
285                 case Status.STATUS_ROLLEDBACK:
286                     throw new IllegalStateException JavaDoc("Transaction already rolled back");
287                 case Status.STATUS_COMMITTED:
288                     throw new IllegalStateException JavaDoc("Transaction already committed");
289                 case Status.STATUS_NO_TRANSACTION:
290                 case Status.STATUS_UNKNOWN:
291                     throw new IllegalStateException JavaDoc("Cannot getRollbackOnly outside transaction");
292                 default:
293                     throw new IllegalStateException JavaDoc("Invalid status");
294             }
295         } catch (SystemException JavaDoc e) {
296             throw new IllegalStateException JavaDoc("Cannot get transaction status", e);
297         }
298     }
299
300     /**
301      * Get access to the EJB Timer Service.
302      * @return Timer service.
303      * @throws java.lang.IllegalStateException The Container throws the
304      * exception if the instance is not allowed to use this method (e.g.
305      * if the bean is a stateful session bean)
306      */

307     public TimerService JavaDoc getTimerService() throws IllegalStateException JavaDoc {
308         return new DummyTimerService();
309     }
310
311     /**
312      * @return string representation.
313      */

314     @Override JavaDoc
315     public String JavaDoc toString() {
316         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
317         // classname
318
sb.append(this.getClass().getName().substring(this.getClass().getPackage().getName().length() + 1));
319         return sb.toString();
320     }
321
322     /**
323      * Lookup object with given name.
324      * @param name given name
325      * @return result of the lookup
326      */

327     public Object JavaDoc lookup(final String JavaDoc name) {
328         // Search in java:comp/env first
329
try {
330             return new InitialContext JavaDoc().lookup(JAVA_COMP_ENV + name);
331         } catch (NamingException JavaDoc ne) {
332             // try in registry
333
try {
334                 return new InitialContext JavaDoc().lookup(name);
335             } catch (NamingException JavaDoc e) {
336                 throw new IllegalArgumentException JavaDoc("Lookup on '" + name + "' was not found");
337             }
338         }
339     }
340
341
342     /**
343      * Gets the bean of this context.
344      * @return bean used by this context.
345      */

346     public BeanType getBean() {
347         return null;
348     }
349
350     /**
351      * Dummy class for handling Timer Service.
352      * @author Florent Benoit
353      */

354     class DummyTimerService implements TimerService JavaDoc {
355
356         /**
357          * Create an interval timer whose first expiration occurs after a
358          * specified duration, and whose subsequent expirations occur after a
359          * specified interval.
360          * @param initialDuration - The number of milliseconds that must elapse
361          * before the first timer expiration notification.
362          * @param intervalDuration - The number of milliseconds that must elapse
363          * between timer expiration notifications. Expiration
364          * notifications are scheduled relative to the time of the first
365          * expiration. If expiration is delayed(e.g. due to the
366          * interleaving of other method calls on the bean) two or more
367          * expiration notifications may occur in close succession to
368          * "catch up".
369          * @param info - Application information to be delivered along with the
370          * timer expiration. This can be null.
371          * @return The newly created Timer.
372          * @throws IllegalArgumentException - If initialDuration is negative, or
373          * intervalDuration is negative.
374          * @throws IllegalStateException - If this method is invoked while the
375          * instance is in a state that does not allow access to this
376          * method.
377          * @throws EJBException - If this method could not complete due to a
378          * system-level failure.
379          */

380         public Timer JavaDoc createTimer(final Date JavaDoc initialDuration, final long intervalDuration, final Serializable JavaDoc info)
381                 throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, EJBException JavaDoc {
382             throw new IllegalStateException JavaDoc("Not yet implemented");
383         }
384
385         /**
386          * Create a single-action timer that expires at a given point in time.
387          * @param expiration - The point in time at which the timer must expire.
388          * @param info - Application information to be delivered along with the
389          * timer expiration notification. This can be null.
390          * @return The newly created Timer.
391          * @throws IllegalArgumentException - If expiration is null, or
392          * expiration.getTime() is negative.
393          * @throws IllegalStateException - If this method is invoked while the
394          * instance is in a state that does not allow access to this
395          * method.
396          * @throws EJBException - If this method could not complete due to a
397          * system-level failure.
398          */

399         public Timer JavaDoc createTimer(final Date JavaDoc expiration, final Serializable JavaDoc info) throws IllegalArgumentException JavaDoc,
400                 IllegalStateException JavaDoc, EJBException JavaDoc {
401             throw new IllegalStateException JavaDoc("Not yet implemented");
402         }
403
404         /**
405          * Create an interval timer whose first expiration occurs after a
406          * specified duration, and whose subsequent expirations occur after a
407          * specified interval.
408          * @param initialDuration - The number of milliseconds that must elapse
409          * before the first timer expiration notification.
410          * @param intervalDuration - The number of milliseconds that must elapse
411          * between timer expiration notifications. Expiration
412          * notifications are scheduled relative to the time of the first
413          * expiration. If expiration is delayed(e.g. due to the
414          * interleaving of other method calls on the bean) two or more
415          * expiration notifications may occur in close succession to
416          * "catch up".
417          * @param info - Application information to be delivered along with the
418          * timer expiration. This can be null.
419          * @return The newly created Timer.
420          * @throws IllegalArgumentException - If initialDuration is negative, or
421          * intervalDuration is negative.
422          * @throws IllegalStateException - If this method is invoked while the
423          * instance is in a state that does not allow access to this
424          * method.
425          * @throws EJBException - If this method could not complete due to a
426          * system-level failure.
427          */

428         public Timer JavaDoc createTimer(final long initialDuration, final long intervalDuration, final Serializable JavaDoc info)
429                 throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, EJBException JavaDoc {
430             throw new IllegalStateException JavaDoc("Not yet implemented");
431         }
432
433         /**
434          * Create a single-action timer that expires after a specified duration.
435          * @param duration - The number of milliseconds that must elapse before
436          * the timer expires.
437          * @param info - Application information to be delivered along with the
438          * timer expiration notification. This can be null.
439          * @return The newly created Timer.
440          * @throws IllegalArgumentException - If duration is negative
441          * @throws IllegalStateException - If this method is invoked while the
442          * instance is in a state that does not allow access to this
443          * method.
444          * @throws EJBException - If this method fails due to a system-level
445          * failure.
446          */

447         public Timer JavaDoc createTimer(final long duration, final Serializable JavaDoc info) throws IllegalArgumentException JavaDoc,
448                 IllegalStateException JavaDoc, EJBException JavaDoc {
449             throw new IllegalStateException JavaDoc("Not yet implemented");
450         }
451
452         /**
453          * Get all the active timers associated with this bean.
454          * @return A collection of javax.ejb.Timer objects.
455          * @throws IllegalStateException - If this method is invoked while the
456          * instance is in a state that does not allow access to this
457          * method.
458          * @throws EJBException - If this method could not complete due to a
459          * system-level failure.
460          */

461         public Collection JavaDoc getTimers() throws IllegalStateException JavaDoc, EJBException JavaDoc {
462             throw new IllegalStateException JavaDoc("Not yet implemented");
463         }
464
465     }
466 }
467
Popular Tags