KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.rmi.RemoteException JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26
27 import org.jboss.ejb.Container;
28 import org.jboss.invocation.Invocation;
29 import org.jboss.invocation.InvocationKey;
30 import org.jboss.invocation.InvocationType;
31 import org.jboss.ejb.EnterpriseContext;
32 import org.jboss.ejb.StatelessSessionContainer;
33 import org.jboss.ejb.InstancePool;
34 import org.jboss.ejb.AllowedOperationsAssociation;
35 import org.jboss.ejb.StatelessSessionEnterpriseContext;
36
37 import javax.ejb.EJBException JavaDoc;
38 import javax.ejb.TimedObject JavaDoc;
39 import javax.ejb.Timer JavaDoc;
40
41 /**
42  * This container acquires the given instance. This must be used after
43  * the EnvironmentInterceptor, since acquiring instances requires a proper
44  * JNDI environment to be set
45  *
46  * @author Rickard Oberg
47  * @author Scott.Stark@jboss.org
48  * @version $Revision: 54975 $
49  */

50 public class StatelessSessionInstanceInterceptor
51    extends AbstractInterceptor
52 {
53    // Constants -----------------------------------------------------
54

55    // Attributes ----------------------------------------------------
56

57    protected StatelessSessionContainer container;
58
59    // Static --------------------------------------------------------
60

61    /** A reference to {@link javax.ejb.TimedObject#ejbTimeout}. */
62    protected static final Method JavaDoc ejbTimeout;
63    static
64    {
65       try
66       {
67          ejbTimeout = TimedObject JavaDoc.class.getMethod("ejbTimeout", new Class JavaDoc[]{Timer JavaDoc.class});
68       }
69       catch (Exception JavaDoc e)
70       {
71          throw new ExceptionInInitializerError JavaDoc(e);
72       }
73    }
74
75    // Constructors --------------------------------------------------
76

77    // Public --------------------------------------------------------
78

79    public void setContainer(final Container container)
80    {
81       super.setContainer(container);
82       this.container = (StatelessSessionContainer)container;
83    }
84
85    // Interceptor implementation --------------------------------------
86

87    public Object JavaDoc invokeHome(final Invocation mi) throws Exception JavaDoc
88    {
89       InstancePool pool = container.getInstancePool();
90       StatelessSessionEnterpriseContext ctx = null;
91       try
92       {
93          // Acquire an instance in case the ejbCreate throws a CreateException
94
ctx = (StatelessSessionEnterpriseContext) pool.get();
95          mi.setEnterpriseContext(ctx);
96          // Dispatch the method to the container
97
return getNext().invokeHome(mi);
98       }
99       finally
100       {
101          mi.setEnterpriseContext(null);
102          // If an instance was created, return it to the pool
103
if( ctx != null )
104             pool.free(ctx);
105       }
106
107    }
108
109    public Object JavaDoc invoke(final Invocation mi) throws Exception JavaDoc
110    {
111       // Get context
112
InstancePool pool = container.getInstancePool();
113       StatelessSessionEnterpriseContext ctx = null;
114       try
115       {
116          ctx = (StatelessSessionEnterpriseContext) pool.get();
117       }
118       catch (EJBException JavaDoc e)
119       {
120          throw e;
121       }
122       catch (RemoteException JavaDoc e)
123       {
124          throw e;
125       }
126       catch (Exception JavaDoc e)
127       {
128          InvocationType type = mi.getType();
129          boolean isLocal = (type == InvocationType.LOCAL || type == InvocationType.LOCALHOME);
130          if (isLocal)
131             throw new EJBException JavaDoc("Unable to get an instance from the pool", e);
132          else
133             throw new RemoteException JavaDoc("Unable to get an intance from the pool", e);
134       }
135
136       // Set the current security information
137
ctx.setPrincipal(mi.getPrincipal());
138       // Set the JACC EnterpriseBean PolicyContextHandler data
139
EnterpriseBeanPolicyContextHandler.setEnterpriseBean(ctx.getInstance());
140
141       // Use this context
142
mi.setEnterpriseContext(ctx);
143
144       // JAXRPC/JAXWS message context
145
Object JavaDoc msgContext = mi.getValue(InvocationKey.SOAP_MESSAGE_CONTEXT);
146
147       // Timer invocation
148
if (ejbTimeout.equals(mi.getMethod()))
149       {
150          AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_TIMEOUT);
151       }
152
153       // Service Endpoint invocation
154
else if (msgContext != null)
155       {
156          if (msgContext instanceof javax.xml.rpc.handler.MessageContext JavaDoc)
157             ctx.setMessageContext((javax.xml.rpc.handler.MessageContext JavaDoc)msgContext);
158
159          AllowedOperationsAssociation.pushInMethodFlag(IN_SERVICE_ENDPOINT_METHOD);
160       }
161
162       // Business Method Invocation
163
else
164       {
165          AllowedOperationsAssociation.pushInMethodFlag(IN_BUSINESS_METHOD);
166       }
167
168       // There is no need for synchronization since the instance is always fresh also there should
169
// never be a tx associated with the instance.
170
try
171       {
172          Object JavaDoc obj = getNext().invoke(mi);
173          return obj;
174
175       }
176       catch (RuntimeException JavaDoc e) // Instance will be GC'ed at MI return
177
{
178          mi.setEnterpriseContext(null);
179          throw e;
180       }
181       catch (RemoteException JavaDoc e) // Instance will be GC'ed at MI return
182
{
183          mi.setEnterpriseContext(null);
184          throw e;
185       }
186       catch (Error JavaDoc e) // Instance will be GC'ed at MI return
187
{
188          mi.setEnterpriseContext(null);
189          throw e;
190       }
191       finally
192       {
193          AllowedOperationsAssociation.popInMethodFlag();
194
195          // Return context
196
if (mi.getEnterpriseContext() != null)
197          {
198             pool.free(((EnterpriseContext) mi.getEnterpriseContext()));
199          }
200          else
201          {
202             pool.discard(ctx);
203          }
204       }
205    }
206 }
207
Popular Tags