KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > component > ComponentProxyGenerator


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.avalon.excalibur.component;
18
19 import java.lang.reflect.InvocationHandler JavaDoc;
20 import java.lang.reflect.InvocationTargetException JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22 import java.lang.reflect.Proxy JavaDoc;
23
24 import java.util.HashSet JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import org.apache.avalon.framework.component.Component;
28
29 /**
30  * Create a Component proxy. Requires JDK 1.3+
31  *
32  * @deprecated ECM is no longer supported
33  *
34  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
35  */

36 public final class ComponentProxyGenerator
37 {
38     private final ClassLoader JavaDoc m_classLoader;
39
40     /**
41      * Initialize the ComponentProxyGenerator with the default classloader.
42      * The default classloader is the Thread context classloader.
43      */

44     public ComponentProxyGenerator()
45     {
46         this( Thread.currentThread().getContextClassLoader() );
47     }
48
49     /**
50      * Initialize the ComponentProxyGenerator with the supplied classloader.
51      * If the supplied class loader is null, we use the Thread context class
52      * loader. If that is null, we use this class's classloader.
53      */

54     public ComponentProxyGenerator( final ClassLoader JavaDoc parentClassLoader )
55     {
56         m_classLoader = ( null == parentClassLoader ) ?
57             ( ( null == Thread.currentThread().getContextClassLoader() ) ?
58             getClass().getClassLoader()
59             : Thread.currentThread().getContextClassLoader() )
60             : parentClassLoader;
61     }
62
63     /**
64      * Get the Component wrapped in the proxy. The role must be the service
65      * interface's fully qualified classname to work.
66      */

67     public Component getProxy( String JavaDoc role, Object JavaDoc service ) throws Exception JavaDoc
68     {
69         // Trim any trailing "/type" from the role name.
70
if( role.indexOf( '/' ) != -1 )
71         {
72             role = role.substring( 0, role.indexOf( '/' ) );
73         }
74
75         Class JavaDoc serviceInterface = m_classLoader.loadClass( role );
76
77         return (Component)Proxy.newProxyInstance( m_classLoader,
78                                                   new Class JavaDoc[]{Component.class, serviceInterface},
79                                                   new ComponentInvocationHandler( service ) );
80     }
81
82     /**
83      * Get the component wrapped in a proxy. The proxy will implement all the
84      * object's interfaces, for compatibility with, for example, the pool code.
85      * The proxy is guaranteed to implement Component.
86      */

87     public Component getCompatibleProxy( Object JavaDoc service ) throws Exception JavaDoc
88     {
89         Set JavaDoc interfaces = new HashSet JavaDoc();
90         getAllInterfaces( service.getClass(), interfaces );
91
92         interfaces.add( Component.class );
93
94         Class JavaDoc[] proxyInterfaces = (Class JavaDoc[]) interfaces.toArray( new Class JavaDoc[0] );
95
96         return (Component)Proxy.newProxyInstance( m_classLoader,
97             proxyInterfaces,
98             new ComponentInvocationHandler( service ) );
99     }
100
101     private void getAllInterfaces( Class JavaDoc clazz, Set JavaDoc interfaces ) throws Exception JavaDoc
102     {
103         if (clazz == null)
104         {
105             return;
106         }
107
108         Class JavaDoc[] objectInterfaces = clazz.getInterfaces();
109         for( int i = 0; i < objectInterfaces.length; i++ )
110         {
111             interfaces.add( objectInterfaces[i] );
112         }
113
114         getAllInterfaces( clazz.getSuperclass(), interfaces );
115     }
116
117
118     /**
119      * Internal class to handle the wrapping with Component
120      */

121     private final static class ComponentInvocationHandler
122         implements InvocationHandler JavaDoc
123     {
124         private final Object JavaDoc m_delagate;
125
126         public ComponentInvocationHandler( final Object JavaDoc delegate )
127         {
128             if( null == delegate )
129             {
130                 throw new NullPointerException JavaDoc( "delegate" );
131             }
132
133             m_delagate = delegate;
134         }
135
136         public Object JavaDoc invoke( final Object JavaDoc proxy,
137                               final Method JavaDoc meth,
138                               final Object JavaDoc[] args )
139             throws Throwable JavaDoc
140         {
141             try
142             {
143                 return meth.invoke( m_delagate, args );
144             }
145             catch( final InvocationTargetException JavaDoc ite )
146             {
147                 throw ite.getTargetException();
148             }
149         }
150     }
151 }
152
Popular Tags