Java Code Examples for org.springframework.aop.framework.ProxyFactory#addAdvisor()

The following examples show how to use org.springframework.aop.framework.ProxyFactory#addAdvisor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ACLEntryVoterTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testBasicDenyChildAssocNode() throws Exception
{
    runAs("andy");

    Object o = new ClassWithMethods();
    Method method = o.getClass().getMethod("testOneChildAssociationRef", new Class[] { ChildAssociationRef.class });

    AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance();

    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.addAdvisor(advisorAdapterRegistry.wrap(new Interceptor("ACL_NODE.0.sys:base.Read")));

    proxyFactory.setTargetSource(new SingletonTargetSource(o));

    Object proxy = proxyFactory.getProxy();

    try
    {
        method.invoke(proxy, new Object[] { nodeService.getPrimaryParent(rootNodeRef) });
        assertNotNull(null);
    }
    catch (InvocationTargetException e)
    {

    }
}
 
Example 2
Source File: DelegatingIntroductionInterceptorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testIntroductionInterceptorWithSuperInterface() throws Exception {
	TestBean raw = new TestBean();
	assertTrue(! (raw instanceof TimeStamped));
	ProxyFactory factory = new ProxyFactory(raw);

	TimeStamped ts = mock(SubTimeStamped.class);
	long timestamp = 111L;
	given(ts.getTimeStamp()).willReturn(timestamp);

	factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts), TimeStamped.class));

	TimeStamped tsp = (TimeStamped) factory.getProxy();
	assertTrue(!(tsp instanceof SubTimeStamped));
	assertTrue(tsp.getTimeStamp() == timestamp);
}
 
Example 3
Source File: DelegatingIntroductionInterceptorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testSerializableDelegatingIntroductionInterceptorSerializable() throws Exception {
	SerializablePerson serializableTarget = new SerializablePerson();
	String name = "Tony";
	serializableTarget.setName("Tony");

	ProxyFactory factory = new ProxyFactory(serializableTarget);
	factory.addInterface(Person.class);
	long time = 1000;
	TimeStamped ts = new SerializableTimeStamped(time);

	factory.addAdvisor(new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts)));
	factory.addAdvice(new SerializableNopInterceptor());

	Person p = (Person) factory.getProxy();

	assertEquals(name, p.getName());
	assertEquals(time, ((TimeStamped) p).getTimeStamp());

	Person p1 = (Person) SerializationTestUtils.serializeAndDeserialize(p);
	assertEquals(name, p1.getName());
	assertEquals(time, ((TimeStamped) p1).getTimeStamp());
}
 
Example 4
Source File: GroovyAspectTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private void testAdvice(Advisor advisor, LogUserAdvice logAdvice, TestService target, String message,
		boolean proxyTargetClass) throws Exception {

	logAdvice.reset();

	ProxyFactory factory = new ProxyFactory(target);
	factory.setProxyTargetClass(proxyTargetClass);
	factory.addAdvisor(advisor);
	TestService bean = (TestService) factory.getProxy();

	assertEquals(0, logAdvice.getCountThrows());
	try {
		bean.sayHello();
		fail("Expected exception");
	}
	catch (TestException ex) {
		assertEquals(message, ex.getMessage());
	}
	assertEquals(1, logAdvice.getCountThrows());
}
 
Example 5
Source File: ControlFlowPointcutTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testMatches() {
	TestBean target = new TestBean();
	target.setAge(27);
	NopInterceptor nop = new NopInterceptor();
	ControlFlowPointcut cflow = new ControlFlowPointcut(One.class, "getAge");
	ProxyFactory pf = new ProxyFactory(target);
	ITestBean proxied = (ITestBean) pf.getProxy();
	pf.addAdvisor(new DefaultPointcutAdvisor(cflow, nop));

	// Not advised, not under One
	assertEquals(target.getAge(), proxied.getAge());
	assertEquals(0, nop.getCount());

	// Will be advised
	assertEquals(target.getAge(), new One().getAge(proxied));
	assertEquals(1, nop.getCount());

	// Won't be advised
	assertEquals(target.getAge(), new One().nomatch(proxied));
	assertEquals(1, nop.getCount());
	assertEquals(3, cflow.getEvaluations());
}
 
Example 6
Source File: DelegatingIntroductionInterceptorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testIntroductionMasksTargetImplementation() throws Exception {
	final long t = 1001L;
	@SuppressWarnings("serial")
	class TestII extends DelegatingIntroductionInterceptor implements TimeStamped {
		@Override
		public long getTimeStamp() {
			return t;
		}
	}

	DelegatingIntroductionInterceptor ii = new TestII();

	// != t
	TestBean target = new TargetClass(t + 1);

	ProxyFactory pf = new ProxyFactory(target);
	pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii));

	TimeStamped ts = (TimeStamped) pf.getProxy();
	// From introduction interceptor, not target
	assertTrue(ts.getTimeStamp() == t);
}
 
