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 java.lang.reflect.Method;
019: import java.security.Principal;
020: import java.util.List;
021: import java.util.ArrayList;
022: import javax.ejb.EJBLocalObject;
023: import javax.ejb.EJBObject;
024: import javax.ejb.SessionContext;
025: import javax.ejb.TimerService;
026: import javax.transaction.TransactionManager;
027: import javax.transaction.UserTransaction;
028: import javax.transaction.NotSupportedException;
029: import javax.transaction.SystemException;
030: import javax.transaction.HeuristicMixedException;
031: import javax.transaction.HeuristicRollbackException;
032: import javax.transaction.RollbackException;
033: import javax.xml.rpc.handler.MessageContext;
034:
035: import org.apache.openejb.DeploymentInfo;
036: import org.apache.openejb.InterfaceType;
037: import org.apache.openejb.InternalErrorException;
038: import org.apache.openejb.RpcContainer;
039: import org.apache.openejb.core.ivm.EjbObjectProxyHandler;
040: import org.apache.openejb.core.ivm.IntraVmProxy;
041: import org.apache.openejb.core.stateless.StatelessEjbObjectHandler;
042: import org.apache.openejb.core.stateful.StatefulEjbObjectHandler;
043: import org.apache.openejb.spi.SecurityService;
044: import org.apache.openejb.util.proxy.ProxyManager;
045:
046: /**
047: * @version $Rev: 602704 $ $Date: 2007-12-09 09:58:22 -0800 $
048: */
049: public abstract class BaseSessionContext extends BaseContext implements
050: SessionContext {
051:
052: public BaseSessionContext(TransactionManager transactionManager,
053: SecurityService securityService) {
054: super (transactionManager, securityService);
055: }
056:
057: public BaseSessionContext(TransactionManager transactionManager,
058: SecurityService securityService,
059: UserTransaction userTransaction) {
060: super (transactionManager, securityService, userTransaction);
061: }
062:
063: public EJBLocalObject getEJBLocalObject()
064: throws IllegalStateException {
065: return ((SessionState) getState()).getEJBLocalObject();
066: }
067:
068: public EJBObject getEJBObject() throws IllegalStateException {
069: return ((SessionState) getState()).getEJBObject();
070: }
071:
072: public MessageContext getMessageContext()
073: throws IllegalStateException {
074: return ((SessionState) getState()).getMessageContext();
075: }
076:
077: public Object getBusinessObject(Class aClass) {
078: return ((SessionState) getState()).getBusinessObject(aClass);
079: }
080:
081: public Class getInvokedBusinessInterface() {
082: return ((SessionState) getState())
083: .getInvokedBusinessInterface();
084: }
085:
086: protected static class SessionState extends State {
087:
088: public EJBLocalObject getEJBLocalObject()
089: throws IllegalStateException {
090: ThreadContext threadContext = ThreadContext
091: .getThreadContext();
092: DeploymentInfo di = threadContext.getDeploymentInfo();
093:
094: if (di.getLocalHomeInterface() == null)
095: throw new IllegalStateException(
096: "Bean does not have an EJBLocalObject interface: "
097: + di.getDeploymentID());
098:
099: return (EJBLocalObject) EjbObjectProxyHandler.createProxy(
100: di, threadContext.getPrimaryKey(),
101: InterfaceType.EJB_LOCAL);
102: }
103:
104: public EJBObject getEJBObject() throws IllegalStateException {
105: ThreadContext threadContext = ThreadContext
106: .getThreadContext();
107: DeploymentInfo di = threadContext.getDeploymentInfo();
108: if (di.getHomeInterface() == null)
109: throw new IllegalStateException(
110: "Bean does not have an EJBObject interface: "
111: + di.getDeploymentID());
112:
113: return (EJBObject) EjbObjectProxyHandler.createProxy(di,
114: threadContext.getPrimaryKey(),
115: InterfaceType.EJB_OBJECT);
116: }
117:
118: public MessageContext getMessageContext()
119: throws IllegalStateException {
120: ThreadContext threadContext = ThreadContext
121: .getThreadContext();
122: MessageContext messageContext = threadContext
123: .get(MessageContext.class);
124: if (messageContext == null)
125: throw new IllegalStateException(
126: "Only calls on the service-endpoint have a MessageContext.");
127: return messageContext;
128: }
129:
130: public Object getBusinessObject(Class interfce) {
131: if (interfce == null)
132: throw new IllegalStateException(
133: "Interface argument cannot me null.");
134:
135: ThreadContext threadContext = ThreadContext
136: .getThreadContext();
137: DeploymentInfo di = threadContext.getDeploymentInfo();
138:
139: InterfaceType interfaceType = di.getInterfaceType(interfce);
140:
141: if (interfaceType == null) {
142: throw new IllegalStateException(
143: "Component has no such interface: "
144: + interfce.getName());
145: }
146:
147: if (!interfaceType.isBusiness()) {
148: throw new IllegalStateException(
149: "Interface is not a business interface for this bean: "
150: + interfce.getName());
151: }
152:
153: try {
154: EjbObjectProxyHandler handler;
155: switch (di.getComponentType()) {
156: case STATEFUL: {
157: handler = new StatefulEjbObjectHandler(di,
158: threadContext.getPrimaryKey(),
159: interfaceType, new ArrayList<Class>());
160: break;
161: }
162: case STATELESS: {
163: handler = new StatelessEjbObjectHandler(di,
164: threadContext.getPrimaryKey(),
165: interfaceType, new ArrayList<Class>());
166: break;
167: }
168: default:
169: throw new IllegalStateException(
170: "Bean is not a session bean: "
171: + di.getComponentType());
172: }
173:
174: List<Class> interfaces = new ArrayList<Class>();
175: interfaces.addAll(di.getInterfaces(interfaceType));
176: interfaces.add(IntraVmProxy.class);
177: return ProxyManager.newProxyInstance(interfaces
178: .toArray(new Class[] {}), handler);
179: } catch (IllegalAccessException iae) {
180: throw new InternalErrorException(
181: "Could not create IVM proxy for "
182: + interfce.getName() + " interface",
183: iae);
184: }
185: }
186:
187: public Class getInvokedBusinessInterface() {
188: ThreadContext threadContext = ThreadContext
189: .getThreadContext();
190: Class invokedInterface = threadContext
191: .getInvokedInterface();
192: InterfaceType type = threadContext.getDeploymentInfo()
193: .getInterfaceType(invokedInterface);
194: if (!type.isBusiness())
195: throw new IllegalStateException(
196: "The EJB spec requires us to cripple the use of this method for anything but business interface proxy. But FYI, your invoked interface is: "
197: + invokedInterface.getName());
198:
199: if (invokedInterface == null) {
200: throw new IllegalStateException(
201: "Business interface not set into ThreadContext.");
202: }
203: return invokedInterface;
204: }
205: }
206:
207: /**
208: * Dependency injection methods (e.g., setSessionContext)
209: */
210: public static class InjectionSessionState extends SessionState {
211:
212: public EJBLocalObject getEJBLocalObject()
213: throws IllegalStateException {
214: throw new IllegalStateException();
215: }
216:
217: public EJBObject getEJBObject() throws IllegalStateException {
218: throw new IllegalStateException();
219: }
220:
221: public MessageContext getMessageContext()
222: throws IllegalStateException {
223: throw new IllegalStateException();
224: }
225:
226: public Object getBusinessObject(Class interfce) {
227: throw new IllegalStateException();
228: }
229:
230: public Class getInvokedBusinessInterface() {
231: throw new IllegalStateException();
232: }
233:
234: public Principal getCallerPrincipal(
235: SecurityService securityService) {
236: throw new IllegalStateException();
237: }
238:
239: public boolean isCallerInRole(SecurityService securityService,
240: String roleName) {
241: throw new IllegalStateException();
242: }
243:
244: public UserTransaction getUserTransaction(
245: UserTransaction userTransaction)
246: throws IllegalStateException {
247: throw new IllegalStateException();
248: }
249:
250: public void setRollbackOnly(
251: TransactionManager transactionManager)
252: throws IllegalStateException {
253: throw new IllegalStateException();
254: }
255:
256: public boolean getRollbackOnly(
257: TransactionManager transactionManager)
258: throws IllegalStateException {
259: throw new IllegalStateException();
260: }
261:
262: public TimerService getTimerService()
263: throws IllegalStateException {
264: throw new IllegalStateException();
265: }
266:
267: public boolean isUserTransactionAccessAllowed() {
268: return false;
269: }
270:
271: public boolean isMessageContextAccessAllowed() {
272: return false;
273: }
274:
275: public boolean isEntityManagerFactoryAccessAllowed() {
276: return false;
277: }
278:
279: public boolean isEntityManagerAccessAllowed() {
280: return false;
281: }
282:
283: public boolean isTimerAccessAllowed() {
284: return false;
285: }
286:
287: public boolean isTimerMethodAllowed() {
288: return false;
289: }
290: }
291:
292: /**
293: * PostConstruct, Pre-Destroy lifecycle callback interceptor methods
294: */
295: public static class LifecycleSessionState extends SessionState {
296:
297: public MessageContext getMessageContext()
298: throws IllegalStateException {
299: throw new IllegalStateException();
300: }
301:
302: public Class getInvokedBusinessInterface() {
303: throw new IllegalStateException();
304: }
305:
306: public Principal getCallerPrincipal(
307: SecurityService securityService) {
308: throw new IllegalStateException();
309: }
310:
311: public boolean isCallerInRole(SecurityService securityService,
312: String roleName) {
313: throw new IllegalStateException();
314: }
315:
316: public void setRollbackOnly(
317: TransactionManager transactionManager)
318: throws IllegalStateException {
319: throw new IllegalStateException();
320: }
321:
322: public boolean getRollbackOnly(
323: TransactionManager transactionManager)
324: throws IllegalStateException {
325: throw new IllegalStateException();
326: }
327:
328: public boolean isUserTransactionAccessAllowed() {
329: return false;
330: }
331:
332: public boolean isMessageContextAccessAllowed() {
333: return false;
334: }
335:
336: public boolean isJNDIAccessAllowed() {
337: return false;
338: }
339:
340: public boolean isEntityManagerFactoryAccessAllowed() {
341: return false;
342: }
343:
344: public boolean isEntityManagerAccessAllowed() {
345: return false;
346: }
347:
348: public boolean isTimerAccessAllowed() {
349: return false;
350: }
351:
352: public boolean isTimerMethodAllowed() {
353: return false;
354: }
355: }
356:
357: public static class PostConstructSessionState extends
358: LifecycleSessionState {
359:
360: public UserTransaction getUserTransaction(
361: UserTransaction userTransaction)
362: throws IllegalStateException {
363: return new RestrictedUserTransaction(super
364: .getUserTransaction(userTransaction));
365: }
366: }
367:
368: /**
369: * Business method from business interface or component interface; business
370: * method interceptor method
371: */
372: public static class BusinessSessionState extends SessionState {
373:
374: public MessageContext getMessageContext()
375: throws IllegalStateException {
376: throw new IllegalStateException();
377: }
378:
379: public boolean isMessageContextAccessAllowed() {
380: return false;
381: }
382: }
383:
384: /**
385: * Timeout callback method
386: */
387: public static class TimeoutSessionState extends SessionState {
388:
389: public Class getInvokedBusinessInterface() {
390: throw new IllegalStateException();
391: }
392:
393: public MessageContext getMessageContext()
394: throws IllegalStateException {
395: throw new IllegalStateException();
396: }
397:
398: public boolean isMessageContextAccessAllowed() {
399: return false;
400: }
401: }
402:
403: }
|