KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > StatelessSessionEnterpriseContext


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb;
23
24 import javax.ejb.*;
25 import javax.transaction.UserTransaction JavaDoc;
26 import javax.xml.rpc.handler.MessageContext JavaDoc;
27 import java.io.Serializable JavaDoc;
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30 import java.rmi.RemoteException JavaDoc;
31 import java.security.Principal JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.Date JavaDoc;
34
35 /**
36  * The enterprise context for stateless session beans.
37  *
38  * @author <a HREF="mailto:rickard.oberg@telkel.com">Rickard Öberg</a>
39  * @author <a HREF="sebastien.alborini@m4x.org">Sebastien Alborini</a>
40  * @version $Revision: 46279 $
41  */

42 public class StatelessSessionEnterpriseContext
43    extends EnterpriseContext
44 {
45    // Constants -----------------------------------------------------
46

47    // Attributes ----------------------------------------------------
48

49    EJBObject ejbObject;
50    EJBLocalObject ejbLocalObject;
51    MessageContext JavaDoc soapMessageContext;
52    SessionContext ctx;
53    
54    // Static --------------------------------------------------------
55

56    // Constructors --------------------------------------------------
57

58    public StatelessSessionEnterpriseContext(Object JavaDoc instance, Container con)
59       throws Exception JavaDoc
60    {
61       super(instance, con);
62       ctx = new SessionContextImpl();
63
64       try
65       {
66          AllowedOperationsAssociation.pushInMethodFlag(IN_SET_SESSION_CONTEXT);
67          ((SessionBean)instance).setSessionContext(ctx);
68       }
69       finally
70       {
71          AllowedOperationsAssociation.popInMethodFlag();
72       }
73
74       try
75       {
76          AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_CREATE);
77          Method JavaDoc ejbCreate = instance.getClass().getMethod("ejbCreate", new Class JavaDoc[0]);
78          ejbCreate.invoke(instance, new Object JavaDoc[0]);
79       } catch (InvocationTargetException JavaDoc e)
80       {
81          Throwable JavaDoc ex = e.getTargetException();
82          if (ex instanceof EJBException)
83             throw (Exception JavaDoc)ex;
84          else if (ex instanceof RuntimeException JavaDoc)
85             throw new EJBException((Exception JavaDoc)ex); // Transform runtime exception into what a bean *should* have thrown
86
else if (ex instanceof Exception JavaDoc)
87             throw (Exception JavaDoc)ex;
88          else
89             throw (Error JavaDoc)ex;
90       }
91       finally
92       {
93          AllowedOperationsAssociation.popInMethodFlag();
94       }
95    }
96    
97    // Public --------------------------------------------------------
98

99    public void setEJBObject(EJBObject eo) {
100       ejbObject = eo;
101    }
102    
103    public EJBObject getEJBObject() {
104       return ejbObject;
105    }
106    
107    public void setEJBLocalObject(EJBLocalObject eo) {
108       ejbLocalObject = eo;
109    }
110    
111    public EJBLocalObject getEJBLocalObject() {
112       return ejbLocalObject;
113    }
114    
115    public SessionContext getSessionContext() {
116       return ctx;
117    }
118
119    public void setMessageContext(MessageContext JavaDoc msgContext)
120    {
121       this.soapMessageContext = msgContext;
122    }
123
124    // EnterpriseContext overrides -----------------------------------
125

126    public void discard() throws RemoteException JavaDoc
127    {
128       ((SessionBean)instance).ejbRemove();
129    }
130    
131    public EJBContext getEJBContext()
132    {
133       return ctx;
134    }
135    
136    // Package protected ---------------------------------------------
137

138    // Protected -----------------------------------------------------
139

140    // Private -------------------------------------------------------
141

142    // Inner classes -------------------------------------------------
143

