KickJava   Java API By Example, From Geeks To Geeks.

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


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.ObjectStreamException JavaDoc;
25 import java.io.Serializable JavaDoc;
26 import java.lang.reflect.Constructor JavaDoc;
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28
29 import javassist.util.proxy.MethodHandler;
30 import javassist.util.proxy.ProxyFactory;
31 import javassist.util.proxy.ProxyObject;
32
33 /**
34  * Comment
35  *
36  * @author <a HREF="mailto:carlo@nerdnet.nl">Carlo de Wolf</a>
37  * @version $Revision: 46054 $
38  */

39 public class JavassistProxy implements Serializable JavaDoc
40 {
41    private static final long serialVersionUID = 1L;
42    
43    private MethodHandler handler;
44    private Class JavaDoc interfaces[];
45    
46 // /**
47
// * Public because of classloading?
48
// */
49
// public static class JavassistProxyReplacement extends JavassistProxy
50
// {
51
// public void setHandler(MethodHandler handler)
52
// {
53
// throw new RuntimeException("should never happen");
54
// }
55
// }
56
//
57
/**
58     * With this method you can directly poke interfaces into the
59     * proxy without setting of the method handler.
60     *
61     * @param proxy
62     * @param interfaces
63     */

64    protected static void pokeInterfaces(JavassistProxy proxy, Class JavaDoc interfaces[]) {
65       proxy.interfaces = interfaces;
66    }
67    
68    private Object JavaDoc readResolve() throws ObjectStreamException JavaDoc
69    {
70       System.err.println("JavassistProxy.readResolve");
71       try {
72          ProxyFactory proxyFactory = new ProxyFactory() {
73             protected ClassLoader JavaDoc getClassLoader()
74             {
75                return Thread.currentThread().getContextClassLoader();
76             }
77          };
78          proxyFactory.setInterfaces(interfaces);
79          proxyFactory.setSuperclass(JavassistProxy.class);
80          Class JavaDoc proxyClass = proxyFactory.createClass();
81          Constructor JavaDoc proxyConstructor = proxyClass.getConstructor((Class JavaDoc[]) null);
82          JavassistProxy proxy = (JavassistProxy) proxyConstructor.newInstance((Object JavaDoc[]) null);
83          proxy.setMethodHandler(this.handler);
84          proxy.interfaces = this.interfaces;
85          return proxy;
86       }
87       catch (NoSuchMethodException JavaDoc e)
88       {
89          throw new RuntimeException JavaDoc(e);
90       }
91       catch (InstantiationException JavaDoc e)
92       {
93          throw new RuntimeException JavaDoc(e);
94       }
95       catch (IllegalAccessException JavaDoc e)
96       {
97          throw new RuntimeException JavaDoc(e);
98       }
99       catch (InvocationTargetException JavaDoc e)
100       {
101          throw new RuntimeException JavaDoc(e.getTargetException());
102       }
103    }
104    
105    protected void setInterfaces(Class JavaDoc interfaces[])
106    {
107       // TODO: check whether I get true interfaces
108
this.interfaces = interfaces;
109    }
110    
111    protected void setMethodHandler(MethodHandler handler)
112    {
113       // LOL
114
((ProxyObject) this).setHandler(handler);
115       this.handler = handler;
116    }
117    
118    private Object JavaDoc writeReplace() throws ObjectStreamException JavaDoc
119    {
120       System.err.println("JavassistProxy.writeReplace");
121       JavassistProxy replacement = new JavassistProxyReplacement();
122       replacement.handler = this.handler;
123       replacement.interfaces = this.interfaces;
124       return replacement;
125    }
126 }
127
Popular Tags