Search in sources :

Example 1 with Binding

use of com.alipay.sofa.runtime.spi.binding.Binding 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 2 with Binding

use of com.alipay.sofa.runtime.spi.binding.Binding 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 3 with Binding

use of com.alipay.sofa.runtime.spi.binding.Binding in project sofa-boot by sofastack.

the class AbstractContractFactoryBean method parseBindings.

protected List<Binding> parseBindings(List<Element> parseElements, ApplicationContext appContext, boolean isInBinding) {
    List<Binding> result = new ArrayList<>();
    if (parseElements != null) {
        for (Element element : parseElements) {
            String tagName = element.getLocalName();
            BindingConverter bindingConverter = bindingConverterFactory.getBindingConverterByTagName(tagName);
            if (bindingConverter == null) {
                dealWithbindingConverterNotExist(tagName);
                continue;
            }
            BindingConverterContext bindingConverterContext = new BindingConverterContext();
            bindingConverterContext.setInBinding(isInBinding);
            bindingConverterContext.setApplicationContext(appContext);
            bindingConverterContext.setAppName(sofaRuntimeContext.getAppName());
            bindingConverterContext.setAppClassLoader(sofaRuntimeContext.getAppClassLoader());
            bindingConverterContext.setRepeatReferLimit(repeatReferLimit);
            bindingConverterContext.setBeanId(beanId);
            setProperties(bindingConverterContext);
            Binding binding = bindingConverter.convert(element, bindingConverterContext);
            result.add(binding);
        }
    }
    return result;
}
Also used : Binding(com.alipay.sofa.runtime.spi.binding.Binding) Element(org.w3c.dom.Element) BindingConverter(com.alipay.sofa.runtime.spi.service.BindingConverter) ArrayList(java.util.ArrayList) BindingConverterContext(com.alipay.sofa.runtime.spi.service.BindingConverterContext)

Example 4 with Binding

use of com.alipay.sofa.runtime.spi.binding.Binding in project sofa-boot by sofastack.

the class ServiceClientImpl method service.

@SuppressWarnings("unchecked")
public void service(ServiceParam serviceParam) {
    Implementation implementation = new DefaultImplementation();
    implementation.setTarget(serviceParam.getInstance());
    if (serviceParam.getInterfaceType() == null) {
        throw new ServiceRuntimeException(ErrorCode.convert("01-00201"));
    }
    Service service = new ServiceImpl(serviceParam.getUniqueId(), serviceParam.getInterfaceType(), InterfaceMode.api, serviceParam.getInstance(), null);
    for (BindingParam bindingParam : serviceParam.getBindingParams()) {
        BindingConverter bindingConverter = bindingConverterFactory.getBindingConverter(bindingParam.getBindingType());
        if (bindingConverter == null) {
            throw new ServiceRuntimeException(ErrorCode.convert("01-00200", bindingParam.getBindingType()));
        }
        BindingConverterContext bindingConverterContext = new BindingConverterContext();
        bindingConverterContext.setInBinding(false);
        bindingConverterContext.setAppName(sofaRuntimeContext.getAppName());
        bindingConverterContext.setAppClassLoader(sofaRuntimeContext.getAppClassLoader());
        Binding binding = bindingConverter.convert(bindingParam, bindingConverterContext);
        service.addBinding(binding);
    }
    boolean hasJvmBinding = false;
    for (Binding binding : service.getBindings()) {
        if (binding.getBindingType().equals(JvmBinding.JVM_BINDING_TYPE)) {
            hasJvmBinding = true;
            break;
        }
    }
    if (!hasJvmBinding) {
        service.addBinding(new JvmBinding());
    }
    ComponentInfo componentInfo = new ServiceComponent(implementation, service, bindingAdapterFactory, sofaRuntimeContext);
    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) ServiceImpl(com.alipay.sofa.runtime.service.component.impl.ServiceImpl) BindingConverter(com.alipay.sofa.runtime.spi.service.BindingConverter) Service(com.alipay.sofa.runtime.service.component.Service) DefaultImplementation(com.alipay.sofa.runtime.spi.component.DefaultImplementation) BindingParam(com.alipay.sofa.runtime.api.client.param.BindingParam) DefaultImplementation(com.alipay.sofa.runtime.spi.component.DefaultImplementation) Implementation(com.alipay.sofa.runtime.spi.component.Implementation) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) BindingConverterContext(com.alipay.sofa.runtime.spi.service.BindingConverterContext)

