/** * Create a new ProxyFactory for the given interface and interceptor. * <p>Convenience method for creating a proxy for a single interceptor, * assuming that the interceptor handles all calls itself rather than * delegating to a target, like in the case of remoting proxies. * @param proxyInterface the interface that the proxy should implement * @param interceptor the interceptor that the proxy should invoke */ public ProxyFactory(Class<?> proxyInterface, Interceptor interceptor) { addInterface(proxyInterface); addAdvice(interceptor); }
@Override public void afterPropertiesSet() { super.afterPropertiesSet(); Class<?> ifc = getServiceInterface(); Assert.notNull(ifc, "Property 'serviceInterface' is required"); // Build a proxy that also exposes the JAX-WS BindingProvider interface. ProxyFactory pf = new ProxyFactory(); pf.addInterface(ifc); pf.addInterface(BindingProvider.class); pf.addAdvice(this); this.serviceProxy = pf.getProxy(getBeanClassLoader()); }
/** * Wrap each concrete endpoint instance with an AOP proxy, * exposing the message listener's interfaces as well as the * endpoint SPI through an AOP introduction. */ @Override public MessageEndpoint createEndpoint(XAResource xaResource) throws UnavailableException { GenericMessageEndpoint endpoint = (GenericMessageEndpoint) super.createEndpoint(xaResource); ProxyFactory proxyFactory = new ProxyFactory(getMessageListener()); DelegatingIntroductionInterceptor introduction = new DelegatingIntroductionInterceptor(endpoint); introduction.suppressInterface(MethodInterceptor.class); proxyFactory.addAdvice(introduction); return (MessageEndpoint) proxyFactory.getProxy(); }
protected Object configuredProxy(SimpleRemoteSlsbInvokerInterceptor si, Class<?> ifc) throws NamingException { si.afterPropertiesSet(); ProxyFactory pf = new ProxyFactory(new Class<?>[] {ifc}); pf.addAdvice(si); return pf.getProxy(); }
@Override protected Object advised(Object target, PlatformTransactionManager ptm, TransactionAttributeSource[] tas) throws Exception { TransactionInterceptor ti = new TransactionInterceptor(); ti.setTransactionManager(ptm); ti.setTransactionAttributeSources(tas); ProxyFactory pf = new ProxyFactory(target); pf.addAdvice(0, ti); return pf.getProxy(); }
@Override protected void addPersistenceExceptionTranslation(ProxyFactory pf, PersistenceExceptionTranslator pet) { if (AnnotationUtils.findAnnotation(pf.getTargetClass(), Repository.class) != null) { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("peti", new RootBeanDefinition(PersistenceExceptionTranslationInterceptor.class)); bf.registerSingleton("pet", pet); pf.addAdvice((PersistenceExceptionTranslationInterceptor) bf.getBean("peti")); } }
protected Object createProxy(Object target, List<Advisor> advisors, Class<?>... interfaces) { ProxyFactory pf = new ProxyFactory(target); if (interfaces.length > 1 || interfaces[0].isInterface()) { pf.setInterfaces(interfaces); } else { pf.setProxyTargetClass(true); } // Required everywhere we use AspectJ proxies pf.addAdvice(ExposeInvocationInterceptor.INSTANCE); pf.addAdvisors(advisors); pf.setExposeProxy(true); return pf.getProxy(); }
private void testManyProxies(int howMany) { int age1 = 33; TestBean target1 = new TestBean(); target1.setAge(age1); ProxyFactory pf1 = new ProxyFactory(target1); pf1.addAdvice(new NopInterceptor()); pf1.addAdvice(new NopInterceptor()); ITestBean[] proxies = new ITestBean[howMany]; for (int i = 0; i < howMany; i++) { proxies[i] = (ITestBean) createAopProxy(pf1).getProxy(); assertEquals(age1, proxies[i].getAge()); } }
@Test public void testMethodValidationInterceptor() { MyValidBean bean = new MyValidBean(); ProxyFactory proxyFactory = new ProxyFactory(bean); proxyFactory.addAdvice(new MethodValidationInterceptor()); proxyFactory.addAdvisor(new AsyncAnnotationAdvisor()); doTestProxyValidation((MyValidInterface) proxyFactory.getProxy()); }
@Test public void testMethodValidationInterceptor() { MyValidBean bean = new MyValidBean(); ProxyFactory proxyFactory = new ProxyFactory(bean); proxyFactory.addAdvice(new MethodValidationInterceptor()); proxyFactory.addAdvisor(new AsyncAnnotationAdvisor()); doTestProxyValidation((MyValidInterface) proxyFactory.getProxy()); }
@Test public void crossClassInterfaceOnJdkProxy() { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTarget(new OtherServiceImpl()); proxyFactory.addInterface(OtherService.class); proxyFactory.addAdvice(this.ti); OtherService otherService = (OtherService) proxyFactory.getProxy(); otherService.foo(); assertGetTransactionAndCommitCount(1); }
@Test public void testInvokesMethodOnEjbInstance() throws Exception { Object retVal = new Object(); LocalInterfaceWithBusinessMethods ejb = mock(LocalInterfaceWithBusinessMethods.class); given(ejb.targetMethod()).willReturn(retVal); String jndiName= "foobar"; Context mockContext = mockContext(jndiName, ejb); LocalSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName); ProxyFactory pf = new ProxyFactory(new Class<?>[] { BusinessMethods.class }); pf.addAdvice(si); BusinessMethods target = (BusinessMethods) pf.getProxy(); assertTrue(target.targetMethod() == retVal); verify(mockContext).close(); verify(ejb).remove(); }
@Test public void testInvokesMethodOnEjbInstanceWithSeparateBusinessMethods() throws Exception { Object retVal = new Object(); LocalInterface ejb = mock(LocalInterface.class); given(ejb.targetMethod()).willReturn(retVal); String jndiName= "foobar"; Context mockContext = mockContext(jndiName, ejb); LocalSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName); ProxyFactory pf = new ProxyFactory(new Class<?>[] { BusinessMethods.class }); pf.addAdvice(si); BusinessMethods target = (BusinessMethods) pf.getProxy(); assertTrue(target.targetMethod() == retVal); verify(mockContext).close(); verify(ejb).remove(); }
@Test public void testMixinWithIntroductionInfo() throws Throwable { TestBean tb = new TestBean(); ProxyFactory pc = new ProxyFactory(); pc.addInterface(ITestBean.class); // We don't use an IntroductionAdvisor, we can just add an advice that implements IntroductionInfo pc.addAdvice(new LockMixin()); pc.setTarget(tb); testTestBeanIntroduction(pc); }
@Test public void testProxyProtectedMethod() { CountingBeforeAdvice advice = new CountingBeforeAdvice(); ProxyFactory proxyFactory = new ProxyFactory(new MyBean()); proxyFactory.addAdvice(advice); proxyFactory.setProxyTargetClass(true); MyBean proxy = (MyBean) proxyFactory.getProxy(); assertEquals(4, proxy.add(1, 3)); assertEquals(1, advice.getCalls("add")); }
@Test public void crossClassInterfaceMethodLevelOnJdkProxy() { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTarget(new SomeServiceImpl()); proxyFactory.addInterface(SomeService.class); proxyFactory.addAdvice(this.ti); SomeService someService = (SomeService) proxyFactory.getProxy(); someService.bar(); assertGetTransactionAndCommitCount(1); someService.foo(); assertGetTransactionAndCommitCount(2); someService.fooBar(); assertGetTransactionAndCommitCount(3); }
@Test public void withMultiMethodOverride() { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTarget(new TestWithMultiMethodOverride()); proxyFactory.addAdvice(this.ti); TestWithMultiMethodOverride proxy = (TestWithMultiMethodOverride) proxyFactory.getProxy(); proxy.doSomething(); assertGetTransactionAndCommitCount(1); proxy.doSomethingElse(); assertGetTransactionAndCommitCount(2); proxy.doSomethingCompletelyElse(); assertGetTransactionAndCommitCount(3); proxy.doSomething(); assertGetTransactionAndCommitCount(4); }
@Test public void withSingleMethodOverride() { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTarget(new TestWithSingleMethodOverride()); proxyFactory.addAdvice(this.ti); TestWithSingleMethodOverride proxy = (TestWithSingleMethodOverride) proxyFactory.getProxy(); proxy.doSomething(); assertGetTransactionAndCommitCount(1); proxy.doSomethingElse(); assertGetTransactionAndCommitCount(2); proxy.doSomethingCompletelyElse(); assertGetTransactionAndCommitCount(3); proxy.doSomething(); assertGetTransactionAndCommitCount(4); }
@Test public void testSerializationAdviceAndTargetNotSerializable() throws Exception { TestBean tb = new TestBean(); assertFalse(SerializationTestUtils.isSerializable(tb)); ProxyFactory pf = new ProxyFactory(tb); pf.addAdvice(new NopInterceptor()); ITestBean proxy = (ITestBean) createAopProxy(pf).getProxy(); assertFalse(SerializationTestUtils.isSerializable(proxy)); }
@Test public void testSerializationAdviceNotSerializable() throws Exception { SerializablePerson sp = new SerializablePerson(); assertTrue(SerializationTestUtils.isSerializable(sp)); ProxyFactory pf = new ProxyFactory(sp); // This isn't serializable Advice i = new NopInterceptor(); pf.addAdvice(i); assertFalse(SerializationTestUtils.isSerializable(i)); Object proxy = createAopProxy(pf).getProxy(); assertFalse(SerializationTestUtils.isSerializable(proxy)); }