Search in sources :

Example 16 with TargetSource

use of cn.taketoday.aop.TargetSource in project today-infrastructure by TAKETODAY.

the class StandardProxyInvoker method dynamicAdvisedProceed.

public static Object dynamicAdvisedProceed(Object proxy, AdvisedSupport advised, TargetInvocation targetInv, Object[] args) throws Throwable {
    Object target = null;
    Object oldProxy = null;
    boolean restore = false;
    final TargetSource targetSource = advised.getTargetSource();
    try {
        if (advised.isExposeProxy()) {
            // Make invocation available if necessary.
            oldProxy = AopContext.setCurrentProxy(proxy);
            restore = true;
        }
        target = targetSource.getTarget();
        final MethodInterceptor[] interceptors = targetInv.getDynamicInterceptors(advised);
        // but just use MethodInvoker invocation of the target.
        if (ObjectUtils.isEmpty(interceptors)) {
            return targetInv.proceed(target, args);
        }
        // We need to create a DynamicStandardMethodInvocation...
        final Object retVal = new DynamicStandardMethodInvocation(proxy, target, targetInv, args, interceptors).proceed();
        assertReturnValue(retVal, targetInv.getMethod());
        return retVal;
    } finally {
        if (target != null && !targetSource.isStatic()) {
            targetSource.releaseTarget(target);
        }
        if (restore) {
            // Restore old proxy.
            AopContext.setCurrentProxy(oldProxy);
        }
    }
}
Also used : TargetSource(cn.taketoday.aop.TargetSource) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor)

Example 17 with TargetSource

use of cn.taketoday.aop.TargetSource in project today-infrastructure by TAKETODAY.

the class MBeanServerConnectionFactoryBean method createLazyConnection.

/**
 * Creates lazy proxies for the {@code JMXConnector} and {@code MBeanServerConnection}.
 */
private void createLazyConnection() {
    this.connectorTargetSource = new JMXConnectorLazyInitTargetSource();
    TargetSource connectionTargetSource = new MBeanServerConnectionLazyInitTargetSource();
    this.connector = (JMXConnector) new ProxyFactory(JMXConnector.class, this.connectorTargetSource).getProxy(this.beanClassLoader);
    this.connection = (MBeanServerConnection) new ProxyFactory(MBeanServerConnection.class, connectionTargetSource).getProxy(this.beanClassLoader);
}
Also used : AbstractLazyCreationTargetSource(cn.taketoday.aop.target.AbstractLazyCreationTargetSource) TargetSource(cn.taketoday.aop.TargetSource) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection)

Example 18 with TargetSource

use of cn.taketoday.aop.TargetSource in project today-infrastructure by TAKETODAY.

the class PrototypeBasedTargetSourceTests method testSerializability.

@Test
public void testSerializability() throws Exception {
    PropertyValues tsPvs = new PropertyValues();
    tsPvs.add("targetBeanName", "person");
    RootBeanDefinition tsBd = new RootBeanDefinition(TestTargetSource.class);
    tsBd.setPropertyValues(tsPvs);
    PropertyValues pvs = new PropertyValues();
    RootBeanDefinition bd = new RootBeanDefinition(SerializablePerson.class);
    bd.setPropertyValues(pvs);
    bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    StandardBeanFactory bf = new StandardBeanFactory();
    bf.registerBeanDefinition("ts", tsBd);
    bf.registerBeanDefinition("person", bd);
    TestTargetSource cpts = (TestTargetSource) bf.getBean("ts");
    TargetSource serialized = SerializationTestUtils.serializeAndDeserialize(cpts);
    boolean condition = serialized instanceof SingletonTargetSource;
    assertThat(condition).as("Changed to SingletonTargetSource on deserialization").isTrue();
    SingletonTargetSource sts = (SingletonTargetSource) serialized;
    assertThat(sts.getTarget()).isNotNull();
}
Also used : TargetSource(cn.taketoday.aop.TargetSource) PropertyValues(cn.taketoday.beans.PropertyValues) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) Test(org.junit.jupiter.api.Test)

Example 19 with TargetSource

use of cn.taketoday.aop.TargetSource in project today-framework by TAKETODAY.

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(cn.taketoday.beans.testfixture.beans.ITestBean) HotSwappableTargetSource(cn.taketoday.aop.target.HotSwappableTargetSource) TargetSource(cn.taketoday.aop.TargetSource) SingletonTargetSource(cn.taketoday.aop.target.SingletonTargetSource) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Advised(cn.taketoday.aop.framework.Advised) DebugInterceptor(cn.taketoday.aop.interceptor.DebugInterceptor) AopConfigException(cn.taketoday.aop.framework.AopConfigException) LockedException(cn.taketoday.aop.mixin.LockedException) 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) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Test(org.junit.jupiter.api.Test)

Example 20 with TargetSource

use of cn.taketoday.aop.TargetSource in project today-framework by TAKETODAY.

the class NoneProxyMethodGenerator method generate.

@Override
public boolean generate(Method method, GeneratorContext context) {
    final AdvisedSupport config = context.getConfig();
    final MethodInterceptor[] interceptors = context.getConfig().getInterceptors(method, context.getTargetClass());
    if (ObjectUtils.isEmpty(interceptors)) {
        final TargetSource targetSource = config.getTargetSource();
        if (targetSource.isStatic()) {
            invokeStaticTarget(method, context);
        } else {
            invokeTargetFromTargetSource(method, context);
        }
        return true;
    }
    return false;
}
Also used : TargetSource(cn.taketoday.aop.TargetSource) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) AdvisedSupport(cn.taketoday.aop.framework.AdvisedSupport)

Aggregations

TargetSource (cn.taketoday.aop.TargetSource)22 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)8 Test (org.junit.jupiter.api.Test)7 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)6 SingletonTargetSource (cn.taketoday.aop.target.SingletonTargetSource)6 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)4 AdvisedSupport (cn.taketoday.aop.framework.AdvisedSupport)3 DebugInterceptor (cn.taketoday.aop.interceptor.DebugInterceptor)3 HotSwappableTargetSource (cn.taketoday.aop.target.HotSwappableTargetSource)3 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)3 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)3 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)3 MarshalException (java.rmi.MarshalException)3 SQLException (java.sql.SQLException)3 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)3 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)3 AopInvocationException (cn.taketoday.aop.AopInvocationException)2 AbstractLazyCreationTargetSource (cn.taketoday.aop.target.AbstractLazyCreationTargetSource)2