Search in sources :

Example 1 with NameMatchMethodPointcutAdvisor

use of org.springframework.aop.support.NameMatchMethodPointcutAdvisor in project spring-cloud-stream by spring-cloud.

the class DefaultPollableMessageSource method setSource.

public void setSource(MessageSource<?> source) {
    ProxyFactory pf = new ProxyFactory(source);
    class ReceiveAdvice implements MethodInterceptor {

        private final List<ChannelInterceptor> interceptors = new ArrayList<>();

        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
            Object result = invocation.proceed();
            if (result instanceof Message) {
                Message<?> received = (Message<?>) result;
                for (ChannelInterceptor interceptor : this.interceptors) {
                    received = interceptor.preSend(received, null);
                    if (received == null) {
                        return null;
                    }
                }
                return received;
            }
            return result;
        }
    }
    final ReceiveAdvice advice = new ReceiveAdvice();
    advice.interceptors.addAll(this.interceptors);
    NameMatchMethodPointcutAdvisor sourceAdvisor = new NameMatchMethodPointcutAdvisor(advice);
    sourceAdvisor.addMethodName("receive");
    pf.addAdvisor(sourceAdvisor);
    this.source = (MessageSource<?>) pf.getProxy();
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Message(org.springframework.messaging.Message) ProxyFactory(org.springframework.aop.framework.ProxyFactory) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) NameMatchMethodPointcutAdvisor(org.springframework.aop.support.NameMatchMethodPointcutAdvisor) ArrayList(java.util.ArrayList) List(java.util.List) MethodInvocation(org.aopalliance.intercept.MethodInvocation)

Example 2 with NameMatchMethodPointcutAdvisor

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

the class AbstractMethodAnnotationPostProcessor method postProcess.

