Example usage for org.springframework.aop TargetSource getTarget

List of usage examples for org.springframework.aop TargetSource getTarget

Introduction

In this page you can find the example usage for org.springframework.aop TargetSource getTarget.

Prototype

@Nullable
Object getTarget() throws Exception;

Source Link

Document

Return a target instance.

Usage

From source file:cn.guoyukun.spring.utils.AopProxyUtils.java

/**
 * ??//from  www .j a  va 2  s  .  com
 * see http://jinnianshilongnian.iteye.com/blog/1894465
 * @param proxy
 * @return
 */
public static boolean isMultipleProxy(Object proxy) {
    try {
        ProxyFactory proxyFactory = null;
        if (AopUtils.isJdkDynamicProxy(proxy)) {
            proxyFactory = findJdkDynamicProxyFactory(proxy);
        }
        if (AopUtils.isCglibProxy(proxy)) {
            proxyFactory = findCglibProxyFactory(proxy);
        }
        TargetSource targetSource = (TargetSource) ReflectionUtils.getField(ProxyFactory_targetSource_FIELD,
                proxyFactory);
        return AopUtils.isAopProxy(targetSource.getTarget());
    } catch (Exception e) {
        throw new IllegalArgumentException(
                "proxy args maybe not proxy with cglib or jdk dynamic proxy. this method not support", e);
    }
}

From source file:com.alibaba.druid.support.ibatis.SpringIbatisBeanNameAutoProxyCreator.java

@SuppressWarnings("rawtypes")
protected Object createProxy(Class beanClass, String beanName, Object[] specificInterceptors,
        TargetSource targetSource) {
    try {//w  w  w  .jav  a2s . co m
        Object target = targetSource.getTarget();

        if (target instanceof SqlMapClientWrapper) {
            proxyBeanNames.add(beanName);
            return target;
        }

        if (target instanceof SqlMapClient) {
            proxyBeanNames.add(beanName);

            return new SqlMapClientWrapper((ExtendedSqlMapClient) target);
        }

        return super.createProxy(beanClass, beanName, specificInterceptors, targetSource);
    } catch (Throwable ex) {
        LOG.error(ex.getMessage(), ex);
        return super.createProxy(beanClass, beanName, specificInterceptors, targetSource);
    }
}

From source file:com.alibaba.dragoon.patrol.spring.SpringIbatisBeanNameAutoProxyCreator.java

@SuppressWarnings("rawtypes")
protected Object createProxy(Class beanClass, String beanName, Object[] specificInterceptors,
        TargetSource targetSource) {
    try {/*from   w  w  w  . ja v a2  s  .c om*/
        Object target = targetSource.getTarget();

        if (target instanceof SqlMapClientWrapper) {
            proxyBeanNames.add(beanName);
            stats.getProxyBeanNames().add(beanName);
            return target;
        }

        if (target instanceof SqlMapClient) {
            proxyBeanNames.add(beanName);
            stats.getProxyBeanNames().add(beanName);

            return new SqlMapClientWrapper(stats, (ExtendedSqlMapClient) target);
        }

        return super.createProxy(beanClass, beanName, specificInterceptors, targetSource);
    } catch (Throwable ex) {
        LOG.error(ex.getMessage(), ex);
        return super.createProxy(beanClass, beanName, specificInterceptors, targetSource);
    }
}

From source file:com.alibaba.druid.support.ibatis.SpringIbatisBeanTypeAutoProxyCreator.java

@SuppressWarnings({ "rawtypes" })
protected Object createProxy(Class beanClass, String beanName, Object[] specificInterceptors,
        TargetSource targetSource) {
    try {/*w w w .  ja v  a 2s.  c o m*/
        Object target = targetSource.getTarget();

        if (target instanceof SqlMapClientWrapper) {
            proxyBeanNames.add(beanName);
            return target;
        }

        if (target instanceof SqlMapClient) {
            proxyBeanNames.add(beanName);

            return new SqlMapClientWrapper((ExtendedSqlMapClient) target);
        }

        return super.createProxy(beanClass, beanName, specificInterceptors, targetSource);
    } catch (Throwable ex) {
        LOG.error(ex.getMessage(), ex);
        return super.createProxy(beanClass, beanName, specificInterceptors, targetSource);
    }
}

From source file:com.liferay.portal.remote.json.web.service.extender.internal.ServiceJSONWebServiceScannerStrategy.java

/**
 * @see com.liferay.portal.jsonwebservice.SpringJSONWebServiceScannerStrategy#getTargetClass(
 *      Object)/*ww  w .j ava2s .c  o  m*/
 */
protected Class<?> getTargetClass(Object service) throws Exception {
    while (ProxyUtil.isProxyClass(service.getClass())) {
        InvocationHandler invocationHandler = ProxyUtil.getInvocationHandler(service);

        if (invocationHandler instanceof AdvisedSupportProxy) {
            AdvisedSupport advisedSupport = ServiceBeanAopProxy.getAdvisedSupport(service);

            TargetSource targetSource = advisedSupport.getTargetSource();

            service = targetSource.getTarget();
        } else if (invocationHandler instanceof ClassLoaderBeanHandler) {
            ClassLoaderBeanHandler classLoaderBeanHandler = (ClassLoaderBeanHandler) invocationHandler;

            Object bean = classLoaderBeanHandler.getBean();

            if (bean instanceof ServiceWrapper) {
                ServiceWrapper<?> serviceWrapper = (ServiceWrapper<?>) bean;

                service = serviceWrapper.getWrappedService();
            } else {
                service = bean;
            }
        }
    }

    return service.getClass();
}

