Search in sources :

Example 1 with DefaultBeanFactoryPointcutAdvisor

use of org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor 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 2 with DefaultBeanFactoryPointcutAdvisor

use of org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor 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 3 with DefaultBeanFactoryPointcutAdvisor

use of org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor 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)

Aggregations

DefaultBeanFactoryPointcutAdvisor (org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Advisor (org.springframework.aop.Advisor)2 NameMatchMethodPointcut (org.springframework.aop.support.NameMatchMethodPointcut)2 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Set (java.util.Set)1 Advice (org.aopalliance.aop.Advice)1 Advised (org.springframework.aop.framework.Advised)1 ProxyFactory (org.springframework.aop.framework.ProxyFactory)1 NameMatchMethodPointcutAdvisor (org.springframework.aop.support.NameMatchMethodPointcutAdvisor)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 Orderable (org.springframework.integration.context.Orderable)1 AbstractEndpoint (org.springframework.integration.endpoint.AbstractEndpoint)1 AbstractMessageProducingHandler (org.springframework.integration.handler.AbstractMessageProducingHandler)1 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)1