Search in sources :

Example 11 with JvmBinding

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

the class ReferenceRegisterHelper method registerReference.

public static Object registerReference(Reference reference, BindingAdapterFactory bindingAdapterFactory, SofaRuntimeContext sofaRuntimeContext, ApplicationContext applicationContext) {
    Binding binding = (Binding) reference.getBindings().toArray()[0];
    if (!binding.getBindingType().equals(JvmBinding.JVM_BINDING_TYPE) && !SofaRuntimeProperties.isDisableJvmFirst(sofaRuntimeContext) && reference.isJvmFirst()) {
        // as rpc invocation would be serialized, so here would Not ignore serialized
        reference.addBinding(new JvmBinding());
    }
    ComponentManager componentManager = sofaRuntimeContext.getComponentManager();
    ReferenceComponent referenceComponent = new ReferenceComponent(reference, new DefaultImplementation(), bindingAdapterFactory, sofaRuntimeContext);
    if (componentManager.isRegistered(referenceComponent.getName())) {
        return componentManager.getComponentInfo(referenceComponent.getName()).getImplementation().getTarget();
    }
    ComponentInfo componentInfo = componentManager.registerAndGet(referenceComponent);
    componentInfo.setApplicationContext(applicationContext);
    return componentInfo.getImplementation().getTarget();
}
Also used : JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Binding(com.alipay.sofa.runtime.spi.binding.Binding) ReferenceComponent(com.alipay.sofa.runtime.service.component.ReferenceComponent) ComponentManager(com.alipay.sofa.runtime.spi.component.ComponentManager) DefaultImplementation(com.alipay.sofa.runtime.spi.component.DefaultImplementation) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding)

Example 12 with JvmBinding

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

the class SofaBindingTest method testReferenceBinding.

@Test
public void testReferenceBinding() {
    ComponentManager componentManager = sofaRuntimeContext.getComponentManager();
    ReferenceComponent serializeTrueViaAnnotation = null;
    ReferenceComponent defaultSerializeFalseViaAnnotation = null;
    ReferenceComponent defaultElement = null;
    ReferenceComponent element = null;
    ReferenceComponent noneUniqueId = null;
    Collection<ComponentInfo> componentInfos = componentManager.getComponentInfosByType(ReferenceComponent.REFERENCE_COMPONENT_TYPE);
    for (ComponentInfo componentInfo : componentInfos) {
        String rawName = componentInfo.getName().getRawName();
        if (rawName.contains(ComponentNameUtil.getReferenceComponentName(SampleService.class, "serializeTrueViaAnnotation").getRawName())) {
            serializeTrueViaAnnotation = (ReferenceComponent) componentInfo;
        } else if (rawName.contains(ComponentNameUtil.getReferenceComponentName(SampleService.class, "defaultSerializeFalseViaAnnotation").getRawName())) {
            defaultSerializeFalseViaAnnotation = (ReferenceComponent) componentInfo;
        } else if (rawName.contains(ComponentNameUtil.getReferenceComponentName(SampleService.class, "default-element").getRawName())) {
            defaultElement = (ReferenceComponent) componentInfo;
        } else if (componentInfo.getName().getRawName().contains(ComponentNameUtil.getReferenceComponentName(SampleService.class, "element").getRawName())) {
            element = (ReferenceComponent) componentInfo;
        } else if (rawName.contains(":#") && rawName.contains(ComponentNameUtil.getReferenceComponentName(SampleService.class, "").getRawName())) {
            noneUniqueId = (ReferenceComponent) componentInfo;
        }
    }
    Assert.assertNotNull(serializeTrueViaAnnotation);
    Assert.assertNotNull(defaultSerializeFalseViaAnnotation);
    Assert.assertNotNull(defaultElement);
    Assert.assertNotNull(element);
    Assert.assertNotNull(noneUniqueId);
    JvmBinding jvmBinding;
    jvmBinding = (JvmBinding) serializeTrueViaAnnotation.getReference().getBinding(JvmBinding.JVM_BINDING_TYPE);
    Assert.assertTrue(jvmBinding.getJvmBindingParam().isSerialize());
    jvmBinding = (JvmBinding) defaultSerializeFalseViaAnnotation.getReference().getBinding(JvmBinding.JVM_BINDING_TYPE);
    Assert.assertFalse(jvmBinding.getJvmBindingParam().isSerialize());
    jvmBinding = (JvmBinding) defaultElement.getReference().getBinding(JvmBinding.JVM_BINDING_TYPE);
    Assert.assertFalse(jvmBinding.getJvmBindingParam().isSerialize());
    jvmBinding = (JvmBinding) element.getReference().getBinding(JvmBinding.JVM_BINDING_TYPE);
    Assert.assertTrue(jvmBinding.getJvmBindingParam().isSerialize());
    jvmBinding = (JvmBinding) noneUniqueId.getReference().getBinding(JvmBinding.JVM_BINDING_TYPE);
    Assert.assertFalse(jvmBinding.getJvmBindingParam().isSerialize());
}
Also used : DefaultSampleService(com.alipay.sofa.runtime.test.beans.service.DefaultSampleService) SampleService(com.alipay.sofa.runtime.test.beans.facade.SampleService) ReferenceComponent(com.alipay.sofa.runtime.service.component.ReferenceComponent) ComponentManager(com.alipay.sofa.runtime.spi.component.ComponentManager) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 13 with JvmBinding

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

