Example usage for org.springframework.aop.support AopUtils canApply

List of usage examples for org.springframework.aop.support AopUtils canApply

Introduction

In this page you can find the example usage for org.springframework.aop.support AopUtils canApply.

Prototype

public static boolean canApply(Advisor advisor, Class<?> targetClass) 

Source Link

Document

Can the given advisor apply at all on the given class?

Usage

From source file:com.ryantenney.metrics.spring.AdvisingBeanPostProcessor.java

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
    if (bean instanceof AopInfrastructureBean) {
        return bean;
    }/*from  w  w w . jav a2  s . c o  m*/

    final Class<?> targetClass = AopUtils.getTargetClass(bean);

    if (AopUtils.canApply(pointcut, targetClass)) {
        final Advice advice = adviceFactory.getAdvice(bean, targetClass);
        final Advisor advisor = new DefaultPointcutAdvisor(pointcut, advice);

        if (bean instanceof Advised) {
            LOG.debug("Bean {} is already proxied, adding Advisor to existing proxy", beanName);

            ((Advised) bean).addAdvisor(0, advisor);

            return bean;
        } else {
            LOG.debug("Proxying bean {} of type {}", beanName, targetClass.getCanonicalName());

            final ProxyFactory proxyFactory = new ProxyFactory(bean);
            if (proxyConfig != null) {
                proxyFactory.copyFrom(proxyConfig);
            }
            proxyFactory.addAdvisor(advisor);

            final Object proxy = proxyFactory.getProxy(this.beanClassLoader);

            return proxy;
        }
    }

    return bean;
}

From source file:org.pshow.ecm.security.MethodAuditPostProcessor.java

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if (bean instanceof AopInfrastructureBean) {
        // Ignore AOP infrastructure such as scoped proxies.
        return bean;
    }/*www .j  a v  a2  s.  c  om*/
    Class<?> targetClass = AopUtils.getTargetClass(bean);
    if (AopUtils.canApply(this.advisor, targetClass)) {
        if (bean instanceof Advised) {
            ((Advised) bean).addAdvisor(this.advisor);
            return bean;
        } else {
            ProxyFactory proxyFactory = new ProxyFactory(bean);
            // Copy our properties (proxyTargetClass etc) inherited from
            // ProxyConfig.
            proxyFactory.copyFrom(this);
            proxyFactory.addAdvisor(this.advisor);
            return proxyFactory.getProxy(this.beanClassLoader);
        }
    } else {
        // This is not a repository.
        return bean;
    }
}

From source file:prospring3.ch5.beanpostprocessor.DeassociateBeanPostProcessor.java

public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if (bean instanceof AopInfrastructureBean) {
        // Ignore AOP infrastructure such as scoped proxies.
        return bean;
    }//from  w ww.ja  va2s. com
    Class<?> targetClass = AopUtils.getTargetClass(bean);
    if (AopUtils.canApply(this.deassociatePointcutAdvisor, targetClass)) {
        if (bean instanceof Advised) {
            ((Advised) bean).addAdvisor(this.deassociatePointcutAdvisor);
            return bean;
        } else {
            ProxyFactory proxyFactory = new ProxyFactory(bean);
            // Copy our properties (proxyTargetClass etc) inherited from ProxyConfig.
            proxyFactory.copyFrom(this);
            proxyFactory.addAdvisor(this.deassociatePointcutAdvisor);
            return proxyFactory.getProxy(this.classLoader);
        }
    } else {
        // This is not a repository.
        return bean;
    }

}

From source file:flex.contrib.factories.config.SecurityExceptionTranslationPostProcessor.java

public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    Class<?> targetClass = (bean instanceof Advised ? ((Advised) bean).getTargetSource().getTargetClass()
            : bean.getClass());/*from   w w w.  ja  va  2 s  .co  m*/
    if (targetClass == null) {
        // Can't do much here
        return bean;
    }

    if (AopUtils.canApply(this.securityExceptionTranslationAdvisor, targetClass)) {
        if (bean instanceof Advised) {
            ((Advised) bean).addAdvisor(this.securityExceptionTranslationAdvisor);
            return bean;
        } else {
            ProxyFactory proxyFactory = new ProxyFactory(bean);
            // Copy our properties (proxyTargetClass etc) inherited from ProxyConfig.
            proxyFactory.copyFrom(this);
            proxyFactory.addAdvisor(this.securityExceptionTranslationAdvisor);
            return proxyFactory.getProxy(this.beanClassLoader);
        }
    } else {
        // This is not a repository.
        return bean;
    }
}

