Search in sources :

Example 1 with SofaReference

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

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

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

the class ServiceBeanFactoryPostProcessor method generateSofaServiceDefinitionOnMethod.

private void generateSofaServiceDefinitionOnMethod(String beanId, AnnotatedBeanDefinition beanDefinition, BeanDefinitionRegistry registry) {
    Class<?> returnType;
    Class<?> declaringClass;
    List<Method> candidateMethods = new ArrayList<>();
    MethodMetadata methodMetadata = beanDefinition.getFactoryMethodMetadata();
    try {
        returnType = ClassUtils.forName(methodMetadata.getReturnTypeName(), null);
        declaringClass = ClassUtils.forName(methodMetadata.getDeclaringClassName(), null);
    } catch (Throwable throwable) {
        // it's impossible to catch throwable here
        SofaLogger.error(ErrorCode.convert("01-02001", beanId), throwable);
        return;
    }
    if (methodMetadata instanceof StandardMethodMetadata) {
        candidateMethods.add(((StandardMethodMetadata) methodMetadata).getIntrospectedMethod());
    } else {
        for (Method m : declaringClass.getDeclaredMethods()) {
            // check methodName and return type
            if (!m.getName().equals(methodMetadata.getMethodName()) || !m.getReturnType().getTypeName().equals(methodMetadata.getReturnTypeName())) {
                continue;
            }
            // check bean method
            if (!AnnotatedElementUtils.hasAnnotation(m, Bean.class)) {
                continue;
            }
            Bean bean = m.getAnnotation(Bean.class);
            Set<String> beanNames = new HashSet<>();
            beanNames.add(m.getName());
            if (bean != null) {
                beanNames.addAll(Arrays.asList(bean.name()));
                beanNames.addAll(Arrays.asList(bean.value()));
            }
            // check bean name
            if (!beanNames.contains(beanId)) {
                continue;
            }
            candidateMethods.add(m);
        }
    }
    if (candidateMethods.size() == 1) {
        SofaService sofaServiceAnnotation = candidateMethods.get(0).getAnnotation(SofaService.class);
        if (sofaServiceAnnotation == null) {
            sofaServiceAnnotation = returnType.getAnnotation(SofaService.class);
        }
        generateSofaServiceDefinition(beanId, sofaServiceAnnotation, returnType, beanDefinition, registry);
        generateSofaReferenceDefinition(beanId, candidateMethods.get(0), registry);
    } else if (candidateMethods.size() > 1) {
        for (Method m : candidateMethods) {
            if (AnnotatedElementUtils.hasAnnotation(m, SofaService.class) || AnnotatedElementUtils.hasAnnotation(returnType, SofaService.class)) {
                throw new FatalBeanException(ErrorCode.convert("01-02002", declaringClass.getCanonicalName()));
            }
            if (Stream.of(m.getParameterAnnotations()).flatMap(Stream::of).anyMatch(annotation -> annotation instanceof SofaReference)) {
                throw new FatalBeanException(ErrorCode.convert("01-02003", declaringClass.getCanonicalName()));
            }
        }
    }
}
Also used : Arrays(java.util.Arrays) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) ServiceFactoryBean(com.alipay.sofa.runtime.spring.factory.ServiceFactoryBean) SofaBeanNameGenerator(com.alipay.sofa.runtime.spring.bean.SofaBeanNameGenerator) SofaServiceBinding(com.alipay.sofa.runtime.api.annotation.SofaServiceBinding) SofaLogger(com.alipay.sofa.runtime.log.SofaLogger) Method(java.lang.reflect.Method) ReferenceFactoryBean(com.alipay.sofa.runtime.spring.factory.ReferenceFactoryBean) ClassUtils(org.springframework.util.ClassUtils) MethodMetadata(org.springframework.core.type.MethodMetadata) AbstractContractDefinitionParser(com.alipay.sofa.runtime.spring.parser.AbstractContractDefinitionParser) AnnotationUtils(org.springframework.core.annotation.AnnotationUtils) Set(java.util.Set) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException) BindingConverter(com.alipay.sofa.runtime.spi.service.BindingConverter) Collectors(java.util.stream.Collectors) List(java.util.List) BindingConverterContext(com.alipay.sofa.runtime.spi.service.BindingConverterContext) Stream(java.util.stream.Stream) SofaReference(com.alipay.sofa.runtime.api.annotation.SofaReference) Environment(org.springframework.core.env.Environment) Binding(com.alipay.sofa.runtime.spi.binding.Binding) Annotation(java.lang.annotation.Annotation) BindingConverterFactory(com.alipay.sofa.runtime.spi.service.BindingConverterFactory) ApplicationContextAware(org.springframework.context.ApplicationContextAware) PlaceHolderBinder(com.alipay.sofa.boot.annotation.PlaceHolderBinder) AnnotatedElementUtils(org.springframework.core.annotation.AnnotatedElementUtils) Ordered(org.springframework.core.Ordered) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) BindingType(com.alipay.sofa.runtime.api.binding.BindingType) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) BeanDefinitionUtil(com.alipay.sofa.boot.util.BeanDefinitionUtil) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ServiceDefinitionParser(com.alipay.sofa.runtime.spring.parser.ServiceDefinitionParser) SofaRuntimeContext(com.alipay.sofa.runtime.spi.component.SofaRuntimeContext) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ErrorCode(com.alipay.sofa.boot.error.ErrorCode) SofaService(com.alipay.sofa.runtime.api.annotation.SofaService) AnnotatedGenericBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition) Order(org.springframework.core.annotation.Order) ScannedGenericBeanDefinition(org.springframework.context.annotation.ScannedGenericBeanDefinition) FatalBeanException(org.springframework.beans.FatalBeanException) ObjectUtils(org.springframework.util.ObjectUtils) StandardMethodMetadata(org.springframework.core.type.StandardMethodMetadata) BeansException(org.springframework.beans.BeansException) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) BeanDefinitionRegistryPostProcessor(org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor) ApplicationContext(org.springframework.context.ApplicationContext) EnvironmentAware(org.springframework.context.EnvironmentAware) Bean(org.springframework.context.annotation.Bean) SofaReferenceBinding(com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding) AnnotationWrapperBuilder(com.alipay.sofa.boot.annotation.PlaceHolderAnnotationInvocationHandler.AnnotationWrapperBuilder) Assert(org.springframework.util.Assert) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) StandardMethodMetadata(org.springframework.core.type.StandardMethodMetadata) SofaService(com.alipay.sofa.runtime.api.annotation.SofaService) ServiceFactoryBean(com.alipay.sofa.runtime.spring.factory.ServiceFactoryBean) ReferenceFactoryBean(com.alipay.sofa.runtime.spring.factory.ReferenceFactoryBean) Bean(org.springframework.context.annotation.Bean) SofaReference(com.alipay.sofa.runtime.api.annotation.SofaReference) FatalBeanException(org.springframework.beans.FatalBeanException) MethodMetadata(org.springframework.core.type.MethodMetadata) StandardMethodMetadata(org.springframework.core.type.StandardMethodMetadata) Stream(java.util.stream.Stream) HashSet(java.util.HashSet)