Example 5 with Binding

use of com.alipay.sofa.runtime.spi.binding.Binding in project sofa-boot by sofastack.

the class ReferenceComponent method isHealthy.

@Override
public HealthResult isHealthy() {
    HealthResult result = new HealthResult(componentName.getRawName());
    List<HealthResult> bindingHealth = new ArrayList<>();
    JvmBinding jvmBinding = null;
    HealthResult jvmBindingHealthResult = null;
    if (reference.hasBinding()) {
        for (Binding binding : reference.getBindings()) {
            bindingHealth.add(binding.healthCheck());
            if (JvmBinding.JVM_BINDING_TYPE.equals(binding.getBindingType())) {
                jvmBinding = (JvmBinding) binding;
                jvmBindingHealthResult = bindingHealth.get(bindingHealth.size() - 1);
            }
        }
    }
    // check reference has a corresponding service
    if (!SofaRuntimeProperties.isSkipJvmReferenceHealthCheck(sofaRuntimeContext) && jvmBinding != null) {
        Object serviceTarget = getServiceTarget();
        if (serviceTarget == null && !jvmBinding.hasBackupProxy()) {
            jvmBindingHealthResult.setHealthy(false);
            jvmBindingHealthResult.setHealthReport("can not find corresponding jvm service");
        }
    }
    List<HealthResult> failedBindingHealth = new ArrayList<>();
    for (HealthResult healthResult : bindingHealth) {
        if (healthResult != null && !healthResult.isHealthy()) {
            failedBindingHealth.add(healthResult);
        }
    }
    result.setHealthy(failedBindingHealth.size() == 0);
    String report = aggregateBindingHealth(reference.getBindings());
    if (e != null) {
        report += " [" + e.getMessage() + "]";
        result.setHealthy(false);
    }
    result.setHealthReport(report);
    return result;
}
Also used : JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Binding(com.alipay.sofa.runtime.spi.binding.Binding) ArrayList(java.util.ArrayList) HealthResult(com.alipay.sofa.runtime.spi.health.HealthResult) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding)

Aggregations

Binding (com.alipay.sofa.runtime.spi.binding.Binding)42 ServiceRuntimeException (com.alipay.sofa.runtime.api.ServiceRuntimeException)23 JvmBinding (com.alipay.sofa.runtime.service.binding.JvmBinding)22 BindingConverter (com.alipay.sofa.runtime.spi.service.BindingConverter)12 BindingConverterContext (com.alipay.sofa.runtime.spi.service.BindingConverterContext)12 ComponentInfo (com.alipay.sofa.runtime.spi.component.ComponentInfo)10 DefaultImplementation (com.alipay.sofa.runtime.spi.component.DefaultImplementation)10 ArrayList (java.util.ArrayList)10 SofaReferenceBinding (com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding)6 BindingType (com.alipay.sofa.runtime.api.binding.BindingType)6 ReferenceComponent (com.alipay.sofa.runtime.service.component.ReferenceComponent)6 SofaServiceBinding (com.alipay.sofa.runtime.api.annotation.SofaServiceBinding)4 BindingParam (com.alipay.sofa.runtime.api.client.param.BindingParam)4 JvmBindingParam (com.alipay.sofa.runtime.service.binding.JvmBindingParam)4 Reference (com.alipay.sofa.runtime.service.component.Reference)4 ServiceComponent (com.alipay.sofa.runtime.service.component.ServiceComponent)4 ReferenceImpl (com.alipay.sofa.runtime.service.component.impl.ReferenceImpl)4 ComponentManager (com.alipay.sofa.runtime.spi.component.ComponentManager)4 Implementation (com.alipay.sofa.runtime.spi.component.Implementation)4 HealthResult (com.alipay.sofa.runtime.spi.health.HealthResult)4