001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb;
023:
024: import javax.ejb.*;
025: import javax.transaction.UserTransaction;
026: import javax.xml.rpc.handler.MessageContext;
027: import java.io.Serializable;
028: import java.lang.reflect.InvocationTargetException;
029: import java.lang.reflect.Method;
030: import java.rmi.RemoteException;
031: import java.security.Principal;
032: import java.util.Collection;
033: import java.util.Date;
034:
035: /**
036: * The enterprise context for stateless session beans.
037: *
038: * @author <a href="mailto:rickard.oberg@telkel.com">Rickard Öberg</a>
039: * @author <a href="sebastien.alborini@m4x.org">Sebastien Alborini</a>
040: * @version $Revision: 57209 $
041: */
042: public class StatelessSessionEnterpriseContext extends
043: EnterpriseContext {
044: // Constants -----------------------------------------------------
045:
046: // Attributes ----------------------------------------------------
047:
048: EJBObject ejbObject;
049: EJBLocalObject ejbLocalObject;
050: MessageContext soapMessageContext;
051: SessionContext ctx;
052:
053: // Static --------------------------------------------------------
054:
055: // Constructors --------------------------------------------------
056:
057: public StatelessSessionEnterpriseContext(Object instance,
058: Container con) throws Exception {
059: super (instance, con);
060: ctx = new SessionContextImpl();
061:
062: try {
063: AllowedOperationsAssociation
064: .pushInMethodFlag(IN_SET_SESSION_CONTEXT);
065: ((SessionBean) instance).setSessionContext(ctx);
066: } finally {
067: AllowedOperationsAssociation.popInMethodFlag();
068: }
069:
070: try {
071: AllowedOperationsAssociation
072: .pushInMethodFlag(IN_EJB_CREATE);
073: Method ejbCreate = instance.getClass().getMethod(
074: "ejbCreate", new Class[0]);
075: ejbCreate.invoke(instance, new Object[0]);
076: } catch (InvocationTargetException e) {
077: Throwable ex = e.getTargetException();
078: if (ex instanceof EJBException)
079: throw (Exception) ex;
080: else if (ex instanceof RuntimeException)
081: throw new EJBException((Exception) ex); // Transform runtime exception into what a bean *should* have thrown
082: else if (ex instanceof Exception)
083: throw (Exception) ex;
084: else
085: throw (Error) ex;
086: } finally {
087: AllowedOperationsAssociation.popInMethodFlag();
088: }
089: }
090:
091: // Public --------------------------------------------------------
092:
093: public void setEJBObject(EJBObject eo) {
094: ejbObject = eo;
095: }
096:
097: public EJBObject getEJBObject() {
098: return ejbObject;
099: }
100:
101: public void setEJBLocalObject(EJBLocalObject eo) {
102: ejbLocalObject = eo;
103: }
104:
105: public EJBLocalObject getEJBLocalObject() {
106: return ejbLocalObject;
107: }
108:
109: public SessionContext getSessionContext() {
110: return ctx;
111: }
112:
113: public void setMessageContext(MessageContext msgContext) {
114: this .soapMessageContext = msgContext;
115: }
116:
117: // EnterpriseContext overrides -----------------------------------
118:
119: public void discard() throws RemoteException {
120: ((SessionBean) instance).ejbRemove();
121: }
122:
123: public EJBContext getEJBContext() {
124: return ctx;
125: }
126:
127: // Package protected ---------------------------------------------
128:
129: // Protected -----------------------------------------------------
130:
131: // Private -------------------------------------------------------
132:
133: // Inner classes -------------------------------------------------
134:
135: protected class SessionContextImpl extends EJBContextImpl implements
136: SessionContext {
137: public EJBHome getEJBHome() {
138: AllowedOperationsAssociation.assertAllowedIn("getEJBHome",
139: IN_SET_SESSION_CONTEXT | IN_EJB_CREATE
140: | IN_EJB_REMOVE | IN_BUSINESS_METHOD
141: | IN_EJB_TIMEOUT
142: | IN_SERVICE_ENDPOINT_METHOD);
143:
144: return super .getEJBHome();
145: }
146:
147: public EJBLocalHome getEJBLocalHome() {
148: AllowedOperationsAssociation.assertAllowedIn(
149: "getEJBLocalHome", IN_SET_SESSION_CONTEXT
150: | IN_EJB_CREATE | IN_EJB_REMOVE
151: | IN_BUSINESS_METHOD | IN_EJB_TIMEOUT
152: | IN_SERVICE_ENDPOINT_METHOD);
153:
154: return super .getEJBLocalHome();
155: }
156:
157: public EJBObject getEJBObject() {
158: AllowedOperationsAssociation.assertAllowedIn(
159: "getEJBObject", IN_EJB_CREATE | IN_EJB_REMOVE
160: | IN_BUSINESS_METHOD | IN_EJB_TIMEOUT
161: | IN_SERVICE_ENDPOINT_METHOD);
162:
163: if (((StatelessSessionContainer) con).getProxyFactory() == null)
164: throw new IllegalStateException(
165: "No remote interface defined.");
166:
167: if (ejbObject == null) {
168: EJBProxyFactory proxyFactory = con.getProxyFactory();
169: if (proxyFactory == null) {
170: String defaultInvokerName = con.getBeanMetaData()
171: .getContainerConfiguration()
172: .getDefaultInvokerName();
173: proxyFactory = con
174: .lookupProxyFactory(defaultInvokerName);
175: }
176: ejbObject = (EJBObject) proxyFactory
177: .getStatelessSessionEJBObject();
178: }
179:
180: return ejbObject;
181: }
182:
183: public Object getBusinessObject(Class businessInterface)
184: throws IllegalStateException {
185: throw new RuntimeException("NOT IMPLEMENTED");
186: }
187:
188: public Class getInvokedBusinessInterface()
189: throws IllegalStateException {
190: throw new RuntimeException("NOT IMPLEMENTED");
191: }
192:
193: public EJBLocalObject getEJBLocalObject() {
194: AllowedOperationsAssociation.assertAllowedIn(
195: "getEJBLocalObject", IN_EJB_CREATE | IN_EJB_REMOVE
196: | IN_BUSINESS_METHOD | IN_EJB_TIMEOUT
197: | IN_SERVICE_ENDPOINT_METHOD);
198:
199: if (con.getLocalHomeClass() == null)
200: throw new IllegalStateException(
201: "No local interface for bean.");
202: if (ejbLocalObject == null) {
203: ejbLocalObject = ((StatelessSessionContainer) con)
204: .getLocalProxyFactory()
205: .getStatelessSessionEJBLocalObject();
206: }
207: return ejbLocalObject;
208: }
209:
210: public TimerService getTimerService()
211: throws IllegalStateException {
212: AllowedOperationsAssociation.assertAllowedIn(
213: "getTimerService", IN_EJB_CREATE | IN_EJB_REMOVE
214: | IN_BUSINESS_METHOD | IN_EJB_TIMEOUT
215: | IN_SERVICE_ENDPOINT_METHOD);
216: return new TimerServiceWrapper(this , super
217: .getTimerService());
218: }
219:
220: public Principal getCallerPrincipal() {
221: AllowedOperationsAssociation.assertAllowedIn(
222: "getCallerPrincipal", IN_BUSINESS_METHOD
223: | IN_EJB_TIMEOUT
224: | IN_SERVICE_ENDPOINT_METHOD);
225: return super .getCallerPrincipal();
226: }
227:
228: public boolean getRollbackOnly() {
229: AllowedOperationsAssociation.assertAllowedIn(
230: "getRollbackOnly", IN_BUSINESS_METHOD
231: | IN_EJB_TIMEOUT
232: | IN_SERVICE_ENDPOINT_METHOD);
233: return super .getRollbackOnly();
234: }
235:
236: public void setRollbackOnly() {
237: AllowedOperationsAssociation.assertAllowedIn(
238: "setRollbackOnly", IN_BUSINESS_METHOD
239: | IN_EJB_TIMEOUT
240: | IN_SERVICE_ENDPOINT_METHOD);
241: super .setRollbackOnly();
242: }
243:
244: public boolean isCallerInRole(String id) {
245: AllowedOperationsAssociation.assertAllowedIn(
246: "isCallerInRole", IN_BUSINESS_METHOD
247: | IN_EJB_TIMEOUT
248: | IN_SERVICE_ENDPOINT_METHOD);
249: return super .isCallerInRole(id);
250: }
251:
252: public UserTransaction getUserTransaction() {
253: AllowedOperationsAssociation.assertAllowedIn(
254: "getUserTransaction", IN_EJB_CREATE | IN_EJB_REMOVE
255: | IN_BUSINESS_METHOD | IN_EJB_TIMEOUT
256: | IN_SERVICE_ENDPOINT_METHOD);
257: return super .getUserTransaction();
258: }
259:
260: public MessageContext getMessageContext()
261: throws IllegalStateException {
262: AllowedOperationsAssociation.assertAllowedIn(
263: "getMessageContext", IN_SERVICE_ENDPOINT_METHOD);
264: return soapMessageContext;
265: }
266: }
267:
268: /**
269: * Delegates to the underlying TimerService, after checking access
270: */
271: public class TimerServiceWrapper implements TimerService {
272:
273: private EnterpriseContext.EJBContextImpl context;
274: private TimerService timerService;
275:
276: public TimerServiceWrapper(
277: EnterpriseContext.EJBContextImpl ctx,
278: TimerService timerService) {
279: this .context = ctx;
280: this .timerService = timerService;
281: }
282:
283: public Timer createTimer(long duration, Serializable info)
284: throws IllegalArgumentException, IllegalStateException,
285: EJBException {
286: assertAllowedIn("TimerService.createTimer");
287: return timerService.createTimer(duration, info);
288: }
289:
290: public Timer createTimer(long initialDuration,
291: long intervalDuration, Serializable info)
292: throws IllegalArgumentException, IllegalStateException,
293: EJBException {
294: assertAllowedIn("TimerService.createTimer");
295: return timerService.createTimer(initialDuration,
296: intervalDuration, info);
297: }
298:
299: public Timer createTimer(Date expiration, Serializable info)
300: throws IllegalArgumentException, IllegalStateException,
301: EJBException {
302: assertAllowedIn("TimerService.createTimer");
303: return timerService.createTimer(expiration, info);
304: }
305:
306: public Timer createTimer(Date initialExpiration,
307: long intervalDuration, Serializable info)
308: throws IllegalArgumentException, IllegalStateException,
309: EJBException {
310: assertAllowedIn("TimerService.createTimer");
311: return timerService.createTimer(initialExpiration,
312: intervalDuration, info);
313: }
314:
315: public Collection getTimers() throws IllegalStateException,
316: EJBException {
317: assertAllowedIn("TimerService.getTimers");
318: return timerService.getTimers();
319: }
320:
321: private void assertAllowedIn(String timerMethod) {
322: AllowedOperationsAssociation.assertAllowedIn(timerMethod,
323: IN_BUSINESS_METHOD | IN_EJB_TIMEOUT
324: | IN_SERVICE_ENDPOINT_METHOD);
325: }
326: }
327: }
|