private Object applyChannelInterceptor(
     Object bean, DirectChannelMetrics interceptor, ClassLoader beanClassLoader) {
   NameMatchMethodPointcutAdvisor channelsAdvice = new NameMatchMethodPointcutAdvisor(interceptor);
   channelsAdvice.addMethodName("send");
   channelsAdvice.addMethodName("receive");
   return applyAdvice(bean, channelsAdvice, beanClassLoader);
 }
  @Test
  public void exportAdvisedChannel() throws Exception {

    DummyInterceptor interceptor = new DummyInterceptor();
    NameMatchMethodPointcutAdvisor advisor = new NameMatchMethodPointcutAdvisor(interceptor);
    advisor.addMethodName("send");

    ProxyFactory factory = new ProxyFactory(channel);
    factory.addAdvisor(advisor);
    MessageChannel advised = (MessageChannel) factory.getProxy();

    MessageChannel exported =
        (MessageChannel) mBeanExporter.postProcessAfterInitialization(advised, "test");
    exported.send(MessageBuilder.withPayload("test").build());
  }
 @Override
 protected void applyReceiveOnlyAdviceChain(Collection<Advice> chain) {
   if (AopUtils.isAopProxy(this.source)) {
     for (Advice advice : chain) {
       NameMatchMethodPointcutAdvisor sourceAdvice = new NameMatchMethodPointcutAdvisor(advice);
       sourceAdvice.addMethodName("receive");
       ((Advised) this.source).addAdvice(advice);
     }
   } else {
     ProxyFactory proxyFactory = new ProxyFactory(this.source);
     for (Advice advice : chain) {
       proxyFactory.addAdvice(advice);
     }
     this.source = (MessageSource<?>) proxyFactory.getProxy(getBeanClassLoader());
   }
 }
  public static void main(String[] args) {
    NameBean target = new NameBean();

    // create advisor
    NameMatchMethodPointcutAdvisor advisor = new NameMatchMethodPointcutAdvisor(new SimpleAdvice());
    advisor.addMethodName("foo");
    advisor.addMethodName("bar");

    // create the proxy
    ProxyFactory pf = new ProxyFactory();
    pf.setTarget(target);
    pf.addAdvisor(advisor);
    NameBean proxy = (NameBean) pf.getProxy();

    proxy.foo();
    proxy.foo(999);
    proxy.bar();
    proxy.yup();
  }
  @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);
      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;
  }
 private Object applySourceInterceptor(
     Object bean, SimpleMessageSourceMetrics interceptor, ClassLoader beanClassLoader) {
   NameMatchMethodPointcutAdvisor sourceAdvice = new NameMatchMethodPointcutAdvisor(interceptor);
   sourceAdvice.addMethodName("receive");
   return applyAdvice(bean, sourceAdvice, beanClassLoader);
 }
 private Object applyHandlerInterceptor(
     Object bean, SimpleMessageHandlerMetrics interceptor, ClassLoader beanClassLoader) {
   NameMatchMethodPointcutAdvisor handlerAdvice = new NameMatchMethodPointcutAdvisor(interceptor);
   handlerAdvice.addMethodName("handleMessage");
   return applyAdvice(bean, handlerAdvice, beanClassLoader);
 }