@Override
public Object postProcess(Object bean, String beanName, Method method, List<Annotation> annotations) {
    if (this.beanAnnotationAware() && AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) {
        try {
            resolveTargetBeanFromMethodWithBeanAnnotation(method);
        } catch (NoSuchBeanDefinitionException e) {
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Skipping endpoint creation; " + e.getMessage() + "; perhaps due to some '@Conditional' annotation.");
            }
            return null;
        }
    }
    List<Advice> adviceChain = extractAdviceChain(beanName, annotations);
    MessageHandler handler = createHandler(bean, method, annotations);
    if (!CollectionUtils.isEmpty(adviceChain) && handler instanceof AbstractReplyProducingMessageHandler) {
        ((AbstractReplyProducingMessageHandler) handler).setAdviceChain(adviceChain);
    }
    if (handler instanceof Orderable) {
        Order orderAnnotation = AnnotationUtils.findAnnotation(method, Order.class);
        if (orderAnnotation != null) {
            ((Orderable) handler).setOrder(orderAnnotation.value());
        }
    }
    if (handler instanceof AbstractMessageProducingHandler || handler instanceof AbstractMessageRouter) {
        String sendTimeout = MessagingAnnotationUtils.resolveAttribute(annotations, "sendTimeout", String.class);
        if (sendTimeout != null) {
            Long value = Long.valueOf(this.beanFactory.resolveEmbeddedValue(sendTimeout));
            if (handler instanceof AbstractMessageProducingHandler) {
                ((AbstractMessageProducingHandler) handler).setSendTimeout(value);
            } else {
                ((AbstractMessageRouter) handler).setSendTimeout(value);
            }
        }
    }
    boolean handlerExists = false;
    if (this.beanAnnotationAware() && AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) {
        Object handlerBean = this.resolveTargetBeanFromMethodWithBeanAnnotation(method);
        handlerExists = handlerBean != null && handler == handlerBean;
    }
    if (!handlerExists) {
        String handlerBeanName = generateHandlerBeanName(beanName, method);
        if (handler instanceof ReplyProducingMessageHandlerWrapper && StringUtils.hasText(MessagingAnnotationUtils.endpointIdValue(method))) {
            handlerBeanName = handlerBeanName + ".wrapper";
        }
        this.beanFactory.registerSingleton(handlerBeanName, handler);
        handler = (MessageHandler) this.beanFactory.initializeBean(handler, handlerBeanName);
    }
    if (AnnotatedElementUtils.isAnnotated(method, IdempotentReceiver.class.getName()) && !AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) {
        String[] interceptors = AnnotationUtils.getAnnotation(method, IdempotentReceiver.class).value();
        for (String interceptor : interceptors) {
            DefaultBeanFactoryPointcutAdvisor advisor = new DefaultBeanFactoryPointcutAdvisor();
            advisor.setAdviceBeanName(interceptor);
            NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut();
            pointcut.setMappedName("handleMessage");
            advisor.setPointcut(pointcut);
            advisor.setBeanFactory(this.beanFactory);
            if (handler instanceof Advised) {
                ((Advised) handler).addAdvisor(advisor);
            } else {
                ProxyFactory proxyFactory = new ProxyFactory(handler);
                proxyFactory.addAdvisor(advisor);
                handler = (MessageHandler) proxyFactory.getProxy(this.beanFactory.getBeanClassLoader());
            }
        }
    }
    if (!CollectionUtils.isEmpty(adviceChain)) {
        for (Advice advice : adviceChain) {
            if (advice instanceof HandleMessageAdvice) {
                NameMatchMethodPointcutAdvisor handlerAdvice = new NameMatchMethodPointcutAdvisor(advice);
                handlerAdvice.addMethodName("handleMessage");
                if (handler instanceof Advised) {
                    ((Advised) handler).addAdvisor(handlerAdvice);
                } else {
                    ProxyFactory proxyFactory = new ProxyFactory(handler);
                    proxyFactory.addAdvisor(handlerAdvice);
                    handler = (MessageHandler) proxyFactory.getProxy(this.beanFactory.getBeanClassLoader());
                }
            }
        }
    }
    AbstractEndpoint endpoint = createEndpoint(handler, method, annotations);
    if (endpoint != null) {
        return endpoint;
    }
    return handler;
}
Also used : Order(org.springframework.core.annotation.Order) AbstractEndpoint(org.springframework.integration.endpoint.AbstractEndpoint) HandleMessageAdvice(org.springframework.integration.handler.advice.HandleMessageAdvice) MessageHandler(org.springframework.messaging.MessageHandler) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) ProxyFactory(org.springframework.aop.framework.ProxyFactory) AbstractMessageRouter(org.springframework.integration.router.AbstractMessageRouter) Orderable(org.springframework.integration.context.Orderable) Bean(org.springframework.context.annotation.Bean) DefaultBeanFactoryPointcutAdvisor(org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor) Advised(org.springframework.aop.framework.Advised) NameMatchMethodPointcutAdvisor(org.springframework.aop.support.NameMatchMethodPointcutAdvisor) AbstractMessageProducingHandler(org.springframework.integration.handler.AbstractMessageProducingHandler) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) HandleMessageAdvice(org.springframework.integration.handler.advice.HandleMessageAdvice) Advice(org.aopalliance.aop.Advice) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) IdempotentReceiver(org.springframework.integration.annotation.IdempotentReceiver) ReplyProducingMessageHandlerWrapper(org.springframework.integration.handler.ReplyProducingMessageHandlerWrapper) NameMatchMethodPointcut(org.springframework.aop.support.NameMatchMethodPointcut)

Example 3 with NameMatchMethodPointcutAdvisor

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

the class SourcePollingChannelAdapter method adviceToReceiveAdvisor.

private NameMatchMethodPointcutAdvisor adviceToReceiveAdvisor(Advice advice) {
    NameMatchMethodPointcutAdvisor sourceAdvisor = new NameMatchMethodPointcutAdvisor(advice);
    sourceAdvisor.addMethodName("receive");
    return sourceAdvisor;
}
Also used : NameMatchMethodPointcutAdvisor(org.springframework.aop.support.NameMatchMethodPointcutAdvisor)

Example 4 with NameMatchMethodPointcutAdvisor

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

