KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > core > CoreContext


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 1999 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: CoreContext.java 1921 2005-06-19 22:40:34Z jlaskowski $
44  */

45 package org.openejb.core;
46
47 import javax.ejb.EJBHome JavaDoc;
48 import javax.ejb.EJBLocalHome JavaDoc;
49 import javax.ejb.EJBLocalObject JavaDoc;
50 import javax.ejb.TimerService JavaDoc;
51 import javax.transaction.Status JavaDoc;
52
53 import org.openejb.OpenEJB;
54 import org.openejb.RpcContainer;
55 import org.openejb.core.ivm.EjbObjectProxyHandler;
56 import org.openejb.util.proxy.ProxyManager;
57
58 /**
59 * CoreContext is serializable so that it can be serialized if its
60 * referenced by a stateful bean that is being passivated (written to disk).
61 */

62 public abstract class CoreContext implements java.io.Serializable JavaDoc {
63
64     //==========================
65
// method categories
66
//
67

68     public final static byte SECURITY_METHOD = (byte)1;
69
70     public final static byte USER_TRANSACTION_METHOD = (byte)2;
71
72     public final static byte ROLLBACK_METHOD = (byte)3;
73
74     public final static byte EJBOBJECT_METHOD = (byte)4;
75
76     public final static byte EJBHOME_METHOD = (byte)5;
77     //
78
// method categories
79
//==========================
80

81
82     CoreUserTransaction userTransaction;
83
84
85
86     public CoreContext() {
87         userTransaction = new CoreUserTransaction(OpenEJB.getTransactionManager());
88     }
89
90
91     public abstract void checkBeanState(byte methodCategory) throws IllegalStateException JavaDoc;
92
93
94     public java.security.Principal JavaDoc getCallerPrincipal() {
95         checkBeanState(SECURITY_METHOD);
96         Object JavaDoc securityIdentity = ThreadContext.getThreadContext().getSecurityIdentity();
97         return(java.security.Principal JavaDoc)OpenEJB.getSecurityService().translateTo(securityIdentity, java.security.Principal JavaDoc.class);
98     }
99
100     public boolean isCallerInRole(java.lang.String JavaDoc roleName) {
101         checkBeanState(SECURITY_METHOD);
102         ThreadContext threadContext = ThreadContext.getThreadContext();
103         org.openejb.core.DeploymentInfo di = (org.openejb.core.DeploymentInfo)threadContext.getDeploymentInfo();
104         String JavaDoc physicalRoles [] = di.getPhysicalRole(roleName);
105         Object JavaDoc caller = threadContext.getSecurityIdentity();
106         return OpenEJB.getSecurityService().isCallerAuthorized(caller,physicalRoles);
107     }
108
109     public EJBHome JavaDoc getEJBHome() {
110         checkBeanState(EJBHOME_METHOD);
111
112         ThreadContext threadContext = ThreadContext.getThreadContext();
113         org.openejb.core.DeploymentInfo di = (org.openejb.core.DeploymentInfo)threadContext.getDeploymentInfo();
114
115         return di.getEJBHome();
116     }
117
118     public javax.ejb.EJBObject JavaDoc getEJBObject() {
119         checkBeanState(EJBOBJECT_METHOD);
120
121         //Possible Fix Needed: How to handle reenterant behavior.
122

123         ThreadContext threadContext = ThreadContext.getThreadContext();
124         org.openejb.DeploymentInfo di = threadContext.getDeploymentInfo();
125
126         EjbObjectProxyHandler handler = newEjbObjectHandler((RpcContainer)di.getContainer(), threadContext.getPrimaryKey(), di.getDeploymentID());
127         Object JavaDoc newProxy = null;
128         try {
129             Class JavaDoc[] interfaces = new Class JavaDoc[]{ di.getRemoteInterface(), org.openejb.core.ivm.IntraVmProxy.class };
130             newProxy = ProxyManager.newProxyInstance( interfaces , handler );
131         } catch ( IllegalAccessException JavaDoc iae ) {
132             throw new RuntimeException JavaDoc("Could not create IVM proxy for "+di.getRemoteInterface()+" interface");
133         }
134         return(javax.ejb.EJBObject JavaDoc)newProxy;
135     }
136
137     public EJBLocalObject JavaDoc getEJBLocalObject() {
138         ThreadContext threadContext = ThreadContext.getThreadContext();
139         org.openejb.DeploymentInfo di = threadContext.getDeploymentInfo();
140
141         EjbObjectProxyHandler handler = newEjbObjectHandler((RpcContainer)di.getContainer(), threadContext.getPrimaryKey(), di.getDeploymentID());
142         handler.setLocal(true);
143         Object JavaDoc newProxy = null;
144         try {
145             Class JavaDoc[] interfaces = new Class JavaDoc[]{ di.getLocalInterface(), org.openejb.core.ivm.IntraVmProxy.class };
146             newProxy = ProxyManager.newProxyInstance( interfaces , handler );
147         } catch ( IllegalAccessException JavaDoc iae ) {
148             throw new RuntimeException JavaDoc("Could not create IVM proxy for "+di.getLocalInterface()+" interface");
149         }
150         return(EJBLocalObject JavaDoc)newProxy;
151     }
152     
153     public EJBLocalHome JavaDoc getEJBLocalHome() {
154         ThreadContext threadContext = ThreadContext.getThreadContext();
155         org.openejb.core.DeploymentInfo di = (org.openejb.core.DeploymentInfo)threadContext.getDeploymentInfo();
156
157         return di.getEJBLocalHome();
158     }
159     public TimerService JavaDoc getTimerService() {
160         return null; //TODO: implement this
161
}
162
163     public Object JavaDoc getPrimaryKey( ) {
164         /*
165         * This method is only declared in the EntityContext interface and is therefor
166         * unavailable in the SessionContext and doesn't not require a check for bean kind (Entity vs Session).
167         */

168         checkBeanState(EJBOBJECT_METHOD);
169
170         // this method is only called on EntityContext interface which is only provided to the entity containers
171
ThreadContext threadContext = ThreadContext.getThreadContext();
172         return threadContext.getPrimaryKey();
173     }
174
175     public boolean getRollbackOnly() {
176
177         ThreadContext threadContext = ThreadContext.getThreadContext();
178         org.openejb.DeploymentInfo di = threadContext.getDeploymentInfo();
179         if ( di.isBeanManagedTransaction() )
180             throw new IllegalStateException JavaDoc("bean-managed transaction beans can not access the getRollbackOnly( ) method");
181
182         checkBeanState(ROLLBACK_METHOD);
183         try {
184             int status = OpenEJB.getTransactionManager().getStatus();
185             if ( status == Status.STATUS_MARKED_ROLLBACK || status == Status.STATUS_ROLLEDBACK )
186                 return true;
187             else if ( status == Status.STATUS_NO_TRANSACTION )// this would be true for Supports tx attribute where no tx was propagated
188
throw new IllegalStateException JavaDoc("No current transaction");
189             else
190                 return false;
191         } catch ( javax.transaction.SystemException JavaDoc se ) {
192             throw new RuntimeException JavaDoc("Transaction service has thrown a SystemException");
193         }
194     }
195
196     public void setRollbackOnly() {
197         ThreadContext threadContext = ThreadContext.getThreadContext();
198         org.openejb.DeploymentInfo di = threadContext.getDeploymentInfo();
199         if ( di.isBeanManagedTransaction() )
200             throw new IllegalStateException JavaDoc("bean-managed transaction beans can not access the setRollbackOnly( ) method");
201
202         checkBeanState(ROLLBACK_METHOD);
203
204         try {
205             OpenEJB.getTransactionManager().setRollbackOnly();
206         } catch ( javax.transaction.SystemException JavaDoc se ) {
207             throw new RuntimeException JavaDoc("Transaction service has thrown a SystemException");
208         }
209
210     }
211
212     public javax.transaction.UserTransaction JavaDoc getUserTransaction() {
213
214         ThreadContext threadContext = ThreadContext.getThreadContext();
215         org.openejb.DeploymentInfo di = threadContext.getDeploymentInfo();
216         if ( di.isBeanManagedTransaction() ) {
217             checkBeanState(USER_TRANSACTION_METHOD);
218             return userTransaction;
219         } else
220             throw new java.lang.IllegalStateException JavaDoc("container-managed transaction beans can not access the UserTransaction");
221     }
222
223     /*----------------------------------------------------*/
224     /* UNSUPPORTED DEPRICATED METHODS */
225     /*----------------------------------------------------*/
226
227     public boolean isCallerInRole(java.security.Identity JavaDoc role) {
228         throw new java.lang.UnsupportedOperationException JavaDoc();
229     }
230
231     public java.security.Identity JavaDoc getCallerIdentity() {
232         throw new java.lang.UnsupportedOperationException JavaDoc();
233     }
234
235     public java.util.Properties JavaDoc getEnvironment() {
236         throw new java.lang.UnsupportedOperationException JavaDoc();
237     }
238
239     protected abstract EjbObjectProxyHandler newEjbObjectHandler(RpcContainer container, Object JavaDoc pk, Object JavaDoc depID);
240 }
Popular Tags