001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins;
023:
024: import javax.ejb.EJBException;
025:
026: import org.jboss.ejb.Interceptor;
027: import org.jboss.invocation.Invocation;
028: import org.jboss.invocation.InvocationKey;
029: import org.jboss.util.naming.ENCThreadLocalKey;
030:
031: /**
032: * This interceptor injects the ProxyFactory into the ThreadLocal container variable
033: *
034: * @author <a href="mailto:rickard.oberg@telkel.com">Rickard �berg</a>
035: * @author <a href="mailto:Scott.Stark@jboss.org">Scott Stark</a>
036: * @version $Revision: 61255 $
037: */
038: public class ProxyFactoryFinderInterceptor extends AbstractInterceptor {
039:
040: public void create() throws Exception {
041: }
042:
043: protected void setProxyFactory(String invokerBinding, Invocation mi)
044: throws Exception {
045: // if (BeanMetaData.LOCAL_INVOKER_PROXY_BINDING.equals(invokerBinding)) return;
046: if (invokerBinding == null) {
047: log.trace("invokerBInding is null in ProxyFactoryFinder");
048: return;
049: }
050: /*
051: if (invokerBinding == null)
052: {
053: log.error("***************** invokerBinding is null ********");
054: log.error("Method name: " + mi.getMethod().getName());
055: log.error("jmx name: " + container.getJmxName().toString());
056: new Throwable().printStackTrace();
057: log.error("*************************");
058: throw new EJBException("Couldn't insert proxy factory, " +
059: "invokerBinding was null");
060: }
061: */
062: Object proxyFactory = container
063: .lookupProxyFactory(invokerBinding);
064: if (proxyFactory == null) {
065: String methodName;
066: if (mi.getMethod() != null) {
067: methodName = mi.getMethod().getName();
068: } else {
069: methodName = "<no method>";
070: }
071:
072: log
073: .error("***************** proxyFactory is null ********");
074: log.error("Method name: " + methodName);
075: log.error("jmx name: " + container.getJmxName().toString());
076: log.error("invokerBinding: " + invokerBinding);
077: log.error("Stack trace", new Throwable());
078: log.error("*************************");
079: throw new EJBException("Couldn't find proxy factory");
080: }
081: container.setProxyFactory(proxyFactory);
082: }
083:
084: public Object invokeHome(Invocation mi) throws Exception {
085: String invokerBinding = (String) mi
086: .getAsIsValue(InvocationKey.INVOKER_PROXY_BINDING);
087: setProxyFactory(invokerBinding, mi);
088:
089: String oldInvokerBinding = ENCThreadLocalKey.getKey();
090: // Only override current ENC binding if we're not local
091: // if ((!BeanMetaData.LOCAL_INVOKER_PROXY_BINDING.equals(invokerBinding)) || oldInvokerBinding == null)
092: if (invokerBinding != null || oldInvokerBinding == null) {
093: ENCThreadLocalKey.setKey(invokerBinding);
094: }
095:
096: Interceptor next = getNext();
097: Object value = null;
098: try {
099: value = next.invokeHome(mi);
100: } finally {
101: ENCThreadLocalKey.setKey(oldInvokerBinding);
102: // TODO: we should probably clear the thread local, i.e.
103: //container.setProxyFactory(null);
104: }
105:
106: return value;
107: }
108:
109: public Object invoke(Invocation mi) throws Exception {
110: String invokerBinding = (String) mi
111: .getAsIsValue(InvocationKey.INVOKER_PROXY_BINDING);
112: setProxyFactory(invokerBinding, mi);
113:
114: String oldInvokerBinding = ENCThreadLocalKey.getKey();
115: // Only override current ENC binding if we're not local or there has not been a previous call
116: // if ((!BeanMetaData.LOCAL_INVOKER_PROXY_BINDING.equals(invokerBinding)) || oldInvokerBinding == null)
117: if (invokerBinding != null || oldInvokerBinding == null) {
118: ENCThreadLocalKey.setKey(invokerBinding);
119: }
120:
121: Interceptor next = getNext();
122: Object value = null;
123: try {
124: value = next.invoke(mi);
125: } finally {
126: ENCThreadLocalKey.setKey(oldInvokerBinding);
127: // TODO: we should probably clear the thread local, i.e.
128: //container.setProxyFactory(null);
129: }
130:
131: return value;
132: }
133:
134: }
|