Skip to content

Commit

Permalink
Merge pull request apache#3096, code review config center.
Browse files Browse the repository at this point in the history
* polish javadoc

* git rid of template class for apollo

* get rid of template class for NopDynamicConfiguration

* get rid of tempate class for ZookeeperDynamicConfiguration

* get rid of AbstractDynamicConfiguration

* get rid of AbstractConfiguration

* fix compilation error

* fix compilation error

* polish code
  • Loading branch information
beiwei30 authored and khanimteyaz committed Jan 18, 2019
1 parent 7ffcd70 commit 874154d
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 384 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
* This is an abstraction specially customized for the sequence Dubbo retrieves properties.
*/
public abstract class AbstractPrefixConfiguration extends AbstractConfiguration {
public abstract class AbstractPrefixConfiguration implements Configuration {
protected String id;
protected String prefix;

Expand All @@ -44,9 +44,10 @@ public Object getProperty(String key, Object defaultValue) {
if (value == null && StringUtils.isNotEmpty(prefix)) {
value = getInternalProperty(prefix + key);
}

if (value == null) {
value = super.getProperty(key, defaultValue);
value = getInternalProperty(key);
}
return value;
return value != null ? value : defaultValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@
*/
package org.apache.dubbo.common.config;

import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
*
*/
public class CompositeConfiguration extends AbstractConfiguration {
public class CompositeConfiguration implements Configuration {
private Logger logger = LoggerFactory.getLogger(CompositeConfiguration.class);

/**
* List holding all the configuration
Expand Down Expand Up @@ -56,7 +60,7 @@ public void addConfiguration(int pos, Configuration configuration) {
}

@Override
protected Object getInternalProperty(String key) {
public Object getInternalProperty(String key) {
Configuration firstMatchingConfiguration = null;
for (Configuration config : configList) {
try {
Expand All @@ -65,7 +69,7 @@ protected Object getInternalProperty(String key) {
break;
}
} catch (Exception e) {
e.printStackTrace();
logger.error("Error when trying to get value for key " + key + " from " + config + ", will continue to try the next one.");
}
}
if (firstMatchingConfiguration != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public interface Configuration {
* @param key The configuration key.
* @return The associated string.
*/
String getString(String key);
default String getString(String key) {
return convert(String.class, key, null);
}

/**
* Get a string associated with the given configuration key.
Expand All @@ -38,7 +40,9 @@ public interface Configuration {
* @return The associated string if key is found and has valid
* format, default value otherwise.
*/
String getString(String key, String defaultValue);
default String getString(String key, String defaultValue) {
return convert(String.class, key, defaultValue);
}

/**
* Gets a property from the configuration. This is the most basic get
Expand All @@ -56,7 +60,9 @@ public interface Configuration {
* @return the value to which this configuration maps the specified key, or
* null if the configuration contains no mapping for this key.
*/
Object getProperty(String key);
default Object getProperty(String key) {
return getProperty(key, null);
}

/**
* Gets a property from the configuration. The default value will return if the configuration doesn't contain
Expand All @@ -67,7 +73,12 @@ public interface Configuration {
* @return the value to which this configuration maps the specified key, or default value if the configuration
* contains no mapping for this key.
*/
Object getProperty(String key, Object defaultValue);
default Object getProperty(String key, Object defaultValue) {
Object value = getInternalProperty(key);
return value != null ? value : defaultValue;
}

Object getInternalProperty(String key);

/**
* Check if the configuration contains the specified key.
Expand All @@ -76,7 +87,50 @@ public interface Configuration {
* @return {@code true} if the configuration contains a value for this
* key, {@code false} otherwise
*/
boolean containsKey(String key);
default boolean containsKey(String key) {
return getProperty(key) != null;
}


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

if (value == null) {
return defaultValue;
}

Object obj = value;
if (cls.isInstance(value)) {
return cls.cast(value);
}

if (String.class.equals(cls)) {
return cls.cast(value);
}

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

return cls.cast(obj);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public EnvironmentConfiguration() {
}

@Override
protected Object getInternalProperty(String key) {
public Object getInternalProperty(String key) {
return System.getenv(key);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public InmemoryConfiguration() {
}

@Override
protected Object getInternalProperty(String key) {
public Object getInternalProperty(String key) {
return store.get(key);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public PropertiesConfiguration() {
}

@Override
protected Object getInternalProperty(String key) {
public Object getInternalProperty(String key) {
return ConfigUtils.getProperty(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public SystemConfiguration() {
}

@Override
protected Object getInternalProperty(String key) {
public Object getInternalProperty(String key) {
return System.getProperty(key);
}

Expand Down
Loading

0 comments on commit 874154d

Please sign in to comment.