KickJava   Java API By Example, From Geeks To Geeks.

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


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: ProxyManager.java 1921 2005-06-19 22:40:34Z jlaskowski $
44  */

45 package org.openejb.client.proxy;
46
47 import java.util.Properties JavaDoc;
48
49
50 /**
51  *
52  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
53  * @since 11/25/2001
54  */

55 public class ProxyManager {
56
57
58     //=============================================================
59
// Methods and members for the ProxyManager abstract factory
60
//
61
private static ProxyFactory defaultFactory;
62     private static String JavaDoc defaultFactoryName;
63
64     static {
65         String JavaDoc version = null;
66         Class JavaDoc factory = null;
67         try {
68             version = System.getProperty("java.vm.version");
69         } catch ( Exception JavaDoc e ) {
70             //TODO: Better exception handling
71
throw new RuntimeException JavaDoc("Unable to determine the version of your VM. No ProxyFactory Can be installed");
72         }
73         ClassLoader JavaDoc cl = getContextClassLoader();
74
75         if ( version.startsWith("1.1") ) {
76             throw new RuntimeException JavaDoc("This VM version is not supported: "+version);
77         } else if ( version.startsWith("1.2") ) {
78             defaultFactoryName = "JDK 1.2 ProxyFactory";
79
80             try {
81                 Class.forName("org.opentools.proxies.Proxy", true, cl);
82             } catch ( Exception JavaDoc e ) {
83                 //TODO: Better exception handling
84
throw new RuntimeException JavaDoc("No ProxyFactory Can be installed. Unable to load the class org.opentools.proxies.Proxy. This class is needed for generating proxies in JDK 1.2 VMs.");
85             }
86
87             try {
88                 factory = Class.forName("org.openejb.client.proxy.Jdk12ProxyFactory", true, cl);
89             } catch ( Exception JavaDoc e ) {
90                 //TODO: Better exception handling
91
throw new RuntimeException JavaDoc("No ProxyFactory Can be installed. Unable to load the class org.openejb.client.proxy.Jdk12ProxyFactory.");
92             }
93         } else {
94             defaultFactoryName = "JDK 1.3 ProxyFactory";
95
96             try {
97                 factory = Class.forName("org.openejb.client.proxy.Jdk13ProxyFactory", true, cl);
98             } catch ( Exception JavaDoc e ) {
99                 //TODO: Better exception handling
100
throw new RuntimeException JavaDoc("No ProxyFactory Can be installed. Unable to load the class org.openejb.client.proxy.Jdk13ProxyFactory.");
101             }
102         }
103
104         try {
105             
106             defaultFactory = (ProxyFactory)factory.newInstance();
107             defaultFactory.init( new Properties JavaDoc() );
108
109         } catch ( Exception JavaDoc e ) {
110             //TODO: Better exception handling
111
throw new RuntimeException JavaDoc("No ProxyFactory Can be installed. Unable to load the class org.openejb.client.proxy.Jdk13ProxyFactory.");
112         }
113
114     }
115
116     public static ProxyFactory getDefaultFactory() {
117         return defaultFactory;
118     }
119
120     public static String JavaDoc getDefaultFactoryName() {
121         return defaultFactoryName;
122     }
123     /**
124      * Casts the object passed in to the appropriate proxy type and retreives
125      * the InvocationHandler assigned to it.
126      *
127      * Executes on the default ProxyFactory instance.
128      *
129      * @param proxy The Proxy object to retreive the InvocationHandler from.
130      * @return The implementation of InvocationHandler handling invocations on the specified Proxy object.
131      */

132     public static InvocationHandler getInvocationHandler(Object JavaDoc proxy) {
133         return defaultFactory.getInvocationHandler(proxy);
134     }
135
136     /**
137      * Casts the object passed in to the appropriate proxy type and sets
138      * the InvocationHandler assigned to it.
139      *
140      * @param proxy The Proxy object to retreive the InvocationHandler from.
141      * @return The Proxy object with the new InvocationHandler.
142      */

143     public static Object JavaDoc setInvocationHandler(Object JavaDoc proxy, InvocationHandler handler) {
144         return defaultFactory.setInvocationHandler(proxy, handler);
145     }
146
147     /**
148      * Loads and returns the proxy implementation for the specified interface.
149      *
150      * @param interfaceType
151      * @return Class
152      * @exception IllegalAccessException
153      */

154     public static Class JavaDoc getProxyClass(Class JavaDoc interfaceType) throws IllegalAccessException JavaDoc{
155         return getProxyClass(new Class JavaDoc[]{interfaceType});
156     }
157
158     public static Class JavaDoc getProxyClass(Class JavaDoc[] interfaces) throws IllegalAccessException JavaDoc{
159         return defaultFactory.getProxyClass( interfaces);
160     }
161
162     /**
163      * Throws a RuntimeException if there is a problem
164      * instantiating the new proxy instance.
165      *
166      * @param interfaceType
167      * A bean's home or remote interface that the Proxy
168      * object should implement.
169      * @param h
170      * @return Object
171      * @exception IllegalAccessException
172      */

173     public static Object JavaDoc newProxyInstance(Class JavaDoc interfaceType, InvocationHandler h) throws IllegalAccessException JavaDoc {
174         return newProxyInstance(new Class JavaDoc[]{interfaceType}, h);
175     }
176
177     public static Object JavaDoc newProxyInstance(Class JavaDoc[] interfaces, InvocationHandler h) throws IllegalAccessException JavaDoc {
178         return defaultFactory.newProxyInstance(interfaces, h);
179     }
180
181     /**
182      *
183      * @param cl
184      * @return boolean
185      */

186     public static boolean isProxyClass(Class JavaDoc cl) {
187         return defaultFactory.isProxyClass(cl);
188     }
189
190     /**
191     * Create a new proxy instance given a proxy class.
192     */

193     public static Object JavaDoc newProxyInstance(Class JavaDoc proxyClass) throws IllegalAccessException JavaDoc {
194         return defaultFactory.newProxyInstance(proxyClass);
195     }
196     //
197
// Methods and members for the ProxyFactory abstract factory
198
//===================================================
199

200     public static ClassLoader JavaDoc getContextClassLoader() {
201         return (ClassLoader JavaDoc) java.security.AccessController.doPrivileged(
202             new java.security.PrivilegedAction JavaDoc() {
203                 public Object JavaDoc run() {
204                     return Thread.currentThread().getContextClassLoader();
205                 }
206             }
207         );
208     }
209 }
210
Popular Tags