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);
}
}
}
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);
}
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();
}
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);
}
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;
}
Aggregations