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 javax.ejb.EJBException;
25
26 import org.jboss.ejb.Interceptor;
27 import org.jboss.invocation.Invocation;
28 import org.jboss.invocation.InvocationKey;
29 import org.jboss.naming.ENCThreadLocalKey;
30
31 /**
32 * This interceptor injects the ProxyFactory into the ThreadLocal container variable
33 *
34 * @author <a href="mailto:rickard.oberg@telkel.com">Rickard Oberg</a>
35 * @author <a href="mailto:Scott.Stark@jboss.org">Scott Stark</a>
36 * @version $Revision: 66439 $
37 */
38 public class ProxyFactoryFinderInterceptor
39 extends AbstractInterceptor
40 {
41
42 public void create() throws Exception
43 {
44 }
45
46 protected void setProxyFactory(String invokerBinding, Invocation mi) throws Exception
47 {
48 // if (BeanMetaData.LOCAL_INVOKER_PROXY_BINDING.equals(invokerBinding)) return;
49 if (invokerBinding == null)
50 {
51 log.trace("invokerBInding is null in ProxyFactoryFinder");
52 return;
53 }
54 /*
55 if (invokerBinding == null)
56 {
57 log.error("***************** invokerBinding is null ********");
58 log.error("Method name: " + mi.getMethod().getName());
59 log.error("jmx name: " + container.getJmxName().toString());
60 new Throwable().printStackTrace();
61 log.error("*************************");
62 throw new EJBException("Couldn't insert proxy factory, " +
63 "invokerBinding was null");
64 }
65 */
66 Object proxyFactory = container.lookupProxyFactory(invokerBinding);
67 if (proxyFactory == null)
68 {
69 String methodName;
70 if(mi.getMethod() != null) {
71 methodName = mi.getMethod().getName();
72 } else
73 {
74 methodName ="<no method>";
75 }
76
77 log.error("***************** proxyFactory is null ********");
78 log.error("Method name: " + methodName);
79 log.error("jmx name: " + container.getJmxName().toString());
80 log.error("invokerBinding: " + invokerBinding);
81 log.error("Stack trace", new Throwable());
82 log.error("*************************");
83 throw new EJBException("Couldn't find proxy factory");
84 }
85 container.setProxyFactory(proxyFactory);
86 }
87
88 public Object invokeHome(Invocation mi)
89 throws Exception
90 {
91 String invokerBinding =
92 (String)mi.getAsIsValue(InvocationKey.INVOKER_PROXY_BINDING);
93 setProxyFactory(invokerBinding, mi);
94
95 String oldInvokerBinding = ENCThreadLocalKey.getKey();
96 // Only override current ENC binding if we're not local
97 // if ((!BeanMetaData.LOCAL_INVOKER_PROXY_BINDING.equals(invokerBinding)) || oldInvokerBinding == null)
98 if (invokerBinding != null || oldInvokerBinding == null)
99 {
100 ENCThreadLocalKey.setKey(invokerBinding);
101 }
102
103 Interceptor next = getNext();
104 Object value = null;
105 try
106 {
107 value = next.invokeHome(mi);
108 }
109 finally
110 {
111 ENCThreadLocalKey.setKey(oldInvokerBinding);
112 // TODO: we should probably clear the thread local, i.e.
113 //container.setProxyFactory(null);
114 }
115
116 return value;
117 }
118
119 public Object invoke(Invocation mi)
120 throws Exception
121 {
122 String invokerBinding =
123 (String)mi.getAsIsValue(InvocationKey.INVOKER_PROXY_BINDING);
124 setProxyFactory(invokerBinding, mi);
125
126 String oldInvokerBinding = ENCThreadLocalKey.getKey();
127 // Only override current ENC binding if we're not local or there has not been a previous call
128 // if ((!BeanMetaData.LOCAL_INVOKER_PROXY_BINDING.equals(invokerBinding)) || oldInvokerBinding == null)
129 if (invokerBinding != null || oldInvokerBinding == null)
130 {
131 ENCThreadLocalKey.setKey(invokerBinding);
132 }
133
134 Interceptor next = getNext();
135 Object value = null;
136 try
137 {
138 value = next.invoke(mi);
139 }
140 finally
141 {
142 ENCThreadLocalKey.setKey(oldInvokerBinding);
143 // TODO: we should probably clear the thread local, i.e.
144 //container.setProxyFactory(null);
145 }
146
147 return value;
148 }
149
150 }