KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > util > 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 "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES 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  * EXOFFICE TECHNOLOGIES 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 1999 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: Jdk13ProxyFactory.java 1096 2004-03-26 21:41:16Z dblevins $
44  */

45 package org.openejb.util.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 import org.openejb.OpenEJBException;
53
54 /**
55  * Implementation of ProxyFactory for JDK 1.3 Proxies. This only
56  * compiles on JDK 1.3 or better. It is very fast because it builds
57  * the proxies out of raw bytecode.
58  *
59  * @author Aaron Mulder (ammulder@alumni.princeton.edu)
60  * @version $Revision: 1096 $
61  */

62 public class Jdk13ProxyFactory implements ProxyFactory {
63     public void init(Properties JavaDoc props) throws OpenEJBException {
64         String JavaDoc version = "";
65         String JavaDoc badVersion = "1.3.0-";
66         try{
67             version = System.getProperty("java.vm.version");
68         } catch(Exception JavaDoc e){
69         }
70         if (version.indexOf(badVersion) != -1) {
71             String JavaDoc message = ""+
72                 "INCOMPATIBLE VM: \n\n"+
73                 "The Java Virtual Machine you are using contains a bug\n"+
74                 "in the proxy generation logic. This bug has been \n"+
75                 "documented by Sun and has been fixed in later VMs. \n"+
76                 "Please download the latest 1.3 Virtual Machine. \n"+
77                 "For more details see: \n"+
78                 "http://developer.java.sun.com/developer/bugParade/bugs/4346224.html\n ";
79             throw new OpenEJBException(message);
80         }
81     }
82
83     /**
84      * Returns the invocation handler for the specified proxy instance.
85      */

86     public org.openejb.util.proxy.InvocationHandler getInvocationHandler(Object JavaDoc proxy) throws IllegalArgumentException JavaDoc {
87         Jdk13InvocationHandler handler = (Jdk13InvocationHandler)Proxy.getInvocationHandler(proxy);
88         if(handler == null)
89             return null;
90         return handler.getInvocationHandler();
91     }
92
93     /**
94      * Sets the invocation handler for the specified proxy instance.
95      */

96     public Object JavaDoc setInvocationHandler(Object JavaDoc proxy, org.openejb.util.proxy.InvocationHandler handler) throws IllegalArgumentException JavaDoc {
97         Jdk13InvocationHandler jdk13 = (Jdk13InvocationHandler)Proxy.getInvocationHandler(proxy);
98         if(jdk13 == null)
99             throw new IllegalArgumentException JavaDoc("Proxy "+proxy+" unknown!");
100         return jdk13.setInvocationHandler(handler);
101     }
102
103     /**
104      * Returns the java.lang.Class object for a proxy class given a class loader and an array of interfaces.
105      */

106     public Class JavaDoc getProxyClass(Class JavaDoc interfce) throws IllegalArgumentException JavaDoc {
107         return Proxy.getProxyClass(interfce.getClassLoader(), new Class JavaDoc[]{interfce});
108     }
109     /**
110      * Returns the java.lang.Class object for a proxy class given a class loader and an array of interfaces.
111      */

112     public Class JavaDoc getProxyClass(Class JavaDoc[] interfaces) throws IllegalArgumentException JavaDoc {
113         if(interfaces.length < 1) {
114             throw new IllegalArgumentException JavaDoc("It's boring to implement 0 interfaces!");
115         }
116         return Proxy.getProxyClass(interfaces[0].getClassLoader(), interfaces);
117     }
118
119
120     /*
121      * Returns true if and only if the specified class was dynamically generated to be a proxy class using the getProxyClass method or the newProxyInstance method.
122      */

123     public boolean isProxyClass(Class JavaDoc cl) {
124         return Proxy.isProxyClass(cl);
125     }
126
127     private final static Class JavaDoc[] constructorParams = { java.lang.reflect.InvocationHandler JavaDoc.class };
128
129     public Object JavaDoc newProxyInstance(Class JavaDoc proxyClass) throws IllegalArgumentException JavaDoc {
130         if(!Proxy.isProxyClass(proxyClass))
131             throw new IllegalArgumentException JavaDoc();
132         try {
133             Constructor JavaDoc cons = proxyClass.getConstructor(constructorParams);
134             return (Object JavaDoc) cons.newInstance(new Object JavaDoc[] { new Jdk13InvocationHandler() });
135         } catch (NoSuchMethodException JavaDoc e) {
136             throw new InternalError JavaDoc(e.toString());
137         } catch (IllegalAccessException JavaDoc e) {
138             throw new InternalError JavaDoc(e.toString());
139         } catch (InstantiationException JavaDoc e) {
140             throw new InternalError JavaDoc(e.toString());
141         } catch (InvocationTargetException JavaDoc e) {
142             throw new InternalError JavaDoc(e.toString());
143         }
144     }
145
146     /*
147      * Returns an instance of a proxy class for the specified interface that dispatches method invocations to
148      * the specified invocation handler.
149      */

150     public Object JavaDoc newProxyInstance(Class JavaDoc interfce, org.openejb.util.proxy.InvocationHandler h) throws IllegalArgumentException JavaDoc {
151         Jdk13InvocationHandler handler = new Jdk13InvocationHandler(h);
152         return Proxy.newProxyInstance(interfce.getClassLoader(), new Class JavaDoc[]{interfce}, handler);
153     }
154
155     /*
156      * Returns an instance of a proxy class for the specified interface that dispatches method invocations to
157      * the specified invocation handler.
158      */

159     public Object JavaDoc newProxyInstance(Class JavaDoc[] interfaces, org.openejb.util.proxy.InvocationHandler h) throws IllegalArgumentException JavaDoc {
160         if(interfaces.length < 1) {
161             throw new IllegalArgumentException JavaDoc("It's boring to implement 0 interfaces!");
162         }
163         Jdk13InvocationHandler handler = new Jdk13InvocationHandler(h);
164         return Proxy.newProxyInstance(interfaces[0].getClassLoader(), interfaces, handler);
165     }
166 }
167
168
Popular Tags