the class ConsumerEndpointFactoryBean method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    if (this.beanName == null) {
        logger.error("The MessageHandler [" + this.handler + "] will be created without a 'componentName'. " + "Consider specifying the 'beanName' property on this ConsumerEndpointFactoryBean.");
    } else {
        try {
            if (!this.beanName.startsWith("org.springframework")) {
                MessageHandler targetHandler = this.handler;
                if (AopUtils.isAopProxy(targetHandler)) {
                    Object target = ((Advised) targetHandler).getTargetSource().getTarget();
                    if (target instanceof MessageHandler) {
                        targetHandler = (MessageHandler) target;
                    }
                }
                if (targetHandler instanceof IntegrationObjectSupport) {
                    ((IntegrationObjectSupport) targetHandler).setComponentName(this.beanName);
                }
            }
        } catch (Exception e) {
            if (logger.isDebugEnabled()) {
                logger.debug("Could not set component name for handler " + this.handler + " for " + this.beanName + " :" + e.getMessage());
            }
        }
    }
    if (!CollectionUtils.isEmpty(this.adviceChain)) {
        /*
			 *  ARPMHs advise the handleRequestMessage method internally and already have the advice chain injected.
			 *  So we only advise handlers that are not reply-producing.
			 *  Or if one (or more) of advices is IdempotentReceiverInterceptor.
			 *  If the handler is already advised,
			 *  add the configured advices to its chain, otherwise create a proxy.
			 */
        Class<?> targetClass = AopUtils.getTargetClass(this.handler);
        boolean replyMessageHandler = AbstractReplyProducingMessageHandler.class.isAssignableFrom(targetClass);
        for (Advice advice : this.adviceChain) {
            if (!replyMessageHandler || advice instanceof HandleMessageAdvice) {
                NameMatchMethodPointcutAdvisor handlerAdvice = new NameMatchMethodPointcutAdvisor(advice);
                handlerAdvice.addMethodName("handleMessage");
                if (this.handler instanceof Advised) {
                    ((Advised) this.handler).addAdvisor(handlerAdvice);
                } else {
                    ProxyFactory proxyFactory = new ProxyFactory(this.handler);
                    proxyFactory.addAdvisor(handlerAdvice);
                    this.handler = (MessageHandler) proxyFactory.getProxy(this.beanClassLoader);
                }
            }
        }
    }
    if (this.channelResolver == null) {
        this.channelResolver = new BeanFactoryMessageChannelDestinationResolver(this.beanFactory);
    }
    initializeEndpoint();
}
Also used : HandleMessageAdvice(org.springframework.integration.handler.advice.HandleMessageAdvice) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MessageHandler(org.springframework.messaging.MessageHandler) IntegrationObjectSupport(org.springframework.integration.context.IntegrationObjectSupport) Advised(org.springframework.aop.framework.Advised) ProxyFactory(org.springframework.aop.framework.ProxyFactory) BeanFactoryMessageChannelDestinationResolver(org.springframework.messaging.core.BeanFactoryMessageChannelDestinationResolver) NameMatchMethodPointcutAdvisor(org.springframework.aop.support.NameMatchMethodPointcutAdvisor) Advice(org.aopalliance.aop.Advice) HandleMessageAdvice(org.springframework.integration.handler.advice.HandleMessageAdvice)

Example 5 with NameMatchMethodPointcutAdvisor

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

the class MessageMetricsAdviceTests method exportAdvisedHandler.

@Test
public void exportAdvisedHandler() throws Exception {
    DummyInterceptor interceptor = new DummyInterceptor();
    NameMatchMethodPointcutAdvisor advisor = new NameMatchMethodPointcutAdvisor(interceptor);
    advisor.addMethodName("handleMessage");
    ProxyFactory factory = new ProxyFactory(this.handler);
    factory.addAdvisor(advisor);
    MessageHandler advised = (MessageHandler) factory.getProxy();
    this.beanFactory.registerSingleton("test", advised);
    this.beanFactory.initializeBean(advised, "test");
    mBeanExporter.afterSingletonsInstantiated();
    MessageHandler exported = this.beanFactory.getBean("test", MessageHandler.class);
    exported.handleMessage(MessageBuilder.withPayload("test").build());
}
Also used : MessageHandler(org.springframework.messaging.MessageHandler) ProxyFactory(org.springframework.aop.framework.ProxyFactory) NameMatchMethodPointcutAdvisor(org.springframework.aop.support.NameMatchMethodPointcutAdvisor) Test(org.junit.Test)

Aggregations

NameMatchMethodPointcutAdvisor (org.springframework.aop.support.NameMatchMethodPointcutAdvisor)6 ProxyFactory (org.springframework.aop.framework.ProxyFactory)5 MessageHandler (org.springframework.messaging.MessageHandler)3 Advice (org.aopalliance.aop.Advice)2 Test (org.junit.Test)2 Advised (org.springframework.aop.framework.Advised)2 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)2 HandleMessageAdvice (org.springframework.integration.handler.advice.HandleMessageAdvice)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)1 MethodInvocation (org.aopalliance.intercept.MethodInvocation)1 DefaultBeanFactoryPointcutAdvisor (org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor)1 NameMatchMethodPointcut (org.springframework.aop.support.NameMatchMethodPointcut)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 Bean (org.springframework.context.annotation.Bean)1 Order (org.springframework.core.annotation.Order)1 IdempotentReceiver (org.springframework.integration.annotation.IdempotentReceiver)1 IntegrationObjectSupport (org.springframework.integration.context.IntegrationObjectSupport)1 Orderable (org.springframework.integration.context.Orderable)1