KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ejb > container > JContext


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2005 Bull S.A.
4  * Contact: jonas-team@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: JContext.java,v 1.34 2005/07/13 06:29:45 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_ejb.container;
27
28 import java.security.Identity JavaDoc;
29 import java.security.Principal JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Properties JavaDoc;
33
34 import javax.ejb.EJBContext JavaDoc;
35 import javax.ejb.EJBHome JavaDoc;
36 import javax.ejb.EJBLocalHome JavaDoc;
37 import javax.ejb.EnterpriseBean JavaDoc;
38 import javax.ejb.TimerService JavaDoc;
39 import javax.resource.spi.work.WorkManager JavaDoc;
40 import javax.transaction.Status JavaDoc;
41 import javax.transaction.SystemException JavaDoc;
42 import javax.transaction.UserTransaction JavaDoc;
43
44 import org.objectweb.transaction.jta.TransactionManager;
45
46 import org.objectweb.jonas_lib.deployment.api.SecurityRoleRefDesc;
47
48 import org.objectweb.util.monolog.api.BasicLevel;
49
50 /**
51  * This class implements javax.ejb.EJBContext class.
52  * It may be extended in JSessionContext or JEntityContext
53  * @author Philippe Coq, Philippe Durieux, Jeff Mesnil (security)
54  * @author Florent Benoit (JACC security)
55  */

