KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > proxy > Proxy


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.proxy;
5
6
7 import com.tc.aspectwerkz.DeploymentModel;
8 import com.tc.aspectwerkz.definition.DefinitionParserHelper;
9 import com.tc.aspectwerkz.definition.SystemDefinition;
10 import com.tc.aspectwerkz.intercept.AdvisableImpl;
11
12 /**
13  * Facade for Proxy service. Proxy are exposed to the weaver upon compilation, and can be made Advisable as well. <p/>
14  * We provide 2 proxy strategy: one by subclassing a non final concrete class, and thus having the proxy delegate to the
15  * real implementation thru super.xxx(..) calls, and one by delegating to N implementations of N interfaces. <p/> Proxy
16  * strategy provide a cache mechanism if ones wants to cache the compiled proxy. <p/> Pointcut to match delegating
17  * proxies should use a "+" as for regular subtype matching. <p/> Pointcut to match subclassing proxies don't need to
18  * use a "+" - precisely to avoid pointcut refactoring to match them.
19  *
20  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
21  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
22  */

23 public class Proxy {
24
25   /**
26    * Creates a new subclassing proxy instance based for the class specified and instantiates it using its default
27    * no-argument constructor. <p/> The proxy will be cached and non-advisable.
28    *
29    * @param clazz the target class to make a proxy for
30    * @return the proxy instance
31    */

32   public static Object JavaDoc newInstance(final Class JavaDoc clazz,
33                                    final SystemDefinition definition) {
34     return ProxySubclassingStrategy.newInstance(clazz, definition);
35   }
36
37   /**
38    * Creates a new subclassing proxy instance for the class specified and instantiates it using the constructor matching
39    * the argument type array specified. <p/> The proxy will be cached and non-advisable.
40    *
41    * @param clazz the target class to make a proxy for
42    * @param argumentTypes the argument types matching the signature of the constructor to use when instantiating the
43    * proxy
44    * @param argumentValues the argument values to use when instantiating the proxy
45    * @return the proxy instance
46    */

47   public static Object JavaDoc newInstance(final Class JavaDoc clazz,
48                                    final Class JavaDoc[] argumentTypes,
49                                    final Object JavaDoc[] argumentValues,
50                                    final SystemDefinition definition) {
51     return ProxySubclassingStrategy.newInstance(
52             clazz, argumentTypes, argumentValues, definition);
53   }
54
55   /**
56    * Creates a new subclassing proxy instance based for the class specified and instantiates it using its default
57    * no-argument constructor.
58    *
59    * @param clazz the target class to make a proxy for
60    * @param useCache true if a cached instance of the proxy classed should be used
61    * @param makeAdvisable true if the proxy class should implement the <code>Advisable</code> interface, e.g. be
62    * prepared for programmatic, runtime, per instance hot deployement of advice
63    * @return the proxy instance
64    */

65   public static Object JavaDoc newInstance(final Class JavaDoc clazz,
66                                    final boolean useCache,
67                                    final boolean makeAdvisable,
68                                    final SystemDefinition definition) {
69     return ProxySubclassingStrategy.newInstance(
70             clazz, useCache, makeAdvisable, definition);
71   }
72
73   /**
74    * Creates a new subclassing proxy instance for the class specified and instantiates it using the constructor matching
75    * the argument type array specified.
76    *
77    * @param clazz the target class to make a proxy for
78    * @param argumentTypes the argument types matching the signature of the constructor to use when instantiating the
79    * proxy
80    * @param argumentValues the argument values to use when instantiating the proxy
81    * @param useCache true if a cached instance of the proxy classed should be used
82    * @param makeAdvisable true if the proxy class should implement the <code>Advisable</code> interface, e.g. be
83    * prepared for programmatic, runtime, per instance hot deployement of advice
84    * @return the proxy instance
85    */

86   public static Object JavaDoc newInstance(final Class JavaDoc clazz,
87                                    final Class JavaDoc[] argumentTypes,
88                                    final Object JavaDoc[] argumentValues,
89                                    final boolean useCache,
90                                    final boolean makeAdvisable,
91                                    final SystemDefinition definition) {
92     return ProxySubclassingStrategy.newInstance(
93             clazz,
94             argumentTypes,
95             argumentValues,
96             useCache,
97             makeAdvisable,
98             definition);
99   }
100
101   /**
102    * Create a delegation proxy or retrieve it from cache and instantiate it, using the given implementations. <p/> Each
103    * implementation must implement the respective given interface.
104    *
105    * @param interfaces
106    * @param implementations
107    * @param useCache
108    * @param makeAdvisable
109    * @return
110    */

111   public static Object JavaDoc newInstance(final Class JavaDoc[] interfaces,
112                                    final Object JavaDoc[] implementations,
113                                    final boolean useCache,
114                                    final boolean makeAdvisable,
115                                    final SystemDefinition definition) {
116     return ProxyDelegationStrategy.newInstance(
117             interfaces,
118             implementations,
119             useCache,
120             makeAdvisable,
121             definition);
122   }
123
124   /**
125    * Enhances the proxy class with the Advisable mixin, to allow runtime per instance additions of interceptors. Simply
126    * register in the system definition.
127    *
128    * @param proxyClassName
129    * @param loader
130    */

131   static void makeProxyAdvisable(final String JavaDoc proxyClassName,
132                                  ClassLoader JavaDoc loader,
133                                  final SystemDefinition definition) {
134     // changes occurs in the virtual definition only
135
String JavaDoc withinPointcut = "within(" + proxyClassName.replace('/', '.') + ')';
136     definition.addMixinDefinition(DefinitionParserHelper.createAndAddMixinDefToSystemDef(AdvisableImpl.CLASS_INFO,
137             withinPointcut,
138             DeploymentModel.PER_INSTANCE,
139             false,
140             definition));
141     DefinitionParserHelper.createAndAddAdvisableDef('(' + withinPointcut + " && execution(!static * *.*(..)))",
142             definition);
143   }
144 }
145                  
Popular Tags