KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dynaop > ProxyFactory


1 package dynaop;
2
3 import dynaop.util.Cache;
4 import dynaop.util.Classes;
5
6 /**
7  * Manufactures proxies.
8  *
9  * @author Bob Lee (crazybob@crazybob.org)
10  */

11 public class ProxyFactory {
12     
13     Aspects aspects;
14     
15     Cache classProxyCreators = new Cache() {
16         protected Object JavaDoc create(Object JavaDoc key) {
17             if (aspects == null)
18                 return null;
19             Class JavaDoc parentClass = (Class JavaDoc) key;
20             ProxyType proxyType =
21                 aspects.createProxyType(parentClass,
22                         new ProxyTypeBuilder(parentClass));
23             return (proxyType == null) ? null :
24                 new ClassProxyCreator(proxyType, parentClass);
25         }
26     };
27
28     Cache dynamicProxyCreators = new Cache() {
29         ProxyType create(Class JavaDoc targetClass) {
30             if (aspects == null)
31                 return null;
32             return aspects.createProxyType(targetClass,
33                     new ProxyTypeBuilder(targetClass));
34         }
35         
36         protected Object JavaDoc create(Object JavaDoc key) {
37             ProxyType proxyType = create((Class JavaDoc) key);
38             return (proxyType == null) ? null :
39                 new DynamicProxyCreator(proxyType);
40         }
41     };
42     
43     ProxyFactory(Aspects aspects) {
44         this.aspects = new Aspects(aspects);
45     }
46
47     /**
48      * Creates dynamic proxy for the given target object. Returns the target
49      * object unproxied in the event that no interceptors or mixins are
50      * present.
51      *
52      * <p>Creates a completely new proxy instance that wraps a
53      * target instance and implements the specified interfaces. Keeps proxy
54      * separate from target object.</p>
55      */

56     public Object JavaDoc wrap(Object JavaDoc target) {
57         DynamicProxyCreator creator =
58             (DynamicProxyCreator) dynamicProxyCreators.get(target.getClass());
59
60         if (creator == null)
61             return target;
62         
63         DynamicProxyInvocationHandler handler =
64         new DynamicProxyInvocationHandler(creator.getProxyType(), target);
65         Proxy proxy = creator.createProxy(handler);
66         handler.setProxy(proxy);
67         
68         return proxy;
69     }
70     
71     /**
72      * Creates class proxy for the given target class.
73      * Returns the target object unproxied in the event that no
74      * interceptors or mixins are present.
75      *
76      * <p>Extends the parent class and overrides
77      * methods to support interception, and implements specified interfaces.
78      * Ties proxy and proxied class together but works
79      * with classes that don't have interfaces (<code>java.util.Date</code>
80      * for example).</p>
81      */

82     public Object JavaDoc extend(Class JavaDoc parentClass) {
83         ClassProxyCreator creator =
84             (ClassProxyCreator) classProxyCreators.get(parentClass);
85         if (creator == null)
86             return Classes.newInstance(parentClass);
87         
88         ClassProxyInvocationHandler handler =
89             new ClassProxyInvocationHandler(creator.getProxyType());
90         Proxy proxy = creator.createProxy(handler);
91         handler.setProxy(proxy);
92         
93         return proxy;
94     }
95     
96     /**
97      * Creates proxy factory instance configured using given aspects.
98      */

99     public static ProxyFactory getInstance(Aspects aspects) {
100         return new ProxyFactory(aspects);
101     }
102     
103     static ProxyFactory defaultInstance;
104     
105     /**
106      * Gets default proxy factory instance.&#160;Uses default
107      * {@link Aspects} instance.
108      */

109     public static synchronized ProxyFactory getInstance() {
110         if (defaultInstance == null)
111             defaultInstance = getInstance(Aspects.getInstance());
112         return defaultInstance;
113     }
114 }
115
Popular Tags