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;
31 import javax.ejb.TimedObject;
32 import javax.ejb.Timer;
33 import java.rmi.RemoteException;
34 import java.lang.reflect.Method;
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 Oberg</a>
43 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
44 * @version $Revision: 66439 $
45 */
46 public class MessageDrivenInstanceInterceptor
47 extends AbstractInterceptor
48 {
49
50 /** A reference to {@link javax.ejb.TimedObject#ejbTimeout}. */
51 protected static final Method ejbTimeout;
52 static
53 {
54 try
55 {
56 ejbTimeout = TimedObject.class.getMethod("ejbTimeout", new Class[]{Timer.class});
57 }
58 catch (Exception e)
59 {
60 throw new ExceptionInInitializerError(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 invokeHome(final Invocation mi)
70 throws Exception
71 {
72 throw new Error("Not valid for MessageDriven beans");
73 }
74
75 // Interceptor implementation --------------------------------------
76
77 public Object invoke(final Invocation mi)
78 throws Exception
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 e)
89 {
90 throw e;
91 }
92 catch (Exception e)
93 {
94 throw new EJBException("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 obj = getNext().invoke(mi);
116 return obj;
117 }
118 catch (RuntimeException e) // Instance will be GC'ed at MI return
119 {
120 mi.setEnterpriseContext(null);
121 throw e;
122 }
123 catch (RemoteException e) // Instance will be GC'ed at MI return
124 {
125 mi.setEnterpriseContext(null);
126 throw e;
127 }
128 catch (Error 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 EnterpriseBeanPolicyContextHandler.setEnterpriseBean(null);
137
138 // Return context
139 if (mi.getEnterpriseContext() != null)
140 {
141 pool.free((EnterpriseContext) mi.getEnterpriseContext());
142 }
143 else
144 {
145 pool.discard(ctx);
146 }
147 }
148 }
149
150 }
151