Skip to content

Commit

Permalink
[SCB-2237] allow InjectProperties inject to spring bean instance (#2314)
Browse files Browse the repository at this point in the history
  • Loading branch information
wujimin committed Mar 27, 2021
1 parent 30d01bf commit e608843
Show file tree
Hide file tree
Showing 23 changed files with 597 additions and 319 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.servicecomb.core;

import org.apache.servicecomb.config.priority.PriorityPropertyManager;
import org.apache.servicecomb.core.filter.FilterChainsManager;
import org.apache.servicecomb.foundation.common.utils.BeanUtils;
import org.apache.servicecomb.foundation.vertx.client.http.HttpClients;
Expand Down Expand Up @@ -77,6 +78,7 @@ public void onApplicationEvent(ApplicationEvent event) {
// SCBEngine.getInstance().setConsumerProviderManager(applicationContext.getBean(ConsumerProviderManager.class));
// SCBEngine.getInstance().setTransportManager(applicationContext.getBean(TransportManager.class));
scbEngine.setApplicationContext(applicationContext);
scbEngine.setPriorityPropertyManager(applicationContext.getBean(PriorityPropertyManager.class));
scbEngine.setFilterChainsManager(applicationContext.getBean(FilterChainsManager.class));
scbEngine.getConsumerProviderManager().getConsumerProviderList()
.addAll(applicationContext.getBeansOfType(ConsumerProvider.class).values());
Expand Down
12 changes: 10 additions & 2 deletions core/src/main/java/org/apache/servicecomb/core/SCBEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public class SCBEngine {

private ExecutorManager executorManager = new ExecutorManager();

private PriorityPropertyManager priorityPropertyManager = new PriorityPropertyManager();
private PriorityPropertyManager priorityPropertyManager;

protected List<BootUpInformationCollector> bootUpInformationCollectors = SPIServiceUtils
.getSortedService(BootUpInformationCollector.class);
Expand Down Expand Up @@ -206,6 +206,11 @@ public PriorityPropertyManager getPriorityPropertyManager() {
return priorityPropertyManager;
}

public SCBEngine setPriorityPropertyManager(PriorityPropertyManager priorityPropertyManager) {
this.priorityPropertyManager = priorityPropertyManager;
return this;
}

public EventBus getEventBus() {
return eventBus;
}
Expand Down Expand Up @@ -451,7 +456,10 @@ private void doDestroy() {

//Step 5: destroy config center source
ConfigUtil.destroyConfigCenterConfigurationSource();
priorityPropertyManager.close();
// only be null for some test cases
if (priorityPropertyManager != null) {
priorityPropertyManager.close();
}

//Step 6: Stop vertx to prevent blocking exit
// delete the following one line when every refactor is done.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import java.util.Arrays;
import java.util.List;

import org.apache.servicecomb.config.priority.ConfigObjectFactory;
import org.apache.servicecomb.config.priority.PriorityPropertyFactory;
import org.apache.servicecomb.config.priority.PriorityPropertyManager;
import org.apache.servicecomb.core.SCBEngine;
import org.apache.servicecomb.core.executor.GroupExecutor;
import org.apache.servicecomb.core.filter.Filter;
Expand All @@ -43,6 +46,10 @@ public SCBEngineForTest() {
);
setFilterChainsManager(new FilterChainsManager()
.addFilters(filters));

PriorityPropertyFactory propertyFactory = new PriorityPropertyFactory();
ConfigObjectFactory configObjectFactory = new ConfigObjectFactory(propertyFactory);
setPriorityPropertyManager(new PriorityPropertyManager(configObjectFactory));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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 org.apache.servicecomb.config.inject;

import java.util.Collections;

import org.apache.servicecomb.config.priority.PriorityPropertyManager;
import org.apache.servicecomb.foundation.common.utils.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

@Component
public class InjectBeanPostProcessor implements BeanPostProcessor {
private final PriorityPropertyManager priorityPropertyManager;

public InjectBeanPostProcessor(PriorityPropertyManager priorityPropertyManager) {
this.priorityPropertyManager = priorityPropertyManager;
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
Class<?> beanCls = BeanUtils.getImplClassFromBean(bean);
InjectProperties injectProperties = beanCls.getAnnotation(InjectProperties.class);
if (injectProperties == null) {
return bean;
}

priorityPropertyManager.createConfigObject(bean, Collections.emptyMap());

return bean;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 org.apache.servicecomb.config.priority;

import java.util.List;

public class ConfigObject<T> {
private final T instance;

private final List<ConfigObjectProperty> properties;

public ConfigObject(T instance, List<ConfigObjectProperty> properties) {
this.instance = instance;
this.properties = properties;
}

public T getInstance() {
return instance;
}

public List<ConfigObjectProperty> getProperties() {
return properties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.servicecomb.config.inject;
package org.apache.servicecomb.config.priority;

import static org.apache.servicecomb.foundation.common.utils.LambdaMetafactoryUtils.createObjectSetter;

Expand All @@ -23,10 +23,12 @@
import java.util.List;
import java.util.Map;

import org.apache.servicecomb.config.priority.PriorityProperty;
import org.apache.servicecomb.config.priority.PriorityPropertyManager;
import org.apache.servicecomb.config.inject.InjectProperties;
import org.apache.servicecomb.config.inject.InjectProperty;
import org.apache.servicecomb.config.inject.PlaceholderResolver;
import org.apache.servicecomb.foundation.common.utils.JsonUtils;
import org.apache.servicecomb.foundation.common.utils.bean.Setter;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.JavaType;
Expand All @@ -40,55 +42,49 @@
* ${} or ${not-exist-key} is valid key in archaius<br>
* so this wrapper mechanism will not throw exception even can not find value by placeholder
*/
@Component
public class ConfigObjectFactory {
private PriorityPropertyManager priorityPropertyManager;
private final PriorityPropertyFactory propertyFactory;

private Class<?> cls;

private Map<String, Object> parameters;

private Object instance;

private String prefix = "";

private List<PriorityProperty<?>> priorityProperties = new ArrayList<>();
public ConfigObjectFactory(PriorityPropertyFactory propertyFactory) {
this.propertyFactory = propertyFactory;
}

@SuppressWarnings("unchecked")
public <T> T create(PriorityPropertyManager priorityPropertyManager, Class<T> cls, Map<String, Object> parameters) {
this.priorityPropertyManager = priorityPropertyManager;
this.cls = cls;
this.parameters = parameters;
public PriorityPropertyFactory getPropertyFactory() {
return propertyFactory;
}

public <T> ConfigObject<T> create(Class<T> cls, Map<String, Object> parameters) {
try {
instance = cls.newInstance();
return create(cls.newInstance(), parameters);
} catch (Throwable e) {
throw new IllegalStateException("create config object failed, class=" + cls.getName(), e);
}

initPrefix();
doCreate();

return (T) instance;
}

public List<PriorityProperty<?>> getPriorityProperties() {
return priorityProperties;
public <T> ConfigObject<T> create(T instance, Map<String, Object> parameters) {
String prefix = initPrefix(instance.getClass());
List<ConfigObjectProperty> properties = createProperties(instance, prefix, parameters);

return new ConfigObject<>(instance, properties);
}

private void initPrefix() {
private String initPrefix(Class<?> cls) {
InjectProperties injectProperties = cls.getAnnotation(InjectProperties.class);
if (injectProperties == null) {
return;
return "";
}

String prefix = injectProperties.prefix();
if (!prefix.isEmpty()) {
this.prefix = prefix + ".";
if (prefix.isEmpty()) {
return "";
}
return prefix + ".";
}

private void doCreate() {
JavaType javaType = TypeFactory.defaultInstance().constructType(cls);
public List<ConfigObjectProperty> createProperties(Object instance, String prefix, Map<String, Object> parameters) {
List<ConfigObjectProperty> properties = new ArrayList<>();
JavaType javaType = TypeFactory.defaultInstance().constructType(instance.getClass());
BeanDescription beanDescription = JsonUtils.OBJ_MAPPER.getSerializationConfig().introspect(javaType);
for (BeanPropertyDefinition propertyDefinition : beanDescription.findProperties()) {
if (propertyDefinition.getField() == null) {
Expand All @@ -100,14 +96,16 @@ private void doCreate() {
}

Setter<Object, Object> setter = createObjectSetter(propertyDefinition);
PriorityProperty<?> priorityProperty = createPriorityProperty(propertyDefinition.getField().getAnnotated());
priorityProperty.setCallback((value, target) -> setter.set(target, value), instance);
priorityProperties.add(priorityProperty);
PriorityProperty<?> priorityProperty = createPriorityProperty(propertyDefinition.getField().getAnnotated(),
prefix, parameters);
setter.set(instance, priorityProperty.getValue());
properties.add(new ConfigObjectProperty(setter, priorityProperty));
}
return properties;
}

private PriorityProperty<?> createPriorityProperty(Field field) {
String[] keys = collectPropertyKeys(field);
private PriorityProperty<?> createPriorityProperty(Field field, String prefix, Map<String, Object> parameters) {
String[] keys = collectPropertyKeys(field, prefix, parameters);

Class<?> fieldCls = field.getType();
switch (fieldCls.getName()) {
Expand Down Expand Up @@ -147,7 +145,7 @@ private PriorityProperty<?> createStringProperty(Field field, String[] keys) {
}
}

return priorityPropertyManager.newPriorityProperty(String.class, null, defaultValue, keys);
return propertyFactory.getOrCreate(String.class, null, defaultValue, keys);
}

private PriorityProperty<?> createDoubleProperty(Field field, String[] keys, Double defaultValue) {
Expand All @@ -158,7 +156,7 @@ private PriorityProperty<?> createDoubleProperty(Field field, String[] keys, Dou
}
}

return priorityPropertyManager.newPriorityProperty(Double.class, null, defaultValue, keys);
return propertyFactory.getOrCreate(Double.class, null, defaultValue, keys);
}

private PriorityProperty<?> createFloatProperty(Field field, String[] keys, Float defaultValue) {
Expand All @@ -169,7 +167,7 @@ private PriorityProperty<?> createFloatProperty(Field field, String[] keys, Floa
}
}

return priorityPropertyManager.newPriorityProperty(Float.class, null, defaultValue, keys);
return propertyFactory.getOrCreate(Float.class, null, defaultValue, keys);
}

private PriorityProperty<?> createBooleanProperty(Field field, String[] keys, Boolean defaultValue) {
Expand All @@ -180,7 +178,7 @@ private PriorityProperty<?> createBooleanProperty(Field field, String[] keys, Bo
}
}

return priorityPropertyManager.newPriorityProperty(Boolean.class, null, defaultValue, keys);
return propertyFactory.getOrCreate(Boolean.class, null, defaultValue, keys);
}

private PriorityProperty<?> createLongProperty(Field field, String[] keys, Long defaultValue) {
Expand All @@ -191,7 +189,7 @@ private PriorityProperty<?> createLongProperty(Field field, String[] keys, Long
}
}

return priorityPropertyManager.newPriorityProperty(Long.class, null, defaultValue, keys);
return propertyFactory.getOrCreate(Long.class, null, defaultValue, keys);
}

private PriorityProperty<?> createIntProperty(Field field, String[] keys, Integer defaultValue) {
Expand All @@ -202,10 +200,10 @@ private PriorityProperty<?> createIntProperty(Field field, String[] keys, Intege
}
}

return priorityPropertyManager.newPriorityProperty(Integer.class, null, defaultValue, keys);
return propertyFactory.getOrCreate(Integer.class, null, defaultValue, keys);
}

private String[] collectPropertyKeys(Field field) {
private String[] collectPropertyKeys(Field field, String prefix, Map<String, Object> parameters) {
String propertyPrefix = prefix;
String[] keys = new String[] {field.getName()};

Expand All @@ -225,6 +223,6 @@ private String[] collectPropertyKeys(Field field) {
finalKeys.addAll(resolvedKeys);
}

return finalKeys.toArray(new String[finalKeys.size()]);
return finalKeys.toArray(new String[0]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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 org.apache.servicecomb.config.priority;

import org.apache.servicecomb.foundation.common.utils.bean.Setter;

/**
* do not reference config object instance, otherwise gc for weak hash map will failed
*/
public class ConfigObjectProperty {
private final Setter<Object, Object> setter;

private final PriorityProperty<?> property;

public ConfigObjectProperty(Setter<Object, Object> setter, PriorityProperty<?> property) {
this.setter = setter;
this.property = property;
}

public void updateValue(Object instance) {
setter.set(instance, property.getValue());
}
}

0 comments on commit e608843

Please sign in to comment.