Skip to content

Commit

Permalink
Merge branch 'apache-3.1' into 3.1.4-release
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbumenJ committed Dec 16, 2022
2 parents b58a064 + 705b327 commit 941f1b4
Show file tree
Hide file tree
Showing 30 changed files with 274 additions and 440 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,4 @@ public Object getInternalProperty(String key) {
return null;
}

@Override
public Object getInternalProperty(String key, Object defaultValue) {
for (Configuration config : configList) {
try {
Object value = config.getProperty(key, defaultValue);
if (!ConfigurationUtils.isEmptyValue(value)) {
return value;
}
} catch (Exception e) {
logger.error("Error when trying to get value for key " + key + " from " + config + ", " +
"will continue to try the next one.");
}
}
return defaultValue;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,18 @@ default Object getProperty(String key) {
* Gets a property from the configuration. The default value will return if the configuration doesn't contain
* the mapping for the specified key.
*
* @param key property to retrieve
* @param key property to retrieve
* @param defaultValue default value
* @return the value to which this configuration maps the specified key, or default value if the configuration
* contains no mapping for this key.
*/
default Object getProperty(String key, Object defaultValue) {
return getInternalProperty(key, defaultValue);
Object value = getInternalProperty(key);
return value != null ? value : defaultValue;
}

Object getInternalProperty(String key);

Object getInternalProperty(String key, Object defaultValue);

/**
* Check if the configuration contains the specified key.
*
Expand All @@ -154,12 +153,9 @@ default boolean containsKey(String key) {

default <T> T convert(Class<T> cls, String key, T defaultValue) {
// we only process String properties for now
Object value = getProperty(key, defaultValue);
String value = (String) getProperty(key);

if (!String.class.isInstance(value)) {
if (cls.isInstance(value)) {
return cls.cast(value);
}
if (value == null) {
return defaultValue;
}

Expand All @@ -168,26 +164,24 @@ default <T> T convert(Class<T> cls, String key, T defaultValue) {
return cls.cast(value);
}

String str = (String) value;

if (Boolean.class.equals(cls) || Boolean.TYPE.equals(cls)) {
obj = Boolean.valueOf(str);
obj = Boolean.valueOf(value);
} else if (Number.class.isAssignableFrom(cls) || cls.isPrimitive()) {
if (Integer.class.equals(cls) || Integer.TYPE.equals(cls)) {
obj = Integer.valueOf(str);
obj = Integer.valueOf(value);
} else if (Long.class.equals(cls) || Long.TYPE.equals(cls)) {
obj = Long.valueOf(str);
obj = Long.valueOf(value);
} else if (Byte.class.equals(cls) || Byte.TYPE.equals(cls)) {
obj = Byte.valueOf(str);
obj = Byte.valueOf(value);
} else if (Short.class.equals(cls) || Short.TYPE.equals(cls)) {
obj = Short.valueOf(str);
obj = Short.valueOf(value);
} else if (Float.class.equals(cls) || Float.TYPE.equals(cls)) {
obj = Float.valueOf(str);
obj = Float.valueOf(value);
} else if (Double.class.equals(cls) || Double.TYPE.equals(cls)) {
obj = Double.valueOf(str);
obj = Double.valueOf(value);
}
} else if (cls.isEnum()) {
obj = Enum.valueOf(cls.asSubclass(Enum.class), str);
obj = Enum.valueOf(cls.asSubclass(Enum.class), value);
}

return cls.cast(obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public Environment(ScopeModel scopeModel) {
public void initialize() throws IllegalStateException {
if (initialized.compareAndSet(false, true)) {
this.propertiesConfiguration = new PropertiesConfiguration(scopeModel);
this.systemConfiguration = new SystemConfiguration(scopeModel);
this.systemConfiguration = new SystemConfiguration();
this.environmentConfiguration = new EnvironmentConfiguration();
this.externalConfiguration = new InmemoryConfiguration("ExternalConfig");
this.appExternalConfiguration = new InmemoryConfiguration("AppExternalConfig");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,6 @@ public Object getInternalProperty(String key) {
return value;
}

@Override
public Object getInternalProperty(String key, Object defaultValue) {
String value = System.getenv(key);
if (StringUtils.isEmpty(value)) {
value = System.getenv(StringUtils.toOSStyleKey(key));
}

if (StringUtils.isEmpty(value)) {
return defaultValue;
}
return value;
}

public Map<String, String> getProperties() {
return System.getenv();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ public Object getInternalProperty(String key) {
return store.get(key);
}

@Override
public Object getInternalProperty(String key, Object defaultValue) {
Object v = store.get(key);
if (v != null) {
return v;
} else {
return defaultValue;
}
}

/**
* Add one property into the store, the previous value will be replaced if the key exists
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,6 @@ public Object getInternalProperty(String key) {
return properties.getProperty(key);
}

@Override
public Object getInternalProperty(String key, Object defaultValue) {
Object v = properties.getProperty(key);
if (v != null){
return v;
}else {
return defaultValue;
}
}

public void setProperty(String key, String value) {
properties.setProperty(key, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,4 @@ public Object getInternalProperty(String key) {
return null;
}

@Override
public Object getInternalProperty(String key, Object defaultValue) {
if (StringUtils.isBlank(prefix)) {
return origin.getInternalProperty(key, defaultValue);
}

Object value = origin.getInternalProperty(prefix + "." + key, defaultValue);
if (!ConfigurationUtils.isEmptyValue(value)) {
return value;
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,6 @@ public Object getInternalProperty(String key) {
return properties.getProperty(key);
}

@Override
public Object getInternalProperty(String key, Object defaultValue) {
Object v = properties.getProperty(key);
if (v != null){
return v;
}else {
return defaultValue;
}
}

public void setProperty(String key, String value) {
properties.setProperty(key, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,94 +17,19 @@
package org.apache.dubbo.common.config;


import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.threadpool.manager.FrameworkExecutorRepository;
import org.apache.dubbo.rpc.model.ScopeModel;
import org.apache.dubbo.rpc.model.ScopeModelUtil;

import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
* FIXME: is this really necessary? PropertiesConfiguration should have already covered this:
*
* @See ConfigUtils#getProperty(String)
* @see PropertiesConfiguration
* Configuration from system properties
*/
public class SystemConfiguration implements Configuration {

private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(SystemConfiguration.class);

private final Map<String, Object> cache = new ConcurrentHashMap<>();

private final ScheduledExecutorService sharedScheduledExecutor;

public SystemConfiguration(ScopeModel scopeModel) {
sharedScheduledExecutor = ScopeModelUtil.getFrameworkModel(scopeModel).getBeanFactory()
.getBean(FrameworkExecutorRepository.class).getSharedScheduledExecutor();
sharedScheduledExecutor.scheduleWithFixedDelay(() -> {
if (!cache.isEmpty()) {
Set<String> keys = cache.keySet();
keys.forEach((key) -> overwriteCache(key, System.getProperty(key)));
}
}, 60000, 60000, TimeUnit.MILLISECONDS);
}

@Override
public Object getInternalProperty(String key) {
if (cache.containsKey(key)) {
return cache.get(key);
} else {
Object val = System.getProperty(key);
if (val != null) {
cache.putIfAbsent(key, val);
}
return val;
}
return System.getProperty(key);
}

@Override
public Object getInternalProperty(String key, Object defaultValue) {
if (cache.containsKey(key)) {
return cache.get(key);
} else {
Object val = System.getProperty(key);
if (val != null) {
cache.putIfAbsent(key, val);
} else {
val = defaultValue;
if (defaultValue != null) {
cache.putIfAbsent(key, defaultValue);
}
}
return val;
}
}

public void overwriteCache(String key, Object value) {
if (value != null) {
cache.put(key, value);
}
}

public void clearCache() {
cache.clear();
}


public Map<String, String> getProperties() {
Properties properties = System.getProperties();
Map<String, String> res = new ConcurrentHashMap<>(properties.size());
try {
res.putAll((Map) properties);
} catch (Exception e) {
logger.warn("System property get failed", e);
}
return res;
return (Map) System.getProperties();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,6 @@ public Object getInternalProperty(String key) {
return null;
}

@Override
public Object getInternalProperty(String key, Object defaultValue) {
return null;
}

@Override
public final void close() throws Exception {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ public Object getInternalProperty(String key) {
return null;
}

@Override
public Object getInternalProperty(String key, Object defaultValue) {
return null;
}

@Override
public void addListener(String key, String group, ConfigurationListener listener) {
// no-op
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,6 @@ public Object getInternalProperty(String key) {
return iterateConfigOperation(configuration -> configuration.getInternalProperty(key));
}

@Override
public Object getInternalProperty(String key, Object defaultValue) {
return iterateConfigOperation(configuration -> configuration.getInternalProperty(key, defaultValue));
}

@Override
public boolean publishConfig(String key, String group, String content) throws UnsupportedOperationException {
boolean publishedAll = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@ public Object getInternalProperty(String key) {
return metaData.get(key);
}

@Override
public Object getInternalProperty(String key, Object defaultValue) {
Object v = metaData.get(key);
if (v != null) {
return v;
} else {
return defaultValue;
}
}

public Map<String, String> getProperties() {
return metaData;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.apache.dubbo.common.config;

import org.apache.dubbo.rpc.model.ApplicationModel;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -45,7 +44,7 @@ class SystemConfigurationTest {
@BeforeEach
public void init() {

sysConfig = new SystemConfiguration(ApplicationModel.defaultModel().getDefaultModule());
sysConfig = new SystemConfiguration();
}

/**
Expand All @@ -71,23 +70,23 @@ void testGetSysProperty() {
void testConvert() {
Assertions.assertEquals(
MOCK_STRING_VALUE, sysConfig.convert(String.class, NOT_EXIST_KEY, MOCK_STRING_VALUE));
sysConfig.overwriteCache(MOCK_KEY, String.valueOf(MOCK_BOOL_VALUE));
System.setProperty(MOCK_KEY, String.valueOf(MOCK_BOOL_VALUE));
Assertions.assertEquals(MOCK_BOOL_VALUE, sysConfig.convert(Boolean.class, MOCK_KEY, null));
sysConfig.overwriteCache(MOCK_KEY, String.valueOf(MOCK_STRING_VALUE));
System.setProperty(MOCK_KEY, String.valueOf(MOCK_STRING_VALUE));
Assertions.assertEquals(MOCK_STRING_VALUE, sysConfig.convert(String.class, MOCK_KEY, null));
sysConfig.overwriteCache(MOCK_KEY, String.valueOf(MOCK_INT_VALUE));
System.setProperty(MOCK_KEY, String.valueOf(MOCK_INT_VALUE));
Assertions.assertEquals(MOCK_INT_VALUE, sysConfig.convert(Integer.class, MOCK_KEY, null));
sysConfig.overwriteCache(MOCK_KEY, String.valueOf(MOCK_LONG_VALUE));
System.setProperty(MOCK_KEY, String.valueOf(MOCK_LONG_VALUE));
Assertions.assertEquals(MOCK_LONG_VALUE, sysConfig.convert(Long.class, MOCK_KEY, null));
sysConfig.overwriteCache(MOCK_KEY, String.valueOf(MOCK_SHORT_VALUE));
System.setProperty(MOCK_KEY, String.valueOf(MOCK_SHORT_VALUE));
Assertions.assertEquals(MOCK_SHORT_VALUE, sysConfig.convert(Short.class, MOCK_KEY, null));
sysConfig.overwriteCache(MOCK_KEY, String.valueOf(MOCK_FLOAT_VALUE));
System.setProperty(MOCK_KEY, String.valueOf(MOCK_FLOAT_VALUE));
Assertions.assertEquals(MOCK_FLOAT_VALUE, sysConfig.convert(Float.class, MOCK_KEY, null));
sysConfig.overwriteCache(MOCK_KEY, String.valueOf(MOCK_DOUBLE_VALUE));
System.setProperty(MOCK_KEY, String.valueOf(MOCK_DOUBLE_VALUE));
Assertions.assertEquals(MOCK_DOUBLE_VALUE, sysConfig.convert(Double.class, MOCK_KEY, null));
sysConfig.overwriteCache(MOCK_KEY, String.valueOf(MOCK_BYTE_VALUE));
System.setProperty(MOCK_KEY, String.valueOf(MOCK_BYTE_VALUE));
Assertions.assertEquals(MOCK_BYTE_VALUE, sysConfig.convert(Byte.class, MOCK_KEY, null));
sysConfig.overwriteCache(MOCK_KEY, String.valueOf(ConfigMock.MockOne));
System.setProperty(MOCK_KEY, String.valueOf(ConfigMock.MockOne));
Assertions.assertEquals(ConfigMock.MockOne, sysConfig.convert(ConfigMock.class, MOCK_KEY, null));
}

Expand Down
Loading

0 comments on commit 941f1b4

Please sign in to comment.