Search in sources :

Example 16 with JvmBinding

use of com.alipay.sofa.runtime.service.binding.JvmBinding in project sofa-boot by alipay.

the class ReferenceComponent method publishAsService.

private void publishAsService(Reference reference, Object target) {
    if (!reference.jvmService()) {
        return;
    }
    Implementation serviceImplementation = new DefaultImplementation();
    serviceImplementation.setTarget(target);
    Service service = new ServiceImpl("", reference.getInterfaceType(), InterfaceMode.api, target);
    service.addBinding(new JvmBinding());
    ComponentInfo serviceComponent = new ServiceComponent(implementation, service, sofaRuntimeContext);
    sofaRuntimeContext.getComponentManager().register(serviceComponent);
}
Also used : ServiceImpl(com.alipay.sofa.runtime.service.component.impl.ServiceImpl) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding)

Example 17 with JvmBinding

use of com.alipay.sofa.runtime.service.binding.JvmBinding in project sofa-boot by alipay.

the class SofaBindingTest method testServiceBinding.

@Test
public void testServiceBinding() {
    ComponentManager componentManager = sofaRuntimeContext.getComponentManager();
    ServiceComponent serializeFalseViaAnnotation = (ServiceComponent) componentManager.getComponentInfo(ComponentNameUtil.getServiceComponentName(SampleService.class, "serializeFalseViaAnnotation"));
    ServiceComponent defaultSerializeTrueViaAnnotation = (ServiceComponent) componentManager.getComponentInfo(ComponentNameUtil.getServiceComponentName(SampleService.class, "defaultSerializeTrueViaAnnotation"));
    ServiceComponent defaultElement = (ServiceComponent) componentManager.getComponentInfo(ComponentNameUtil.getServiceComponentName(SampleService.class, "default-element"));
    ServiceComponent element = (ServiceComponent) componentManager.getComponentInfo(ComponentNameUtil.getServiceComponentName(SampleService.class, "element"));
    ServiceComponent noneUniqueId = (ServiceComponent) componentManager.getComponentInfo(ComponentNameUtil.getServiceComponentName(SampleService.class, ""));
    Assert.assertNotNull(serializeFalseViaAnnotation);
    Assert.assertNotNull(defaultSerializeTrueViaAnnotation);
    Assert.assertNotNull(defaultElement);
    Assert.assertNotNull(element);
    Assert.assertNotNull(noneUniqueId);
    JvmBinding jvmBinding;
    jvmBinding = (JvmBinding) serializeFalseViaAnnotation.getService().getBinding(JvmBinding.JVM_BINDING_TYPE);
    Assert.assertFalse(jvmBinding.getJvmBindingParam().isSerialize());
    jvmBinding = (JvmBinding) defaultSerializeTrueViaAnnotation.getService().getBinding(JvmBinding.JVM_BINDING_TYPE);
    Assert.assertTrue(jvmBinding.getJvmBindingParam().isSerialize());
    jvmBinding = (JvmBinding) defaultElement.getService().getBinding(JvmBinding.JVM_BINDING_TYPE);
    Assert.assertTrue(jvmBinding.getJvmBindingParam().isSerialize());
    jvmBinding = (JvmBinding) element.getService().getBinding(JvmBinding.JVM_BINDING_TYPE);
    Assert.assertFalse(jvmBinding.getJvmBindingParam().isSerialize());
    jvmBinding = (JvmBinding) noneUniqueId.getService().getBinding(JvmBinding.JVM_BINDING_TYPE);
    Assert.assertTrue(jvmBinding.getJvmBindingParam().isSerialize());
}
Also used : ServiceComponent(com.alipay.sofa.runtime.service.component.ServiceComponent) ComponentManager(com.alipay.sofa.runtime.spi.component.ComponentManager) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 18 with JvmBinding

use of com.alipay.sofa.runtime.service.binding.JvmBinding 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 19 with JvmBinding

use of com.alipay.sofa.runtime.service.binding.JvmBinding in project sofa-boot by alipay.

the class ServiceFactoryBean method doAfterPropertiesSet.

