Search in sources :

Example 1 with ReferenceImpl

use of com.alipay.sofa.runtime.service.component.impl.ReferenceImpl 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 2 with ReferenceImpl

use of com.alipay.sofa.runtime.service.component.impl.ReferenceImpl 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

JvmBinding (com.alipay.sofa.runtime.service.binding.JvmBinding)2 Reference (com.alipay.sofa.runtime.service.component.Reference)2 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 Binding (com.alipay.sofa.runtime.spi.binding.Binding)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