KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > components > application > BlockInvocationHandler


1 /* ====================================================================
2  * Loom Software License, version 1.1
3  *
4  * Copyright (c) 2003, Loom Group. All rights reserved.
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. Neither the name of the Loom Group nor the name "Loom" nor
18  * the names of its contributors may be used to endorse or promote
19  * products derived from this software without specific prior
20  * written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * ====================================================================
36  *
37  * Loom includes code from the Apache Software Foundation
38  *
39  * ====================================================================
40  * The Apache Software License, Version 1.1
41  *
42  * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
43  * reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  *
49  * 1. Redistributions of source code must retain the above copyright
50  * notice, this list of conditions and the following disclaimer.
51  *
52  * 2. Redistributions in binary form must reproduce the above copyright
53  * notice, this list of conditions and the following disclaimer in
54  * the documentation and/or other materials provided with the
55  * distribution.
56  *
57  * 3. The end-user documentation included with the redistribution,
58  * if any, must include the following acknowledgment:
59  * "This product includes software developed by the
60  * Apache Software Foundation (http://www.apache.org/)."
61  * Alternately, this acknowledgment may appear in the software
62  * itself, if and wherever such third-party acknowledgments
63  * normally appear.
64  *
65  * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
66  * must not be used to endorse or promote products derived from this
67  * software without prior written permission. For written
68  * permission, please contact apache@apache.org.
69  *
70  * 5. Products derived from this software may not be called "Apache",
71  * nor may "Apache" appear in their name, without prior written
72  * permission of the Apache Software Foundation.
73  *
74  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
75  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
76  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
77  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
78  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
79  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
80  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
81  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
82  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
83  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
84  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85  * SUCH DAMAGE.
86  */

87 package org.codehaus.loom.components.application;
88
89 import java.lang.reflect.InvocationHandler JavaDoc;
90 import java.lang.reflect.InvocationTargetException JavaDoc;
91 import java.lang.reflect.Method JavaDoc;
92 import java.lang.reflect.Proxy JavaDoc;
93
94 /**
95  * This makes a dynamic proxy for an object. The object can be represented by
96  * one, some or all of it's interfaces.
97  *
98  * <p>Amongst other things, it's an anti hackinge measure. Suitable armed code
99  * could have case an interface for a thing back to it's impl and used methods
100  * and properties that were not it's authors intention. Reflection too allows
101  * some powerful introspection things and some traversal even more things
102  * including private member vars by a serialisation trick... hence the
103  * transient.</p>
104  *
105  * <p>This proxy also allows itself to be invalidated thus making it impossible
106  * to call methods on a Block after it has been shutdown.</p>
107  *
108  * @author Peter Donald
109  * @author <a HREF="mailto:Paul_Hammant@yahoo.com">Paul Hammant</a>
110  * @version CVS $Revision: 1.3 $ $Date: 2004/08/17 23:14:32 $
111  */

112 final class BlockInvocationHandler
113     implements InvocationHandler JavaDoc
114 {
115     private transient Object JavaDoc m_object;
116     private transient Object JavaDoc m_proxy;
117
118     /**
119      * Create a proxy object that has specified interfaces implemented by
120      * proxy.
121      *
122      * @param object the underlying object
123      * @param interfaces the interfaces to proxy
124      */

125     BlockInvocationHandler( final Object JavaDoc object, final Class JavaDoc[] interfaces )
126     {
127         final ClassLoader JavaDoc classLoader = object.getClass().getClassLoader();
128
129         m_object = object;
130         m_proxy = Proxy.newProxyInstance( classLoader, interfaces, this );
131     }
132
133     /**
134      * Invalidate Proxy making it impossible to call methods of real-object.
135      */

136     public void invalidate()
137     {
138         m_object = null;
139         m_proxy = null;
140     }
141
142     /**
143      * Return the proxy object.
144      *
145      * @return the proxy object
146      */

147     public Object JavaDoc getProxy()
148     {
149         return m_proxy;
150     }
151
152     /**
153      * Invoke the specified method on underlying object. This is called by proxy
154      * object.
155      *
156      * @param proxy the proxy object
157      * @param method the method invoked on proxy object
158      * @param args the arguments supplied to method
159      * @return the return value of method
160      * @throws Throwable if an error occurs
161      */

162     public Object JavaDoc invoke( final Object JavaDoc proxy,
163                           final Method JavaDoc method,
164                           final Object JavaDoc[] args )
165         throws Throwable JavaDoc
166     {
167         if( null != m_object )
168         {
169             try
170             {
171                 return method.invoke( m_object, args );
172             }
173             catch( final InvocationTargetException JavaDoc ite )
174             {
175                 throw ite.getTargetException();
176             }
177         }
178         else
179         {
180             throw new IllegalStateException JavaDoc( "Using a stale object reference "
181                                              + "to call a disposed Block." );
182         }
183     }
184 }
185
Popular Tags