Search in sources :

Example 11 with Advisor

use of org.springframework.aop.Advisor in project spring-integration by spring-projects.

the class PollingTransactionTests method transactionWithCommitAndAdvices.

@Test
@SuppressWarnings("unchecked")
public void transactionWithCommitAndAdvices() throws InterruptedException {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("transactionTests.xml", this.getClass());
    PollingConsumer advicedPoller = context.getBean("advicedSa", PollingConsumer.class);
    List<Advice> adviceChain = TestUtils.getPropertyValue(advicedPoller, "adviceChain", List.class);
    assertEquals(4, adviceChain.size());
    Runnable poller = TestUtils.getPropertyValue(advicedPoller, "poller", Runnable.class);
    Callable<?> pollingTask = TestUtils.getPropertyValue(poller, "pollingTask", Callable.class);
    assertTrue("Poller is not Advised", pollingTask instanceof Advised);
    Advisor[] advisors = ((Advised) pollingTask).getAdvisors();
    assertEquals(4, advisors.length);
    assertThat("First advisor is not TX", advisors[0].getAdvice(), instanceOf(TransactionInterceptor.class));
    TestTransactionManager txManager = (TestTransactionManager) context.getBean("txManager");
    MessageChannel input = (MessageChannel) context.getBean("goodInputWithAdvice");
    PollableChannel output = (PollableChannel) context.getBean("output");
    assertEquals(0, txManager.getCommitCount());
    assertEquals(0, txManager.getRollbackCount());
    input.send(new GenericMessage<>("test"));
    txManager.waitForCompletion(10000);
    Message<?> message = output.receive(0);
    assertNotNull(message);
    assertEquals(0, txManager.getRollbackCount());
    context.close();
}
Also used : PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) Advisor(org.springframework.aop.Advisor) TransactionInterceptor(org.springframework.transaction.interceptor.TransactionInterceptor) MessageChannel(org.springframework.messaging.MessageChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Advised(org.springframework.aop.framework.Advised) PollableChannel(org.springframework.messaging.PollableChannel) Advice(org.aopalliance.aop.Advice) TestTransactionManager(org.springframework.integration.util.TestTransactionManager) Test(org.junit.Test)

Example 12 with Advisor

use of org.springframework.aop.Advisor in project spring-integration by spring-projects.

the class IdempotentReceiverAutoProxyCreator method getAdvicesAndAdvisorsForBean.

@Override
protected Object[] getAdvicesAndAdvisorsForBean(Class<?> beanClass, String beanName, TargetSource customTargetSource) throws BeansException {
    initIdempotentEndpointsIfNecessary();
    if (MessageHandler.class.isAssignableFrom(beanClass)) {
        List<Advisor> interceptors = new ArrayList<Advisor>();
        for (Map.Entry<String, List<String>> entry : this.idempotentEndpoints.entrySet()) {
            List<String> mappedNames = entry.getValue();
            for (String mappedName : mappedNames) {
                if (isMatch(mappedName, beanName)) {
                    DefaultBeanFactoryPointcutAdvisor idempotentReceiverInterceptor = new DefaultBeanFactoryPointcutAdvisor();
                    idempotentReceiverInterceptor.setAdviceBeanName(entry.getKey());
                    NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut();
                    pointcut.setMappedName("handleMessage");
                    idempotentReceiverInterceptor.setPointcut(pointcut);
                    idempotentReceiverInterceptor.setBeanFactory(getBeanFactory());
                    interceptors.add(idempotentReceiverInterceptor);
                }
            }
        }
        if (!interceptors.isEmpty()) {
            return interceptors.toArray();
        }
    }
    return DO_NOT_PROXY;
}
Also used : DefaultBeanFactoryPointcutAdvisor(org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor) ArrayList(java.util.ArrayList) DefaultBeanFactoryPointcutAdvisor(org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor) Advisor(org.springframework.aop.Advisor) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) NameMatchMethodPointcut(org.springframework.aop.support.NameMatchMethodPointcut)

Example 13 with Advisor

use of org.springframework.aop.Advisor in project spring-integration by spring-projects.

the class ChannelSecurityInterceptorBeanPostProcessor method getAdvicesAndAdvisorsForBean.

@Override
protected Object[] getAdvicesAndAdvisorsForBean(Class<?> beanClass, String beanName, TargetSource customTargetSource) throws BeansException {
    if (MessageChannel.class.isAssignableFrom(beanClass)) {
        List<Advisor> interceptors = new ArrayList<Advisor>();
        for (Map.Entry<String, Set<Pattern>> entry : this.securityInterceptorMappings.entrySet()) {
            if (isMatch(beanName, entry.getValue())) {
                DefaultBeanFactoryPointcutAdvisor channelSecurityInterceptor = new DefaultBeanFactoryPointcutAdvisor();
                channelSecurityInterceptor.setAdviceBeanName(entry.getKey());
                channelSecurityInterceptor.setBeanFactory(getBeanFactory());
                interceptors.add(channelSecurityInterceptor);
            }
        }
        if (!interceptors.isEmpty()) {
            return interceptors.toArray();
        }
    }
    return DO_NOT_PROXY;
}
Also used : Set(java.util.Set) DefaultBeanFactoryPointcutAdvisor(org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor) ArrayList(java.util.ArrayList) DefaultBeanFactoryPointcutAdvisor(org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor) Advisor(org.springframework.aop.Advisor) Map(java.util.Map)

