KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > phoenix > components > application > BlockInvocationHandler


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.phoenix.components.application;
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 or all of it's interfaces.
18  *
19  * <p>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.</p>
24  *
25  * <p>This proxy also allows itself to be invalidated thus making it
26  * impossible to call methods on a Block after it has been shutdown.</p>
27  *
28  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
29  * @author <a HREF="mailto:Paul_Hammant@yahoo.com">Paul Hammant</a>
30  * @version CVS $Revision: 1.8 $ $Date: 2002/08/06 11:57:39 $
31  */

32 final class BlockInvocationHandler
33     implements InvocationHandler JavaDoc
34 {
35     private transient Object JavaDoc m_object;
36
37     private transient Object JavaDoc m_proxy;
38
39     /**
40      * Create a proxy object that has specified interfaces implemented by proxy.
41      *
42      * @param object the underlying object
43      * @param interfaces the interfaces to proxy
44      */

45     protected BlockInvocationHandler( final Object JavaDoc object, final Class JavaDoc[] interfaces )
46     {
47         final ClassLoader JavaDoc classLoader = object.getClass().getClassLoader();
48
49         m_object = object;
50         m_proxy = Proxy.newProxyInstance( classLoader, interfaces, this );
51     }
52
53     /**
54      * Invalidate Proxy making it impossible to call methods
55      * of real-object.
56      */

57     public void invalidate()
58     {
59         m_object = null;
60         m_proxy = null;
61     }
62
63     /**
64      * Return the proxy object.
65      *
66      * @return the proxy object
67      */

68     public Object JavaDoc getProxy()
69     {
70         return m_proxy;
71     }
72
73     /**
74      * Invoke the specified method on underlying object.
75      * This is called by proxy object.
76      *
77      * @param proxy the proxy object
78      * @param method the method invoked on proxy object
79      * @param args the arguments supplied to method
80      * @return the return value of method
81      * @throws Throwable if an error occurs
82      */

83     public Object JavaDoc invoke( final Object JavaDoc proxy,
84                           final Method JavaDoc method,
85                           final Object JavaDoc[] args )
86         throws Throwable JavaDoc
87     {
88         if( null != m_object )
89         {
90             try
91             {
92                 return method.invoke( m_object, args );
93             }
94             catch( final InvocationTargetException JavaDoc ite )
95             {
96                 throw ite.getTargetException();
97             }
98         }
99         else
100         {
101             throw new IllegalStateException JavaDoc( "Using a stale object reference "
102                                              + "to call a disposed Block." );
103         }
104     }
105 }
106
Popular Tags