KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > util > proxy > Proxy


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

45
46
47 package org.openejb.util.proxy;
48
49
50 import java.lang.reflect.Method JavaDoc;
51
52
53 /**
54  * Superclass for dynamically generated proxies.
55  *
56  * This class implements convenience methods that allow us to
57  * generate proxies with considerably less byte-code.
58  *
59  * @author Richard Monson-Haefel
60  * @author David Blevins
61  */

62 public abstract class Proxy implements java.io.Serializable JavaDoc {
63
64     public InvocationHandler handler;
65
66     /**
67      *
68      * @return The implementation of InvocationHandler handling invocations for this Proxy object.
69      */

70     public InvocationHandler getInvocationHandler() {
71         return handler;
72     }
73
74     public InvocationHandler setInvocationHandler(InvocationHandler newHandler) {
75         InvocationHandler oldHandler = handler;
76         handler = newHandler;
77         return oldHandler;
78     }
79
80     /**
81      * Used as the Class array in the Class.getMethod( String methodName, Class[] argTypes )
82      * method when the interface method does not define any arguments.
83      */

84     protected static final Class JavaDoc[] NO_ARGS_C = new Class JavaDoc[0];
85
86     /**
87      * Used as the Object array in the Method.invoke( Object obj, Object[] args )
88      * method when the interface method does not define any arguments.
89      */

90     protected static final Object JavaDoc[] NO_ARGS_O = new Object JavaDoc[0];
91
92     protected final void _proxyMethod$throws_default$returns_void(int methodNumber, String JavaDoc methodName, Class JavaDoc[] argTypes, Object JavaDoc[] args) throws java.rmi.RemoteException JavaDoc {
93         _proxyMethod$throws_default$returns_Object( methodNumber, methodName, argTypes, args);
94         return;
95     }
96
97     protected final Object JavaDoc _proxyMethod$throws_default$returns_Object(int methodNumber, String JavaDoc methodName, Class JavaDoc[] argTypes, Object JavaDoc[] args) throws java.rmi.RemoteException JavaDoc {
98         java.lang.reflect.Method JavaDoc method = _proxyMethod$lookupMethod( methodNumber, methodName, argTypes);
99         try{
100             return handler.invoke(this,method,args);
101         }catch(Throwable JavaDoc t){
102             // rethrow exceptions
103
if(t instanceof java.rmi.RemoteException JavaDoc)
104                      throw (java.rmi.RemoteException JavaDoc)t;
105             if(t instanceof java.lang.RuntimeException JavaDoc)
106                      throw (java.lang.RuntimeException JavaDoc)t;
107             else throw _proxyError$(t);
108         }
109     }
110
111     protected final void _proxyMethod$throws_AppException$returns_void(int methodNumber, String JavaDoc methodName, Class JavaDoc[] argTypes, Object JavaDoc[] args) throws java.rmi.RemoteException JavaDoc, org.openejb.ApplicationException {
112         _proxyMethod$throws_AppException$returns_Object( methodNumber, methodName, argTypes, args);
113         return;
114     }
115
116     protected final Object JavaDoc _proxyMethod$throws_AppException$returns_Object(int methodNumber, String JavaDoc methodName, Class JavaDoc[] argTypes, Object JavaDoc[] args) throws java.rmi.RemoteException JavaDoc , org.openejb.ApplicationException{
117         java.lang.reflect.Method JavaDoc method = _proxyMethod$lookupMethod( methodNumber, methodName, argTypes);
118         try{
119             return handler.invoke(this,method,args);
120         }catch(Throwable JavaDoc t){
121             // rethrow exceptions
122
if(t instanceof java.rmi.RemoteException JavaDoc)
123                      throw (java.rmi.RemoteException JavaDoc)t;
124             if(t instanceof java.lang.RuntimeException JavaDoc)
125                      throw (java.lang.RuntimeException JavaDoc)t;
126             if(t instanceof org.openejb.ApplicationException)
127                      throw (org.openejb.ApplicationException)t;
128             else throw _proxyError$(t);
129
130         }
131     }
132
133     //=============================================================================
134
// Methods that return primitives and only throw the default remote exception
135
//
136

137     protected final int _proxyMethod$throws_default$returns_int(int methodNumber, String JavaDoc methodName, Class JavaDoc[] argTypes, Object JavaDoc[] args) throws java.rmi.RemoteException JavaDoc {
138         Integer JavaDoc retval = (Integer JavaDoc)_proxyMethod$throws_default$returns_Object(methodNumber, methodName, argTypes, args);
139         return retval.intValue();
140     }
141
142     protected final double _proxyMethod$throws_default$returns_double(int methodNumber, String JavaDoc methodName, Class JavaDoc[] argTypes, Object JavaDoc[] args) throws java.rmi.RemoteException JavaDoc {
143         Double JavaDoc retval = (Double JavaDoc) _proxyMethod$throws_default$returns_Object(methodNumber, methodName, argTypes, args);
144         return retval.doubleValue();
145     }
146
147
148     protected final long _proxyMethod$throws_default$returns_long(int methodNumber, String JavaDoc methodName, Class JavaDoc[] argTypes, Object JavaDoc[] args) throws java.rmi.RemoteException JavaDoc {
149         Long JavaDoc retval = (Long JavaDoc) _proxyMethod$throws_default$returns_Object(methodNumber, methodName, argTypes, args);
150         return retval.longValue();
151     }
152
153
154     protected final boolean _proxyMethod$throws_default$returns_boolean(int methodNumber, String JavaDoc methodName, Class JavaDoc[] argTypes, Object JavaDoc[] args) throws java.rmi.RemoteException JavaDoc {
155         Boolean JavaDoc retval = (Boolean JavaDoc) _proxyMethod$throws_default$returns_Object(methodNumber, methodName, argTypes, args);
156         return retval.booleanValue();
157     }
158
159
160     protected final float _proxyMethod$throws_default$returns_float(int methodNumber,String JavaDoc methodName, Class JavaDoc[] argTypes, Object JavaDoc[] args) throws java.rmi.RemoteException JavaDoc {
161         Float JavaDoc retval = (Float JavaDoc) _proxyMethod$throws_default$returns_Object(methodNumber, methodName, argTypes, args);
162         return retval.floatValue();
163     }
164
165
166     protected final char _proxyMethod$throws_default$returns_char(int methodNumber, String JavaDoc methodName, Class JavaDoc[] argTypes, Object JavaDoc[] args) throws java.rmi.RemoteException JavaDoc {
167         Character JavaDoc retval = (Character JavaDoc) _proxyMethod$throws_default$returns_Object(methodNumber, methodName, argTypes, args);
168         return retval.charValue();
169     }
170
171
172     protected final byte _proxyMethod$throws_default$returns_byte(int methodNumber, String JavaDoc methodName, Class JavaDoc[] argTypes, Object JavaDoc[] args) throws java.rmi.RemoteException JavaDoc {
173         Byte JavaDoc retval = (Byte JavaDoc) _proxyMethod$throws_default$returns_Object(methodNumber, methodName, argTypes, args);
174         return retval.byteValue();
175     }
176
177
178     protected final short _proxyMethod$throws_default$returns_short(int methodNumber, String JavaDoc methodName, Class JavaDoc[] argTypes, Object JavaDoc[] args) throws java.rmi.RemoteException JavaDoc {
179         Short JavaDoc retval = (Short JavaDoc) _proxyMethod$throws_default$returns_Object(methodNumber, methodName, argTypes, args);
180         return retval.shortValue();
181     }
182
183     //
184
// Methods that return primitives and only throw the default remote exception
185
//=============================================================================
186

187
188     //===========================================================
189
// Methods that return primitives and throw an AppException
190
//
191

192     protected final int _proxyMethod$throws_AppException$returns_int(int methodNumber, String JavaDoc methodName, Class JavaDoc[] argTypes, Object JavaDoc[] args) throws java.rmi.RemoteException JavaDoc, org.openejb.ApplicationException {
193         Integer JavaDoc retval = (Integer JavaDoc)_proxyMethod$throws_AppException$returns_Object(methodNumber, methodName, argTypes, args);
194         return retval.intValue();
195     }
196
197     protected final double _proxyMethod$throws_AppException$returns_double(int methodNumber, String JavaDoc methodName, Class JavaDoc[] argTypes, Object JavaDoc[] args) throws java.rmi.RemoteException JavaDoc, org.openejb.ApplicationException {
198         Double JavaDoc retval = (Double JavaDoc) _proxyMethod$throws_AppException$returns_Object(methodNumber, methodName, argTypes, args);
199         return retval.doubleValue();
200     }
201
202
203     protected final long _proxyMethod$throws_AppException$returns_long(int methodNumber, String JavaDoc methodName, Class JavaDoc[] argTypes, Object JavaDoc[] args) throws java.rmi.RemoteException JavaDoc, org.openejb.ApplicationException {
204         Long JavaDoc retval = (Long JavaDoc) _proxyMethod$throws_AppException$returns_Object(methodNumber, methodName, argTypes, args);
205         return retval.longValue();
206     }
207
208
209     protected final boolean _proxyMethod$throws_AppException$returns_boolean(int methodNumber, String JavaDoc methodName, Class JavaDoc[] argTypes, Object JavaDoc[] args) throws java.rmi.RemoteException JavaDoc, org.openejb.ApplicationException {
210         Boolean JavaDoc retval = (Boolean JavaDoc) _proxyMethod$throws_AppException$returns_Object(methodNumber, methodName, argTypes, args);
211         return retval.booleanValue();
212     }
213
214
215     protected final float _proxyMethod$throws_AppException$returns_float(int methodNumber,String JavaDoc methodName, Class JavaDoc[] argTypes, Object JavaDoc[] args) throws java.rmi.RemoteException JavaDoc, org.openejb.ApplicationException {
216         Float JavaDoc retval = (Float JavaDoc) _proxyMethod$throws_AppException$returns_Object(methodNumber, methodName, argTypes, args);
217         return retval.floatValue();
218     }
219
220
221     protected final char _proxyMethod$throws_AppException$returns_char(int methodNumber, String JavaDoc methodName, Class JavaDoc[] argTypes, Object JavaDoc[] args) throws java.rmi.RemoteException JavaDoc, org.openejb.ApplicationException {
222         Character JavaDoc retval = (Character JavaDoc) _proxyMethod$throws_AppException$returns_Object(methodNumber, methodName, argTypes, args);
223         return retval.charValue();
224     }
225
226
227     protected final byte _proxyMethod$throws_AppException$returns_byte(int methodNumber, String JavaDoc methodName, Class JavaDoc[] argTypes, Object JavaDoc[] args) throws java.rmi.RemoteException JavaDoc, org.openejb.ApplicationException {
228         Byte JavaDoc retval = (Byte JavaDoc) _proxyMethod$throws_AppException$returns_Object(methodNumber, methodName, argTypes, args);
229         return retval.byteValue();
230     }
231
232
233     protected final short _proxyMethod$throws_AppException$returns_short(int methodNumber, String JavaDoc methodName, Class JavaDoc[] argTypes, Object JavaDoc[] args) throws java.rmi.RemoteException JavaDoc, org.openejb.ApplicationException {
234         Short JavaDoc retval = (Short JavaDoc) _proxyMethod$throws_AppException$returns_Object(methodNumber, methodName, argTypes, args);
235         return retval.shortValue();
236     }
237
238     //
239
// Methods that return primitives and throw an AppException
240
//===========================================================
241

242     protected abstract Method JavaDoc _proxyMethod$lookupMethod(int index, String JavaDoc methodName, Class JavaDoc[] argTypes);
243
244     protected final Method JavaDoc _proxyMethod$lookupMethod(Class JavaDoc interfce, Method JavaDoc [] methodMap, int index, String JavaDoc methodName, Class JavaDoc[] argTypes){
245         // obtain method
246
java.lang.reflect.Method JavaDoc method = methodMap[index];
247         if(method == null){
248             try{ // Lazily create the method.
249
method = interfce.getMethod( methodName, argTypes );
250                 methodMap[index] = method;
251             }catch(NoSuchMethodException JavaDoc nsme){ throw new RuntimeException JavaDoc("Method not found: " +nsme.getMessage());}
252         }
253         return method;
254     }
255
256     protected final java.rmi.RemoteException JavaDoc _proxyError$(Throwable JavaDoc throwable){
257         return new java.rmi.RemoteException JavaDoc("[OpenEJB] Proxy Error: ",throwable );
258     }
259
260     protected final java.rmi.RemoteException JavaDoc _proxyError$(org.openejb.ApplicationException ae){
261         return new java.rmi.RemoteException JavaDoc("[OpenEJB] Proxy Error: The returned application exception is not defined in the throws clause. ", ae.getRootCause());
262     }
263
264 }
265
Popular Tags