144    protected class SessionContextImpl
145       extends EJBContextImpl
146       implements SessionContext
147    {
148       public EJBHome getEJBHome()
149       {
150          AllowedOperationsAssociation.assertAllowedIn("getEJBHome",
151                  IN_SET_SESSION_CONTEXT | IN_EJB_CREATE | IN_EJB_REMOVE |
152                  IN_BUSINESS_METHOD | IN_EJB_TIMEOUT | IN_SERVICE_ENDPOINT_METHOD);
153
154          return super.getEJBHome();
155       }
156
157       public EJBLocalHome getEJBLocalHome()
158       {
159          AllowedOperationsAssociation.assertAllowedIn("getEJBLocalHome",
160                  IN_SET_SESSION_CONTEXT | IN_EJB_CREATE | IN_EJB_REMOVE |
161                  IN_BUSINESS_METHOD | IN_EJB_TIMEOUT | IN_SERVICE_ENDPOINT_METHOD);
162
163          return super.getEJBLocalHome();
164       }
165
166       public EJBObject getEJBObject()
167       {
168          AllowedOperationsAssociation.assertAllowedIn("getEJBObject",
169                  IN_EJB_CREATE | IN_EJB_REMOVE | IN_BUSINESS_METHOD | IN_EJB_TIMEOUT | IN_SERVICE_ENDPOINT_METHOD);
170
171          if (((StatelessSessionContainer)con).getProxyFactory()==null)
172             throw new IllegalStateException JavaDoc( "No remote interface defined." );
173          
174          if (ejbObject == null)
175          {
176             EJBProxyFactory proxyFactory = con.getProxyFactory();
177             if(proxyFactory == null)
178             {
179                String JavaDoc defaultInvokerName = con.getBeanMetaData().
180                   getContainerConfiguration().getDefaultInvokerName();
181                proxyFactory = con.lookupProxyFactory(defaultInvokerName);
182             }
183             ejbObject = (EJBObject) proxyFactory.getStatelessSessionEJBObject();
184          }
185     
186          return ejbObject;
187       }
188       
189       public Object JavaDoc getBusinessObject(Class JavaDoc businessInterface) throws IllegalStateException JavaDoc
190       {
191          throw new RuntimeException JavaDoc("NOT IMPLEMENTED");
192       }
193       
194       public Class JavaDoc getInvokedBusinessInterface() throws IllegalStateException JavaDoc
195       {
196          throw new RuntimeException JavaDoc("NOT IMPLEMENTED");
197       }
198
199       public EJBLocalObject getEJBLocalObject()
200       {
201          AllowedOperationsAssociation.assertAllowedIn("getEJBLocalObject",
202                  IN_EJB_CREATE | IN_EJB_REMOVE | IN_BUSINESS_METHOD | IN_EJB_TIMEOUT | IN_SERVICE_ENDPOINT_METHOD);
203
204          if (con.getLocalHomeClass()==null)
205             throw new IllegalStateException JavaDoc( "No local interface for bean." );
206          if (ejbLocalObject == null) {
207             ejbLocalObject = ((StatelessSessionContainer)con).getLocalProxyFactory().getStatelessSessionEJBLocalObject();
208          }
209          return ejbLocalObject;
210       }
211
212       public TimerService getTimerService() throws IllegalStateException JavaDoc
213       {
214          AllowedOperationsAssociation.assertAllowedIn("getTimerService",
215                  IN_EJB_CREATE | IN_EJB_REMOVE | IN_BUSINESS_METHOD | IN_EJB_TIMEOUT | IN_SERVICE_ENDPOINT_METHOD);
216          return new TimerServiceWrapper(this, super.getTimerService());
217       }
218
219       public Principal JavaDoc getCallerPrincipal()
220       {
221          AllowedOperationsAssociation.assertAllowedIn("getCallerPrincipal",
222                  IN_BUSINESS_METHOD | IN_EJB_TIMEOUT | IN_SERVICE_ENDPOINT_METHOD);
223          return super.getCallerPrincipal();
224       }
225
226       public boolean getRollbackOnly()
227       {
228          AllowedOperationsAssociation.assertAllowedIn("getRollbackOnly",
229                  IN_BUSINESS_METHOD | IN_EJB_TIMEOUT | IN_SERVICE_ENDPOINT_METHOD);
230          return super.getRollbackOnly();
231       }
232
233       public void setRollbackOnly()
234       {
235          AllowedOperationsAssociation.assertAllowedIn("setRollbackOnly",
236                  IN_BUSINESS_METHOD | IN_EJB_TIMEOUT | IN_SERVICE_ENDPOINT_METHOD);
237          super.setRollbackOnly();
238       }
239
240       public boolean isCallerInRole(String JavaDoc id)
241       {
242          AllowedOperationsAssociation.assertAllowedIn("isCallerInRole",
243                  IN_BUSINESS_METHOD | IN_EJB_TIMEOUT | IN_SERVICE_ENDPOINT_METHOD);
244          return super.isCallerInRole(id);
245       }
246
247       public UserTransaction JavaDoc getUserTransaction()
248       {
249          AllowedOperationsAssociation.assertAllowedIn("getUserTransaction",
250                  IN_EJB_CREATE | IN_EJB_REMOVE | IN_BUSINESS_METHOD | IN_EJB_TIMEOUT | IN_SERVICE_ENDPOINT_METHOD);
251          return super.getUserTransaction();
252       }
253
254       public MessageContext JavaDoc getMessageContext() throws IllegalStateException JavaDoc
255       {
256          AllowedOperationsAssociation.assertAllowedIn("getMessageContext",
257                  IN_SERVICE_ENDPOINT_METHOD);
258          return soapMessageContext;
259       }
260    }
261
262    /**
263     * Delegates to the underlying TimerService, after checking access
264     */

265    public class TimerServiceWrapper implements TimerService
266    {
267
268       private EnterpriseContext.EJBContextImpl context;
269       private TimerService timerService;
270
271       public TimerServiceWrapper(EnterpriseContext.EJBContextImpl ctx, TimerService timerService)
272       {
273          this.context = ctx;
274          this.timerService = timerService;
275       }
276
277       public Timer createTimer(long duration, Serializable JavaDoc info) throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, EJBException
278       {
279          assertAllowedIn("TimerService.createTimer");
280          return timerService.createTimer(duration, info);
281       }
282
283       public Timer createTimer(long initialDuration, long intervalDuration, Serializable JavaDoc info) throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, EJBException
284       {
285          assertAllowedIn("TimerService.createTimer");
286          return timerService.createTimer(initialDuration, intervalDuration, info);
287       }
288
289       public Timer createTimer(Date JavaDoc expiration, Serializable JavaDoc info) throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, EJBException
290       {
291          assertAllowedIn("TimerService.createTimer");
292          return timerService.createTimer(expiration, info);
293       }
294
295       public Timer createTimer(Date JavaDoc initialExpiration, long intervalDuration, Serializable JavaDoc info) throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, EJBException
296       {
297          assertAllowedIn("TimerService.createTimer");
298          return timerService.createTimer(initialExpiration, intervalDuration, info);
299       }
300
301       public Collection JavaDoc getTimers() throws IllegalStateException JavaDoc, EJBException
302       {
303          assertAllowedIn("TimerService.getTimers");
304          return timerService.getTimers();
305       }
306
307       private void assertAllowedIn(String JavaDoc timerMethod)
308       {
309          AllowedOperationsAssociation.assertAllowedIn(timerMethod,
310                  IN_BUSINESS_METHOD | IN_EJB_TIMEOUT | IN_SERVICE_ENDPOINT_METHOD);
311       }
312    }
313 }
Popular Tags