/** * Add another eligible method name, in addition to those already named. * Like the set methods, this method is for use when configuring proxies, * before a proxy is used. * @param name name of the additional method that will match * @return this pointcut to allow for multiple additions in one line * @see NameMatchMethodPointcut#addMethodName */ public NameMatchMethodPointcut addMethodName(String name) { return this.pointcut.addMethodName(name); }
/** * */ public void initializeProxy() { ProxyFactory factory = new ProxyFactory(); for (int i = 0; i < advices.length; i++) { DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(advices[i]); NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut(); pointcut.addMethodName("receiveAndExecute"); advisor.setPointcut(pointcut); factory.addAdvisor(advisor); } factory.setProxyTargetClass(false); factory.addInterface(ContainerDelegate.class); factory.setTarget(delegate); proxy = (ContainerDelegate) factory.getProxy(); }
@Test public void testMatchOneMethod() throws Throwable { pc.addMethodName("echo"); pc.addMethodName("set*"); assertEquals(0, nop.getCount()); proxied.getName(); proxied.getName(); assertEquals(0, nop.getCount()); proxied.echo(null); assertEquals(1, nop.getCount()); proxied.setName(""); assertEquals(2, nop.getCount()); proxied.setAge(25); assertEquals(25, proxied.getAge()); assertEquals(3, nop.getCount()); }
private void initializeProxy() throws Exception { if (proxyFactory == null) { proxyFactory = new ProxyFactory(); TransactionInterceptor advice = new TransactionInterceptor(transactionManager, PropertiesConverter.stringToProperties("create*=PROPAGATION_REQUIRES_NEW," + isolationLevelForCreate + "\ngetLastJobExecution*=PROPAGATION_REQUIRES_NEW," + isolationLevelForCreate + "\n*=PROPAGATION_REQUIRED")); if (validateTransactionState) { DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(new MethodInterceptor() { @Override public Object invoke(MethodInvocation invocation) throws Throwable { if (TransactionSynchronizationManager.isActualTransactionActive()) { throw new IllegalStateException( "Existing transaction detected in JobRepository. " + "Please fix this and try again (e.g. remove @Transactional annotations from client)."); } return invocation.proceed(); } }); NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut(); pointcut.addMethodName("create*"); advisor.setPointcut(pointcut); proxyFactory.addAdvisor(advisor); } proxyFactory.addAdvice(advice); proxyFactory.setProxyTargetClass(false); proxyFactory.addInterface(JobRepository.class); proxyFactory.setTarget(getTarget()); } }
@Test public void testCanPreventCastToAdvisedUsingOpaque() { TestBean target = new TestBean(); ProxyFactory pc = new ProxyFactory(target); pc.setInterfaces(ITestBean.class); pc.addAdvice(new NopInterceptor()); CountingBeforeAdvice mba = new CountingBeforeAdvice(); Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut().addMethodName("setAge"), mba); pc.addAdvisor(advisor); assertFalse("Opaque defaults to false", pc.isOpaque()); pc.setOpaque(true); assertTrue("Opaque now true for this config", pc.isOpaque()); ITestBean proxied = (ITestBean) createProxy(pc); proxied.setAge(10); assertEquals(10, proxied.getAge()); assertEquals(1, mba.getCalls()); assertFalse("Cannot be cast to Advised", proxied instanceof Advised); }
/** * Add another eligible method name, in addition to those already named. * Like the set methods, this method is for use when configuring proxies, * before a proxy is used. * @param name name of the additional method that will match * @return this pointcut to allow for multiple additions in one line * @see NameMatchMethodPointcut#addMethodName */ public NameMatchMethodPointcut addMethodName(String name) { return this.pointcut.addMethodName(name); }
/** * Add another eligible method name, in addition to those already named. * Like the set methods, this method is for use when configuring proxies, * before a proxy is used. * @param name name of the additional method that will match * @return this pointcut to allow for multiple additions in one line * @see NameMatchMethodPointcut#addMethodName */ public NameMatchMethodPointcut addMethodName(String name) { return this.pointcut.addMethodName(name); }
public class AuthenticationFactoryDecorator implements AuthFactory { private AuthFactory authenticationFactoryDelegate; private MethodInterceptor interceptor; public AuthenticationFactoryDecorator( final AuthFactory authenticationFactoryDelegate, final MethodInterceptor interceptor ) { this.authenticationFactoryDelegate = authenticationFactoryDelegate; this.interceptor = interceptor; } @Override public Authenticator create() { // Create the needed pointcut NameMatchMethodPointcut pc = new NameMatchMethodPointcut(); pc.addMethodName("authenticate"); // Get an authenticator from your legacy class Authenticator auth = authenticationFactoryDelegate.create(); // Create a new Proxy wrapping your authFactory with the needed pointcut and advice ProxyFactory proxyFactory = new ProxyFactory(auth); proxyFactory.addAdvice(interceptor); return (Authenticator) proxyFactory.getProxy(); } }
public Pointcut getIntersectionPointcut() { ComposablePointcut cp = new ComposablePointcut(); Pointcut pt1 = new ControlFlowPointcut(WaiterDelegate.class, "service"); NameMatchMethodPointcut pt2 = new NameMatchMethodPointcut(); pt2.addMethodName("greetTo"); return cp.intersection(pt1).intersection((Pointcut) pt2); } }
private void initializeProxy() throws Exception { if (proxyFactory == null) { proxyFactory = new ProxyFactory(); TransactionInterceptor advice = new TransactionInterceptor(transactionManager, PropertiesConverter .stringToProperties("create*=PROPAGATION_REQUIRES_NEW," + isolationLevelForCreate + "\ngetLastJobExecution*=PROPAGATION_REQUIRES_NEW," + isolationLevelForCreate + "\n*=PROPAGATION_REQUIRED")); DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(advice); NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut(); pointcut.addMethodName("*"); advisor.setPointcut(pointcut); proxyFactory.addAdvisor(advisor); proxyFactory.setProxyTargetClass(false); proxyFactory.addInterface(JobRepository.class); proxyFactory.setTarget(getTarget()); } }
private void initializeProxy() throws Exception { if (proxyFactory == null) { proxyFactory = new ProxyFactory(); TransactionInterceptor advice = new TransactionInterceptor(transactionManager, PropertiesConverter.stringToProperties("create*=PROPAGATION_REQUIRES_NEW," + isolationLevelForCreate + "\ngetLastJobExecution*=PROPAGATION_REQUIRES_NEW," + isolationLevelForCreate + "\n*=PROPAGATION_REQUIRED")); if (validateTransactionState) { DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(new MethodInterceptor() { @Override public Object invoke(MethodInvocation invocation) throws Throwable { if (TransactionSynchronizationManager.isActualTransactionActive()) { throw new IllegalStateException( "Existing transaction detected in JobRepository. " + "Please fix this and try again (e.g. remove @Transactional annotations from client)."); } return invocation.proceed(); } }); NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut(); pointcut.addMethodName("create*"); advisor.setPointcut(pointcut); proxyFactory.addAdvisor(advisor); } proxyFactory.addAdvice(advice); proxyFactory.setProxyTargetClass(false); proxyFactory.addInterface(JobRepository.class); proxyFactory.setTarget(getTarget()); } }
private void initializeProxy() throws Exception { if (proxyFactory == null) { proxyFactory = new ProxyFactory(); TransactionInterceptor advice = new TransactionInterceptor(transactionManager, PropertiesConverter.stringToProperties("create*=PROPAGATION_REQUIRES_NEW," + isolationLevelForCreate + "\ngetLastJobExecution*=PROPAGATION_REQUIRES_NEW," + isolationLevelForCreate + "\n*=PROPAGATION_REQUIRED")); if (validateTransactionState) { DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(new MethodInterceptor() { @Override public Object invoke(MethodInvocation invocation) throws Throwable { if (TransactionSynchronizationManager.isActualTransactionActive()) { throw new IllegalStateException( "Existing transaction detected in JobRepository. " + "Please fix this and try again (e.g. remove @Transactional annotations from client)."); } return invocation.proceed(); } }); NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut(); pointcut.addMethodName("create*"); advisor.setPointcut(pointcut); proxyFactory.addAdvisor(advisor); } proxyFactory.addAdvice(advice); proxyFactory.setProxyTargetClass(false); proxyFactory.addInterface(JobRepository.class); proxyFactory.setTarget(getTarget()); } }
private void initializeProxy() throws Exception { if (proxyFactory == null) { proxyFactory = new ProxyFactory(); TransactionInterceptor advice = new TransactionInterceptor(transactionManager, PropertiesConverter.stringToProperties("create*=PROPAGATION_REQUIRES_NEW," + isolationLevelForCreate + "\ngetLastJobExecution*=PROPAGATION_REQUIRES_NEW," + isolationLevelForCreate + "\n*=PROPAGATION_REQUIRED")); if (validateTransactionState) { DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(new MethodInterceptor() { @Override public Object invoke(MethodInvocation invocation) throws Throwable { if (TransactionSynchronizationManager.isActualTransactionActive()) { throw new IllegalStateException( "Existing transaction detected in JobRepository. " + "Please fix this and try again (e.g. remove @Transactional annotations from client)."); } return invocation.proceed(); } }); NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut(); pointcut.addMethodName("create*"); advisor.setPointcut(pointcut); proxyFactory.addAdvisor(advisor); } proxyFactory.addAdvice(advice); proxyFactory.setProxyTargetClass(false); proxyFactory.addInterface(JobRepository.class); proxyFactory.setTarget(getTarget()); } }