KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > util > 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 "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: ProxyManager.java 1921 2005-06-19 22:40:34Z jlaskowski $
44  */

45
46
47 package org.openejb.util.proxy;
48
49 import java.util.HashMap JavaDoc;
50
51
52 /**
53  *
54  * @author David Blevins
55  * @author Richard Monson-Haefel
56  * @author Aaron Mulder (ammulder@alumni.princeton.edu)
57  */

58 public class ProxyManager {
59
60
61     //=============================================================
62
// Methods and members for the ProxyManager abstract factory
63
//
64
private static volatile ProxyFactory defaultFactory;
65     private static final HashMap JavaDoc factories = new HashMap JavaDoc();
66     private static volatile String JavaDoc defaultFactoryName;
67
68     public static synchronized ProxyFactory registerFactory(String JavaDoc factoryName, ProxyFactory factory){
69         return (ProxyFactory)factories.put( factoryName, factory );
70     }
71
72     public static synchronized ProxyFactory unregisterFactory(String JavaDoc factoryName){
73         return (ProxyFactory)factories.remove( factoryName );
74     }
75
76     public static void checkDefaultFactory(){
77         if (defaultFactory == null) throw new IllegalStateException JavaDoc("[Proxy Manager] No default proxy factory specified.");
78     }
79
80     public static ProxyFactory getFactory(String JavaDoc factoryName){
81         return (ProxyFactory)factories.get(factoryName);
82     }
83
84     /**
85      * Sets the default factory.
86      *
87      * The factory must already be registered.
88      *
89      * @param factoryName
90      */

91     public static synchronized ProxyFactory setDefaultFactory(String JavaDoc factoryName){
92         ProxyFactory newFactory = getFactory(factoryName);
93         if (newFactory == null) return defaultFactory;
94
95         ProxyFactory oldFactory = defaultFactory;
96         defaultFactory = newFactory;
97         defaultFactoryName = factoryName;
98
99         return oldFactory;
100     }
101
102     public static ProxyFactory getDefaultFactory(){
103         return defaultFactory;
104     }
105
106     public static String JavaDoc getDefaultFactoryName(){
107         return defaultFactoryName;
108     }
109     /**
110      * Casts the object passed in to the appropriate proxy type and retreives
111      * the InvocationHandler assigned to it.
112      *
113      * Executes on the default ProxyFactory instance.
114      *
115      * @param proxy The Proxy object to retreive the InvocationHandler from.
116      * @return The implementation of InvocationHandler handling invocations on the specified Proxy object.
117      */

118     public static InvocationHandler getInvocationHandler(Object JavaDoc proxy) {
119         checkDefaultFactory();
120         return defaultFactory.getInvocationHandler(proxy);
121     }
122
123     /**
124      * Casts the object passed in to the appropriate proxy type and sets
125      * the InvocationHandler assigned to it.
126      *
127      * @param proxy The Proxy object to retreive the InvocationHandler from.
128      * @return The Proxy object with the new InvocationHandler.
129      */

130     public static Object JavaDoc setInvocationHandler(Object JavaDoc proxy, InvocationHandler handler) {
131         checkDefaultFactory();
132         return defaultFactory.setInvocationHandler(proxy, handler);
133     }
134
135     /**
136      * Loads and returns the proxy implementation for the specified interface.
137      *
138      * The Class object is loaded using ProxyClassLoader.loadClass. If the class
139      * definition is not found, the findClass method will be called by the VM; at which
140      * point, the proxy class byte code will be generated by ProxyFactory and resolved by
141      * the VM.
142      *
143      * @param interfaceType
144      * @return Class
145      * @exception IllegalAccessException
146      */

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

166     public static Object JavaDoc newProxyInstance(Class JavaDoc interfaceType, InvocationHandler h) throws IllegalAccessException JavaDoc {
167         return newProxyInstance(new Class JavaDoc[]{interfaceType}, h);
168     }
169     public static Object JavaDoc newProxyInstance(Class JavaDoc[] interfaces, InvocationHandler h) throws IllegalAccessException JavaDoc {
170         checkDefaultFactory();
171         return defaultFactory.newProxyInstance(interfaces, h);
172     }
173
174     /**
175      *
176      * @param cl
177      * @return boolean
178      */

179     public static boolean isProxyClass(Class JavaDoc cl) {
180         checkDefaultFactory();
181         return defaultFactory.isProxyClass(cl);
182     }
183
184     /**
185     * Create a new proxy instance given a proxy class.
186     */

187     public static Object JavaDoc newProxyInstance(Class JavaDoc proxyClass) throws IllegalAccessException JavaDoc {
188         checkDefaultFactory();
189         return defaultFactory.newProxyInstance(proxyClass);
190     }
191     //
192
// Methods and members for the ProxyFactory abstract factory
193
//===================================================
194
}
195
Popular Tags