From source file:org.solmix.runtime.exchange.support.SpringAopClassHelper.java

@Override
protected Class<?> getRealClassInternal(Object o) {
    if (AopUtils.isAopProxy(o) && (o instanceof Advised)) {
        Advised advised = (Advised) o;//from   w ww .  j  ava2 s.c o m
        try {
            TargetSource targetSource = advised.getTargetSource();

            Object target = null;

            try {
                target = targetSource.getTarget();
            } catch (BeanCreationException ex) {
                // some scopes such as 'request' may not 
                // be active on the current thread yet
                return getRealClassFromClassInternal(targetSource.getTargetClass());
            }

            if (target == null) {
                Class<?> targetClass = AopUtils.getTargetClass(o);
                if (targetClass != null) {
                    return getRealClassFromClassInternal(targetClass);
                }
            } else {
                return getRealClassInternal(target);
            }
        } catch (Exception ex) {
            // ignore
        }

    } else if (ClassUtils.isCglibProxyClass(o.getClass())) {
        return getRealClassFromClassInternal(AopUtils.getTargetClass(o));
    }
    return o.getClass();
}

From source file:com.foreveross.modules.jsf.SpringBeanELResolver.java

@Override
public Object getValue(ELContext elContext, Object base, Object property) throws ELException {
    if (base == null) {
        String beanName = property.toString();
        BeanFactory bf = getBeanFactory(elContext);
        if (bf.containsBean(beanName)) {
            if (logger.isTraceEnabled()) {
                logger.trace("Successfully resolved variable '" + beanName + "' in Spring BeanFactory");
            }//  www.  jav a 2  s.  c  om
            elContext.setPropertyResolved(true);
            Object bean = bf.getBean(beanName);
            String className = bean.getClass().getName();
            if (className.startsWith("$Proxy")) {//Spring aop proxy for groovy bean
                try {
                    Object o = com.foreveross.infra.util.mybatis.plugin.ReflectHelper.getValueByFieldName(bean,
                            "h");
                    o = com.foreveross.infra.util.mybatis.plugin.ReflectHelper.getValueByFieldName(o,
                            "advised");
                    o = com.foreveross.infra.util.mybatis.plugin.ReflectHelper.getValueByFieldName(o,
                            "targetSource");
                    org.springframework.aop.TargetSource ts = (org.springframework.aop.TargetSource) o;
                    bean = ts.getTarget();
                } catch (Exception e) {
                    Exceptions.runtime(FormatableCharSequence.get("get [{0}.{1}.{2}.{3}] Error!", className,
                            "h", "advised", "targetSource"), e);
                }
            }
            return bean;
        }
    }
    return null;
}

From source file:com.liferay.portal.spring.extender.internal.bean.ApplicationContextServicePublisher.java

protected Class<?> getTargetClass(Object service) throws Exception {
    Class<?> clazz = service.getClass();

    if (ProxyUtil.isProxyClass(clazz)) {
        AdvisedSupport advisedSupport = ServiceBeanAopProxy.getAdvisedSupport(service);

        TargetSource targetSource = advisedSupport.getTargetSource();

        Object target = targetSource.getTarget();

        clazz = target.getClass();/* w  w  w  .j a v  a 2s  . c  o m*/
    }

    return clazz;
}

From source file:org.jboss.spring.aop.JBossAopProxy.java

public Object getProxy(ClassLoader classLoader) {
    Class<?> clazz = config.getTargetClass();
    if (clazz == null)
        throw new IllegalArgumentException("Null target class");

    try {/*from  www.  j  av  a 2  s.c  om*/
        ClassInfo classInfo = configuration.getClassInfo(clazz);
        constructorInfo = Config.findConstructorInfo(classInfo, new String[] {}); // TODO - arguments

        MetaData metaData = MetaDataStack.peek();
        boolean hasInstanceMetaData = rootHasSubInstanceMetaData(metaData);
        AspectManager manager = AspectManagerFactory.getAspectManager(metaData);
        ContainerCache cache = ContainerCache.initialise(manager, clazz, metaData, hasInstanceMetaData);
        AOPProxyFactoryParameters params = new AOPProxyFactoryParameters();

        Object target = null;
        TargetSource ts = config.getTargetSource();
        if (ts != null)
            target = ts.getTarget();

        if (target == null)
            target = createTarget(cache, params);

        params.setProxiedClass(target.getClass());
        params.setMetaData(metaData);
        params.setTarget(target);
        params.setContainerCache(cache);
        params.setMetaDataHasInstanceLevelData(hasInstanceMetaData);

        return proxyFactory.createAdvisedProxy(params);
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
}

From source file:org.springmodules.cache.interceptor.proxy.CacheProxyFactoryBeanTests.java

/**
 * Verifies that the method//from  ww  w .ja v a2  s.c o  m
 * <code>{@link CacheProxyFactoryBean#createTargetSource(Object)}</code>
 * creates a new <code>org.springframework.aop.TargetSource</code> setting
 * the given object as its target.
 */
public void testCreateTargetSourceWithTargetObjectNotInstanceOfTargetSource() throws Exception {
    Object targetObject = new Object();
    TargetSource targetSource = factoryBean.createTargetSource(targetObject);
    assertSame(targetObject, targetSource.getTarget());
}