Search in sources :

Example 1 with Reference

use of com.alipay.sofa.runtime.service.component.Reference 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.");
    if (bindings.size() == 0) {
        bindings.add(new JvmBinding());
    }
    reference.addBinding(bindings.get(0));
    proxy = ReferenceRegisterHelper.registerReference(reference, sofaRuntimeContext);
}
Also used : Reference(com.alipay.sofa.runtime.service.component.Reference) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding)

Example 2 with Reference

use of com.alipay.sofa.runtime.service.component.Reference 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 3 with Reference

use of com.alipay.sofa.runtime.service.component.Reference in project sofa-boot by alipay.

the class ReferenceClientImpl method removeReference.

@Override
public <T> void removeReference(ReferenceParam<T> referenceParam) {
    Reference reference = getReferenceFromReferenceParam(referenceParam);
    ComponentName referenceComponentName = ComponentNameFactory.createComponentName(ReferenceComponent.REFERENCE_COMPONENT_TYPE, reference.getInterfaceType(), reference.getUniqueId() + "#" + ReferenceRegisterHelper.generateBindingHashCode(reference));
    Collection<ComponentInfo> referenceComponents = sofaRuntimeContext.getComponentManager().getComponentInfosByType(ReferenceComponent.REFERENCE_COMPONENT_TYPE);
    for (ComponentInfo referenceComponent : referenceComponents) {
        if (referenceComponent.getName().equals(referenceComponentName)) {
            sofaRuntimeContext.getComponentManager().unregister(referenceComponent);
        }
    }
}
Also used : Reference(com.alipay.sofa.runtime.service.component.Reference) ComponentName(com.alipay.sofa.runtime.api.component.ComponentName) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo)

Example 4 with Reference

use of com.alipay.sofa.runtime.service.component.Reference in project sofa-boot by alipay.

the class ReferenceClientImpl method getReferenceFromReferenceParam.

private <T> Reference getReferenceFromReferenceParam(ReferenceParam<T> referenceParam) {
    BindingParam bindingParam = referenceParam.getBindingParam();
    Reference reference = new ReferenceImpl(referenceParam.getUniqueId(), referenceParam.getInterfaceType(), InterfaceMode.api, referenceParam.isLocalFirst(), referenceParam.isJvmService(), null);
    if (bindingParam == null) {
        // add JVM Binding Default
        reference.addBinding(new JvmBinding());
    } else {
        BindingConverter bindingConverter = BindingFactoryContainer.getBindingConverterFactory().getBindingConverter(bindingParam.getBindingType());
        if (bindingConverter == null) {
            throw new ServiceRuntimeException("Can not found binding converter for binding type " + 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) ReferenceImpl(com.alipay.sofa.runtime.service.component.impl.ReferenceImpl) BindingConverterContext(com.alipay.sofa.runtime.spi.service.BindingConverterContext) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException)

Aggregations

Reference (com.alipay.sofa.runtime.service.component.Reference)4 JvmBinding (com.alipay.sofa.runtime.service.binding.JvmBinding)3 ReferenceImpl (com.alipay.sofa.runtime.service.component.impl.ReferenceImpl)2 ServiceRuntimeException (com.alipay.sofa.runtime.api.ServiceRuntimeException)1 SofaReference (com.alipay.sofa.runtime.api.annotation.SofaReference)1 BindingParam (com.alipay.sofa.runtime.api.client.param.BindingParam)1 ComponentName (com.alipay.sofa.runtime.api.component.ComponentName)1 Binding (com.alipay.sofa.runtime.spi.binding.Binding)1 ComponentInfo (com.alipay.sofa.runtime.spi.component.ComponentInfo)1 BindingConverter (com.alipay.sofa.runtime.spi.service.BindingConverter)1 BindingConverterContext (com.alipay.sofa.runtime.spi.service.BindingConverterContext)1 Field (java.lang.reflect.Field)1 ReflectionUtils (org.springframework.util.ReflectionUtils)1