KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > MessageDrivenInstanceInterceptor


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.plugins;
23
24 import org.jboss.ejb.EnterpriseContext;
25 import org.jboss.ejb.InstancePool;
26 import org.jboss.ejb.MessageDrivenContainer;
27 import org.jboss.ejb.AllowedOperationsAssociation;
28 import org.jboss.invocation.Invocation;
29
30 import javax.ejb.EJBException JavaDoc;
31 import javax.ejb.TimedObject JavaDoc;
32 import javax.ejb.Timer JavaDoc;
33 import java.rmi.RemoteException JavaDoc;
34 import java.lang.reflect.Method JavaDoc;
35
36 /**
37  * This container acquires the given instance. This must be used after
38  * the EnvironmentInterceptor, since acquiring instances requires a proper
39  * JNDI environment to be set.
40  *
41  * @author <a HREF="mailto:peter.antman@tim.se">Peter Antman</a>.
42  * @author <a HREF="mailto:rickard.oberg@telkel.com">Rickard Öberg</a>
43  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
44  * @version $Revision: 37459 $
45  */

46 public class MessageDrivenInstanceInterceptor
47       extends AbstractInterceptor
48 {
49
50    /** A reference to {@link javax.ejb.TimedObject#ejbTimeout}. */
51    protected static final Method JavaDoc ejbTimeout;
52    static
53    {
54       try
55       {
56          ejbTimeout = TimedObject JavaDoc.class.getMethod("ejbTimeout", new Class JavaDoc[]{Timer JavaDoc.class});
57       }
58       catch (Exception JavaDoc e)
59       {
60          throw new ExceptionInInitializerError JavaDoc(e);
61       }
62    }
63
64    /**
65     * Message driven beans do not have homes.
66     *
67     * @throws Error Not valid for MessageDriven beans.
68     */

69    public Object JavaDoc invokeHome(final Invocation mi)
70          throws Exception JavaDoc
71    {
72       throw new Error JavaDoc("Not valid for MessageDriven beans");
73    }
74
75    // Interceptor implementation --------------------------------------
76

77    public Object JavaDoc invoke(final Invocation mi)
78          throws Exception JavaDoc
79    {
80       // Get context
81
MessageDrivenContainer mdc = (MessageDrivenContainer) container;
82       InstancePool pool = mdc.getInstancePool();
83       EnterpriseContext ctx = null;
84       try
85       {
86          ctx = pool.get();
87       }
88       catch (EJBException JavaDoc e)
89       {
90          throw e;
91       }
92       catch (Exception JavaDoc e)
93       {
94          throw new EJBException JavaDoc("Unable to get an instance from the pool", e);
95       }
96
97       // Set the current security information
98
ctx.setPrincipal(mi.getPrincipal());
99
100       // Use this context
101
mi.setEnterpriseContext(ctx);
102       // Set the JACC EnterpriseBean PolicyContextHandler data
103
EnterpriseBeanPolicyContextHandler.setEnterpriseBean(ctx.getInstance());
104
105       if (ejbTimeout.equals(mi.getMethod()))
106          AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_TIMEOUT);
107       else
108          AllowedOperationsAssociation.pushInMethodFlag(IN_BUSINESS_METHOD);
109
110       // There is no need for synchronization since the instance is always
111
// fresh also there should never be a tx associated with the instance.
112
try
113       {
114          // Invoke through interceptors
115
Object JavaDoc obj = getNext().invoke(mi);
116          return obj;
117       }
118       catch (RuntimeException JavaDoc e) // Instance will be GC'ed at MI return
119
{
120          mi.setEnterpriseContext(null);
121          throw e;
122       }
123       catch (RemoteException JavaDoc e) // Instance will be GC'ed at MI return
124
{
125          mi.setEnterpriseContext(null);
126          throw e;
127       }
128       catch (Error JavaDoc e) // Instance will be GC'ed at MI return
129
{
130          mi.setEnterpriseContext(null);
131          throw e;
132       }
133       finally
134       {
135          AllowedOperationsAssociation.popInMethodFlag();
136
137          // Return context
138
if (mi.getEnterpriseContext() != null)
139          {
140             pool.free((EnterpriseContext) mi.getEnterpriseContext());
141          }
142          else
143          {
144             pool.discard(ctx);
145          }
146       }
147    }
148
149 }
150
151
Popular Tags