Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support placeholder in SofaService and SofaReference annotation. (#264) #282

Merged
merged 1 commit into from
Nov 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.runtime.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;

/**
* @author qilong.zql
* @since 2.5.2
*/
public class PlaceHolderAnnotationInvocationHandler implements InvocationHandler {

private final Annotation delegate;

private final PlaceHolderBinder binder;

private PlaceHolderAnnotationInvocationHandler(Annotation delegate, PlaceHolderBinder binder) {
this.delegate = delegate;
this.binder = binder;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object ret = method.invoke(delegate, args);
if (!ReflectionUtils.isEqualsMethod(method) && !ReflectionUtils.isHashCodeMethod(method)
&& !ReflectionUtils.isToStringMethod(method) && isAttributeMethod(method)) {
return resolvePlaceHolder(ret);
}
return ret;
}

private boolean isAttributeMethod(Method method) {
return (method != null && method.getParameterTypes().length == 0 && method.getReturnType() != void.class);
}

public Object resolvePlaceHolder(Object origin) {
if (origin.getClass().isArray()) {
int length = Array.getLength(origin);
Object ret = Array.newInstance(origin.getClass().getComponentType(), length);
for (int i = 0; i < length; ++i) {
Array.set(ret, i, resolvePlaceHolder(Array.get(origin, i)));
}
return ret;
} else {
return doResolvePlaceHolder(origin);
}
}

private Object doResolvePlaceHolder(Object origin) {
if (origin instanceof String) {
return binder.bind((String) origin);
} else if (origin instanceof Annotation && !(origin instanceof WrapperAnnotation)) {
return AnnotationWrapperBuilder.wrap(origin).withBinder(binder).build();
} else {
return origin;
}
}

public static class AnnotationWrapperBuilder<A> {
private Annotation delegate;
private PlaceHolderBinder binder;

private AnnotationWrapperBuilder() {
}

public static <A> AnnotationWrapperBuilder wrap(A annotation) {
Assert.isTrue(annotation == null || annotation instanceof Annotation,
"Parameter must be annotation type.");
AnnotationWrapperBuilder<A> builder = new AnnotationWrapperBuilder<A>();
builder.delegate = (Annotation) annotation;
return builder;
}

public AnnotationWrapperBuilder withBinder(PlaceHolderBinder binder) {
this.binder = binder;
return this;
}

@SuppressWarnings("unchecked")
public A build() {
if (delegate != null) {
ClassLoader cl = delegate.getClass().getClassLoader();
Class<?>[] exposedInterface = { delegate.annotationType(), WrapperAnnotation.class };
return (A) Proxy.newProxyInstance(cl, exposedInterface,
new PlaceHolderAnnotationInvocationHandler(delegate, binder));
}
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.runtime.annotation;

/**
* Responsible to resolve place holder.
*
* @author qilong.zql
* @since 2.5.2
*/
public interface PlaceHolderBinder {
String bind(String string);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.runtime.annotation;

/**
* A marker interface
*
* @author qilong.zql
* @since 2.5.2
*/
public interface WrapperAnnotation {
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@
*/
package com.alipay.sofa.runtime.spring;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.Ordered;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;

import com.alipay.sofa.runtime.annotation.PlaceHolderAnnotationInvocationHandler.AnnotationWrapperBuilder;
import com.alipay.sofa.runtime.annotation.PlaceHolderBinder;
import com.alipay.sofa.runtime.api.ServiceRuntimeException;
import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaService;
Expand All @@ -39,18 +56,6 @@
import com.alipay.sofa.runtime.spi.service.BindingConverter;
import com.alipay.sofa.runtime.spi.service.BindingConverterContext;
import com.alipay.sofa.runtime.spi.service.BindingConverterFactory;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.Ordered;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
* @author xuanbei 18/5/9
Expand All @@ -62,6 +67,10 @@ public class ServiceAnnotationBeanPostProcessor implements BeanPostProcessor, Or
private BindingConverterFactory bindingConverterFactory;
private ApplicationContext applicationContext;

@Autowired
private Environment environment;
private final PlaceHolderBinder binder = new DefaultPlaceHolderBinder();

/**
* To construct a ServiceAnnotationBeanPostProcessor via a Spring Bean
* sofaRuntimeContext and sofaRuntimeProperties will be obtained from applicationContext
Expand All @@ -88,14 +97,17 @@ public Object postProcessAfterInitialization(Object bean, String beanName)
return bean;
}

@SuppressWarnings("unchecked")
private void processSofaService(Object bean, String beanName) {
final Class<?> beanClass = AopProxyUtils.ultimateTargetClass(bean);

SofaService sofaServiceAnnotation = beanClass.getAnnotation(SofaService.class);

if (sofaServiceAnnotation == null) {
return;
}
AnnotationWrapperBuilder<SofaService> builder = AnnotationWrapperBuilder.wrap(
sofaServiceAnnotation).withBinder(binder);
sofaServiceAnnotation = builder.build();

Class<?> interfaceType = sofaServiceAnnotation.interfaceType();

Expand Down Expand Up @@ -131,8 +143,12 @@ private void processSofaReference(final Object bean) {
ReflectionUtils.doWithFields(beanClass, new ReflectionUtils.FieldCallback() {

@Override
@SuppressWarnings("unchecked")
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
SofaReference sofaReferenceAnnotation = field.getAnnotation(SofaReference.class);
AnnotationWrapperBuilder<SofaReference> builder = AnnotationWrapperBuilder.wrap(
field.getAnnotation(SofaReference.class)).withBinder(binder);
SofaReference sofaReferenceAnnotation = builder.build();

if (sofaReferenceAnnotation == null) {
return;
}
Expand All @@ -157,6 +173,7 @@ public boolean matches(Field field) {

ReflectionUtils.doWithMethods(beanClass, new ReflectionUtils.MethodCallback() {
@Override
@SuppressWarnings("unchecked")
public void doWith(Method method) throws IllegalArgumentException,
IllegalAccessException {
Class[] parameterTypes = method.getParameterTypes();
Expand All @@ -167,6 +184,9 @@ public void doWith(Method method) throws IllegalArgumentException,
if (sofaReferenceAnnotation == null) {
return;
}
AnnotationWrapperBuilder<SofaReference> builder = AnnotationWrapperBuilder.wrap(
sofaReferenceAnnotation).withBinder(binder);
sofaReferenceAnnotation = builder.build();

Class<?> interfaceType = sofaReferenceAnnotation.interfaceType();
if (interfaceType.equals(void.class)) {
Expand Down Expand Up @@ -256,4 +276,11 @@ public int getOrder() {
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}

class DefaultPlaceHolderBinder implements PlaceHolderBinder {
@Override
public String bind(String text) {
return environment.resolvePlaceholders(text);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.runtime.annotation;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
import com.alipay.sofa.runtime.api.annotation.SofaService;
import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;
import com.alipay.sofa.runtime.beans.service.SampleService;

/**
* @author qilong.zql
* @since 2.5.2
*/
@SofaService(interfaceType = SampleService.class, uniqueId = "${annotation.sample.service.uniqueId}", bindings = { @SofaServiceBinding(bindingType = "${annotation.sample.service.bindingType}", filters = {
"${annotation.sample.service.filter-1}",
"filter-2" }, timeout = 300) })
public class AnnotationSample implements SampleService {

@SofaReference(uniqueId = "${annotation.sample.ref.uniqueId}", jvmFirst = false, binding = @SofaReferenceBinding(bindingType = "${annotation.sample.ref.bindingType}", filters = {
"${annotation.sample.ref.filter-1}", "filter-2" }, directUrl = "${annotation.sample.ref.direct-url}"))
public SampleService sampleService;

@Override
public String service() {
return "AnnotationSample";
}
}
Loading