Example 3 with SofaReference

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

the class ServiceBeanFactoryPostProcessor method generateSofaServiceDefinitionOnMethod.

private void generateSofaServiceDefinitionOnMethod(String beanId, AnnotatedBeanDefinition beanDefinition, ConfigurableListableBeanFactory beanFactory) {
    Class<?> returnType;
    Class<?> declaringClass;
    List<Method> candidateMethods = new ArrayList<>();
    MethodMetadata methodMetadata = beanDefinition.getFactoryMethodMetadata();
    try {
        returnType = ClassUtils.forName(methodMetadata.getReturnTypeName(), null);
        declaringClass = ClassUtils.forName(methodMetadata.getDeclaringClassName(), null);
    } catch (Throwable throwable) {
        // it's impossible to catch throwable here
        SofaLogger.error(ErrorCode.convert("01-02001", beanId), throwable);
        return;
    }
    if (methodMetadata instanceof StandardMethodMetadata) {
        candidateMethods.add(((StandardMethodMetadata) methodMetadata).getIntrospectedMethod());
    } else {
        for (Method m : declaringClass.getDeclaredMethods()) {
            // check methodName and return type
            if (!m.getName().equals(methodMetadata.getMethodName()) || !m.getReturnType().getTypeName().equals(methodMetadata.getReturnTypeName())) {
                continue;
            }
            // check bean method
            if (!AnnotatedElementUtils.hasAnnotation(m, Bean.class)) {
                continue;
            }
            Bean bean = m.getAnnotation(Bean.class);
            Set<String> beanNames = new HashSet<>();
            beanNames.add(m.getName());
            if (bean != null) {
                beanNames.addAll(Arrays.asList(bean.name()));
                beanNames.addAll(Arrays.asList(bean.value()));
            }
            // check bean name
            if (!beanNames.contains(beanId)) {
                continue;
            }
            candidateMethods.add(m);
        }
    }
    if (candidateMethods.size() == 1) {
        SofaService sofaServiceAnnotation = candidateMethods.get(0).getAnnotation(SofaService.class);
        if (sofaServiceAnnotation == null) {
            sofaServiceAnnotation = returnType.getAnnotation(SofaService.class);
        }
        generateSofaServiceDefinition(beanId, sofaServiceAnnotation, returnType, beanDefinition, beanFactory);
        generateSofaReferenceDefinition(beanId, candidateMethods.get(0), beanFactory);
    } else if (candidateMethods.size() > 1) {
        for (Method m : candidateMethods) {
            if (AnnotatedElementUtils.hasAnnotation(m, SofaService.class) || AnnotatedElementUtils.hasAnnotation(returnType, SofaService.class)) {
                throw new FatalBeanException(ErrorCode.convert("01-02002", declaringClass.getCanonicalName()));
            }
            if (Stream.of(m.getParameterAnnotations()).flatMap(Stream::of).anyMatch(annotation -> annotation instanceof SofaReference)) {
                throw new FatalBeanException(ErrorCode.convert("01-02003", declaringClass.getCanonicalName()));
            }
        }
    }
}
Also used : Arrays(java.util.Arrays) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) ServiceFactoryBean(com.alipay.sofa.runtime.spring.factory.ServiceFactoryBean) SofaBeanNameGenerator(com.alipay.sofa.runtime.spring.bean.SofaBeanNameGenerator) SofaServiceBinding(com.alipay.sofa.runtime.api.annotation.SofaServiceBinding) SofaLogger(com.alipay.sofa.runtime.log.SofaLogger) Method(java.lang.reflect.Method) ReferenceFactoryBean(com.alipay.sofa.runtime.spring.factory.ReferenceFactoryBean) ClassUtils(org.springframework.util.ClassUtils) MethodMetadata(org.springframework.core.type.MethodMetadata) AbstractContractDefinitionParser(com.alipay.sofa.runtime.spring.parser.AbstractContractDefinitionParser) AnnotationUtils(org.springframework.core.annotation.AnnotationUtils) Set(java.util.Set) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException) BindingConverter(com.alipay.sofa.runtime.spi.service.BindingConverter) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) BindingConverterContext(com.alipay.sofa.runtime.spi.service.BindingConverterContext) SofaReference(com.alipay.sofa.runtime.api.annotation.SofaReference) Environment(org.springframework.core.env.Environment) Annotation(java.lang.annotation.Annotation) Binding(com.alipay.sofa.runtime.spi.binding.Binding) BindingConverterFactory(com.alipay.sofa.runtime.spi.service.BindingConverterFactory) ApplicationContextAware(org.springframework.context.ApplicationContextAware) AnnotatedElementUtils(org.springframework.core.annotation.AnnotatedElementUtils) PlaceHolderBinder(com.alipay.sofa.boot.annotation.PlaceHolderBinder) Ordered(org.springframework.core.Ordered) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) BindingType(com.alipay.sofa.runtime.api.binding.BindingType) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) BeanDefinitionUtil(com.alipay.sofa.boot.util.BeanDefinitionUtil) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ServiceDefinitionParser(com.alipay.sofa.runtime.spring.parser.ServiceDefinitionParser) SofaRuntimeContext(com.alipay.sofa.runtime.spi.component.SofaRuntimeContext) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ErrorCode(com.alipay.sofa.boot.error.ErrorCode) SofaService(com.alipay.sofa.runtime.api.annotation.SofaService) AnnotatedGenericBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition) Order(org.springframework.core.annotation.Order) ScannedGenericBeanDefinition(org.springframework.context.annotation.ScannedGenericBeanDefinition) FatalBeanException(org.springframework.beans.FatalBeanException) BeanFactoryPostProcessor(org.springframework.beans.factory.config.BeanFactoryPostProcessor) ObjectUtils(org.springframework.util.ObjectUtils) StandardMethodMetadata(org.springframework.core.type.StandardMethodMetadata) BeansException(org.springframework.beans.BeansException) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) ApplicationContext(org.springframework.context.ApplicationContext) EnvironmentAware(org.springframework.context.EnvironmentAware) Bean(org.springframework.context.annotation.Bean) SofaReferenceBinding(com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding) AnnotationWrapperBuilder(com.alipay.sofa.boot.annotation.PlaceHolderAnnotationInvocationHandler.AnnotationWrapperBuilder) Assert(org.springframework.util.Assert) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) StandardMethodMetadata(org.springframework.core.type.StandardMethodMetadata) SofaService(com.alipay.sofa.runtime.api.annotation.SofaService) ServiceFactoryBean(com.alipay.sofa.runtime.spring.factory.ServiceFactoryBean) ReferenceFactoryBean(com.alipay.sofa.runtime.spring.factory.ReferenceFactoryBean) Bean(org.springframework.context.annotation.Bean) SofaReference(com.alipay.sofa.runtime.api.annotation.SofaReference) FatalBeanException(org.springframework.beans.FatalBeanException) MethodMetadata(org.springframework.core.type.MethodMetadata) StandardMethodMetadata(org.springframework.core.type.StandardMethodMetadata) Stream(java.util.stream.Stream) HashSet(java.util.HashSet)

Example 4 with SofaReference

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

the class ServiceBeanFactoryPostProcessor method generateSofaReferenceDefinition.

private void generateSofaReferenceDefinition(String beanId, Method method, ConfigurableListableBeanFactory beanFactory) {
    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(beanFactory.getBeanDefinition(beanId), (SofaReference) annotation, parameterTypes[i], beanFactory);
            }
        }
    }
}
Also used : SofaReference(com.alipay.sofa.runtime.api.annotation.SofaReference) Annotation(java.lang.annotation.Annotation)

Example 5 with SofaReference

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

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)

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