Search in sources :

Example 11 with SofaReference

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);
}
Also used : Binding(com.alipay.sofa.runtime.spi.binding.Binding) Reference(com.alipay.sofa.runtime.service.component.Reference) SofaReference(com.alipay.sofa.runtime.api.annotation.SofaReference) BindingType(com.alipay.sofa.runtime.api.binding.BindingType) BindingConverter(com.alipay.sofa.runtime.spi.service.BindingConverter) ReferenceImpl(com.alipay.sofa.runtime.service.component.impl.ReferenceImpl) BindingConverterContext(com.alipay.sofa.runtime.spi.service.BindingConverterContext) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException)

Example 12 with SofaReference

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);
}
Also used : PlaceHolderAnnotationInvocationHandler(com.alipay.sofa.boot.annotation.PlaceHolderAnnotationInvocationHandler) AnnotationSampleService(com.alipay.sofa.runtime.test.beans.service.AnnotationSampleService) SofaReference(com.alipay.sofa.runtime.api.annotation.SofaReference) SofaReferenceBinding(com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding) PlaceHolderBinder(com.alipay.sofa.boot.annotation.PlaceHolderBinder) WrapperAnnotation(com.alipay.sofa.boot.annotation.WrapperAnnotation) Test(org.junit.Test)

Example 13 with SofaReference

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());
}
Also used : RpcBindingConverter(com.alipay.sofa.rpc.boot.runtime.converter.RpcBindingConverter) SofaReference(com.alipay.sofa.runtime.api.annotation.SofaReference) BoltBindingConverter(com.alipay.sofa.rpc.boot.runtime.converter.BoltBindingConverter) RpcBindingMethodInfo(com.alipay.sofa.rpc.boot.runtime.binding.RpcBindingMethodInfo) Test(org.junit.Test)

Example 14 with SofaReference

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);
            }
        }
    }
}
Also used : SofaReference(com.alipay.sofa.runtime.api.annotation.SofaReference) Annotation(java.lang.annotation.Annotation)

Example 15 with SofaReference

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);
    }
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) SofaReference(com.alipay.sofa.runtime.api.annotation.SofaReference)

Aggregations

SofaReference (com.alipay.sofa.runtime.api.annotation.SofaReference)18 Annotation (java.lang.annotation.Annotation)8 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)6 PlaceHolderBinder (com.alipay.sofa.boot.annotation.PlaceHolderBinder)5 ServiceRuntimeException (com.alipay.sofa.runtime.api.ServiceRuntimeException)5 SofaReferenceBinding (com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding)5 BindingType (com.alipay.sofa.runtime.api.binding.BindingType)5 Binding (com.alipay.sofa.runtime.spi.binding.Binding)5 BindingConverter (com.alipay.sofa.runtime.spi.service.BindingConverter)5 BindingConverterContext (com.alipay.sofa.runtime.spi.service.BindingConverterContext)5 JvmBinding (com.alipay.sofa.runtime.service.binding.JvmBinding)4 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)4 AnnotationWrapperBuilder (com.alipay.sofa.boot.annotation.PlaceHolderAnnotationInvocationHandler.AnnotationWrapperBuilder)3 ErrorCode (com.alipay.sofa.boot.error.ErrorCode)3 BeanDefinitionUtil (com.alipay.sofa.boot.util.BeanDefinitionUtil)3 SofaService (com.alipay.sofa.runtime.api.annotation.SofaService)3 SofaServiceBinding (com.alipay.sofa.runtime.api.annotation.SofaServiceBinding)3 SofaLogger (com.alipay.sofa.runtime.log.SofaLogger)3 SofaRuntimeContext (com.alipay.sofa.runtime.spi.component.SofaRuntimeContext)3 BindingConverterFactory (com.alipay.sofa.runtime.spi.service.BindingConverterFactory)3