KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.Serializable JavaDoc;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.rmi.RemoteException JavaDoc;
28 import java.security.Principal JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Date JavaDoc;
31 import javax.ejb.EJBContext JavaDoc;
32 import javax.ejb.EJBException JavaDoc;
33 import javax.ejb.EJBHome JavaDoc;
34 import javax.ejb.EJBLocalHome JavaDoc;
35 import javax.ejb.MessageDrivenBean JavaDoc;
36 import javax.ejb.MessageDrivenContext JavaDoc;
37 import javax.ejb.Timer JavaDoc;
38 import javax.ejb.TimerService JavaDoc;
39 import javax.transaction.UserTransaction JavaDoc;
40
41 import org.jboss.metadata.MessageDrivenMetaData;
42 import org.jboss.metadata.MetaData;
43
44 /**
45  * Context for message driven beans.
46  *
47  * @author <a HREF="mailto:peter.antman@tim.se">Peter Antman</a>.
48  * @author <a HREF="mailto:rickard.oberg@telkel.com">Rickard Öberg</a>
49  * @author <a HREF="sebastien.alborini@m4x.org">Sebastien Alborini</a>
50  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
51  * @version <tt>$Revision: 37459 $</tt>
52  */

53 public class MessageDrivenEnterpriseContext
54    extends EnterpriseContext
55 {
56    private MessageDrivenContext JavaDoc ctx;
57
58    /**
59     * Construct a <tt>MessageDrivenEnterpriseContext</tt>.
60     *
61     * <p>Sets the MDB context and calls ejbCreate().
62     *
63     * @param instance An instance of MessageDrivenBean
64     * @param con The container for this MDB.
65     *
66     * @throws Exception EJBException, Error or Exception. If RuntimeException
67     * was thrown by ejbCreate it will be turned into an
68     * EJBException.
69     */

70    public MessageDrivenEnterpriseContext(Object JavaDoc instance, Container con)
71       throws Exception JavaDoc
72    {
73       super(instance, con);
74       
75       ctx = new MessageDrivenContextImpl();
76       ((MessageDrivenBean JavaDoc)instance).setMessageDrivenContext(ctx);
77
78       try
79       {
80          AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_CREATE);
81          Method JavaDoc ejbCreate = instance.getClass().getMethod("ejbCreate", new Class JavaDoc[0]);
82          ejbCreate.invoke(instance, new Object JavaDoc[0]);
83       }
84       catch (InvocationTargetException JavaDoc e)
85       {
86          Throwable JavaDoc t = e.getTargetException();
87          
88          if (t instanceof RuntimeException JavaDoc) {
89             if (t instanceof EJBException JavaDoc) {
90                throw (EJBException JavaDoc)t;
91             }
92             else {
93                // Transform runtime exception into what a bean *should* have thrown
94
throw new EJBException JavaDoc((RuntimeException JavaDoc)t);
95             }
96          }
97          else if (t instanceof Exception JavaDoc) {
98             throw (Exception JavaDoc)t;
99          }
100          else if (t instanceof Error JavaDoc) {
101             throw (Error JavaDoc)t;
102          }
103          else {
104             throw new org.jboss.util.NestedError("Unexpected Throwable", t);
105          }
106       }
107    }
108
109    public MessageDrivenContext JavaDoc getMessageDrivenContext()
110    {
111       return ctx;
112    }
113
114    // EnterpriseContext overrides -----------------------------------
115

116    /**
117     * Calls ejbRemove() on the MDB instance.
118     */

119    public void discard() throws RemoteException JavaDoc
120    {
121       ((MessageDrivenBean JavaDoc)instance).ejbRemove();
122    }
123
124    public EJBContext JavaDoc getEJBContext()
125    {
126       return ctx;
127    }
128
129    /**
130     * The EJBContext for MDBs.
131     */

