Search in sources :

Example 1 with SofaReferenceBinding

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

the class ServiceBeanFactoryPostProcessor method getSofaReferenceBinding.

/**
 * get sofa reference binding annotated on parameter. At present, only jvm sofa reference is supported .
 * @param sofaReferenceAnnotation
 * @param sofaReferenceBinding
 * @return
 */
private List<Binding> getSofaReferenceBinding(SofaReference sofaReferenceAnnotation, SofaReferenceBinding sofaReferenceBinding) {
    if (!JvmBinding.XmlConstants.BINDING_TYPE.equals(sofaReferenceBinding.bindingType())) {
        throw new ServiceRuntimeException(ErrorCode.convert("01-02005"));
    }
    List<Binding> bindings = new ArrayList<>();
    BindingConverter bindingConverter = bindingConverterFactory.getBindingConverter(new BindingType(sofaReferenceBinding.bindingType()));
    if (bindingConverter == null) {
        throw new ServiceRuntimeException(ErrorCode.convert("01-00200", sofaReferenceBinding.bindingType()));
    }
    BindingConverterContext bindingConverterContext = new BindingConverterContext();
    bindingConverterContext.setInBinding(true);
    bindingConverterContext.setApplicationContext(applicationContext);
    bindingConverterContext.setAppName(sofaRuntimeContext.getAppName());
    bindingConverterContext.setAppClassLoader(sofaRuntimeContext.getAppClassLoader());
    Binding binding = bindingConverter.convert(sofaReferenceAnnotation, sofaReferenceBinding, bindingConverterContext);
    bindings.add(binding);
    return bindings;
}
Also used : SofaServiceBinding(com.alipay.sofa.runtime.api.annotation.SofaServiceBinding) Binding(com.alipay.sofa.runtime.spi.binding.Binding) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) SofaReferenceBinding(com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding) BindingType(com.alipay.sofa.runtime.api.binding.BindingType) BindingConverter(com.alipay.sofa.runtime.spi.service.BindingConverter) ArrayList(java.util.ArrayList) BindingConverterContext(com.alipay.sofa.runtime.spi.service.BindingConverterContext) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException)

Example 2 with SofaReferenceBinding

use of com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding 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 3 with SofaReferenceBinding

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

the class ServiceBeanFactoryPostProcessor method getSofaReferenceBinding.

/**
 * get sofa reference binding annotated on parameter. At present, only jvm sofa reference is supported .
 * @param sofaReferenceAnnotation
 * @param sofaReferenceBinding
 * @return
 */
private List<Binding> getSofaReferenceBinding(SofaReference sofaReferenceAnnotation, SofaReferenceBinding sofaReferenceBinding) {
    if (!JvmBinding.XmlConstants.BINDING_TYPE.equals(sofaReferenceBinding.bindingType())) {
        throw new ServiceRuntimeException(ErrorCode.convert("01-02005"));
    }
    List<Binding> bindings = new ArrayList<>();
    BindingConverter bindingConverter = bindingConverterFactory.getBindingConverter(new BindingType(sofaReferenceBinding.bindingType()));
    if (bindingConverter == null) {
        throw new ServiceRuntimeException(ErrorCode.convert("01-00200", sofaReferenceBinding.bindingType()));
    }
    BindingConverterContext bindingConverterContext = new BindingConverterContext();
    bindingConverterContext.setInBinding(true);
    bindingConverterContext.setApplicationContext(applicationContext);
    bindingConverterContext.setAppName(sofaRuntimeContext.getAppName());
    bindingConverterContext.setAppClassLoader(sofaRuntimeContext.getAppClassLoader());
    Binding binding = bindingConverter.convert(sofaReferenceAnnotation, sofaReferenceBinding, bindingConverterContext);
    bindings.add(binding);
    return bindings;
}
Also used : SofaServiceBinding(com.alipay.sofa.runtime.api.annotation.SofaServiceBinding) Binding(com.alipay.sofa.runtime.spi.binding.Binding) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) SofaReferenceBinding(com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding) BindingType(com.alipay.sofa.runtime.api.binding.BindingType) BindingConverter(com.alipay.sofa.runtime.spi.service.BindingConverter) ArrayList(java.util.ArrayList) BindingConverterContext(com.alipay.sofa.runtime.spi.service.BindingConverterContext) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException)

Example 4 with SofaReferenceBinding

use of com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding 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)

Aggregations

SofaReferenceBinding (com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding)4 PlaceHolderAnnotationInvocationHandler (com.alipay.sofa.boot.annotation.PlaceHolderAnnotationInvocationHandler)2 PlaceHolderBinder (com.alipay.sofa.boot.annotation.PlaceHolderBinder)2 WrapperAnnotation (com.alipay.sofa.boot.annotation.WrapperAnnotation)2 ServiceRuntimeException (com.alipay.sofa.runtime.api.ServiceRuntimeException)2 SofaReference (com.alipay.sofa.runtime.api.annotation.SofaReference)2 SofaServiceBinding (com.alipay.sofa.runtime.api.annotation.SofaServiceBinding)2 BindingType (com.alipay.sofa.runtime.api.binding.BindingType)2 JvmBinding (com.alipay.sofa.runtime.service.binding.JvmBinding)2 Binding (com.alipay.sofa.runtime.spi.binding.Binding)2 BindingConverter (com.alipay.sofa.runtime.spi.service.BindingConverter)2 BindingConverterContext (com.alipay.sofa.runtime.spi.service.BindingConverterContext)2 AnnotationSampleService (com.alipay.sofa.runtime.test.beans.service.AnnotationSampleService)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2