the class ReferenceFactoryBean method doAfterPropertiesSet.

@Override
protected void doAfterPropertiesSet() throws Exception {
    Reference reference = buildReference();
    Assert.isTrue(bindings.size() <= 1, "Found more than one binding in <sofa:reference/>, <sofa:reference/> can only have one binding.");
    // default add jvm binding and reference jvm binding should set serialize as false
    if (bindings.size() == 0) {
        // default reference prefer to ignore serialize
        JvmBindingParam jvmBindingParam = new JvmBindingParam();
        jvmBindingParam.setSerialize(false);
        bindings.add(new JvmBinding().setJvmBindingParam(jvmBindingParam));
    }
    reference.addBinding(bindings.get(0));
    proxy = ReferenceRegisterHelper.registerReference(reference, bindingAdapterFactory, sofaRuntimeContext, applicationContext);
}
Also used : Reference(com.alipay.sofa.runtime.service.component.Reference) JvmBindingParam(com.alipay.sofa.runtime.service.binding.JvmBindingParam) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding)

Example 14 with JvmBinding

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

the class ServiceAnnotationBeanPostProcessor method processSofaReference.

private void processSofaReference(final Object bean) {
    final Class<?> beanClass = bean.getClass();
    ReflectionUtils.doWithFields(beanClass, new ReflectionUtils.FieldCallback() {

        @Override
        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            SofaReference sofaReferenceAnnotation = field.getAnnotation(SofaReference.class);
            if (sofaReferenceAnnotation == null) {
                return;
            }
            Class<?> interfaceType = sofaReferenceAnnotation.interfaceType();
            if (interfaceType.equals(void.class)) {
                interfaceType = field.getType();
            }
            Reference reference = new ReferenceImpl(sofaReferenceAnnotation.uniqueId(), interfaceType, InterfaceMode.annotation, false, false);
            reference.addBinding(new JvmBinding());
            Object proxy = ReferenceRegisterHelper.registerReference(reference, sofaRuntimeContext);
            ReflectionUtils.makeAccessible(field);
            ReflectionUtils.setField(field, bean, proxy);
        }
    }, new ReflectionUtils.FieldFilter() {

        @Override
        public boolean matches(Field field) {
            return !Modifier.isStatic(field.getModifiers()) && field.isAnnotationPresent(SofaReference.class);
        }
    });
}
Also used : Reference(com.alipay.sofa.runtime.service.component.Reference) SofaReference(com.alipay.sofa.runtime.api.annotation.SofaReference) ReferenceImpl(com.alipay.sofa.runtime.service.component.impl.ReferenceImpl) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Field(java.lang.reflect.Field) SofaReference(com.alipay.sofa.runtime.api.annotation.SofaReference) ReflectionUtils(org.springframework.util.ReflectionUtils)

Example 15 with JvmBinding

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

the class ServiceAnnotationBeanPostProcessor method processSofaService.

private void processSofaService(Object bean, String beanName) {
    final Class<?> beanClass = AopProxyUtils.ultimateTargetClass(bean);
    SofaService sofaServiceAnnotation = beanClass.getAnnotation(SofaService.class);
    if (sofaServiceAnnotation == null) {
        return;
    }
    Class<?> interfaceType = sofaServiceAnnotation.interfaceType();
    if (interfaceType.equals(void.class)) {
        Class<?>[] interfaces = beanClass.getInterfaces();
        if (interfaces == null || interfaces.length == 0 || interfaces.length > 1) {
            throw new ServiceRuntimeException("Bean " + beanName + " does not has any interface or has more than one interface.");
        }
        interfaceType = interfaces[0];
    }
    Implementation implementation = new SpringImplementationImpl(beanName, applicationContext);
    implementation.setTarget(bean);
    Service service = new ServiceImpl(sofaServiceAnnotation.uniqueId(), interfaceType, InterfaceMode.annotation, bean);
    service.addBinding(new JvmBinding());
    ComponentInfo componentInfo = new ServiceComponent(implementation, service, sofaRuntimeContext);
    sofaRuntimeContext.getComponentManager().register(componentInfo);
}
Also used : ServiceComponent(com.alipay.sofa.runtime.service.component.ServiceComponent) ServiceImpl(com.alipay.sofa.runtime.service.component.impl.ServiceImpl) Service(com.alipay.sofa.runtime.service.component.Service) SofaService(com.alipay.sofa.runtime.api.annotation.SofaService) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) SofaService(com.alipay.sofa.runtime.api.annotation.SofaService) Implementation(com.alipay.sofa.runtime.spi.component.Implementation) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException) SpringImplementationImpl(com.alipay.sofa.runtime.spi.spring.SpringImplementationImpl) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding)

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