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