Search in sources :

Example 16 with TargetSource

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

the class IntegrationMBeanExporter method enhanceHandlerMonitor.

private MessageHandlerMetrics enhanceHandlerMonitor(MessageHandlerMetrics monitor) {
    MessageHandlerMetrics result = monitor;
    if (monitor.getManagedName() != null && monitor.getManagedType() != null) {
        return monitor;
    }
    // Assignment algorithm and bean id, with bean id pulled reflectively out of enclosing endpoint if possible
    String[] names = this.applicationContext.getBeanNamesForType(AbstractEndpoint.class);
    String name = null;
    String endpointName = null;
    String source = "endpoint";
    Object endpoint = null;
    for (String beanName : names) {
        endpoint = this.applicationContext.getBean(beanName);
        try {
            Object field = extractTarget(getField(endpoint, "handler"));
            if (field == monitor || this.extractTarget(this.handlerInAnonymousWrapper(field)) == monitor) {
                name = beanName;
                endpointName = beanName;
                break;
            }
        } catch (Exception e) {
            logger.trace("Could not get handler from bean = " + beanName);
        }
    }
    if (name != null && endpoint != null && name.startsWith("_org.springframework.integration")) {
        name = getInternalComponentName(name);
        source = "internal";
    }
    if (name != null && endpoint != null && name.startsWith("org.springframework.integration")) {
        Object target = endpoint;
        if (endpoint instanceof Advised) {
            TargetSource targetSource = ((Advised) endpoint).getTargetSource();
            if (targetSource != null) {
                try {
                    target = targetSource.getTarget();
                } catch (Exception e) {
                    logger.error("Could not get handler from bean = " + name);
                }
            }
        }
        Object field = getField(target, "inputChannel");
        if (field != null) {
            if (!this.anonymousHandlerCounters.containsKey(field)) {
                this.anonymousHandlerCounters.put(field, new AtomicLong());
            }
            AtomicLong count = this.anonymousHandlerCounters.get(field);
            long total = count.incrementAndGet();
            String suffix = "";
            /*
				 * Short hack to makes sure object names are unique if more than one endpoint has the same input channel
				 */
            if (total > 1) {
                suffix = "#" + total;
            }
            name = field + suffix;
            source = "anonymous";
        }
    }
    if (endpoint instanceof Lifecycle) {
        // Wrap the monitor in a lifecycle so it exposes the start/stop operations
        if (monitor instanceof MappingMessageRouterManagement) {
            if (monitor instanceof TrackableComponent) {
                result = new TrackableRouterMetrics((Lifecycle) endpoint, (MappingMessageRouterManagement) monitor);
            } else {
                result = new RouterMetrics((Lifecycle) endpoint, (MappingMessageRouterManagement) monitor);
            }
        } else {
            if (monitor instanceof TrackableComponent) {
                result = new LifecycleTrackableMessageHandlerMetrics((Lifecycle) endpoint, monitor);
            } else {
                result = new LifecycleMessageHandlerMetrics((Lifecycle) endpoint, monitor);
            }
        }
    }
    if (name == null) {
        if (monitor instanceof NamedComponent) {
            name = ((NamedComponent) monitor).getComponentName();
        }
        if (name == null) {
            name = monitor.toString();
        }
        source = "handler";
    }
    if (endpointName != null) {
        this.beansByEndpointName.put(name, endpointName);
    }
    monitor.setManagedType(source);
    monitor.setManagedName(name);
    return result;
}
Also used : TargetSource(org.springframework.aop.TargetSource) TrackableRouterMetrics(org.springframework.integration.support.management.TrackableRouterMetrics) Lifecycle(org.springframework.context.Lifecycle) MappingMessageRouterManagement(org.springframework.integration.support.management.MappingMessageRouterManagement) TrackableComponent(org.springframework.integration.support.management.TrackableComponent) NamedComponent(org.springframework.integration.support.context.NamedComponent) UnableToRegisterMBeanException(org.springframework.jmx.export.UnableToRegisterMBeanException) JMException(javax.management.JMException) BeansException(org.springframework.beans.BeansException) LifecycleTrackableMessageHandlerMetrics(org.springframework.integration.support.management.LifecycleTrackableMessageHandlerMetrics) LifecycleMessageHandlerMetrics(org.springframework.integration.support.management.LifecycleMessageHandlerMetrics) MessageHandlerMetrics(org.springframework.integration.support.management.MessageHandlerMetrics) LifecycleTrackableMessageHandlerMetrics(org.springframework.integration.support.management.LifecycleTrackableMessageHandlerMetrics) AtomicLong(java.util.concurrent.atomic.AtomicLong) LifecycleMessageHandlerMetrics(org.springframework.integration.support.management.LifecycleMessageHandlerMetrics) Advised(org.springframework.aop.framework.Advised) TrackableRouterMetrics(org.springframework.integration.support.management.TrackableRouterMetrics) RouterMetrics(org.springframework.integration.support.management.RouterMetrics)

