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);
}
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);
}
});
}
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);
}
}
}
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;
}
Aggregations