Example 14 with Advisor

use of org.springframework.aop.Advisor in project spring-integration by spring-projects.

the class SecuredChannelsParserTests method testAdminOrUserRequiredForReceive.

@Test
public void testAdminOrUserRequiredForReceive() {
    String beanName = "adminOrUserRequiredForReceive";
    messageChannel.setBeanName(beanName);
    MessageChannel proxy = (MessageChannel) applicationContext.getAutowireCapableBeanFactory().applyBeanPostProcessorsAfterInitialization(messageChannel, beanName);
    assertTrue("Channel was not proxied", AopUtils.isAopProxy(proxy));
    Advisor[] advisors = ((Advised) proxy).getAdvisors();
    assertEquals("Wrong number of interceptors", 1, advisors.length);
    ChannelSecurityInterceptor interceptor = (ChannelSecurityInterceptor) advisors[0].getAdvice();
    ChannelAccessPolicy policy = this.retrievePolicyForPatternString(beanName, interceptor);
    assertNotNull("Pattern '" + beanName + "' is not included in mappings", policy);
    Collection<ConfigAttribute> sendDefinition = policy.getConfigAttributesForSend();
    Collection<ConfigAttribute> receiveDefinition = policy.getConfigAttributesForReceive();
    Collection<String> receiveRoles = this.getRolesFromDefintion(receiveDefinition);
    assertTrue("ROLE_ADMIN not found as receive attribute", receiveRoles.contains("ROLE_ADMIN"));
    assertTrue("ROLE_USER not found as receive attribute", receiveRoles.contains("ROLE_USER"));
    assertTrue("Policy applies to receive", sendDefinition.size() == 0);
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) ChannelAccessPolicy(org.springframework.integration.security.channel.ChannelAccessPolicy) Advised(org.springframework.aop.framework.Advised) ConfigAttribute(org.springframework.security.access.ConfigAttribute) Advisor(org.springframework.aop.Advisor) ChannelSecurityInterceptor(org.springframework.integration.security.channel.ChannelSecurityInterceptor) Test(org.junit.Test)

Example 15 with Advisor

use of org.springframework.aop.Advisor in project spring-integration by spring-projects.

the class SecuredChannelsParserTests method testAdminRequiredForReceive.

@Test
public void testAdminRequiredForReceive() {
    String beanName = "adminRequiredForReceive";
    messageChannel.setBeanName(beanName);
    MessageChannel proxy = (MessageChannel) applicationContext.getAutowireCapableBeanFactory().applyBeanPostProcessorsAfterInitialization(messageChannel, beanName);
    assertTrue("Channel was not proxied", AopUtils.isAopProxy(proxy));
    Advisor[] advisors = ((Advised) proxy).getAdvisors();
    assertEquals("Wrong number of interceptors", 1, advisors.length);
    ChannelSecurityInterceptor interceptor = (ChannelSecurityInterceptor) advisors[0].getAdvice();
    ChannelAccessPolicy policy = this.retrievePolicyForPatternString(beanName, interceptor);
    assertNotNull("Pattern '" + beanName + "' is not included in mappings", policy);
    Collection<ConfigAttribute> sendDefinition = policy.getConfigAttributesForSend();
    Collection<ConfigAttribute> receiveDefinition = policy.getConfigAttributesForReceive();
    Collection<String> receiveRoles = this.getRolesFromDefintion(receiveDefinition);
    assertTrue("ROLE_ADMIN not found as receive attribute", receiveRoles.contains("ROLE_ADMIN"));
    assertTrue("Policy applies to receive", sendDefinition.size() == 0);
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) ChannelAccessPolicy(org.springframework.integration.security.channel.ChannelAccessPolicy) Advised(org.springframework.aop.framework.Advised) ConfigAttribute(org.springframework.security.access.ConfigAttribute) Advisor(org.springframework.aop.Advisor) ChannelSecurityInterceptor(org.springframework.integration.security.channel.ChannelSecurityInterceptor) Test(org.junit.Test)

Aggregations

Advisor (org.springframework.aop.Advisor)70 Test (org.junit.jupiter.api.Test)33 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)25 Advised (org.springframework.aop.framework.Advised)21 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)19 Test (org.junit.Test)16 TestBean (org.springframework.beans.testfixture.beans.TestBean)16 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)14 AspectJPointcutAdvisor (org.springframework.aop.aspectj.AspectJPointcutAdvisor)11 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)11 ArrayList (java.util.ArrayList)10 JoinPoint (org.aspectj.lang.JoinPoint)8 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)8 Method (java.lang.reflect.Method)7 SyntheticInstantiationAdvisor (org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor)7 StaticMethodMatcherPointcutAdvisor (org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor)7 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)7 LockMixinAdvisor (test.mixin.LockMixinAdvisor)7 Advice (org.aopalliance.aop.Advice)6 CountingBeforeAdvice (org.springframework.aop.testfixture.advice.CountingBeforeAdvice)6