From source file:org.activiti.spring.components.aop.ProcessStartAnnotationBeanPostProcessor.java

public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if (bean instanceof AopInfrastructureBean) {
        // Ignore AOP infrastructure such as scoped proxies.
        return bean;
    }//from   w  w  w.  j a  v a  2 s  . c  o m
    Class<?> targetClass = AopUtils.getTargetClass(bean);
    if (AopUtils.canApply(this.advisor, targetClass)) {
        if (bean instanceof Advised) {
            ((Advised) bean).addAdvisor(0, this.advisor);
            return bean;
        } else {
            ProxyFactory proxyFactory = new ProxyFactory(bean);
            // Copy our properties (proxyTargetClass etc) inherited from ProxyConfig.
            proxyFactory.copyFrom(this);
            proxyFactory.addAdvisor(this.advisor);
            return proxyFactory.getProxy(this.beanClassLoader);
        }
    } else {
        // No async proxy needed.
        return bean;
    }
}

From source file:org.springframework.aop.support.AopUtils.java

/**
 * Determine the sublist of the <code>candidateAdvisors</code> list
 * that is applicable to the given class.
 * @param candidateAdvisors Advisors to evaluate
 * @param clazz the target class//from   w  w w.  ja  v a 2 s .co m
 * @return sublist of Advisors that can apply to an object of the given class
 */
public static List findAdvisorsThatCanApply(List candidateAdvisors, Class clazz) {
    List eligibleAdvisors = new LinkedList();
    for (Iterator it = candidateAdvisors.iterator(); it.hasNext();) {
        Advisor candidate = (Advisor) it.next();
        if (AopUtils.canApply(candidate, clazz)) {
            eligibleAdvisors.add(candidate);
            if (logger.isDebugEnabled()) {
                logger.debug(
                        "Candidate advisor [" + candidate + "] accepted for class [" + clazz.getName() + "]");
            }
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug(
                        "Candidate advisor [" + candidate + "] rejected for class [" + clazz.getName() + "]");
            }
        }
    }
    return eligibleAdvisors;
}

From source file:org.springframework.integration.config.ConsumerEndpointFactoryBean.java

public void afterPropertiesSet() throws Exception {
    try {// w  ww. j av  a2s.  co  m
        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 handleRequesMessage method internally and already have the advice chain injected.
         *  So we only advise handlers that are not reply-producing. If the handler is already advised,
         *  add the configured advices to its chain, otherwise create a proxy.
         */
        if (!(this.handler instanceof AbstractReplyProducingMessageHandler)) {
            if (AopUtils.isAopProxy(this.handler) && this.handler instanceof Advised) {
                Class<?> targetClass = AopUtils.getTargetClass(this.handler);
                for (Advice advice : this.adviceChain) {
                    NameMatchMethodPointcutAdvisor handlerAdvice = new NameMatchMethodPointcutAdvisor(advice);
                    handlerAdvice.addMethodName("handleMessage");
                    if (AopUtils.canApply(handlerAdvice.getPointcut(), targetClass)) {
                        ((Advised) this.handler).addAdvice(advice);
                    }
                }
            } else {
                ProxyFactory proxyFactory = new ProxyFactory(this.handler);
                for (Advice advice : this.adviceChain) {
                    proxyFactory.addAdvice(advice);
                }
                this.handler = (MessageHandler) proxyFactory.getProxy(this.beanClassLoader);
            }
        }
    }
    this.initializeEndpoint();
}

From source file:org.springframework.integration.monitor.IntegrationMBeanExporter.java

private Object applyAdvice(Object bean, PointcutAdvisor advisor, ClassLoader beanClassLoader) {
    Class<?> targetClass = AopUtils.getTargetClass(bean);
    if (AopUtils.canApply(advisor.getPointcut(), targetClass)) {
        if (bean instanceof Advised) {
            ((Advised) bean).addAdvisor(advisor);
            return bean;
        } else {//  w  w w .  j  av a2s  . c om
            ProxyFactory proxyFactory = new ProxyFactory(bean);
            proxyFactory.addAdvisor(advisor);
            /**
             * N.B. it's not a good idea to use proxyFactory.setProxyTargetClass(true) here because it forces all
             * the integration components to be cglib proxyable (i.e. have a default constructor etc.), which they
             * are not in general (usually for good reason).
             */
            return proxyFactory.getProxy(beanClassLoader);
        }
    }
    return bean;
}