KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > config > SpringAspectModule


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.object.config;
5
6 import com.tc.aspectwerkz.DeploymentModel;
7 import com.tc.aspectwerkz.definition.deployer.AspectDefinitionBuilder;
8 import com.tc.aspectwerkz.definition.deployer.AspectModule;
9 import com.tc.aspectwerkz.definition.deployer.AspectModuleDeployer;
10
11
12 /**
13  * Manages deployment of all AW aspects used to implement the Clustered Spring Runtime Container.
14  *
15  * @author Jonas Bonér
16  * @author Eugene Kuleshov
17  */

18 public class SpringAspectModule implements AspectModule {
19
20   public void deploy(final AspectModuleDeployer deployer) {
21     buildDefinitionForBeanDefinitionProtocol(deployer);
22     
23     buildDefinitionForGetBeanProtocol(deployer);
24     
25     buildDefinitionForScopeProtocol(deployer);
26     
27     buildDefinitionForApplicationContextEventProtocol(deployer);
28
29     buildDefinitionForAopProxyFactoryProtocol(deployer);
30
31     // buildDefinitionForTransactionManagerProtocol(deployer);
32

33     // buildDefinitionForConfigurableAnnotationProtocol(deployer);
34
}
35
36   /**
37    * Handle ignored fields and other class metadata.
38    */

39   private void buildDefinitionForBeanDefinitionProtocol(AspectModuleDeployer deployer) {
40     deployer.addMixin("com.tcspring.DistributableBeanFactoryMixin", DeploymentModel.PER_INSTANCE,
41                       "within(org.springframework.beans.factory.support.AbstractBeanFactory)", true);
42
43     AspectDefinitionBuilder builder = deployer.newAspectBuilder("com.tcspring.BeanDefinitionProtocol",
44                                                                 DeploymentModel.PER_TARGET, null);
45
46     // capture context parameters when BeanFactory is loaded by the
47
builder.addAdvice("before",
48         "execution(int org.springframework.beans.factory.support.BeanDefinitionReader+.loadBeanDefinitions(..)) "
49             + "AND args(resource) AND target(reader)",
50         "captureIdentity(StaticJoinPoint jp, org.springframework.core.io.Resource resource, "
51             + "org.springframework.beans.factory.support.BeanDefinitionReader reader)");
52     
53     builder.addAdvice("around",
54       "execution(* org.springframework.context.support.AbstractRefreshableApplicationContext+.loadBeanDefinitions(..)) "
55           + "AND args(beanFactory)",
56       "collectDefinitions(StaticJoinPoint jp, org.springframework.beans.factory.support.DefaultListableBeanFactory beanFactory)");
57
58     builder.addAdvice("after",
59         "execution(org.springframework.beans.factory.config.BeanDefinitionHolder.new(..)) AND target(holder)",
60         "saveBeanDefinition(StaticJoinPoint jp, org.springframework.beans.factory.config.BeanDefinitionHolder holder)");
61
62     builder.addAdvice("around",
63       "cflow(execution(* org.springframework.context.support.AbstractRefreshableApplicationContext+.loadBeanDefinitions(..))) "
64           + "AND withincode(* org.springframework.beans.factory.support.BeanDefinitionReaderUtils.createBeanDefinition(..)) "
65           + "AND call(* org.springframework.util.ClassUtils.forName(..)) "
66           + "AND args(className, loader)",
67       "disableClassForName(String className, java.lang.ClassLoader loader)");
68   }
69
70   private void buildDefinitionForGetBeanProtocol(AspectModuleDeployer deployer) {
71     AspectDefinitionBuilder builder = deployer.newAspectBuilder("com.tcspring.GetBeanProtocol", DeploymentModel.PER_JVM, null);
72
73 // This approach does not work because of Spring's handling of the circular dependencies
74
// builder.addAdvice("after",
75
// "execution(org.springframework.beans.factory.support.AbstractBeanFactory.new(..)) "
76
// + "AND this(factory)",
77
// "registerBeanPostProcessor(StaticJoinPoint jp, org.springframework.beans.factory.support.AbstractBeanFactory factory)");
78

79     builder.addAdvice("around",
80       "execution(* org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(String, ..)) "
81           + "AND args(beanName, ..) AND target(beanFactory)",
82       "beanNameCflow(StaticJoinPoint jp, String beanName,"
83           + "org.springframework.beans.factory.config.AutowireCapableBeanFactory beanFactory)");
84
85     builder.addAdvice("around",
86       "withincode(* org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(String, ..)) "
87           + "AND call(* org.springframework.beans.BeanWrapper+.getWrappedInstance()) "
88           + "AND this(beanFactory)",
89       "virtualizeSingletonBean(StaticJoinPoint jp, "
90           + "org.springframework.beans.factory.config.AutowireCapableBeanFactory beanFactory)");
91
92     builder.addAdvice("after",
93       "withincode(* org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(String, ..)) "
94           + "AND call(* org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(..)) "
95           + "AND this(beanFactory) AND args(beanName, mergedBeanDefinition, instanceWrapper)",
96       "initializeSingletonBean(String beanName, "
97           + "org.springframework.beans.factory.support.RootBeanDefinition mergedBeanDefinition, "
98           + "org.springframework.beans.BeanWrapper instanceWrapper, "
99           + "org.springframework.beans.factory.config.AutowireCapableBeanFactory beanFactory)");
100   }
101   
102   private void buildDefinitionForScopeProtocol(AspectModuleDeployer deployer) {
103     if (hasClass("org.springframework.beans.factory.config.Scope", deployer)) {
104       deployer.addMixin("com.tcspring.ScopeProtocol$DistributableBeanFactoryAwareMixin", DeploymentModel.PER_INSTANCE,
105           "within(org.springframework.beans.factory.config.Scope+)", true);
106
107       AspectDefinitionBuilder builder = deployer.newAspectBuilder("com.tcspring.ScopeProtocol", DeploymentModel.PER_JVM, null);
108
109       builder.addAdvice("after",
110           "execution(* org.springframework.beans.factory.support.AbstractBeanFactory+.registerScope(String, org.springframework.beans.factory.config.Scope+)) "
111               + "AND target(beanFactory) AND args(scopeName, scope)",
112           "setDistributableBeanFactory(String scopeName, "
113               + "org.springframework.beans.factory.config.Scope scope, "
114               + "org.springframework.beans.factory.support.AbstractBeanFactory beanFactory)");
115
116       builder.addAdvice("around",
117           "withincode(* org.springframework.beans.factory.support.AbstractBeanFactory.getBean(String, ..)) "
118               + "AND call(* org.springframework.beans.factory.config.Scope+.get(String, ..)) "
119               + "AND target(s) AND args(beanName, ..)",
120           "virtualizeScopedBean(StaticJoinPoint jp, org.springframework.beans.factory.config.Scope s, String beanName)");
121       
122       builder.addAdvice("around",
123           "withincode(* org.springframework.beans.factory.ObjectFactory+.getObject()) "
124               + "AND call(* org.springframework.beans.factory.config.Scope+.registerDestructionCallback(..)) "
125               + "AND target(scope) AND args(beanName, callback)",
126           "wrapDestructionCallback(StaticJoinPoint jp, String beanName, java.lang.Runnable callback, "
127               + "org.springframework.beans.factory.config.Scope scope)");
128       
129       builder.addAdvice("around",
130           "withincode(* org.springframework.beans.factory.config.Scope+.get(..)) "
131               + "AND call(* org.springframework.web.context.request.RequestAttributes+.getAttribute(String, ..)) "
132               + "AND this(s) AND args(beanName, ..)",
133           "suspendRequestAttributeGet(StaticJoinPoint jp, org.springframework.beans.factory.config.Scope s, String beanName)");
134       builder.addAdvice("around",
135           "withincode(* org.springframework.beans.factory.config.Scope+.get(..)) "
136               + "AND call(* org.springframework.web.context.request.RequestAttributes+.setAttribute(String, ..)) "
137               + "AND this(s) AND args(beanName, ..)",
138           "suspendRequestAttributeSet(StaticJoinPoint jp, org.springframework.beans.factory.config.Scope s, String beanName)");
139     }
140
141     if(hasClass("javax.servlet.http.HttpSession", deployer)) {
142       AspectDefinitionBuilder sessionBuilder = deployer.newAspectBuilder("com.tcspring.SessionProtocol",
143           DeploymentModel.PER_JVM, null);
144
145       // XXX change to wrap call org.springframework.web.context.request.RequestAttributes+.getSessionId()
146
sessionBuilder.addAdvice("around",
147         "cflow(execution(* org.springframework.web.context.request.SessionScope.getConversationId())) "
148             + "AND withincode(* org.springframework.web.context.request.ServletRequestAttributes.getSessionId()) "
149             + "AND call(* javax.servlet.http.HttpSession+.getId()) AND target(session)",
150         "clusterSessionId(StaticJoinPoint jp, javax.servlet.http.HttpSession session)");
151
152       if(hasClass("javax.servlet.http.HttpSessionBindingListener", deployer)) {
153         sessionBuilder.addAdvice("around",
154             "execution(* org.springframework.web.context.request.ServletRequestAttributes.registerSessionDestructionCallback(..)) "
155                 + "AND args(name, callback)",
156             "captureDestructionCallback(StaticJoinPoint jp, String name, java.lang.Runnable callback)");
157
158         sessionBuilder.addAdvice("around",
159             "withincode(* org.springframework.web.context.request.ServletRequestAttributes.registerSessionDestructionCallback(..)) "
160                 + "AND call(* javax.servlet.http.HttpSession.setAttribute(..)) "
161                 + "AND target(session) AND args(name, ..)",
162             "virtualizeSessionDestructionListener(StaticJoinPoint jp, String name, javax.servlet.http.HttpSession session)");
163       }
164     }
165   }
166
167   private void buildDefinitionForApplicationContextEventProtocol(final AspectModuleDeployer deployer) {
168     AspectDefinitionBuilder builder = deployer.newAspectBuilder("com.tcspring.ApplicationContextEventProtocol",
169                                                                 DeploymentModel.PER_JVM, null);
170
171     builder.addAdvice("before",
172         "withincode(* org.springframework.context.support.AbstractApplicationContext+.refresh()) "
173             + "AND call(* org.springframework.context.support.AbstractApplicationContext+.publishEvent(..)) "
174             + "AND target(ctx)",
175         "registerContext(StaticJoinPoint jp, org.springframework.context.support.AbstractApplicationContext ctx)");
176
177     builder.addAdvice("around",
178         "execution(void org.springframework.context.support.AbstractApplicationContext.publishEvent(..)) "
179             + "AND args(event) AND target(ctx)",
180         "interceptEvent(StaticJoinPoint jp, "
181             + "org.springframework.context.ApplicationEvent event, "
182             + "org.springframework.context.support.AbstractApplicationContext ctx)");
183   }
184
185   private void buildDefinitionForAopProxyFactoryProtocol(final AspectModuleDeployer deployer) {
186     if (hasClass("org.springframework.aop.framework.AopProxyFactory", deployer)) {
187
188       deployer.addMixin("com.tcspring.AopProxyFactoryProtocol$BeanFactoryAwareMixin", DeploymentModel.PER_INSTANCE,
189                         "within(org.springframework.aop.framework.ProxyFactoryBean)", true);
190
191       AspectDefinitionBuilder builder = deployer.newAspectBuilder("com.tcspring.AopProxyFactoryProtocol", DeploymentModel.PER_JVM, null);
192       
193       // save a accessible copy of the BeanFactory
194
builder.addAdvice("before",
195           "execution(void org.springframework.aop.framework.ProxyFactoryBean+.setBeanFactory(..)) "
196               + "AND args(beanFactory) AND this(bean)",
197           "saveBeanFactory(com.tcspring.BeanFactoryAware bean, "
198               + "org.springframework.beans.factory.BeanFactory beanFactory)");
199       
200       builder.addAdvice("around",
201           "execution(* org.springframework.aop.framework.AopProxyFactory+.createAopProxy(..)) "
202                     + "AND args(proxyFactory)",
203           "createAopProxy(StaticJoinPoint, org.springframework.aop.framework.AdvisedSupport proxyFactory)");
204     }
205   }
206   
207   private boolean hasClass(String JavaDoc name, AspectModuleDeployer deployer) {
208     return deployer.getClassLoader().getResource(name.replace('.', '/')+".class") != null;
209   }
210
211
212   // private void buildDefinitionForConfigurableAnnotationProtocol(AspectModuleDeployer deployer) {
213
// try {
214
// AspectDefinitionBuilder builder =
215
// deployer.newAspectBuilder(
216
// "com.tcspring.ConfigurableAnnotationProtocol",
217
// DeploymentModel.PER_JVM, null);
218
//
219
// builder.addAdvice("afterReturning",
220
// "execution(*.new(..)) AND within(@Configurable *) AND this(beanInstance)",
221
// "afterBeanConstruction(Object beanInstance)");
222
//
223
// builder.addAdvice("around",
224
// "execution(* org.springframework.aop.config.SpringConfiguredBeanDefinitionParser.getBeanConfigurerClass())",
225
// "hijackBeanConfigurerClass(StaticJoinPoint jp)");
226
//
227
// } catch(Throwable t) {
228
// System.err.println("[AW::WARNING] unable to build aspect for @Configurable; " + t.toString()+"
229
// "+Thread.currentThread());
230
// }
231
// }
232

233   // private void buildDefinitionForTransactionManagerProtocol(final AspectModuleDeployer deployer) {
234
// AspectDefinitionBuilder builder = deployer.newAspectBuilder("com.tcspring.TransactionManagerProtocol",
235
// DeploymentModel.PER_JVM, null);
236
//
237
// // hijack anything but NOT derivatives of the AbstractPlatformTransactionManager
238
//
239
// builder
240
// .addAdvice(
241
// "around",
242
// "execution(* org.springframework.transaction.PlatformTransactionManager+.getTransaction(..)) "
243
// + "AND !execution(* org.springframework.transaction.support.AbstractPlatformTransactionManager+.getTransaction(..))
244
// "
245
// + "AND target(manager)", "startTransaction(StaticJoinPoint jp, java.lang.Object manager)");
246
//
247
// builder
248
// .addAdvice(
249
// "around",
250
// "execution(* org.springframework.transaction.PlatformTransactionManager+.commit(..)) "
251
// + "AND !execution(* org.springframework.transaction.support.AbstractPlatformTransactionManager+.commit(..)) "
252
// + "AND target(manager)", "commitTransaction(StaticJoinPoint jp, java.lang.Object manager)");
253
//
254
// builder
255
// .addAdvice(
256
// "around",
257
// "execution(void org.springframework.transaction.PlatformTransactionManager+.rollback(..)) "
258
// + "AND !execution(* org.springframework.transaction.support.AbstractPlatformTransactionManager+.rollback(..)) "
259
// + "AND target(manager)", "rollbackTransaction(StaticJoinPoint jp, java.lang.Object manager)");
260
// }
261

262 }
263
264
Popular Tags