Search in sources :

Example 16 with SofaReference

use of com.alipay.sofa.runtime.api.annotation.SofaReference in project sofa-boot by sofastack.

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 17 with SofaReference

use of com.alipay.sofa.runtime.api.annotation.SofaReference in project sofa-boot by sofastack.

the class SofaParameterNameDiscoverer method transformParameterNames.

@SuppressWarnings("unchecked")
protected String[] transformParameterNames(String[] parameterNames, Class<?>[] parameterType, Annotation[][] annotations) {
    for (int i = 0; i < annotations.length; ++i) {
        for (Annotation annotation : annotations[i]) {
            if (annotation instanceof SofaReference) {
                AnnotationWrapperBuilder<SofaReference> wrapperBuilder = AnnotationWrapperBuilder.wrap(annotation).withBinder(binder);
                SofaReference delegate = wrapperBuilder.build();
                Class interfaceType = delegate.interfaceType();
                if (interfaceType.equals(void.class)) {
                    interfaceType = parameterType[i];
                }
                String uniqueId = delegate.uniqueId();
                parameterNames[i] = SofaBeanNameGenerator.generateSofaReferenceBeanName(interfaceType, uniqueId);
            }
        }
    }
    return parameterNames;
}
Also used : SofaReference(com.alipay.sofa.runtime.api.annotation.SofaReference) Annotation(java.lang.annotation.Annotation)

Example 18 with SofaReference

use of com.alipay.sofa.runtime.api.annotation.SofaReference in project sofa-boot by alipay.

the class ServiceBeanFactoryPostProcessor method doGenerateSofaReferenceDefinition.

@SuppressWarnings("unchecked")
private void doGenerateSofaReferenceDefinition(BeanDefinition beanDefinition, SofaReference sofaReference, Class<?> parameterType, ConfigurableListableBeanFactory beanFactory) {
    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 (!beanFactory.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);
        ((BeanDefinitionRegistry) beanFactory).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) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry)

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