Example 7
Source File: ACLEntryAfterInvocationTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testBasicAllowNullNode() throws Exception
{
    runAs("andy");

    Object o = new ClassWithMethods();
    Method method = o.getClass().getMethod("echoNodeRef", new Class[] { NodeRef.class });

    AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance();

    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.addAdvisor(advisorAdapterRegistry.wrap(new Interceptor("AFTER_ACL_NODE.sys:base.Read")));
    proxyFactory.setTargetSource(new SingletonTargetSource(o));
    Object proxy = proxyFactory.getProxy();

    Object answer = method.invoke(proxy, new Object[] { null });
    assertNull(answer);
}
 
Example 8
Source File: ACLEntryVoterTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testBasicDenyStore() throws Exception
{
    runAs("andy");

    Object o = new ClassWithMethods();
    Method method = o.getClass().getMethod("testOneStoreRef", new Class[] { StoreRef.class });

    AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance();

    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.addAdvisor(advisorAdapterRegistry.wrap(new Interceptor("ACL_NODE.0.sys:base.Read")));

    proxyFactory.setTargetSource(new SingletonTargetSource(o));

    Object proxy = proxyFactory.getProxy();

    try
    {
        method.invoke(proxy, new Object[] { rootNodeRef.getStoreRef() });
        assertNotNull(null);
    }
    catch (InvocationTargetException e)
    {

    }

}
 
Example 9
Source File: MethodValidationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 10
Source File: DelegatingIntroductionInterceptorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutomaticInterfaceRecognitionInDelegate() throws Exception {
	final long t = 1001L;
	class Tester implements TimeStamped, ITester {
		@Override
		public void foo() throws Exception {
		}
		@Override
		public long getTimeStamp() {
			return t;
		}
	}

	DelegatingIntroductionInterceptor ii = new DelegatingIntroductionInterceptor(new Tester());

	TestBean target = new TestBean();

	ProxyFactory pf = new ProxyFactory(target);
	pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii));

	//assertTrue(Arrays.binarySearch(pf.getProxiedInterfaces(), TimeStamped.class) != -1);
	TimeStamped ts = (TimeStamped) pf.getProxy();

	assertTrue(ts.getTimeStamp() == t);
	((ITester) ts).foo();

	((ITestBean) ts).getAge();
}
 
Example 11
Source File: RmiProxyFactoryBean.java    From jdal with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void afterPropertiesSet() {
	super.afterPropertiesSet();
	
	if (getServiceInterface() == null) {
		throw new IllegalArgumentException("Property 'serviceInterface' is required");
	}
	ProxyFactory pf = new ProxyFactory(new Class[] { getServiceInterface(), RemoteClient.class });
	pf.addAdvisor(new RemoteClientAdvisor(getRemoteReference()));
	pf.addAdvice(this);
	serviceProxy = pf.getProxy();
}
 
Example 12
Source File: AspectJExpressionPointcutTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private TestBean getAdvisedProxy(String pointcutExpression, CallCountingInterceptor interceptor) {
	TestBean target = new TestBean();

	Pointcut pointcut = getPointcut(pointcutExpression);

	DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
	advisor.setAdvice(interceptor);
	advisor.setPointcut(pointcut);

	ProxyFactory pf = new ProxyFactory();
	pf.setTarget(target);
	pf.addAdvisor(advisor);

	return (TestBean) pf.getProxy();
}
 
Example 13
Source File: DelegatingIntroductionInterceptorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testIntroductionInterceptorWithDelegation() throws Exception {
	TestBean raw = new TestBean();
	assertTrue(! (raw instanceof TimeStamped));
	ProxyFactory factory = new ProxyFactory(raw);

	TimeStamped ts = mock(TimeStamped.class);
	long timestamp = 111L;
	given(ts.getTimeStamp()).willReturn(timestamp);

	factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts)));

	TimeStamped tsp = (TimeStamped) factory.getProxy();
	assertTrue(tsp.getTimeStamp() == timestamp);
}
 
Example 14
Source File: BurlapProxyFactoryBean.java    From jdal with Apache License 2.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() {
	super.afterPropertiesSet();
	ProxyFactory pf = new ProxyFactory(new Class[] {getServiceInterface(), RemoteClient.class});
	pf.addAdvisor(new RemoteClientAdvisor(getRemoteReference()));
	pf.addAdvice(this);
	this.serviceProxy = pf.getProxy(getBeanClassLoader());
}
 
Example 15
Source File: DelegatingIntroductionInterceptorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testIntroductionInterceptorWithInterfaceHierarchy() throws Exception {
	TestBean raw = new TestBean();
	assertTrue(! (raw instanceof SubTimeStamped));
	ProxyFactory factory = new ProxyFactory(raw);

	TimeStamped ts = mock(SubTimeStamped.class);
	long timestamp = 111L;
	given(ts.getTimeStamp()).willReturn(timestamp);

	factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts), SubTimeStamped.class));

	SubTimeStamped tsp = (SubTimeStamped) factory.getProxy();
	assertTrue(tsp.getTimeStamp() == timestamp);
}
 
