Search in sources :

Example 1 with ServiceProxy

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);
    }
}
Also used : Expectations(mockit.Expectations) ServiceProxy(com.alipay.sofa.runtime.spi.service.ServiceProxy) DefaultSampleService(com.alipay.sofa.runtime.test.beans.service.DefaultSampleService) SampleService(com.alipay.sofa.runtime.test.beans.facade.SampleService) MockUp(mockit.MockUp) SofaRuntimeManager(com.alipay.sofa.runtime.spi.component.SofaRuntimeManager) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Test(org.junit.Test)

Example 2 with ServiceProxy

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);
    }
}
Also used : ServiceProxy(com.alipay.sofa.runtime.spi.service.ServiceProxy) ProxyFactory(org.springframework.aop.framework.ProxyFactory)

Example 3 with ServiceProxy

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);
    }
}
Also used : Expectations(mockit.Expectations) ServiceProxy(com.alipay.sofa.runtime.spi.service.ServiceProxy) DefaultSampleService(com.alipay.sofa.runtime.test.beans.service.DefaultSampleService) SampleService(com.alipay.sofa.runtime.test.beans.facade.SampleService) MockUp(mockit.MockUp) SofaRuntimeManager(com.alipay.sofa.runtime.spi.component.SofaRuntimeManager) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Test(org.junit.Test)

Example 4 with ServiceProxy

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);
    }
}
Also used : ServiceProxy(com.alipay.sofa.runtime.spi.service.ServiceProxy) ProxyFactory(org.springframework.aop.framework.ProxyFactory)

Aggregations

ServiceProxy (com.alipay.sofa.runtime.spi.service.ServiceProxy)4 JvmBinding (com.alipay.sofa.runtime.service.binding.JvmBinding)2 SofaRuntimeManager (com.alipay.sofa.runtime.spi.component.SofaRuntimeManager)2 SampleService (com.alipay.sofa.runtime.test.beans.facade.SampleService)2 DefaultSampleService (com.alipay.sofa.runtime.test.beans.service.DefaultSampleService)2 Expectations (mockit.Expectations)2 MockUp (mockit.MockUp)2 Test (org.junit.Test)2 ProxyFactory (org.springframework.aop.framework.ProxyFactory)2