Example 17 with TargetSource

use of org.springframework.aop.TargetSource in project cxf by apache.

the class SpringClassUnwrapper method getRealClass.

@Override
public Class<?> getRealClass(Object o) {
    if (AopUtils.isAopProxy(o) && (o instanceof Advised)) {
        Advised advised = (Advised) o;
        try {
            TargetSource targetSource = advised.getTargetSource();
            final Object target;
            try {
                target = targetSource.getTarget();
            } catch (BeanCreationException ex) {
                // be active on the current thread yet
                return getRealClassFromClass(targetSource.getTargetClass());
            }
            if (target == null) {
                Class<?> targetClass = AopUtils.getTargetClass(o);
                if (targetClass != null) {
                    return getRealClassFromClass(targetClass);
                }
            } else {
                return getRealClass(target);
            }
        } catch (Exception ex) {
        // ignore
        }
    } else if (ClassUtils.isCglibProxyClass(o.getClass())) {
        return getRealClassFromClass(AopUtils.getTargetClass(o));
    }
    return o.getClass();
}
Also used : TargetSource(org.springframework.aop.TargetSource) BeanCreationException(org.springframework.beans.factory.BeanCreationException) Advised(org.springframework.aop.framework.Advised) BeanCreationException(org.springframework.beans.factory.BeanCreationException)

Example 18 with TargetSource

use of org.springframework.aop.TargetSource in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testProxyIsBoundBeforeTargetSourceInvoked.

@Test
public void testProxyIsBoundBeforeTargetSourceInvoked() {
    final TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(new DebugInterceptor());
    pf.setExposeProxy(true);
    final ITestBean proxy = (ITestBean) createProxy(pf);
    Advised config = (Advised) proxy;
    // This class just checks proxy is bound before getTarget() call
    config.setTargetSource(new TargetSource() {

        @Override
        public Class<?> getTargetClass() {
            return TestBean.class;
        }

        @Override
        public boolean isStatic() {
            return false;
        }

        @Override
        public Object getTarget() throws Exception {
            assertThat(AopContext.currentProxy()).isEqualTo(proxy);
            return target;
        }

        @Override
        public void releaseTarget(Object target) throws Exception {
        }
    });
    // Just test anything: it will fail if context wasn't found
    assertThat(proxy.getAge()).isEqualTo(0);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TargetSource(org.springframework.aop.TargetSource) HotSwappableTargetSource(org.springframework.aop.target.HotSwappableTargetSource) SingletonTargetSource(org.springframework.aop.target.SingletonTargetSource) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DebugInterceptor(org.springframework.aop.interceptor.DebugInterceptor) FileNotFoundException(java.io.FileNotFoundException) SQLException(java.sql.SQLException) MarshalException(java.rmi.MarshalException) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) LockedException(test.mixin.LockedException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Test(org.junit.jupiter.api.Test)

Example 19 with TargetSource

use of org.springframework.aop.TargetSource in project spring-framework by spring-projects.

the class AbstractAutoProxyCreator method postProcessBeforeInstantiation.

@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
    Object cacheKey = getCacheKey(beanClass, beanName);
    if (!StringUtils.hasLength(beanName) || !this.targetSourcedBeans.contains(beanName)) {
        if (this.advisedBeans.containsKey(cacheKey)) {
            return null;
        }
        if (isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName)) {
            this.advisedBeans.put(cacheKey, Boolean.FALSE);
            return null;
        }
    }
    // Create proxy here if we have a custom TargetSource.
    // Suppresses unnecessary default instantiation of the target bean:
    // The TargetSource will handle target instances in a custom fashion.
    TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
    if (targetSource != null) {
        if (StringUtils.hasLength(beanName)) {
            this.targetSourcedBeans.add(beanName);
        }
        Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
        Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
        this.proxyTypes.put(cacheKey, proxy.getClass());
        return proxy;
    }
    return null;
}
Also used : TargetSource(org.springframework.aop.TargetSource) SingletonTargetSource(org.springframework.aop.target.SingletonTargetSource)

Aggregations

TargetSource (org.springframework.aop.TargetSource)19 Advised (org.springframework.aop.framework.Advised)6 SingletonTargetSource (org.springframework.aop.target.SingletonTargetSource)5 Test (org.junit.jupiter.api.Test)3 ProxyFactory (org.springframework.aop.framework.ProxyFactory)3 BeanCreationException (org.springframework.beans.factory.BeanCreationException)3 Method (java.lang.reflect.Method)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 JMException (javax.management.JMException)2 BeansException (org.springframework.beans.BeansException)2 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 Lifecycle (org.springframework.context.Lifecycle)2 TrackableComponent (org.springframework.integration.support.management.TrackableComponent)2 UnableToRegisterMBeanException (org.springframework.jmx.export.UnableToRegisterMBeanException)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 MarshalException (java.rmi.MarshalException)1 SQLException (java.sql.SQLException)1