KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > client > proxy > Jdk13ProxyFactory


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: Jdk13ProxyFactory.java 1921 2005-06-19 22:40:34Z jlaskowski $
44  */

45 package org.openejb.client.proxy;
46
47 import java.lang.reflect.Constructor JavaDoc;
48 import java.lang.reflect.InvocationTargetException JavaDoc;
49 import java.lang.reflect.Proxy JavaDoc;
50 import java.util.Properties JavaDoc;
51
52 /**
53  * Implementation of ProxyFactory for JDK 1.3 Proxies. This only
54  * compiles on JDK 1.3 or better. It is very fast because it builds
55  * the proxies out of raw bytecode.
56  *
57  * @author Aaron Mulder (ammulder@alumni.princeton.edu)
58  */

59 public class Jdk13ProxyFactory implements ProxyFactory {
60
61     /**
62      * Paramaters for the contstructor of the proxy class.
63      */

64     private final Class JavaDoc[] constructorParams = { java.lang.reflect.InvocationHandler JavaDoc.class};
65
66     /**
67      * Prepare the factory for use. Called once when the ProxyFactory
68      * is instaniated.
69      *
70      * @param props
71      */

72     public void init(Properties JavaDoc props) {
73         String JavaDoc version = "";
74         String JavaDoc badVersion = "1.3.0-";
75         try {
76             version = System.getProperty("java.vm.version");
77         } catch ( Exception JavaDoc e ) {
78         }
79         
80         if ( version.indexOf(badVersion) != -1 ) {
81             String JavaDoc message = ""+
82                              "INCOMPATIBLE VM: \n\n"+
83                              "The Java Virtual Machine you are using contains a bug\n"+
84                              "in the proxy generation logic. This bug has been \n"+
85                              "documented by Sun and has been fixed in later VMs. \n"+
86                              "Please download the latest 1.3 Virtual Machine. \n"+
87                              "For more details see: \n"+
88                              "http://developer.java.sun.com/developer/bugParade/bugs/4346224.html\n ";
89             throw new RuntimeException JavaDoc(message);
90         }
91     }
92
93     /**
94      * Returns the invocation handler for the specified proxy instance.
95      */

96     public InvocationHandler getInvocationHandler(Object JavaDoc proxy) throws IllegalArgumentException JavaDoc {
97
98         Jdk13InvocationHandler handler = (Jdk13InvocationHandler)Proxy.getInvocationHandler(proxy);
99
100         if ( handler == null ) return null;
101         
102         return handler.getInvocationHandler();
103     }
104
105     /**
106      * Sets the invocation handler for the specified proxy instance.
107      */

108     public Object JavaDoc setInvocationHandler(Object JavaDoc proxy, InvocationHandler handler) throws IllegalArgumentException JavaDoc {
109         
110         Jdk13InvocationHandler jdk13 = (Jdk13InvocationHandler)Proxy.getInvocationHandler(proxy);
111
112         if ( jdk13 == null ) throw new IllegalArgumentException JavaDoc("Proxy "+proxy+" unknown!");
113
114         return jdk13.setInvocationHandler(handler);
115     }
116
117     /**
118      * Returns the java.lang.Class object for a proxy class given a class loader
119      * and an array of interfaces.
120      *
121      * @param interfce
122      * @return Class
123      * @exception IllegalArgumentException
124      */

125     public Class JavaDoc getProxyClass(Class JavaDoc interfce) throws IllegalArgumentException JavaDoc {
126         return Proxy.getProxyClass(interfce.getClassLoader(), new Class JavaDoc[]{interfce});
127     }
128
129     /**
130      * Returns the java.lang.Class object for a proxy class given a class loader
131      * and an array of interfaces.
132      *
133      * @param interfaces
134      * @return Class
135      * @exception IllegalArgumentException
136      */

137     public Class JavaDoc getProxyClass(Class JavaDoc[] interfaces) throws IllegalArgumentException JavaDoc {
138         if ( interfaces.length < 1 ) {
139             throw new IllegalArgumentException JavaDoc("There must be at least one interface to implement.");
140         }
141
142         return Proxy.getProxyClass(interfaces[0].getClassLoader(), interfaces);
143     }
144
145     /**
146      * Returns true if and only if the specified class was dynamically generated
147      * to be a proxy class using the getProxyClass method or the newProxyInstance
148      * method.
149      *
150      * @param cl
151      * @return boolean
152      */

153     public boolean isProxyClass(Class JavaDoc cl) {
154         return Proxy.isProxyClass(cl);
155     }
156
157
158     /**
159      * Creates a new proxy instance using the handler of the proxy passed in.
160      *
161      * @param proxyClass
162      * @return Object
163      * @exception IllegalArgumentException
164      */

165     public Object JavaDoc newProxyInstance(Class JavaDoc proxyClass) throws IllegalArgumentException JavaDoc {
166         if ( !Proxy.isProxyClass(proxyClass) ) {
167             throw new IllegalArgumentException JavaDoc("This class is not a proxy.");
168         }
169
170         try {
171
172             Constructor JavaDoc cons = proxyClass.getConstructor(constructorParams);
173             return cons.newInstance(new Object JavaDoc[] { new Jdk13InvocationHandler()});
174
175         } catch ( NoSuchMethodException JavaDoc e ) {
176             throw new InternalError JavaDoc(e.toString());
177         } catch ( IllegalAccessException JavaDoc e ) {
178             throw new InternalError JavaDoc(e.toString());
179         } catch ( InstantiationException JavaDoc e ) {
180             throw new InternalError JavaDoc(e.toString());
181         } catch ( InvocationTargetException JavaDoc e ) {
182             throw new InternalError JavaDoc(e.toString());
183         }
184     }
185
186     /**
187      * Returns an instance of a proxy class for the specified interface that
188      * dispatches method invocations to the specified invocation handler.
189      *
190      * @param interfce
191      * @param h
192      * @return Object
193      * @exception IllegalArgumentException
194      */

195     public Object JavaDoc newProxyInstance(Class JavaDoc interfce, InvocationHandler h) throws IllegalArgumentException JavaDoc {
196
197         Jdk13InvocationHandler handler = new Jdk13InvocationHandler(h);
198
199         return Proxy.newProxyInstance(interfce.getClassLoader(), new Class JavaDoc[]{interfce}, handler);
200     }
201
202     /**
203      * Returns an instance of a proxy class for the specified interface that
204      * dispatches method invocations to the specified invocation handler.
205      *
206      * @param interfaces
207      * @param h
208      * @return Object
209      * @exception IllegalArgumentException
210      */

211     public Object JavaDoc newProxyInstance(Class JavaDoc[] interfaces, InvocationHandler h) throws IllegalArgumentException JavaDoc {
212         if ( interfaces.length < 1 ) {
213             throw new IllegalArgumentException JavaDoc("There must be at least one interface to implement.");
214         }
215
216         Jdk13InvocationHandler handler = new Jdk13InvocationHandler(h);
217
218         return Proxy.newProxyInstance(interfaces[0].getClassLoader(), interfaces, handler);
219     }
220 }
221
Popular Tags