Example 16
Source File: ACLEntryVoterTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void testBasicAllowChildAssocNode() throws Exception
{
    runAs("andy");

    permissionService.setPermission(new SimplePermissionEntry(rootNodeRef, getPermission(PermissionService.READ),
            "andy", AccessStatus.ALLOWED));

    Object o = new ClassWithMethods();
    Method method = o.getClass().getMethod("testOneChildAssociationRef", new Class[] { ChildAssociationRef.class });

    AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance();

    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.addAdvisor(advisorAdapterRegistry.wrap(new Interceptor("ACL_NODE.0.sys:base.Read")));

    proxyFactory.setTargetSource(new SingletonTargetSource(o));

    Object proxy = proxyFactory.getProxy();

    method.invoke(proxy, new Object[] { nodeService.getPrimaryParent(rootNodeRef) });
}
 
Example 17
Source File: PersistenceExceptionTranslationAdvisorTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
protected void addPersistenceExceptionTranslation(ProxyFactory pf, PersistenceExceptionTranslator pet) {
	pf.addAdvisor(new PersistenceExceptionTranslationAdvisor(pet, Repository.class));
}
 
Example 18
Source File: ACLEntryVoterTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void testAllowNullParent() throws Exception
{
    runAs("andy");

    Object o = new ClassWithMethods();
    Method method = o.getClass().getMethod("testOneChildAssociationRef", new Class[] { ChildAssociationRef.class });

    AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance();

    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.addAdvisor(advisorAdapterRegistry.wrap(new Interceptor("ACL_PARENT.0.sys:base.Read")));

    proxyFactory.setTargetSource(new SingletonTargetSource(o));

    Object proxy = proxyFactory.getProxy();

    method.invoke(proxy, new Object[] { null });

}
 
Example 19
Source File: MethodInvocationProceedingJoinPointTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void testCanGetMethodSignatureFromJoinPoint() {
	final Object raw = new TestBean();
	// Will be set by advice during a method call
	final int newAge = 23;

	ProxyFactory pf = new ProxyFactory(raw);
	pf.setExposeProxy(true);
	pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
	pf.addAdvice(new MethodBeforeAdvice() {
		private int depth;

		@Override
		public void before(Method method, Object[] args, @Nullable Object target) throws Throwable {
			JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
			assertTrue("Method named in toString", jp.toString().contains(method.getName()));
			// Ensure that these don't cause problems
			jp.toShortString();
			jp.toLongString();

			assertSame(target, AbstractAspectJAdvice.currentJoinPoint().getTarget());
			assertFalse(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getTarget()));

			ITestBean thisProxy = (ITestBean) AbstractAspectJAdvice.currentJoinPoint().getThis();
			assertTrue(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getThis()));

			assertNotSame(target, thisProxy);

			// Check getting again doesn't cause a problem
			assertSame(thisProxy, AbstractAspectJAdvice.currentJoinPoint().getThis());

			// Try reentrant call--will go through this advice.
			// Be sure to increment depth to avoid infinite recursion
			if (depth++ == 0) {
				// Check that toString doesn't cause a problem
				thisProxy.toString();
				// Change age, so this will be returned by invocation
				thisProxy.setAge(newAge);
				assertEquals(newAge, thisProxy.getAge());
			}

			assertSame(AopContext.currentProxy(), thisProxy);
			assertSame(target, raw);

			assertSame(method.getName(), AbstractAspectJAdvice.currentJoinPoint().getSignature().getName());
			assertEquals(method.getModifiers(), AbstractAspectJAdvice.currentJoinPoint().getSignature().getModifiers());

			MethodSignature msig = (MethodSignature) AbstractAspectJAdvice.currentJoinPoint().getSignature();
			assertSame("Return same MethodSignature repeatedly", msig, AbstractAspectJAdvice.currentJoinPoint().getSignature());
			assertSame("Return same JoinPoint repeatedly", AbstractAspectJAdvice.currentJoinPoint(), AbstractAspectJAdvice.currentJoinPoint());
			assertEquals(method.getDeclaringClass(), msig.getDeclaringType());
			assertTrue(Arrays.equals(method.getParameterTypes(), msig.getParameterTypes()));
			assertEquals(method.getReturnType(), msig.getReturnType());
			assertTrue(Arrays.equals(method.getExceptionTypes(), msig.getExceptionTypes()));
			msig.toLongString();
			msig.toShortString();
		}
	});
	ITestBean itb = (ITestBean) pf.getProxy();
	// Any call will do
	assertEquals("Advice reentrantly set age", newAge, itb.getAge());
}
 
Example 20
Source File: PersistenceExceptionTranslationAdvisorTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
protected void addPersistenceExceptionTranslation(ProxyFactory pf, PersistenceExceptionTranslator pet) {
	pf.addAdvisor(new PersistenceExceptionTranslationAdvisor(pet, Repository.class));
}