KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > proxy > DynamicProxy


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.proxy;
9
10 import java.lang.reflect.InvocationHandler JavaDoc;
11 import java.lang.reflect.InvocationTargetException JavaDoc;
12 import java.lang.reflect.Method JavaDoc;
13 import java.lang.reflect.Proxy JavaDoc;
14
15 /**
16  * This makes a dynamic proxy for an object. The object can be represented
17  * by one, some of all of it's interfaces.
18  *
19  * Amongst other things, it's an anti hackinge measure. Suitable armed code
20  * could have case an interface for a thing back to it's impl and used methods
21  * and properties that were not it's authors intention. Reflection too allows
22  * some powerful introspection things and some traversal even more things
23  * including private member vars by a serialisation trick... hence the transient.
24  *
25  *
26  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
27  * @author <a HREF="mailto:Paul_Hammant@yahoo.com">Paul Hammant</a>
28  * @version CVS $Revision: 1.5 $ $Date: 2001/12/11 09:53:32 $
29  * @since 4.0b5
30  */

31 public final class DynamicProxy
32     implements InvocationHandler JavaDoc
33 {
34     private transient Object JavaDoc m_object;
35
36     /**
37      * Private constructor that blocks instantiation outside this class.
38      *
39      * @param object the underlying object
40      */

41     private DynamicProxy( final Object JavaDoc object )
42     {
43         m_object = object;
44     }
45
46     /**
47      * Create a proxy object that has all of it's underlying
48      * interfaces implemented by proxy.
49      *
50      * @param object the underling object to proxy
51      * @return the proxied object
52      */

53     public static Object JavaDoc newInstance( final Object JavaDoc object )
54     {
55         return newInstance( object, object.getClass().getInterfaces() );
56     }
57
58     /**
59      * Create a proxy object that has specified interfaces implemented by proxy.
60      *
61      * @param object the underling object to proxy
62      * @return the proxied object
63      */

64     public static Object JavaDoc newInstance( final Object JavaDoc object, final Class JavaDoc[] interfaces )
65     {
66         final ClassLoader JavaDoc classLoader = object.getClass().getClassLoader();
67         final DynamicProxy proxy = new DynamicProxy( object );
68
69         return Proxy.newProxyInstance( classLoader, interfaces, proxy );
70     }
71
72     /**
73      * Invoke the specified method on underlying object.
74      * This is called by proxy object.
75      *
76      * @param proxy the proxy object
77      * @param method the method invoked on proxy object
78      * @param args the arguments supplied to method
79      * @return the return value of method
80      * @exception Throwable if an error occurs
81      */

82     public Object JavaDoc invoke( final Object JavaDoc proxy,
83                           final Method JavaDoc method,
84                           final Object JavaDoc[] args )
85         throws Throwable JavaDoc
86     {
87         try
88         {
89             return method.invoke( m_object, args );
90         }
91         catch( final InvocationTargetException JavaDoc ite )
92         {
93             throw ite.getTargetException();
94         }
95     }
96 }
97
Popular Tags