KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > stateless > BaseStatelessProxyFactory


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.ejb3.stateless;
23
24 import java.io.Serializable JavaDoc;
25 import java.lang.reflect.Constructor JavaDoc;
26 import java.lang.reflect.InvocationHandler JavaDoc;
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import java.lang.reflect.Proxy JavaDoc;
30
31 import javassist.util.proxy.MethodHandler;
32 import javassist.util.proxy.ProxyObject;
33
34 import javax.naming.Context JavaDoc;
35 import javax.naming.NamingException JavaDoc;
36
37 import org.jboss.aop.Advisor;
38 import org.jboss.ejb3.Container;
39 import org.jboss.ejb3.ProxyFactory;
40 import org.jboss.logging.Logger;
41 import org.jboss.naming.Util;
42
43 /**
44  * Comment
45  *
46  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
47  * @version $Revision: 56518 $
48  */

49 public abstract class BaseStatelessProxyFactory extends org.jboss.ejb3.session.BaseSessionProxyFactory implements ProxyFactory
50 {
51    private static final Logger log = Logger.getLogger(BaseStatelessProxyFactory.class);
52
53 // protected Class proxyClass;
54
// protected Constructor proxyConstructor;
55
protected Context JavaDoc proxyFactoryContext;
56    protected String JavaDoc jndiName;
57    
58    private javassist.util.proxy.ProxyFactory proxyFactory;
59    private Class JavaDoc proxyClass;
60    private Constructor JavaDoc proxyConstructor;
61    
62    /**
63     * Adapt the JDK to cglib.
64     *
65     * This is a named class because it implements both InvocationHandler and Serializable.
66     */

67    /* TODO: fix EJBTHREE-485 without cglib
68    private static class CGLibInvocationHandlerAdapter implements net.sf.cglib.proxy.InvocationHandler, Serializable
69    {
70       private static final long serialVersionUID = 1L;
71
72       private InvocationHandler delegate;
73       
74       private CGLibInvocationHandlerAdapter(InvocationHandler delegate)
75       {
76          if(delegate == null)
77             throw new IllegalArgumentException("delegate must not be null");
78          this.delegate = delegate;
79       }
80       
81       public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
82       {
83          return delegate.invoke(proxy, method, args);
84       }
85       
86    }
87    */

88    
89    /**
90     * Adapt the InvocationHandler to MethodHandler.
91     *
92     * This is a named class because it implements both MethodHandler and Serializable.
93     */

94    private static class MethodHandlerAdapter implements MethodHandler, Serializable JavaDoc
95    {
96       private static final long serialVersionUID = 1L;
97       
98       private InvocationHandler JavaDoc delegate;
99       
100       private MethodHandlerAdapter(InvocationHandler JavaDoc delegate)
101       {
102          if(delegate == null)
103             throw new IllegalArgumentException JavaDoc("delegate must not be null");
104          this.delegate = delegate;
105       }
106       
107       public Object JavaDoc invoke(Object JavaDoc self, Method JavaDoc thisMethod, Method JavaDoc process, Object JavaDoc[] args) throws Throwable JavaDoc
108       {
109          return delegate.invoke(self, thisMethod, args);
110       }
111    }
112    
113    /**
114     * Hide the fact that I'm now using javassist.
115     *
116     * @param handler a JDK proxy InvocationHandler
117     * @return a true proxy
118     */

119    protected Object JavaDoc constructProxy(final InvocationHandler JavaDoc handler)
120    {
121       try
122       {
123          /* plain jdk */
124          Object JavaDoc args[] = { handler };
125          Object JavaDoc proxy = proxyConstructor.newInstance(args);
126          
127          /* javassist */
128          /*
129          MethodHandler realHandler = new MethodHandlerAdapter(handler);
130 // ProxyObject proxy = (ProxyObject) proxyConstructor.newInstance((Object[]) null);
131 // proxy.setHandler(realHandler);
132          JavassistProxy proxy = (JavassistProxy) proxyConstructor.newInstance((Object[]) null);
133          proxy.setMethodHandler(realHandler);
134          JavassistProxy.pokeInterfaces(proxy, getInterfaces());
135          */

136          
137          /* cglib */
138          /*
139          Object args[] = { new CGLibInvocationHandlerAdapter(handler) };
140          Object proxy = proxyConstructor.newInstance(args);
141          */

142          
143          return proxy;
144       }
145       catch (IllegalArgumentException JavaDoc e)
146       {
147          throw new RuntimeException JavaDoc(e);
148       }
149       catch (InstantiationException JavaDoc e)
150       {
151          throw new RuntimeException JavaDoc(e);
152       }
153       catch (IllegalAccessException JavaDoc e)
154       {
155          throw new RuntimeException JavaDoc(e);
156       }
157       catch (InvocationTargetException JavaDoc e)
158       {
159          throw new RuntimeException JavaDoc(e.getTargetException());
160       }
161    }
162    
163    public final Object JavaDoc createProxy(Object JavaDoc id)
164    {
165       assert id == null : "stateless bean must not have an id";
166       return createProxy();
167    }
168    
169    public void init() throws Exception JavaDoc
170    {
171       initializeJndiName();
172       Class JavaDoc[] interfaces = getInterfaces();
173       
174       /* plain jdk */
175       Class JavaDoc proxyClass = java.lang.reflect.Proxy.getProxyClass(container.getBeanClass().getClassLoader(), interfaces);
176       final Class JavaDoc[] constructorParams =
177               {InvocationHandler JavaDoc.class};
178       proxyConstructor = proxyClass.getConstructor(constructorParams);
179       
180       /* javassist */
181       /*
182       proxyFactory = new javassist.util.proxy.ProxyFactory()
183       {
184          @Override
185          protected ClassLoader getClassLoader()
186          {
187             return container.getBeanClass().getClassLoader();
188          }
189       };
190       proxyFactory.setInterfaces(interfaces);
191       proxyFactory.setSuperclass(JavassistProxy.class);
192       proxyClass = proxyFactory.createClass();
193       proxyConstructor = proxyClass.getConstructor((Class[]) null);
194       */

195       
196       /* cglib */
197       /*
198       proxyClass = net.sf.cglib.proxy.Proxy.getProxyClass(container.getBeanClass().getClassLoader(), interfaces);
199       final Class[] constructorParams = {net.sf.cglib.proxy.InvocationHandler.class};
200       proxyConstructor = proxyClass.getConstructor(constructorParams);
201       */

202    }
203
204    /* for debugging purposes * /
205    private static void describeClass(Class cls) {
206       System.err.println("class " + cls + " has the following:");
207       for(Class i : cls.getInterfaces()) {
208          System.err.println(" interface: " + i);
209       }
210       for(Method m : cls.getDeclaredMethods()) {
211          System.err.println(" method: " + m);
212       }
213       System.err.println(" classloader = " + cls.getClassLoader());
214       if(cls.getSuperclass() != null)
215          describeClass(cls.getSuperclass());
216    }
217    */

218    
219    public void start() throws Exception JavaDoc
220    {
221       init();
222
223       Object JavaDoc proxy = createProxy();
224       //describeClass(proxy.getClass());
225
try
226       {
227          Util.rebind(container.getInitialContext(), jndiName, proxy);
228       } catch (NamingException JavaDoc e)
229       {
230          NamingException JavaDoc namingException = new NamingException JavaDoc("Could not bind stateless proxy with ejb name " + container.getEjbName() + " into JNDI under jndiName: " + container.getInitialContext().getNameInNamespace() + "/" + jndiName);
231          namingException.setRootCause(e);
232          throw namingException;
233       }
234    }
235
236    public void stop() throws Exception JavaDoc
237    {
238       Util.unbind(container.getInitialContext(), jndiName);
239    }
240
241    protected abstract Class JavaDoc[] getInterfaces();
242
243    protected abstract void initializeJndiName();
244
245    public void setContainer(Container container)
246    {
247       this.container = container;
248       this.advisor = (Advisor) container;
249    }
250
251 }
252
Popular Tags