132    protected class MessageDrivenContextImpl
133       extends EJBContextImpl
134       implements MessageDrivenContext JavaDoc
135    {
136
137       public EJBHome JavaDoc getEJBHome()
138       {
139          throw new IllegalStateException JavaDoc("getEJBHome should not be access from a message driven bean");
140       }
141
142       public EJBLocalHome JavaDoc getEJBLocalHome()
143       {
144          throw new IllegalStateException JavaDoc("getEJBHome should not be access from a message driven bean");
145       }
146
147       public TimerService JavaDoc getTimerService() throws IllegalStateException JavaDoc
148       {
149          AllowedOperationsAssociation.assertAllowedIn("getTimerService",
150                  IN_EJB_CREATE | IN_EJB_REMOVE | IN_BUSINESS_METHOD | IN_EJB_TIMEOUT);
151          return new TimerServiceWrapper(this, super.getTimerService());
152       }
153
154       public Principal JavaDoc getCallerPrincipal()
155       {
156          AllowedOperationsAssociation.assertAllowedIn("getCallerPrincipal",
157                  IN_BUSINESS_METHOD | IN_EJB_TIMEOUT);
158          return super.getCallerPrincipal();
159       }
160
161       public boolean isCallerInRole(String JavaDoc id)
162       {
163          throw new IllegalStateException JavaDoc("isCallerInRole should not be access from a message driven bean");
164       }
165
166       public UserTransaction JavaDoc getUserTransaction()
167       {
168          if (isContainerManagedTx())
169             throw new IllegalStateException JavaDoc("getUserTransaction should not be access for container managed Tx");
170
171          AllowedOperationsAssociation.assertAllowedIn("getUserTransaction",
172                  IN_EJB_CREATE | IN_EJB_REMOVE | IN_BUSINESS_METHOD | IN_EJB_TIMEOUT);
173
174          return super.getUserTransaction();
175       }
176
177       /**
178        * If transaction type is not Container or there is no transaction
179        * then throw an exception.
180        *
181        * @throws IllegalStateException If transaction type is not Container,
182        * or no transaction.
183        */

184       public boolean getRollbackOnly()
185       {
186          if (isUserManagedTx())
187             throw new IllegalStateException JavaDoc("getRollbackOnly should not be access for user managed Tx");
188
189          AllowedOperationsAssociation.assertAllowedIn("getRollbackOnly",
190                  IN_BUSINESS_METHOD | IN_EJB_TIMEOUT);
191
192          //
193
// jason: I think this is lame... but the spec says this is how it is.
194
// I think it would be better to silently ignore... or not so silently
195
// but still continue.
196
//
197

198          if (!isTxRequired()) {
199             throw new IllegalStateException JavaDoc
200                ("getRollbackOnly must only be called in the context of a transaction (EJB 2.0 - 15.5.1)");
201          }
202
203          return super.getRollbackOnly();
204       }
205
206       /**
207        * If transaction type is not Container or there is no transaction
208        * then throw an exception.
209        *
210        * @throws IllegalStateException If transaction type is not Container,
211        * or no transaction.
212        */

213       public void setRollbackOnly()
214       {
215          if (isUserManagedTx())
216             throw new IllegalStateException JavaDoc("setRollbackOnly should not be access for user managed Tx");
217
218          AllowedOperationsAssociation.assertAllowedIn("getRollbackOnly",
219                  IN_BUSINESS_METHOD | IN_EJB_TIMEOUT);
220
221          if (!isTxRequired()) {
222             throw new IllegalStateException JavaDoc
223                ("setRollbackOnly must only be called in the context of a transaction (EJB 2.0 - 15.5.1)");
224          }
225
226          super.setRollbackOnly();
227       }
228
229       /** Helper to check if the tx type is TX_REQUIRED. */
230       private boolean isTxRequired()
231       {
232          MessageDrivenMetaData md = (MessageDrivenMetaData)con.getBeanMetaData();
233          return md.getMethodTransactionType() == MetaData.TX_REQUIRED;
234       }
235    }
236
237    /**
238     * Delegates to the underlying TimerService, after checking access
239     */

240    public class TimerServiceWrapper implements TimerService JavaDoc
241    {
242
243       private EnterpriseContext.EJBContextImpl context;
244       private TimerService JavaDoc timerService;
245
246       public TimerServiceWrapper(EnterpriseContext.EJBContextImpl ctx, TimerService JavaDoc timerService)
247       {
248          this.context = ctx;
249          this.timerService = timerService;
250       }
251
252       public Timer JavaDoc createTimer(long duration, Serializable JavaDoc info) throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, EJBException JavaDoc
253       {
254          assertAllowedIn("TimerService.createTimer");
255          return timerService.createTimer(duration, info);
256       }
257
258       public Timer JavaDoc createTimer(long initialDuration, long intervalDuration, Serializable JavaDoc info) throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, EJBException JavaDoc
259       {
260          assertAllowedIn("TimerService.createTimer");
261          return timerService.createTimer(initialDuration, intervalDuration, info);
262       }
263
264       public Timer JavaDoc createTimer(Date JavaDoc expiration, Serializable JavaDoc info) throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, EJBException JavaDoc
265       {
266          assertAllowedIn("TimerService.createTimer");
267          return timerService.createTimer(expiration, info);
268       }
269
270       public Timer JavaDoc createTimer(Date JavaDoc initialExpiration, long intervalDuration, Serializable JavaDoc info) throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, EJBException JavaDoc
271       {
272          assertAllowedIn("TimerService.createTimer");
273          return timerService.createTimer(initialExpiration, intervalDuration, info);
274       }
275
276       public Collection JavaDoc getTimers() throws IllegalStateException JavaDoc, EJBException JavaDoc
277       {
278          assertAllowedIn("TimerService.getTimers");
279          return timerService.getTimers();
280       }
281
282       private void assertAllowedIn(String JavaDoc timerMethod)
283       {
284          AllowedOperationsAssociation.assertAllowedIn(timerMethod,
285                  IN_BUSINESS_METHOD | IN_EJB_TIMEOUT);
286       }
287    }
288 }
Popular Tags