56 public abstract class JContext implements EJBContext JavaDoc {
57
58     protected EnterpriseBean JavaDoc instance;
59     protected final JFactory bf;
60     protected final TransactionManager tm;
61     private final JHome home;
62     private final JLocalHome localhome;
63     private final JContainer cont;
64
65     /**
66      * Permission manager of this container. JACC
67      */

68     private PermissionManager permissionManager = null;
69
70
71     /**
72      * State of this Context
73      * 0 = init, 1 = setContext done, 2 = ejbCreate done.
74      */

75     int instanceState = 0;
76
77     // ------------------------------------------------------------------
78
// constructors
79
// ------------------------------------------------------------------
80

81     /**
82      * Constructs a JContext
83      * @param bf - the BeanFactory
84      * @param i - the bean instance
85      */

86     protected JContext(JFactory bf, EnterpriseBean JavaDoc i) {
87         this.bf = bf;
88         this.instance = i;
89         if (i == null) {
90             TraceEjb.logger.log(BasicLevel.ERROR, "null EnterpriseBean!");
91         }
92         this.home = bf.getHome();
93         this.localhome = bf.getLocalHome();
94         this.cont = bf.getContainer();
95         this.tm = bf.getTransactionManager();
96         this.permissionManager = cont.getPermissionManager();
97
98     }
99
100     /**
101      * Set the instance State
102      */

103     public void setState(int newState) {
104         instanceState = newState;
105         if (TraceEjb.isDebugContext()) {
106             TraceEjb.context.log(BasicLevel.DEBUG, "" + instanceState);
107         }
108     }
109
110     /**
111      * Get the Instance State
112      * @throws Exception
113      */

114     public int getState() {
115         if (TraceEjb.isDebugContext()) {
116             TraceEjb.context.log(BasicLevel.DEBUG, "" + instanceState);
117         }
118         return instanceState;
119     }
120
121     // ------------------------------------------------------------------
122
// JOnAS specific added functions
123
// ------------------------------------------------------------------
124

125     /**
126      * Get the WorkManager
127      */

128     public WorkManager JavaDoc getWorkManager() {
129         return bf.getWorkManager();
130     }
131
132     // ------------------------------------------------------------------
133
// EJBContext implementation
134
// ------------------------------------------------------------------
135

136     /**
137      * Obtains the java.security.Identity of the caller.
138      * @deprecated
139      * @return The Identity object that identifies the caller.
140      */

141     public Identity JavaDoc getCallerIdentity() {
142         throw new RuntimeException JavaDoc ("getCallerIdentity() method deprecated. use instead getCallerPrincipal()");
143     }
144
145
146     /**
147      * Obtain the java.security.Principal that identifies the caller.
148      * throws a java.lang.IllegalStateException if there is no security context available
149      * @return The Principal object that identifies the caller.
150      * @throws IllegalStateException no security context exists
151      */

152     public Principal JavaDoc getCallerPrincipal() throws IllegalStateException JavaDoc {
153
154         if (getState() == 0) {
155             throw new IllegalStateException JavaDoc("the instance is not allowed to call this method");
156         }
157         boolean inRunAs = false;
158         if (bf.dd.getRunAsRole() != null) {
159             inRunAs = true;
160         }
161
162         Principal JavaDoc principal = cont.getPrincipalFactory().getCallerPrincipal(inRunAs);
163         if (principal == null) {
164             throw new IllegalStateException JavaDoc("no security context exists");
165         }
166         return principal;
167
168     }
169
170     /**
171      * the enterprise bean's home interface.
172      * @return The enterprise bean's home interface.
173      * @throws IllegalStateException - if the enterprise bean does not have a local home interface.
174      */

175     public EJBHome JavaDoc getEJBHome() throws IllegalStateException JavaDoc {
176         return home;
177     }
178
179     /**
180      * Obtain the enterprise bean's local home interface.
181      * @return The enterprise bean's local home interface.
182      * @throws IllegalStateException if the enterprise bean does not have a local home interface.
183      */

184     public EJBLocalHome JavaDoc getEJBLocalHome() throws IllegalStateException JavaDoc {
185         if (!bf.dd.hasDefinedLocalInterface()) {
186             TraceEjb.logger.log(BasicLevel.ERROR, "No Local Interface declared for this bean");
187             throw new IllegalStateException JavaDoc("No Local Interface declared for this bean");
188         }
189         return localhome;
190     }
191
192     /**
193      * Obtains the enterprise bean's environment properties for EJB 1.0 style.
194      * (Conform to EJB 1.1 specification for a container chooses to provide support
195      * for EJB 1.0 style environment properties. Cf EJB 1.1 Chapter 14.5, pages 216-217)
196      * Note: If the enterprise bean has no environment properties, this method returns an empty
197      * java.util.Properties object. This method never returns null.
198      * @deprecated
199      * @return The environment properties for the enterprise bean.
200      */

201     public Properties JavaDoc getEnvironment() {
202         if (TraceEjb.isDebugIc()) {
203             TraceEjb.interp.log(BasicLevel.DEBUG, "");
204         }
205         return bf.getEjb10Environment();
206     }
207
208     /**
209      * Tests if the transaction has been marked for rollback only.
210      * @return true if transaction will rollback
211      * @throws IllegalStateException if state is 0
212      */

213     public boolean getRollbackOnly() throws IllegalStateException JavaDoc {
214         if (TraceEjb.isDebugIc()) {
215             TraceEjb.interp.log(BasicLevel.DEBUG, "");
216         }
217
218         if (getState() == 0) {
219             throw new IllegalStateException JavaDoc("the instance is not allowed to call this method");
220         }
221
222         try {
223             switch (tm.getStatus()) {
224             case Status.STATUS_MARKED_ROLLBACK:
225             case Status.STATUS_ROLLING_BACK:
226                 return true;
227             case Status.STATUS_ACTIVE:
228             case Status.STATUS_COMMITTING:
229             case Status.STATUS_PREPARED:
230             case Status.STATUS_PREPARING:
231                 return false;
232             case Status.STATUS_ROLLEDBACK:
233                 throw new IllegalStateException JavaDoc("Transaction already rolled back");
234             case Status.STATUS_COMMITTED:
235                 throw new IllegalStateException JavaDoc("Transaction already committed");
236             case Status.STATUS_NO_TRANSACTION:
237             case Status.STATUS_UNKNOWN:
238                 throw new IllegalStateException JavaDoc("Cannot getRollbackOnly outside transaction");
239             }
240         } catch (SystemException JavaDoc e) {
241             TraceEjb.logger.log(BasicLevel.ERROR, "cannot get transaction status:", e);
242             throw new IllegalStateException JavaDoc("Cannot get transaction status");
243         }
244         return true;
245     }
246
247     /**
248      * Get access to the EJB Timer Service.
249      * @return the EJB Timer Service
250      * @throws IllegalStateException Thrown if the instance is not
251      * allowed to use this method (e.g. if the bean is a stateful session bean)
252      */

253     public abstract TimerService JavaDoc getTimerService() throws IllegalStateException JavaDoc;
254
255     /**
256      * Obtains the transaction demarcation interface.
257      * @return The UserTransaction interface that the enterprise bean instance
258      * can use for transaction demarcation.
259      * @throws IllegalStateException Thrown if the instance container does
260      * not make the UserTransaction interface available to the
261      * instance. (not bean managed)
262      */

263     public UserTransaction JavaDoc getUserTransaction() throws IllegalStateException JavaDoc {
264
265         if (TraceEjb.isDebugIc()) {
266             TraceEjb.interp.log(BasicLevel.DEBUG, "");
267         }
268
269         if (!bf.isTxBeanManaged()) {
270             throw new IllegalStateException JavaDoc("This bean is not allowed to use UserTransaction interface");
271         }
272         if (getState() == 0) {
273             throw new IllegalStateException JavaDoc("the instance is not allowed to call this method");
274         }
275         return (UserTransaction JavaDoc) tm;
276     }
277
278     /**
279      * @deprecated Use boolean isCallerInRole(String roleName) instead.
280      * Tests if the caller has a given role.
281      * @param role - The java.security.Identity of the role to be tested.
282      * @return True if the caller has the specified role.
283      */

284     public boolean isCallerInRole(Identity JavaDoc role) {
285         throw new RuntimeException JavaDoc ("isCallerInRole(Identity) method deprecated. use instead isCallerInRole(String)");
286     }
287
288     /**
289      * Test if the caller has a given role.
290      *
291      * @param roleName The name of the security role.
292      * The role must be one of the security-role-ref that is defined in the
293      * deployment descriptor.
294      * @return True if the caller has the specified role.
295      * @throws IllegalStateException Security service not started
296      */

297     public boolean isCallerInRole(String JavaDoc roleName) throws IllegalStateException JavaDoc {
298         if (TraceEjb.isDebugSecurity()) {
299             TraceEjb.security.log(BasicLevel.DEBUG, "");
300         }
301
302        if (getState() == 0) {
303             throw new IllegalStateException JavaDoc("the instance is not allowed to call this method");
304         }
305
306         // See EJB 2.1 specification
307
// Chapter 21 : 21.2.5.2
308

309         // Check that the roleName is in security-role-ref
310
List JavaDoc list = bf.dd.getSecurityRoleRefDescList();
311
312         if (list == null) {
313             TraceEjb.logger.log(BasicLevel.WARN, "EJB 2.1 spec, Chapter 21 : 21.2.5.2 : No security-role-ref list. Invalid usage of isCallerInRole without security-role-ref elements.");
314             return false;
315         }
316         boolean foundItem = false;
317         Iterator JavaDoc it = bf.dd.getSecurityRoleRefDescList().iterator();
318         String JavaDoc tmpRoleName = null;
319         SecurityRoleRefDesc sRoleRefDesc = null;
320         while (!foundItem && it.hasNext()) {
321             sRoleRefDesc = (SecurityRoleRefDesc) it.next();
322             tmpRoleName = sRoleRefDesc.getRoleName();
323             if (tmpRoleName.equals(roleName)) {
324                 foundItem = true;
325             }
326         }
327
328         if (!foundItem) {
329             if (TraceEjb.isDebugSecurity()) {
330                 TraceEjb.security.log(BasicLevel.DEBUG, "No security-role-ref with role name '" + roleName
331                                 + "' was found in the deployment descriptor of bean '"
332                                 + bf.getEJBName() + ".");
333             }
334             return false;
335         }
336
337         boolean inRunAs = false;
338         if (bf.dd.getRunAsRole() != null) {
339             inRunAs = true;
340         }
341         // Check with JACC manager
342
boolean inRole = permissionManager.isCallerInRole(bf.getEJBName(), roleName, inRunAs);
343
344         if (TraceEjb.isDebugSecurity()) {
345             TraceEjb.security.log(BasicLevel.DEBUG, "isCallerInRole: " + inRole);
346         }
347         return inRole;
348
349     }
350
351     /**
352      * Marks the current transaction for rollback.
353      * The transaction will become permanently marked for rollback.
354      * @throws IllegalStateException in getRollbackOnly() method
355      */

356     public void setRollbackOnly() throws IllegalStateException JavaDoc {
357         if (TraceEjb.isDebugIc()) {
358             TraceEjb.interp.log(BasicLevel.DEBUG, "");
359         }
360
361         // Check transaction state.
362
// Should throw IllegalStateException if not in a correct state.
363
// This may be not handled correctly by the TM.
364
getRollbackOnly();
365
366         try {
367             tm.setRollbackOnly();
368         } catch (IllegalStateException JavaDoc e) {
369             TraceEjb.logger.log(BasicLevel.ERROR, "current thread not associated with transaction");
370             throw e;
371         } catch (SystemException JavaDoc e) {
372             TraceEjb.logger.log(BasicLevel.ERROR, "setRollbackOnly unexpected exception:", e);
373         }
374     }
375
376 }
377
Popular Tags