use of com.alipay.sofa.runtime.api.annotation.SofaReference in project sofa-boot by alipay.
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);
}
use of com.alipay.sofa.runtime.api.annotation.SofaReference in project sofa-boot by alipay.
the class AnnotationPlaceHolderTest method testReferenceAnnotationPlaceHolder.
@Test
@SuppressWarnings("unchecked")
public void testReferenceAnnotationPlaceHolder() throws Exception {
PlaceHolderBinder binder = new PlaceHolderBinder() {
@Override
public String bind(String origin) {
return environment.resolvePlaceholders(origin);
}
};
SofaReference sofaReference = AnnotationSampleService.class.getField("sampleService").getAnnotation(SofaReference.class);
PlaceHolderAnnotationInvocationHandler.AnnotationWrapperBuilder<SofaReference> builder = PlaceHolderAnnotationInvocationHandler.AnnotationWrapperBuilder.wrap(sofaReference).withBinder(binder);
SofaReference delegate = builder.build();
Assert.assertEquals("${annotation.sample.ref.uniqueId}", sofaReference.uniqueId());
Assert.assertEquals("sample-reference-uniqueId", delegate.uniqueId());
Assert.assertFalse(sofaReference.jvmFirst());
Assert.assertFalse(delegate.jvmFirst());
SofaReferenceBinding binding = sofaReference.binding();
SofaReferenceBinding delegateBinding = delegate.binding();
Assert.assertEquals("${annotation.sample.ref.bindingType}", binding.bindingType());
Assert.assertEquals("bolt", delegateBinding.bindingType());
Assert.assertEquals("${annotation.sample.ref.direct-url}", binding.directUrl());
Assert.assertEquals("127.0.0.1", delegateBinding.directUrl());
String[] filters = binding.filters();
String[] delegateFilters = delegateBinding.filters();
Assert.assertEquals(2, filters.length);
Assert.assertEquals(2, delegateFilters.length);
Assert.assertEquals("${annotation.sample.ref.filter-1}", filters[0]);
Assert.assertEquals("reference-filter-1", delegateFilters[0]);
Assert.assertEquals("filter-2", filters[1]);
Assert.assertEquals("filter-2", delegateFilters[1]);
Assert.assertTrue(delegate instanceof WrapperAnnotation);
Assert.assertTrue(delegateBinding instanceof WrapperAnnotation);
}
use of com.alipay.sofa.runtime.api.annotation.SofaReference in project sofa-boot by sofastack.
the class RpcBindingConverterTest method parseSofaMethods.
@Test
public void parseSofaMethods() {
RpcBindingConverter rpcBindingConverter = new BoltBindingConverter();
SofaReference reference = null;
try {
reference = RpcBindingConverterTest.class.getDeclaredField("testAnnotation").getAnnotation(SofaReference.class);
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
List<RpcBindingMethodInfo> result = rpcBindingConverter.parseSofaMethods(reference.binding().methodInfos());
Assert.assertEquals(1, result.size());
final RpcBindingMethodInfo rpcBindingMethodInfo = result.get(0);
Assert.assertEquals("test", rpcBindingMethodInfo.getName());
Assert.assertEquals(1, rpcBindingMethodInfo.getRetries().intValue());
Assert.assertEquals("callback", rpcBindingMethodInfo.getType());
Assert.assertEquals("class", rpcBindingMethodInfo.getCallbackClass());
Assert.assertEquals("ref", rpcBindingMethodInfo.getCallbackRef());
Assert.assertEquals(2000, rpcBindingMethodInfo.getTimeout().intValue());
}
use of com.alipay.sofa.runtime.api.annotation.SofaReference in project sofa-boot by sofastack.
the class ServiceBeanFactoryPostProcessor method generateSofaReferenceDefinition.
private void generateSofaReferenceDefinition(String beanId, Method method, BeanDefinitionRegistry registry) {
Class<?>[] parameterTypes = method.getParameterTypes();
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
for (int i = 0; i < parameterAnnotations.length; ++i) {
for (Annotation annotation : parameterAnnotations[i]) {
if (annotation instanceof SofaReference) {
doGenerateSofaReferenceDefinition(registry.getBeanDefinition(beanId), (SofaReference) annotation, parameterTypes[i], registry);
}
}
}
}
use of com.alipay.sofa.runtime.api.annotation.SofaReference in project sofa-boot by sofastack.
the class ServiceBeanFactoryPostProcessor method doGenerateSofaReferenceDefinition.
@SuppressWarnings("unchecked")
private void doGenerateSofaReferenceDefinition(BeanDefinition beanDefinition, SofaReference sofaReference, Class<?> parameterType, BeanDefinitionRegistry registry) {
Assert.isTrue(JvmBinding.JVM_BINDING_TYPE.getType().equals(sofaReference.binding().bindingType()), "Only jvm type of @SofaReference on parameter is supported.");
AnnotationWrapperBuilder<SofaReference> wrapperBuilder = AnnotationWrapperBuilder.wrap(sofaReference).withBinder(binder);
sofaReference = wrapperBuilder.build();
Class<?> interfaceType = sofaReference.interfaceType();
if (interfaceType.equals(void.class)) {
interfaceType = parameterType;
}
String uniqueId = sofaReference.uniqueId();
String referenceId = SofaBeanNameGenerator.generateSofaReferenceBeanName(interfaceType, uniqueId);
// build sofa reference definition
if (!registry.containsBeanDefinition(referenceId)) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition();
builder.getRawBeanDefinition().setScope(beanDefinition.getScope());
builder.getRawBeanDefinition().setLazyInit(beanDefinition.isLazyInit());
builder.getRawBeanDefinition().setBeanClass(ReferenceFactoryBean.class);
builder.addAutowiredProperty(AbstractContractDefinitionParser.SOFA_RUNTIME_CONTEXT);
builder.addAutowiredProperty(AbstractContractDefinitionParser.BINDING_CONVERTER_FACTORY);
builder.addAutowiredProperty(AbstractContractDefinitionParser.BINDING_ADAPTER_FACTORY);
builder.addPropertyValue(AbstractContractDefinitionParser.UNIQUE_ID_PROPERTY, uniqueId);
builder.addPropertyValue(AbstractContractDefinitionParser.INTERFACE_CLASS_PROPERTY, interfaceType);
builder.addPropertyValue(AbstractContractDefinitionParser.BINDINGS, getSofaReferenceBinding(sofaReference, sofaReference.binding()));
builder.addPropertyValue(AbstractContractDefinitionParser.DEFINITION_BUILDING_API_TYPE, true);
registry.registerBeanDefinition(referenceId, builder.getBeanDefinition());
}
// add bean dependency relationship
if (beanDefinition.getDependsOn() == null) {
beanDefinition.setDependsOn(referenceId);
} else {
String[] added = ObjectUtils.addObjectToArray(beanDefinition.getDependsOn(), referenceId);
beanDefinition.setDependsOn(added);
}
}
Aggregations