use of com.alipay.sofa.runtime.spi.service.ServiceProxy in project sofa-boot by sofastack.
the class SofaEventHandlerTest method testDynamicProxyFinder.
@Test
public void testDynamicProxyFinder() throws Exception {
SofaFramework.registerSofaRuntimeManager(sofaRuntimeManager);
new MockUp<DynamicJvmServiceProxyFinder>() {
@Mock
public Biz getBiz(SofaRuntimeManager sofaRuntimeManager) {
return biz;
}
};
new Expectations() {
{
biz.getIdentity();
result = "MockName:MockVersion";
biz.getBizState();
result = BizState.ACTIVATED;
sofaRuntimeManager.getAppClassLoader();
result = ctx.getClassLoader().getParent();
}
};
new Expectations() {
{
sofaRuntimeManager.getComponentManager();
result = ((SofaRuntimeContext) ctx.getBean("sofaRuntimeContext")).getComponentManager();
contract.getInterfaceType();
result = SampleService.class;
contract.getUniqueId();
result = "";
contract.getBinding(JvmBinding.JVM_BINDING_TYPE);
result = new JvmBinding();
invocation.getArguments();
result = new Object[] {};
invocation.getMethod();
result = SampleService.class.getMethod("service");
}
};
DynamicJvmServiceProxyFinder.getDynamicJvmServiceProxyFinder().setHasFinishStartup(true);
ServiceProxy serviceProxy = DynamicJvmServiceProxyFinder.getDynamicJvmServiceProxyFinder().findServiceProxy(ctx.getClassLoader(), contract);
try {
Assert.assertTrue(SofaEventHandlerTest.class.getName().equals(serviceProxy.invoke(invocation)));
} catch (Throwable throwable) {
throw new RuntimeException("testDynamicProxyFinder case failed.", throwable);
}
}
use of com.alipay.sofa.runtime.spi.service.ServiceProxy in project sofa-boot by alipay.
the class JvmBindingAdapter method createServiceProxy.
/**
* create service proxy object
*
* @return proxy object
*/
private Object createServiceProxy(Contract contract, JvmBinding binding, SofaRuntimeContext sofaRuntimeContext) {
ClassLoader newClassLoader;
ClassLoader appClassLoader = sofaRuntimeContext.getAppClassLoader();
Class<?> javaClass = contract.getInterfaceType();
try {
Class appLoadedClass = appClassLoader.loadClass(javaClass.getName());
if (appLoadedClass == javaClass) {
newClassLoader = appClassLoader;
} else {
newClassLoader = javaClass.getClassLoader();
}
} catch (ClassNotFoundException e) {
newClassLoader = javaClass.getClassLoader();
}
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(newClassLoader);
ServiceProxy handler = new JvmServiceInvoker(contract, binding, sofaRuntimeContext);
ProxyFactory factory = new ProxyFactory();
if (javaClass.isInterface()) {
factory.addInterface(javaClass);
factory.addInterface(JvmBindingInterface.class);
} else {
factory.setTargetClass(javaClass);
factory.setProxyTargetClass(true);
}
factory.addAdvice(handler);
return factory.getProxy(newClassLoader);
} finally {
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
}
use of com.alipay.sofa.runtime.spi.service.ServiceProxy in project sofa-boot by alipay.
the class SofaEventHandlerTest method testDynamicProxyFinder.
@Test
public void testDynamicProxyFinder() throws Exception {
SofaFramework.registerSofaRuntimeManager(sofaRuntimeManager);
new MockUp<DynamicJvmServiceProxyFinder>() {
@Mock
public Biz getBiz(SofaRuntimeManager sofaRuntimeManager) {
return biz;
}
};
new Expectations() {
{
biz.getIdentity();
result = "MockName:MockVersion";
biz.getBizState();
result = BizState.ACTIVATED;
sofaRuntimeManager.getAppClassLoader();
result = ctx.getClassLoader().getParent();
}
};
new Expectations() {
{
sofaRuntimeManager.getComponentManager();
result = ((SofaRuntimeContext) ctx.getBean("sofaRuntimeContext")).getComponentManager();
contract.getInterfaceType();
result = SampleService.class;
contract.getUniqueId();
result = "";
contract.getBinding(JvmBinding.JVM_BINDING_TYPE);
result = new JvmBinding();
invocation.getArguments();
result = new Object[] {};
invocation.getMethod();
result = SampleService.class.getMethod("service");
}
};
DynamicJvmServiceProxyFinder.getDynamicJvmServiceProxyFinder().setHasFinishStartup(true);
ServiceProxy serviceProxy = DynamicJvmServiceProxyFinder.getDynamicJvmServiceProxyFinder().findServiceProxy(ctx.getClassLoader(), contract);
try {
Assert.assertTrue(SofaEventHandlerTest.class.getName().equals(serviceProxy.invoke(invocation)));
} catch (Throwable throwable) {
throw new RuntimeException("testDynamicProxyFinder case failed.", throwable);
}
}
use of com.alipay.sofa.runtime.spi.service.ServiceProxy in project sofa-boot by sofastack.
the class JvmBindingAdapter method createServiceProxy.
/**
* create service proxy object
*
* @return proxy object
*/
private Object createServiceProxy(Contract contract, JvmBinding binding, SofaRuntimeContext sofaRuntimeContext) {
ClassLoader newClassLoader;
ClassLoader appClassLoader = sofaRuntimeContext.getAppClassLoader();
Class<?> javaClass = contract.getInterfaceType();
try {
Class appLoadedClass = appClassLoader.loadClass(javaClass.getName());
if (appLoadedClass == javaClass) {
newClassLoader = appClassLoader;
} else {
newClassLoader = javaClass.getClassLoader();
}
} catch (ClassNotFoundException e) {
newClassLoader = javaClass.getClassLoader();
}
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(newClassLoader);
ServiceProxy handler = new JvmServiceInvoker(contract, binding, sofaRuntimeContext);
ProxyFactory factory = new ProxyFactory();
if (javaClass.isInterface()) {
factory.addInterface(javaClass);
} else {
factory.setTargetClass(javaClass);
factory.setProxyTargetClass(true);
}
factory.addAdvice(handler);
return factory.getProxy(newClassLoader);
} finally {
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
}
Aggregations