@Override
protected void doAfterPropertiesSet() {
    if (!apiType && hasSofaServiceAnnotation()) {
        throw new ServiceRuntimeException(ErrorCode.convert("01-00103", beanId, ref.getClass()));
    }
    Implementation implementation = new DefaultImplementation();
    implementation.setTarget(ref);
    service = buildService();
    // default add jvm binding and service jvm binding should set serialize as true
    if (bindings.size() == 0) {
        JvmBindingParam jvmBindingParam = new JvmBindingParam().setSerialize(true);
        bindings.add(new JvmBinding().setJvmBindingParam(jvmBindingParam));
    }
    for (Binding binding : bindings) {
        service.addBinding(binding);
    }
    ComponentInfo componentInfo = new ServiceComponent(implementation, service, bindingAdapterFactory, sofaRuntimeContext);
    componentInfo.setApplicationContext(applicationContext);
    sofaRuntimeContext.getComponentManager().register(componentInfo);
}
Also used : JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Binding(com.alipay.sofa.runtime.spi.binding.Binding) ServiceComponent(com.alipay.sofa.runtime.service.component.ServiceComponent) DefaultImplementation(com.alipay.sofa.runtime.spi.component.DefaultImplementation) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) DefaultImplementation(com.alipay.sofa.runtime.spi.component.DefaultImplementation) Implementation(com.alipay.sofa.runtime.spi.component.Implementation) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException) JvmBindingParam(com.alipay.sofa.runtime.service.binding.JvmBindingParam) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding)

Example 20 with JvmBinding

use of com.alipay.sofa.runtime.service.binding.JvmBinding in project sofa-boot by alipay.

the class ReferenceClientImpl method getReferenceFromReferenceParam.

@SuppressWarnings("unchecked")
private <T> Reference getReferenceFromReferenceParam(ReferenceParam<T> referenceParam) {
    BindingParam bindingParam = referenceParam.getBindingParam();
    Reference reference = new ReferenceImpl(referenceParam.getUniqueId(), referenceParam.getInterfaceType(), InterfaceMode.api, referenceParam.isJvmFirst(), null);
    if (bindingParam == null) {
        // default add jvm binding and reference jvm binding should set serialize as false
        JvmBindingParam jvmBindingParam = new JvmBindingParam();
        jvmBindingParam.setSerialize(false);
        reference.addBinding(new JvmBinding().setJvmBindingParam(jvmBindingParam));
    } else {
        BindingConverter bindingConverter = bindingConverterFactory.getBindingConverter(bindingParam.getBindingType());
        if (bindingConverter == null) {
            throw new ServiceRuntimeException(ErrorCode.convert("01-00200", bindingParam.getBindingType()));
        }
        BindingConverterContext bindingConverterContext = new BindingConverterContext();
        bindingConverterContext.setInBinding(true);
        bindingConverterContext.setAppName(sofaRuntimeContext.getAppName());
        bindingConverterContext.setAppClassLoader(sofaRuntimeContext.getAppClassLoader());
        Binding binding = bindingConverter.convert(bindingParam, bindingConverterContext);
        reference.addBinding(binding);
    }
    return reference;
}
Also used : JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Binding(com.alipay.sofa.runtime.spi.binding.Binding) Reference(com.alipay.sofa.runtime.service.component.Reference) BindingConverter(com.alipay.sofa.runtime.spi.service.BindingConverter) BindingParam(com.alipay.sofa.runtime.api.client.param.BindingParam) JvmBindingParam(com.alipay.sofa.runtime.service.binding.JvmBindingParam) ReferenceImpl(com.alipay.sofa.runtime.service.component.impl.ReferenceImpl) BindingConverterContext(com.alipay.sofa.runtime.spi.service.BindingConverterContext) JvmBindingParam(com.alipay.sofa.runtime.service.binding.JvmBindingParam) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException)

Aggregations

JvmBinding (com.alipay.sofa.runtime.service.binding.JvmBinding)27 Binding (com.alipay.sofa.runtime.spi.binding.Binding)14 ComponentInfo (com.alipay.sofa.runtime.spi.component.ComponentInfo)11 DefaultImplementation (com.alipay.sofa.runtime.spi.component.DefaultImplementation)10 ServiceComponent (com.alipay.sofa.runtime.service.component.ServiceComponent)9 ServiceRuntimeException (com.alipay.sofa.runtime.api.ServiceRuntimeException)8 ComponentManager (com.alipay.sofa.runtime.spi.component.ComponentManager)8 JvmBindingParam (com.alipay.sofa.runtime.service.binding.JvmBindingParam)6 ReferenceComponent (com.alipay.sofa.runtime.service.component.ReferenceComponent)6 Test (org.junit.Test)6 Reference (com.alipay.sofa.runtime.service.component.Reference)5 Implementation (com.alipay.sofa.runtime.spi.component.Implementation)5 BindingParam (com.alipay.sofa.runtime.api.client.param.BindingParam)4 ServiceImpl (com.alipay.sofa.runtime.service.component.impl.ServiceImpl)4 SofaRuntimeManager (com.alipay.sofa.runtime.spi.component.SofaRuntimeManager)4 BindingConverter (com.alipay.sofa.runtime.spi.service.BindingConverter)4 BindingConverterContext (com.alipay.sofa.runtime.spi.service.BindingConverterContext)4 SampleService (com.alipay.sofa.runtime.test.beans.facade.SampleService)4 DefaultSampleService (com.alipay.sofa.runtime.test.beans.service.DefaultSampleService)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4