Search in sources :

Example 1 with BindingType

use of com.alipay.sofa.runtime.api.binding.BindingType in project sofa-boot by sofastack.

the class SofaServiceAndReferenceTest method testSofaServiceWithMultipleBindings.

@Test
public void testSofaServiceWithMultipleBindings() {
    Map<String, Object> properties = new HashMap<>();
    properties.put("spring.application.name", "SofaServiceAndReferenceTest");
    SpringApplication springApplication = new SpringApplication(MultipleBindingsSofaServiceConfiguration.class, RuntimeConfiguration.class);
    springApplication.setWebApplicationType(WebApplicationType.NONE);
    springApplication.setDefaultProperties(properties);
    ApplicationContext ctx = springApplication.run();
    ServiceFactoryBean bean = ctx.getBean(ServiceFactoryBean.class);
    Assert.assertEquals(2, bean.getBindings().size());
    Assert.assertEquals(new BindingType("jvm"), bean.getBindings().get(0).getBindingType());
    Assert.assertEquals(new BindingType("jvm"), bean.getBindings().get(1).getBindingType());
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) HashMap(java.util.HashMap) SpringApplication(org.springframework.boot.SpringApplication) BindingType(com.alipay.sofa.runtime.api.binding.BindingType) ServiceFactoryBean(com.alipay.sofa.runtime.spring.factory.ServiceFactoryBean) Test(org.junit.Test)

Example 2 with BindingType

use of com.alipay.sofa.runtime.api.binding.BindingType in project sofa-boot by sofastack.

the class ReferenceAnnotationBeanPostProcessor method createReferenceProxy.

private Object createReferenceProxy(SofaReference sofaReferenceAnnotation, Class<?> interfaceType) {
    Reference reference = new ReferenceImpl(sofaReferenceAnnotation.uniqueId(), interfaceType, InterfaceMode.annotation, sofaReferenceAnnotation.jvmFirst());
    BindingConverter bindingConverter = bindingConverterFactory.getBindingConverter(new BindingType(sofaReferenceAnnotation.binding().bindingType()));
    if (bindingConverter == null) {
        throw new ServiceRuntimeException(ErrorCode.convert("01-00200", sofaReferenceAnnotation.binding().bindingType()));
    }
    BindingConverterContext bindingConverterContext = new BindingConverterContext();
    bindingConverterContext.setInBinding(true);
    bindingConverterContext.setApplicationContext(applicationContext);
    bindingConverterContext.setAppName(sofaRuntimeContext.getAppName());
    bindingConverterContext.setAppClassLoader(sofaRuntimeContext.getAppClassLoader());
    Binding binding = bindingConverter.convert(sofaReferenceAnnotation, sofaReferenceAnnotation.binding(), bindingConverterContext);
    reference.addBinding(binding);
    return ReferenceRegisterHelper.registerReference(reference, bindingAdapterFactory, sofaRuntimeContext, applicationContext);
}
Also used : Binding(com.alipay.sofa.runtime.spi.binding.Binding) Reference(com.alipay.sofa.runtime.service.component.Reference) SofaReference(com.alipay.sofa.runtime.api.annotation.SofaReference) BindingType(com.alipay.sofa.runtime.api.binding.BindingType) BindingConverter(com.alipay.sofa.runtime.spi.service.BindingConverter) ReferenceImpl(com.alipay.sofa.runtime.service.component.impl.ReferenceImpl) BindingConverterContext(com.alipay.sofa.runtime.spi.service.BindingConverterContext) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException)

Example 3 with BindingType

use of com.alipay.sofa.runtime.api.binding.BindingType in project sofa-boot by sofastack.

the class ServiceBeanFactoryPostProcessor method getSofaServiceBinding.

private List<Binding> getSofaServiceBinding(SofaService sofaServiceAnnotation, SofaServiceBinding[] sofaServiceBindings) {
    List<Binding> bindings = new ArrayList<>();
    for (SofaServiceBinding sofaServiceBinding : sofaServiceBindings) {
        BindingConverter bindingConverter = bindingConverterFactory.getBindingConverter(new BindingType(sofaServiceBinding.bindingType()));
        if (bindingConverter == null) {
            throw new ServiceRuntimeException(ErrorCode.convert("01-00200", sofaServiceBinding.bindingType()));
        }
        BindingConverterContext bindingConverterContext = new BindingConverterContext();
        bindingConverterContext.setInBinding(false);
        bindingConverterContext.setApplicationContext(applicationContext);
        bindingConverterContext.setAppName(sofaRuntimeContext.getAppName());
        bindingConverterContext.setAppClassLoader(sofaRuntimeContext.getAppClassLoader());
        Binding binding = bindingConverter.convert(sofaServiceAnnotation, sofaServiceBinding, bindingConverterContext);
        bindings.add(binding);
    }
    return bindings;
}
Also used : SofaServiceBinding(com.alipay.sofa.runtime.api.annotation.SofaServiceBinding) Binding(com.alipay.sofa.runtime.spi.binding.Binding) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) SofaReferenceBinding(com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding) SofaServiceBinding(com.alipay.sofa.runtime.api.annotation.SofaServiceBinding) BindingType(com.alipay.sofa.runtime.api.binding.BindingType) BindingConverter(com.alipay.sofa.runtime.spi.service.BindingConverter) ArrayList(java.util.ArrayList) BindingConverterContext(com.alipay.sofa.runtime.spi.service.BindingConverterContext) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException)

