001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.core;
017:
018: import javax.ejb.EJBHome;
019: import javax.ejb.EJBLocalHome;
020: import javax.ejb.EJBLocalObject;
021: import javax.ejb.TimerService;
022: import javax.naming.Context;
023: import javax.naming.InitialContext;
024: import javax.naming.NamingException;
025: import javax.transaction.Status;
026: import javax.transaction.TransactionManager;
027: import javax.transaction.UserTransaction;
028:
029: import org.apache.openejb.DeploymentInfo;
030: import org.apache.openejb.InterfaceType;
031: import org.apache.openejb.RpcContainer;
032: import org.apache.openejb.InternalErrorException;
033: import org.apache.openejb.core.ivm.EjbObjectProxyHandler;
034: import org.apache.openejb.core.ivm.IntraVmProxy;
035: import org.apache.openejb.spi.SecurityService;
036: import org.apache.openejb.util.proxy.ProxyManager;
037:
038: public abstract class CoreContext implements java.io.Serializable {
039:
040: public final static byte SECURITY_METHOD = (byte) 1;
041:
042: public final static byte USER_TRANSACTION_METHOD = (byte) 2;
043:
044: public final static byte ROLLBACK_METHOD = (byte) 3;
045:
046: public final static byte EJBOBJECT_METHOD = (byte) 4;
047:
048: public final static byte EJBHOME_METHOD = (byte) 5;
049:
050: private final UserTransaction userTransaction;
051: private final SecurityService securityService;
052: private final TransactionManager transactionManager;
053:
054: public CoreContext(TransactionManager transactionManager,
055: SecurityService securityService) {
056: this .transactionManager = transactionManager;
057: this .securityService = securityService;
058: this .userTransaction = new CoreUserTransaction(
059: transactionManager);
060: }
061:
062: protected CoreContext(TransactionManager transactionManager,
063: SecurityService securityService,
064: UserTransaction userTransaction) {
065: this .transactionManager = transactionManager;
066: this .securityService = securityService;
067: this .userTransaction = userTransaction;
068: }
069:
070: private TransactionManager getTransactionManager() {
071: return transactionManager;
072: }
073:
074: public abstract void checkBeanState(byte methodCategory)
075: throws IllegalStateException;
076:
077: public java.security.Principal getCallerPrincipal() {
078: checkBeanState(SECURITY_METHOD);
079: return getSecurityService().getCallerPrincipal();
080: }
081:
082: private SecurityService getSecurityService() {
083: return securityService;
084: }
085:
086: public boolean isCallerInRole(java.lang.String roleName) {
087: checkBeanState(SECURITY_METHOD);
088: return securityService.isCallerInRole(roleName);
089: }
090:
091: public EJBHome getEJBHome() {
092: checkBeanState(EJBHOME_METHOD);
093:
094: ThreadContext threadContext = ThreadContext.getThreadContext();
095: org.apache.openejb.core.CoreDeploymentInfo di = (org.apache.openejb.core.CoreDeploymentInfo) threadContext
096: .getDeploymentInfo();
097:
098: return di.getEJBHome();
099: }
100:
101: public javax.ejb.EJBObject getEJBObject() {
102: checkBeanState(EJBOBJECT_METHOD);
103:
104: ThreadContext threadContext = ThreadContext.getThreadContext();
105: org.apache.openejb.DeploymentInfo di = threadContext
106: .getDeploymentInfo();
107:
108: EjbObjectProxyHandler handler = newEjbObjectHandler(
109: (RpcContainer) di.getContainer(), threadContext
110: .getPrimaryKey(), di.getDeploymentID(),
111: InterfaceType.EJB_OBJECT);
112: Object newProxy = null;
113: try {
114: Class[] interfaces = new Class[] { di.getRemoteInterface(),
115: org.apache.openejb.core.ivm.IntraVmProxy.class };
116: newProxy = ProxyManager.newProxyInstance(interfaces,
117: handler);
118: } catch (IllegalAccessException iae) {
119: throw new RuntimeException(
120: "Could not create IVM proxy for "
121: + di.getRemoteInterface() + " interface",
122: iae);
123: }
124: return (javax.ejb.EJBObject) newProxy;
125: }
126:
127: public EJBLocalObject getEJBLocalObject() {
128: ThreadContext threadContext = ThreadContext.getThreadContext();
129: org.apache.openejb.DeploymentInfo di = threadContext
130: .getDeploymentInfo();
131:
132: EjbObjectProxyHandler handler = newEjbObjectHandler(
133: (RpcContainer) di.getContainer(), threadContext
134: .getPrimaryKey(), di.getDeploymentID(),
135: InterfaceType.EJB_LOCAL);
136:
137: Object newProxy = null;
138: try {
139: Class[] interfaces = new Class[] { di.getLocalInterface(),
140: org.apache.openejb.core.ivm.IntraVmProxy.class };
141: newProxy = ProxyManager.newProxyInstance(interfaces,
142: handler);
143: } catch (IllegalAccessException iae) {
144: throw new RuntimeException(
145: "Could not create IVM proxy for "
146: + di.getLocalInterface() + " interface",
147: iae);
148: }
149: return (EJBLocalObject) newProxy;
150: }
151:
152: public Object getBusinessObject(Class interfce) {
153: ThreadContext threadContext = ThreadContext.getThreadContext();
154: DeploymentInfo di = threadContext.getDeploymentInfo();
155:
156: InterfaceType interfaceType;
157: if (di.getBusinessLocalInterface() != null
158: && di.getBusinessLocalInterface().getName().equals(
159: interfce.getName())) {
160: interfaceType = InterfaceType.BUSINESS_LOCAL;
161: } else if (di.getBusinessRemoteInterface() != null
162: && di.getBusinessRemoteInterface().getName().equals(
163: interfce.getName())) {
164: interfaceType = InterfaceType.BUSINESS_REMOTE;
165: } else {
166: throw new IllegalArgumentException(
167: "Component has no such interface "
168: + interfce.getName());
169: }
170:
171: Object newProxy;
172: try {
173: EjbObjectProxyHandler handler = newEjbObjectHandler(
174: (RpcContainer) di.getContainer(), threadContext
175: .getPrimaryKey(), di.getDeploymentID(),
176: interfaceType);
177: Class[] interfaces = new Class[] { interfce,
178: IntraVmProxy.class };
179: newProxy = ProxyManager.newProxyInstance(interfaces,
180: handler);
181: } catch (IllegalAccessException iae) {
182: throw new InternalErrorException(
183: "Could not create IVM proxy for "
184: + interfce.getName() + " interface", iae);
185: }
186: return newProxy;
187: }
188:
189: public EJBLocalHome getEJBLocalHome() {
190: ThreadContext threadContext = ThreadContext.getThreadContext();
191: org.apache.openejb.core.CoreDeploymentInfo di = threadContext
192: .getDeploymentInfo();
193:
194: return di.getEJBLocalHome();
195: }
196:
197: public TimerService getTimerService() {
198: return null;
199: }
200:
201: public Object getPrimaryKey() {
202: /*
203: * This method is only declared in the EntityContext interface and is therefor
204: * unavailable in the SessionContext and doesn't not require a check for bean kind (Entity vs Session).
205: */
206: checkBeanState(EJBOBJECT_METHOD);
207:
208: ThreadContext threadContext = ThreadContext.getThreadContext();
209: return threadContext.getPrimaryKey();
210: }
211:
212: public boolean getRollbackOnly() {
213:
214: ThreadContext threadContext = ThreadContext.getThreadContext();
215: org.apache.openejb.DeploymentInfo di = threadContext
216: .getDeploymentInfo();
217: if (di.isBeanManagedTransaction()) {
218: throw new IllegalStateException(
219: "bean-managed transaction beans can not access the getRollbackOnly( ) method");
220: }
221:
222: checkBeanState(ROLLBACK_METHOD);
223: try {
224: int status = getTransactionManager().getStatus();
225: if (status == Status.STATUS_MARKED_ROLLBACK
226: || status == Status.STATUS_ROLLEDBACK) {
227: return true;
228: } else if (status == Status.STATUS_NO_TRANSACTION) {
229: // this would be true for Supports tx attribute where no tx was propagated
230: throw new IllegalStateException(
231: "No current transaction");
232: } else {
233: return false;
234: }
235: } catch (javax.transaction.SystemException se) {
236: throw new RuntimeException(
237: "Transaction service has thrown a SystemException",
238: se);
239: }
240: }
241:
242: public void setRollbackOnly() {
243: ThreadContext threadContext = ThreadContext.getThreadContext();
244: org.apache.openejb.DeploymentInfo di = threadContext
245: .getDeploymentInfo();
246: if (di.isBeanManagedTransaction()) {
247: throw new IllegalStateException(
248: "bean-managed transaction beans can not access the setRollbackOnly( ) method");
249: }
250:
251: checkBeanState(ROLLBACK_METHOD);
252:
253: try {
254: getTransactionManager().setRollbackOnly();
255: } catch (javax.transaction.SystemException se) {
256: throw new RuntimeException(
257: "Transaction service has thrown a SystemException",
258: se);
259: }
260:
261: }
262:
263: public javax.transaction.UserTransaction getUserTransaction() {
264:
265: ThreadContext threadContext = ThreadContext.getThreadContext();
266: org.apache.openejb.DeploymentInfo di = threadContext
267: .getDeploymentInfo();
268: if (di.isBeanManagedTransaction()) {
269: checkBeanState(USER_TRANSACTION_METHOD);
270: return userTransaction;
271: } else {
272: throw new java.lang.IllegalStateException(
273: "container-managed transaction beans can not access the UserTransaction");
274: }
275: }
276:
277: /**
278: * Lookup a resource within the component's private naming context.
279: *
280: * @param name - Name of the entry (relative to java:comp/env).
281: * @return The looked-up object.
282: * @see http://java.sun.com/javaee/5/docs/api/javax/ejb/EJBContext.html#lookup(java.lang.String)
283: * @see EJB3.0 "Core Contracts and Requirements", section 4.5.2, table 2.
284: */
285: public Object lookup(String name) {
286: Context initialContext = null;
287: Object object = null;
288:
289: try {
290: initialContext = new InitialContext();
291: object = initialContext.lookup("java:comp/env/" + name);
292: } catch (NamingException nex) {
293: throw new IllegalArgumentException(nex);
294: }
295: return object;
296: }
297:
298: /*----------------------------------------------------*/
299: /* UNSUPPORTED DEPRICATED METHODS */
300: /*----------------------------------------------------*/
301:
302: public boolean isCallerInRole(java.security.Identity role) {
303: throw new java.lang.UnsupportedOperationException();
304: }
305:
306: public java.security.Identity getCallerIdentity() {
307: throw new java.lang.UnsupportedOperationException();
308: }
309:
310: public java.util.Properties getEnvironment() {
311: throw new java.lang.UnsupportedOperationException();
312: }
313:
314: protected abstract EjbObjectProxyHandler newEjbObjectHandler(
315: RpcContainer container, Object pk, Object depID,
316: InterfaceType interfaceType);
317: }
|