KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > impl > factory > ProxyObjectFactory


1 /*
2  * Copyright 2003-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
18 package org.apache.avalon.fortress.impl.factory;
19
20 import org.apache.avalon.framework.component.Component;
21 import org.apache.excalibur.mpool.ObjectFactory;
22
23 import java.lang.reflect.Proxy JavaDoc;
24
25 /**
26  * An ObjectFactory that delegates to another ObjectFactory
27  * and proxies results of that factory.
28  *
29  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
30  * @version $Revision: 1.4 $ $Date: 2004/02/28 15:16:25 $
31  */

32 public final class ProxyObjectFactory extends AbstractObjectFactory
33 {
34     /**
35      * Create factory that delegates to specified factory.
36      *
37      * @param objectFactory the factory to delegate to
38      * @exception NullPointerException if the supplied object factory is null
39      */

40     public ProxyObjectFactory( final ObjectFactory objectFactory ) throws NullPointerException JavaDoc
41     {
42         super( objectFactory );
43     }
44
45     /**
46      * Create a new instance from delegated factory and proxy it.
47      *
48      * @return the proxied object
49      * @throws Exception if unable to create new instance
50      */

51     public Object JavaDoc newInstance()
52         throws Exception JavaDoc
53     {
54         final Object JavaDoc object = m_delegateFactory.newInstance();
55         return createProxy( object );
56     }
57
58     /**
59      * Dispose of objects created by this factory.
60      * Involves deproxying object and delegating to real ObjectFactory.
61      *
62      * @param object the proxied object
63      * @throws Exception if unable to dispose of object
64      */

65     public void dispose( final Object JavaDoc object )
66         throws Exception JavaDoc
67     {
68         final Object JavaDoc target = getObject( object );
69         m_delegateFactory.dispose( target );
70     }
71
72     /**
73      * Get the Component wrapped in the proxy.
74      *
75      * @param service the service object to proxy
76      */

77     public static Component createProxy( final Object JavaDoc service )
78     {
79         final Class JavaDoc clazz = service.getClass();
80         final Class JavaDoc[] workInterfaces = guessWorkInterfaces( clazz );
81
82         return (Component) Proxy.newProxyInstance( clazz.getClassLoader(),
83             workInterfaces,
84             new PassThroughInvocationHandler( service ) );
85     }
86
87     /**
88      * Get the target object from specified proxy.
89      *
90      * @param proxy the proxy object
91      * @return the target object
92      * @throws NullPointerException if unable to aquire target object,
93      * or specified object is not a proxy
94      */

95     public static Object JavaDoc getObject( final Object JavaDoc proxy )
96
97     {
98         if ( null == proxy )
99         {
100             throw new NullPointerException JavaDoc( "proxy" );
101         }
102
103         if ( !Proxy.isProxyClass( proxy.getClass() ) )
104         {
105             final String JavaDoc message = "object is not a proxy";
106             throw new IllegalArgumentException JavaDoc( message );
107         }
108
109         final PassThroughInvocationHandler handler =
110             (PassThroughInvocationHandler) Proxy.getInvocationHandler( proxy );
111         return handler.getObject();
112     }
113 }
114
Popular Tags