Example 4 with BindingType

use of com.alipay.sofa.runtime.api.binding.BindingType in project sofa-boot by sofastack.

the class SofaBindingTest method testBindingType.

@Test
public void testBindingType() {
    Assert.assertTrue(new BindingType("jvm").equals(new BindingType("jvm")));
    Assert.assertFalse(new BindingType("jvm").equals(new BindingType("bolt")));
    Assert.assertFalse(new BindingType("jvm").equals(null));
    Assert.assertFalse(new BindingType("jvm").equals("jvm"));
    HashMap<BindingType, String> map = new HashMap<>();
    map.put(new BindingType("jvm"), "jvm");
    map.put(new BindingType("bolt"), "bolt");
    map.put(new BindingType("rest"), "rest");
    Assert.assertEquals(map.get(new BindingType("jvm")), "jvm");
    Assert.assertEquals(map.get(new BindingType("bolt")), "bolt");
    Assert.assertEquals(map.get(new BindingType("rest")), "rest");
    Assert.assertEquals(map.get("jvm"), null);
    Assert.assertEquals(map.get(null), null);
}
Also used : HashMap(java.util.HashMap) BindingType(com.alipay.sofa.runtime.api.binding.BindingType) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with BindingType

use of com.alipay.sofa.runtime.api.binding.BindingType in project sofa-boot by alipay.

the class ServiceBeanFactoryPostProcessor method getSofaReferenceBinding.

/**
 * get sofa reference binding annotated on parameter. At present, only jvm sofa reference is supported .
 * @param sofaReferenceAnnotation
 * @param sofaReferenceBinding
 * @return
 */
private List<Binding> getSofaReferenceBinding(SofaReference sofaReferenceAnnotation, SofaReferenceBinding sofaReferenceBinding) {
    if (!JvmBinding.XmlConstants.BINDING_TYPE.equals(sofaReferenceBinding.bindingType())) {
        throw new ServiceRuntimeException(ErrorCode.convert("01-02005"));
    }
    List<Binding> bindings = new ArrayList<>();
    BindingConverter bindingConverter = bindingConverterFactory.getBindingConverter(new BindingType(sofaReferenceBinding.bindingType()));
    if (bindingConverter == null) {
        throw new ServiceRuntimeException(ErrorCode.convert("01-00200", sofaReferenceBinding.bindingType()));
    }
    BindingConverterContext bindingConverterContext = new BindingConverterContext();
    bindingConverterContext.setInBinding(true);
    bindingConverterContext.setApplicationContext(applicationContext);
    bindingConverterContext.setAppName(sofaRuntimeContext.getAppName());
    bindingConverterContext.setAppClassLoader(sofaRuntimeContext.getAppClassLoader());
    Binding binding = bindingConverter.convert(sofaReferenceAnnotation, sofaReferenceBinding, bindingConverterContext);
    bindings.add(binding);
    return bindings;
}
Also used : SofaServiceBinding(com.alipay.sofa.runtime.api.annotation.SofaServiceBinding) Binding(com.alipay.sofa.runtime.spi.binding.Binding) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) SofaReferenceBinding(com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding) BindingType(com.alipay.sofa.runtime.api.binding.BindingType) BindingConverter(com.alipay.sofa.runtime.spi.service.BindingConverter) ArrayList(java.util.ArrayList) BindingConverterContext(com.alipay.sofa.runtime.spi.service.BindingConverterContext) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException)

Aggregations

BindingType (com.alipay.sofa.runtime.api.binding.BindingType)10 ServiceRuntimeException (com.alipay.sofa.runtime.api.ServiceRuntimeException)6 Binding (com.alipay.sofa.runtime.spi.binding.Binding)6 BindingConverter (com.alipay.sofa.runtime.spi.service.BindingConverter)6 BindingConverterContext (com.alipay.sofa.runtime.spi.service.BindingConverterContext)6 SofaReferenceBinding (com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding)4 SofaServiceBinding (com.alipay.sofa.runtime.api.annotation.SofaServiceBinding)4 JvmBinding (com.alipay.sofa.runtime.service.binding.JvmBinding)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 SofaReference (com.alipay.sofa.runtime.api.annotation.SofaReference)2 Reference (com.alipay.sofa.runtime.service.component.Reference)2 ReferenceImpl (com.alipay.sofa.runtime.service.component.impl.ReferenceImpl)2 ServiceFactoryBean (com.alipay.sofa.runtime.spring.factory.ServiceFactoryBean)2 SpringApplication (org.springframework.boot.SpringApplication)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 ApplicationContext (org.springframework.context.ApplicationContext)2