parent
db8184a576
commit
5425338046
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.polaris.config.exceptions;
|
||||
|
||||
/**
|
||||
*@author : wh
|
||||
*@date : 2022/6/28 09:31
|
||||
*@description:
|
||||
*/
|
||||
public class PolarisConfigException extends RuntimeException {
|
||||
public PolarisConfigException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public PolarisConfigException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.polaris.config.spring.annotation;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.PriorityOrdered;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
*@author : wh
|
||||
*@date : 2022/6/28 09:18
|
||||
*@description:
|
||||
*/
|
||||
public abstract class AbstractPolarisProcessor implements BeanPostProcessor, PriorityOrdered {
|
||||
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName)
|
||||
throws BeansException {
|
||||
Class clazz = bean.getClass();
|
||||
for (Field field : findAllField(clazz)) {
|
||||
processField(bean, beanName, field);
|
||||
}
|
||||
for (Method method : findAllMethod(clazz)) {
|
||||
processMethod(bean, beanName, method);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||
return bean;
|
||||
}
|
||||
|
||||
/**
|
||||
* subclass should implement this method to process field
|
||||
*/
|
||||
protected abstract void processField(Object bean, String beanName, Field field);
|
||||
|
||||
/**
|
||||
* subclass should implement this method to process method
|
||||
*/
|
||||
protected abstract void processMethod(Object bean, String beanName, Method method);
|
||||
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
//make it as late as possible
|
||||
return Ordered.LOWEST_PRECEDENCE;
|
||||
}
|
||||
|
||||
private List<Field> findAllField(Class clazz) {
|
||||
final List<Field> res = new LinkedList<>();
|
||||
ReflectionUtils.doWithFields(clazz, new ReflectionUtils.FieldCallback() {
|
||||
@Override
|
||||
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
|
||||
res.add(field);
|
||||
}
|
||||
});
|
||||
return res;
|
||||
}
|
||||
|
||||
private List<Method> findAllMethod(Class clazz) {
|
||||
final List<Method> res = new LinkedList<>();
|
||||
ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() {
|
||||
@Override
|
||||
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
|
||||
res.add(method);
|
||||
}
|
||||
});
|
||||
return res;
|
||||
}
|
||||
}
|
@ -0,0 +1,160 @@
|
||||
package com.tencent.cloud.polaris.config.spring.annotation;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Member;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
|
||||
import com.google.common.collect.LinkedListMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.tencent.cloud.polaris.config.spring.property.PlaceholderHelper;
|
||||
import com.tencent.cloud.polaris.config.spring.property.SpringValue;
|
||||
import com.tencent.cloud.polaris.config.spring.property.SpringValueDefinition;
|
||||
import com.tencent.cloud.polaris.config.spring.property.SpringValueDefinitionProcessor;
|
||||
import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry;
|
||||
import com.tencent.cloud.polaris.config.util.SpringInjector;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
*@author : wh
|
||||
*@date : 2022/6/28 09:19
|
||||
*@description:
|
||||
*/
|
||||
public class SpringValueProcessor extends AbstractPolarisProcessor implements BeanFactoryPostProcessor, BeanFactoryAware {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(SpringValueProcessor.class);
|
||||
|
||||
private final PlaceholderHelper placeholderHelper;
|
||||
private final SpringValueRegistry springValueRegistry;
|
||||
|
||||
private BeanFactory beanFactory;
|
||||
private Multimap<String, SpringValueDefinition> beanName2SpringValueDefinitions;
|
||||
|
||||
public SpringValueProcessor() {
|
||||
placeholderHelper = SpringInjector.getInstance(PlaceholderHelper.class);
|
||||
springValueRegistry = SpringInjector.getInstance(SpringValueRegistry.class);
|
||||
beanName2SpringValueDefinitions = LinkedListMultimap.create();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
|
||||
throws BeansException {
|
||||
// 默认开启
|
||||
if (beanFactory instanceof BeanDefinitionRegistry) {
|
||||
beanName2SpringValueDefinitions = SpringValueDefinitionProcessor
|
||||
.getBeanName2SpringValueDefinitions((BeanDefinitionRegistry) beanFactory);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName)
|
||||
throws BeansException {
|
||||
// 默认开启
|
||||
super.postProcessBeforeInitialization(bean, beanName);
|
||||
processBeanPropertyValues(bean, beanName);
|
||||
return bean;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void processField(Object bean, String beanName, Field field) {
|
||||
// register @Value on field
|
||||
Value value = field.getAnnotation(Value.class);
|
||||
if (value == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
doRegister(bean, beanName, field, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processMethod(Object bean, String beanName, Method method) {
|
||||
//register @Value on method
|
||||
Value value = method.getAnnotation(Value.class);
|
||||
if (value == null) {
|
||||
return;
|
||||
}
|
||||
//skip Configuration bean methods
|
||||
if (method.getAnnotation(Bean.class) != null) {
|
||||
return;
|
||||
}
|
||||
if (method.getParameterTypes().length != 1) {
|
||||
logger.error("Ignore @Value setter {}.{}, expecting 1 parameter, actual {} parameters",
|
||||
bean.getClass().getName(), method.getName(), method.getParameterTypes().length);
|
||||
return;
|
||||
}
|
||||
|
||||
doRegister(bean, beanName, method, value);
|
||||
}
|
||||
|
||||
private void doRegister(Object bean, String beanName, Member member, Value value) {
|
||||
Set<String> keys = placeholderHelper.extractPlaceholderKeys(value.value());
|
||||
if (keys.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (String key : keys) {
|
||||
SpringValue springValue;
|
||||
if (member instanceof Field) {
|
||||
Field field = (Field) member;
|
||||
springValue = new SpringValue(key, value.value(), bean, beanName, field, false);
|
||||
} else if (member instanceof Method) {
|
||||
Method method = (Method) member;
|
||||
springValue = new SpringValue(key, value.value(), bean, beanName, method, false);
|
||||
} else {
|
||||
logger.error("polaris @Value annotation currently only support to be used on methods and fields, "
|
||||
+ "but is used on {}", member.getClass());
|
||||
return;
|
||||
}
|
||||
springValueRegistry.register(beanFactory, key, springValue);
|
||||
logger.info("Monitoring {}", springValue);
|
||||
}
|
||||
}
|
||||
|
||||
private void processBeanPropertyValues(Object bean, String beanName) {
|
||||
Collection<SpringValueDefinition> propertySpringValues = beanName2SpringValueDefinitions
|
||||
.get(beanName);
|
||||
if (propertySpringValues == null || propertySpringValues.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (SpringValueDefinition definition : propertySpringValues) {
|
||||
try {
|
||||
PropertyDescriptor pd = BeanUtils
|
||||
.getPropertyDescriptor(bean.getClass(), definition.getPropertyName());
|
||||
Method method = pd.getWriteMethod();
|
||||
if (method == null) {
|
||||
continue;
|
||||
}
|
||||
SpringValue springValue = new SpringValue(definition.getKey(), definition.getPlaceholder(),
|
||||
bean, beanName, method, false);
|
||||
springValueRegistry.register(beanFactory, definition.getKey(), springValue);
|
||||
logger.debug("Monitoring {}", springValue);
|
||||
} catch (Throwable ex) {
|
||||
logger.error("Failed to enable auto update feature for {}.{}", bean.getClass(),
|
||||
definition.getPropertyName());
|
||||
}
|
||||
}
|
||||
|
||||
// clear
|
||||
beanName2SpringValueDefinitions.removeAll(beanName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
}
|
@ -0,0 +1,184 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.polaris.config.spring.property;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.Stack;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.BeanExpressionContext;
|
||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.beans.factory.config.Scope;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
*@author : wh
|
||||
*@date : 2022/6/28 09:24
|
||||
*@description:
|
||||
*/
|
||||
public class PlaceholderHelper {
|
||||
|
||||
private static final String PLACEHOLDER_PREFIX = "${";
|
||||
private static final String PLACEHOLDER_SUFFIX = "}";
|
||||
private static final String VALUE_SEPARATOR = ":";
|
||||
private static final String SIMPLE_PLACEHOLDER_PREFIX = "{";
|
||||
private static final String EXPRESSION_PREFIX = "#{";
|
||||
private static final String EXPRESSION_SUFFIX = "}";
|
||||
|
||||
/**
|
||||
* Resolve placeholder property values, e.g.
|
||||
* <br />
|
||||
* <br />
|
||||
* "${somePropertyValue}" -> "the actual property value"
|
||||
*/
|
||||
public Object resolvePropertyValue(ConfigurableBeanFactory beanFactory, String beanName, String placeholder) {
|
||||
// resolve string value
|
||||
String strVal = beanFactory.resolveEmbeddedValue(placeholder);
|
||||
|
||||
BeanDefinition bd = (beanFactory.containsBean(beanName) ? beanFactory
|
||||
.getMergedBeanDefinition(beanName) : null);
|
||||
|
||||
// resolve expressions like "#{systemProperties.myProp}"
|
||||
return evaluateBeanDefinitionString(beanFactory, strVal, bd);
|
||||
}
|
||||
|
||||
private Object evaluateBeanDefinitionString(ConfigurableBeanFactory beanFactory, String value,
|
||||
BeanDefinition beanDefinition) {
|
||||
if (beanFactory.getBeanExpressionResolver() == null) {
|
||||
return value;
|
||||
}
|
||||
Scope scope = (beanDefinition != null ? beanFactory
|
||||
.getRegisteredScope(beanDefinition.getScope()) : null);
|
||||
return beanFactory.getBeanExpressionResolver()
|
||||
.evaluate(value, new BeanExpressionContext(beanFactory, scope));
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract keys from placeholder, e.g.
|
||||
* <ul>
|
||||
* <li>${some.key} => "some.key"</li>
|
||||
* <li>${some.key:${some.other.key:100}} => "some.key", "some.other.key"</li>
|
||||
* <li>${${some.key}} => "some.key"</li>
|
||||
* <li>${${some.key:other.key}} => "some.key"</li>
|
||||
* <li>${${some.key}:${another.key}} => "some.key", "another.key"</li>
|
||||
* <li>#{new java.text.SimpleDateFormat('${some.key}').parse('${another.key}')} => "some.key", "another.key"</li>
|
||||
* </ul>
|
||||
*/
|
||||
public Set<String> extractPlaceholderKeys(String propertyString) {
|
||||
Set<String> placeholderKeys = Sets.newHashSet();
|
||||
|
||||
if (Strings.isNullOrEmpty(propertyString) || (!isNormalizedPlaceholder(propertyString) && !isExpressionWithPlaceholder(propertyString))) {
|
||||
return placeholderKeys;
|
||||
}
|
||||
|
||||
Stack<String> stack = new Stack<>();
|
||||
stack.push(propertyString);
|
||||
|
||||
while (!stack.isEmpty()) {
|
||||
String strVal = stack.pop();
|
||||
int startIndex = strVal.indexOf(PLACEHOLDER_PREFIX);
|
||||
if (startIndex == -1) {
|
||||
placeholderKeys.add(strVal);
|
||||
continue;
|
||||
}
|
||||
int endIndex = findPlaceholderEndIndex(strVal, startIndex);
|
||||
if (endIndex == -1) {
|
||||
// invalid placeholder?
|
||||
continue;
|
||||
}
|
||||
|
||||
String placeholderCandidate = strVal.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex);
|
||||
|
||||
// ${some.key:other.key}
|
||||
if (placeholderCandidate.startsWith(PLACEHOLDER_PREFIX)) {
|
||||
stack.push(placeholderCandidate);
|
||||
} else {
|
||||
// some.key:${some.other.key:100}
|
||||
int separatorIndex = placeholderCandidate.indexOf(VALUE_SEPARATOR);
|
||||
|
||||
if (separatorIndex == -1) {
|
||||
stack.push(placeholderCandidate);
|
||||
} else {
|
||||
stack.push(placeholderCandidate.substring(0, separatorIndex));
|
||||
String defaultValuePart =
|
||||
normalizeToPlaceholder(placeholderCandidate.substring(separatorIndex + VALUE_SEPARATOR.length()));
|
||||
if (!Strings.isNullOrEmpty(defaultValuePart)) {
|
||||
stack.push(defaultValuePart);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// has remaining part, e.g. ${a}.${b}
|
||||
if (endIndex + PLACEHOLDER_SUFFIX.length() < strVal.length() - 1) {
|
||||
String remainingPart = normalizeToPlaceholder(strVal.substring(endIndex + PLACEHOLDER_SUFFIX.length()));
|
||||
if (!Strings.isNullOrEmpty(remainingPart)) {
|
||||
stack.push(remainingPart);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return placeholderKeys;
|
||||
}
|
||||
|
||||
private boolean isNormalizedPlaceholder(String propertyString) {
|
||||
return propertyString.startsWith(PLACEHOLDER_PREFIX) && propertyString.contains(PLACEHOLDER_SUFFIX);
|
||||
}
|
||||
|
||||
private boolean isExpressionWithPlaceholder(String propertyString) {
|
||||
return propertyString.startsWith(EXPRESSION_PREFIX) && propertyString.contains(EXPRESSION_SUFFIX)
|
||||
&& propertyString.contains(PLACEHOLDER_PREFIX) && propertyString.contains(PLACEHOLDER_SUFFIX);
|
||||
}
|
||||
|
||||
private String normalizeToPlaceholder(String strVal) {
|
||||
int startIndex = strVal.indexOf(PLACEHOLDER_PREFIX);
|
||||
if (startIndex == -1) {
|
||||
return null;
|
||||
}
|
||||
int endIndex = strVal.lastIndexOf(PLACEHOLDER_SUFFIX);
|
||||
if (endIndex == -1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return strVal.substring(startIndex, endIndex + PLACEHOLDER_SUFFIX.length());
|
||||
}
|
||||
|
||||
private int findPlaceholderEndIndex(CharSequence buf, int startIndex) {
|
||||
int index = startIndex + PLACEHOLDER_PREFIX.length();
|
||||
int withinNestedPlaceholder = 0;
|
||||
while (index < buf.length()) {
|
||||
if (StringUtils.substringMatch(buf, index, PLACEHOLDER_SUFFIX)) {
|
||||
if (withinNestedPlaceholder > 0) {
|
||||
withinNestedPlaceholder--;
|
||||
index = index + PLACEHOLDER_SUFFIX.length();
|
||||
} else {
|
||||
return index;
|
||||
}
|
||||
} else if (StringUtils.substringMatch(buf, index, SIMPLE_PLACEHOLDER_PREFIX)) {
|
||||
withinNestedPlaceholder++;
|
||||
index = index + SIMPLE_PLACEHOLDER_PREFIX.length();
|
||||
} else {
|
||||
index++;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
@ -0,0 +1,150 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.polaris.config.spring.property;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
|
||||
/**
|
||||
*@author : wh
|
||||
*@date : 2022/6/28 09:25
|
||||
*@description:
|
||||
*/
|
||||
public class SpringValue {
|
||||
|
||||
private MethodParameter methodParameter;
|
||||
private Field field;
|
||||
private WeakReference<Object> beanRef;
|
||||
private String beanName;
|
||||
private String key;
|
||||
private String placeholder;
|
||||
private Class<?> targetType;
|
||||
private Type genericType;
|
||||
private boolean isJson;
|
||||
|
||||
public SpringValue(String key, String placeholder, Object bean, String beanName, Field field, boolean isJson) {
|
||||
this.beanRef = new WeakReference<>(bean);
|
||||
this.beanName = beanName;
|
||||
this.field = field;
|
||||
this.key = key;
|
||||
this.placeholder = placeholder;
|
||||
this.targetType = field.getType();
|
||||
this.isJson = isJson;
|
||||
if(isJson){
|
||||
this.genericType = field.getGenericType();
|
||||
}
|
||||
}
|
||||
|
||||
public SpringValue(String key, String placeholder, Object bean, String beanName, Method method, boolean isJson) {
|
||||
this.beanRef = new WeakReference<>(bean);
|
||||
this.beanName = beanName;
|
||||
this.methodParameter = new MethodParameter(method, 0);
|
||||
this.key = key;
|
||||
this.placeholder = placeholder;
|
||||
Class<?>[] paramTps = method.getParameterTypes();
|
||||
this.targetType = paramTps[0];
|
||||
this.isJson = isJson;
|
||||
if(isJson){
|
||||
this.genericType = method.getGenericParameterTypes()[0];
|
||||
}
|
||||
}
|
||||
|
||||
public void update(Object newVal) throws IllegalAccessException, InvocationTargetException {
|
||||
if (isField()) {
|
||||
injectField(newVal);
|
||||
} else {
|
||||
injectMethod(newVal);
|
||||
}
|
||||
}
|
||||
|
||||
private void injectField(Object newVal) throws IllegalAccessException {
|
||||
Object bean = beanRef.get();
|
||||
if (bean == null) {
|
||||
return;
|
||||
}
|
||||
boolean accessible = field.isAccessible();
|
||||
field.setAccessible(true);
|
||||
field.set(bean, newVal);
|
||||
field.setAccessible(accessible);
|
||||
}
|
||||
|
||||
private void injectMethod(Object newVal)
|
||||
throws InvocationTargetException, IllegalAccessException {
|
||||
Object bean = beanRef.get();
|
||||
if (bean == null) {
|
||||
return;
|
||||
}
|
||||
methodParameter.getMethod().invoke(bean, newVal);
|
||||
}
|
||||
|
||||
public String getBeanName() {
|
||||
return beanName;
|
||||
}
|
||||
|
||||
public Class<?> getTargetType() {
|
||||
return targetType;
|
||||
}
|
||||
|
||||
public String getPlaceholder() {
|
||||
return this.placeholder;
|
||||
}
|
||||
|
||||
public MethodParameter getMethodParameter() {
|
||||
return methodParameter;
|
||||
}
|
||||
|
||||
public boolean isField() {
|
||||
return this.field != null;
|
||||
}
|
||||
|
||||
public Field getField() {
|
||||
return field;
|
||||
}
|
||||
|
||||
public Type getGenericType() {
|
||||
return genericType;
|
||||
}
|
||||
|
||||
public boolean isJson() {
|
||||
return isJson;
|
||||
}
|
||||
|
||||
boolean isTargetBeanValid() {
|
||||
return beanRef.get() != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
Object bean = beanRef.get();
|
||||
if (bean == null) {
|
||||
return "";
|
||||
}
|
||||
if (isField()) {
|
||||
return String
|
||||
.format("key: %s, beanName: %s, field: %s.%s", key, beanName, bean.getClass().getName(), field.getName());
|
||||
}
|
||||
return String.format("key: %s, beanName: %s, method: %s.%s", key, beanName, bean.getClass().getName(),
|
||||
methodParameter.getMethod().getName());
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.polaris.config.spring.property;
|
||||
|
||||
/**
|
||||
*@author : wh
|
||||
*@date : 2022/6/28 09:28
|
||||
*@description:
|
||||
*/
|
||||
public class SpringValueDefinition {
|
||||
|
||||
private final String key;
|
||||
private final String placeholder;
|
||||
private final String propertyName;
|
||||
|
||||
public SpringValueDefinition(String key, String placeholder, String propertyName) {
|
||||
this.key = key;
|
||||
this.placeholder = placeholder;
|
||||
this.propertyName = propertyName;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public String getPlaceholder() {
|
||||
return placeholder;
|
||||
}
|
||||
|
||||
public String getPropertyName() {
|
||||
return propertyName;
|
||||
}
|
||||
}
|
@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.polaris.config.spring.property;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.google.common.collect.LinkedListMultimap;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.google.common.collect.Sets;
|
||||
import com.tencent.cloud.polaris.config.util.SpringInjector;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.beans.PropertyValue;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.config.TypedStringValue;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
|
||||
|
||||
/**
|
||||
*@author : wh
|
||||
*@date : 2022/6/28 09:30
|
||||
*@description:
|
||||
*/
|
||||
public class SpringValueDefinitionProcessor implements BeanDefinitionRegistryPostProcessor {
|
||||
private static final Map<BeanDefinitionRegistry, Multimap<String, SpringValueDefinition>> beanName2SpringValueDefinitions =
|
||||
Maps.newConcurrentMap();
|
||||
private static final Set<BeanDefinitionRegistry> PROPERTY_VALUES_PROCESSED_BEAN_FACTORIES = Sets.newConcurrentHashSet();
|
||||
private final PlaceholderHelper placeholderHelper;
|
||||
|
||||
public SpringValueDefinitionProcessor() {
|
||||
placeholderHelper = SpringInjector.getInstance(PlaceholderHelper.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
|
||||
// 默认开启
|
||||
processPropertyValues(registry);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
||||
|
||||
}
|
||||
|
||||
public static Multimap<String, SpringValueDefinition> getBeanName2SpringValueDefinitions(BeanDefinitionRegistry registry) {
|
||||
Multimap<String, SpringValueDefinition> springValueDefinitions = beanName2SpringValueDefinitions.get(registry);
|
||||
if (springValueDefinitions == null) {
|
||||
springValueDefinitions = LinkedListMultimap.create();
|
||||
}
|
||||
|
||||
return springValueDefinitions;
|
||||
}
|
||||
|
||||
private void processPropertyValues(BeanDefinitionRegistry beanRegistry) {
|
||||
if (!PROPERTY_VALUES_PROCESSED_BEAN_FACTORIES.add(beanRegistry)) {
|
||||
// already initialized
|
||||
return;
|
||||
}
|
||||
|
||||
if (!beanName2SpringValueDefinitions.containsKey(beanRegistry)) {
|
||||
beanName2SpringValueDefinitions.put(beanRegistry, LinkedListMultimap.<String, SpringValueDefinition>create());
|
||||
}
|
||||
|
||||
Multimap<String, SpringValueDefinition> springValueDefinitions = beanName2SpringValueDefinitions.get(beanRegistry);
|
||||
|
||||
String[] beanNames = beanRegistry.getBeanDefinitionNames();
|
||||
for (String beanName : beanNames) {
|
||||
BeanDefinition beanDefinition = beanRegistry.getBeanDefinition(beanName);
|
||||
MutablePropertyValues mutablePropertyValues = beanDefinition.getPropertyValues();
|
||||
List<PropertyValue> propertyValues = mutablePropertyValues.getPropertyValueList();
|
||||
for (PropertyValue propertyValue : propertyValues) {
|
||||
Object value = propertyValue.getValue();
|
||||
if (!(value instanceof TypedStringValue)) {
|
||||
continue;
|
||||
}
|
||||
String placeholder = ((TypedStringValue) value).getValue();
|
||||
Set<String> keys = placeholderHelper.extractPlaceholderKeys(placeholder);
|
||||
|
||||
if (keys.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (String key : keys) {
|
||||
springValueDefinitions.put(beanName, new SpringValueDefinition(key, placeholder, propertyValue.getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.polaris.config.spring.property;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import com.google.common.collect.LinkedListMultimap;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.google.common.collect.Multimaps;
|
||||
import com.tencent.cloud.common.util.PolarisThreadFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
|
||||
/**
|
||||
*@author : wh
|
||||
*@date : 2022/6/28 09:24
|
||||
*@description:
|
||||
*/
|
||||
public class SpringValueRegistry {
|
||||
private static final Logger logger = LoggerFactory.getLogger(SpringValueRegistry.class);
|
||||
|
||||
private static final long CLEAN_INTERVAL_IN_SECONDS = 5;
|
||||
private final Map<BeanFactory, Multimap<String, SpringValue>> registry = Maps.newConcurrentMap();
|
||||
private final AtomicBoolean initialized = new AtomicBoolean(false);
|
||||
private final Object LOCK = new Object();
|
||||
|
||||
public void register(BeanFactory beanFactory, String key, SpringValue springValue) {
|
||||
if (!registry.containsKey(beanFactory)) {
|
||||
synchronized (LOCK) {
|
||||
if (!registry.containsKey(beanFactory)) {
|
||||
registry.put(beanFactory, Multimaps.synchronizedListMultimap(LinkedListMultimap.<String, SpringValue>create()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
registry.get(beanFactory).put(key, springValue);
|
||||
|
||||
// lazy initialize
|
||||
if (initialized.compareAndSet(false, true)) {
|
||||
initialize();
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<SpringValue> get(BeanFactory beanFactory, String key) {
|
||||
Multimap<String, SpringValue> beanFactorySpringValues = registry.get(beanFactory);
|
||||
if (beanFactorySpringValues == null) {
|
||||
return null;
|
||||
}
|
||||
return beanFactorySpringValues.get(key);
|
||||
}
|
||||
|
||||
private void initialize() {
|
||||
Executors.newSingleThreadScheduledExecutor(PolarisThreadFactory.create("SpringValueRegistry", true)).scheduleAtFixedRate(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
scanAndClean();
|
||||
} catch (Throwable ex) {
|
||||
logger.error(ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
}, CLEAN_INTERVAL_IN_SECONDS, CLEAN_INTERVAL_IN_SECONDS, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
private void scanAndClean() {
|
||||
Iterator<Multimap<String, SpringValue>> iterator = registry.values().iterator();
|
||||
while (!Thread.currentThread().isInterrupted() && iterator.hasNext()) {
|
||||
Multimap<String, SpringValue> springValues = iterator.next();
|
||||
Iterator<Map.Entry<String, SpringValue>> springValueIterator = springValues.entries().iterator();
|
||||
while (springValueIterator.hasNext()) {
|
||||
Map.Entry<String, SpringValue> springValue = springValueIterator.next();
|
||||
if (!springValue.getValue().isTargetBeanValid()) {
|
||||
// clear unused spring values
|
||||
springValueIterator.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.polaris.config.util;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.Singleton;
|
||||
import com.tencent.cloud.polaris.config.exceptions.PolarisConfigException;
|
||||
import com.tencent.cloud.polaris.config.spring.property.PlaceholderHelper;
|
||||
import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry;
|
||||
|
||||
/**
|
||||
*@author : wh
|
||||
*@date : 2022/6/28 09:30
|
||||
*@description:
|
||||
*/
|
||||
public class SpringInjector {
|
||||
private static volatile Injector s_injector;
|
||||
private static final Object lock = new Object();
|
||||
|
||||
private static Injector getInjector() {
|
||||
if (s_injector == null) {
|
||||
synchronized (lock) {
|
||||
if (s_injector == null) {
|
||||
try {
|
||||
s_injector = Guice.createInjector(new SpringModule());
|
||||
} catch (Throwable ex) {
|
||||
PolarisConfigException exception = new PolarisConfigException("Unable to initialize Apollo Spring Injector!", ex);
|
||||
throw exception;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return s_injector;
|
||||
}
|
||||
|
||||
public static <T> T getInstance(Class<T> clazz) {
|
||||
try {
|
||||
return getInjector().getInstance(clazz);
|
||||
} catch (Throwable ex) {
|
||||
throw new PolarisConfigException(
|
||||
String.format("Unable to load instance for %s!", clazz.getName()), ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static class SpringModule extends AbstractModule {
|
||||
@Override
|
||||
protected void configure() {
|
||||
bind(PlaceholderHelper.class).in(Singleton.class);
|
||||
// bind(ConfigPropertySourceFactory.class).in(Singleton.class);
|
||||
bind(SpringValueRegistry.class).in(Singleton.class);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
package com.tencent.cloud.common.util;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*@author : wh
|
||||
*@date : 2022/6/28 09:26
|
||||
*@description:
|
||||
*/
|
||||
public class PolarisThreadFactory implements ThreadFactory {
|
||||
private static Logger log = LoggerFactory.getLogger(PolarisThreadFactory.class);
|
||||
|
||||
private final AtomicLong threadNumber = new AtomicLong(1);
|
||||
|
||||
private final String namePrefix;
|
||||
|
||||
private final boolean daemon;
|
||||
|
||||
private static final ThreadGroup threadGroup = new ThreadGroup("Polaris");
|
||||
|
||||
public static ThreadGroup getThreadGroup() {
|
||||
return threadGroup;
|
||||
}
|
||||
|
||||
public static ThreadFactory create(String namePrefix, boolean daemon) {
|
||||
return new PolarisThreadFactory(namePrefix, daemon);
|
||||
}
|
||||
|
||||
public static boolean waitAllShutdown(int timeoutInMillis) {
|
||||
ThreadGroup group = getThreadGroup();
|
||||
Thread[] activeThreads = new Thread[group.activeCount()];
|
||||
group.enumerate(activeThreads);
|
||||
Set<Thread> alives = new HashSet<>(Arrays.asList(activeThreads));
|
||||
Set<Thread> dies = new HashSet<>();
|
||||
log.info("Current ACTIVE thread count is: {}", alives.size());
|
||||
long expire = System.currentTimeMillis() + timeoutInMillis;
|
||||
while (System.currentTimeMillis() < expire) {
|
||||
classify(alives, dies, new ClassifyStandard<Thread>() {
|
||||
@Override
|
||||
public boolean satisfy(Thread thread) {
|
||||
return !thread.isAlive() || thread.isInterrupted() || thread.isDaemon();
|
||||
}
|
||||
});
|
||||
if (alives.size() > 0) {
|
||||
log.info("Alive polaris threads: {}", alives);
|
||||
try {
|
||||
TimeUnit.SECONDS.sleep(2);
|
||||
} catch (InterruptedException ex) {
|
||||
// ignore
|
||||
}
|
||||
} else {
|
||||
log.info("All polaris threads are shutdown.");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
log.warn("Some polaris threads are still alive but expire time has reached, alive threads: {}",
|
||||
alives);
|
||||
return false;
|
||||
}
|
||||
|
||||
private interface ClassifyStandard<T> {
|
||||
boolean satisfy(T thread);
|
||||
}
|
||||
|
||||
private static <T> void classify(Set<T> src, Set<T> des, ClassifyStandard<T> standard) {
|
||||
Set<T> set = new HashSet<>();
|
||||
for (T t : src) {
|
||||
if (standard.satisfy(t)) {
|
||||
set.add(t);
|
||||
}
|
||||
}
|
||||
src.removeAll(set);
|
||||
des.addAll(set);
|
||||
}
|
||||
|
||||
private PolarisThreadFactory(String namePrefix, boolean daemon) {
|
||||
this.namePrefix = namePrefix;
|
||||
this.daemon = daemon;
|
||||
}
|
||||
|
||||
public Thread newThread(Runnable runnable) {
|
||||
Thread thread = new Thread(threadGroup, runnable,//
|
||||
threadGroup.getName() + "-" + namePrefix + "-" + threadNumber.getAndIncrement());
|
||||
thread.setDaemon(daemon);
|
||||
if (thread.getPriority() != Thread.NORM_PRIORITY) {
|
||||
thread.setPriority(Thread.NORM_PRIORITY);
|
||||
}
|
||||
return thread;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue