1 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 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 33 } 35 36 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 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 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 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 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 name, AspectModuleDeployer deployer) { 208 return deployer.getClassLoader().getResource(name.replace('.', '/')+".class") != null; 209 } 210 211 212 233 262 } 263 264 | Popular Tags |