This is an automated email from the ASF dual-hosted git repository.

mikexue pushed a commit to branch config-manage-improve
in repository https://gitbox.apache.org/repos/asf/incubator-eventmesh.git


The following commit(s) were added to refs/heads/config-manage-improve by this 
push:
     new 423d72584 [1] test code
     new b9ca4f2fc Merge pull request #2586 from eight-nines/MR1
423d72584 is described below

commit 423d725841896dfc7f676cf367e88a950a81b865
Author: eight-nines <[email protected]>
AuthorDate: Mon Dec 19 23:05:28 2022 +0800

    [1] test code
---
 .../common/config/CommonConfiguration.java         |  79 +++++++++--
 .../org/apache/eventmesh/common/config/Config.java |   2 +-
 .../config/{Config.java => ConfigFiled.java}       |  31 ++---
 .../apache/eventmesh/common/config/ConfigInfo.java |  38 +-----
 .../eventmesh/common/config/ConfigService.java     |  73 ++++++----
 .../common/config/ConfigurationWrapper.java        |  81 ++++++++---
 .../apache/eventmesh/common/config/FileLoad.java   |  19 +--
 .../org/apache/eventmesh/common/utils/Convert.java | 150 ++++++++++++++-------
 .../eventmesh/common/config/ConfigServiceTest.java |  70 ++++++++++
 .../resources/newConfiguration-common.properties   |  37 +++++
 .../rocketmq/consumer/RocketMQConsumerImpl.java    |   6 +-
 11 files changed, 420 insertions(+), 166 deletions(-)

diff --git 
a/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/CommonConfiguration.java
 
b/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/CommonConfiguration.java
index 8646c0166..d34d181ac 100644
--- 
a/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/CommonConfiguration.java
+++ 
b/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/CommonConfiguration.java
@@ -33,33 +33,86 @@ import com.google.common.base.Preconditions;
 
 import lombok.Getter;
 
+@Config(prefix = "eventMesh")
 public class CommonConfiguration {
+    @ConfigFiled(field = "sysid")
+    public String sysID = "5477";
+
+    @ConfigFiled(field = "server.env")
     public String eventMeshEnv = "P";
+
+    @ConfigFiled(field = "server.idc")
     public String eventMeshIDC = "FT";
-    public String eventMeshCluster = "LS";
+
+    @ConfigFiled(field = "server.name")
     public String eventMeshName = "";
-    public List<String> eventMeshProvideServerProtocols;
-    public String sysID = "5477";
-    public String eventMeshConnectorPluginType = "rocketmq";
-    public String eventMeshSecurityPluginType = "security";
-    public String eventMeshRegistryPluginType = "namesrv";
-    public List<String> eventMeshMetricsPluginType;
-    public String eventMeshTracePluginType;
+
+    @ConfigFiled(field = "server.cluster")
+    public String eventMeshCluster = "LS";
+
+    @ConfigFiled(field = "server.hostIp")
+    public String eventMeshServerIp = null;
+
+    @ConfigFiled(field = "registry.plugin.server-addr")
     public String namesrvAddr = "";
+
+
+    @ConfigFiled(field = "trace.plugin")
+    public String eventMeshTracePluginType;
+
+    @ConfigFiled(field = "metrics.plugin")
+    public List<String> eventMeshMetricsPluginType;
+
+    @ConfigFiled(field = "registry.plugin.type")
+    public String eventMeshRegistryPluginType = "namesrv";
+
+    @ConfigFiled(field = "security.plugin.type")
+    public String eventMeshSecurityPluginType = "security";
+
+    @ConfigFiled(field = "connector.plugin.type")
+    public String eventMeshConnectorPluginType = "rocketmq";
+
+
+    @ConfigFiled(field = "registry.plugin.username")
     public String eventMeshRegistryPluginUsername = "";
+
+    @ConfigFiled(field = "registry.plugin.password")
     public String eventMeshRegistryPluginPassword = "";
+
+    @ConfigFiled(field = "server.registry.registerIntervalInMills")
     public Integer eventMeshRegisterIntervalInMills = 10 * 1000;
+
+    @ConfigFiled(field = "server.registry.fetchRegistryAddrIntervalInMills")
     public Integer eventMeshFetchRegistryAddrInterval = 10 * 1000;
-    public String eventMeshServerIp = null;
+
+
+    @ConfigFiled(field = "server.trace.enabled")
+    public boolean eventMeshServerTraceEnable = false;
+
+    @ConfigFiled(field = "server.security.enabled")
     public boolean eventMeshServerSecurityEnable = false;
+
+    @ConfigFiled(field = "server.registry.enabled")
     public boolean eventMeshServerRegistryEnable = false;
-    public boolean eventMeshServerTraceEnable = false;
-    
+
+
+    @ConfigFiled(field = "server.provide.protocols")
+    public List<String> eventMeshProvideServerProtocols;
+
+
+    @ConfigFiled(reload = true)
+    public String eventMeshWebhookOrigin = "eventmesh." + eventMeshIDC;
+
+    public CommonConfiguration() {
+    }
+
+    public void reload() {
+        this.eventMeshWebhookOrigin = "eventmesh." + eventMeshIDC;
+    }
+
     @Getter
     protected ConfigurationWrapper configurationWrapper;
     
-    public String eventMeshWebhookOrigin = "eventmesh." + eventMeshIDC;
-
     public CommonConfiguration(ConfigurationWrapper configurationWrapper) {
         this.configurationWrapper = configurationWrapper;
     }
diff --git 
a/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/Config.java 
b/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/Config.java
index 3c9197847..45f65a0e1 100644
--- 
a/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/Config.java
+++ 
b/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/Config.java
@@ -26,7 +26,7 @@ import java.lang.annotation.Target;
 @Target({ElementType.METHOD, ElementType.TYPE})
 public @interface Config {
 
-    String field();
+    String field() default "";
 
     String path() default "";
 
diff --git 
a/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/Config.java 
b/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigFiled.java
similarity index 72%
copy from 
eventmesh-common/src/main/java/org/apache/eventmesh/common/config/Config.java
copy to 
eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigFiled.java
index 3c9197847..d12b464ed 100644
--- 
a/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/Config.java
+++ 
b/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigFiled.java
@@ -23,21 +23,18 @@ import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 
 @Retention(RetentionPolicy.RUNTIME)
-@Target({ElementType.METHOD, ElementType.TYPE})
-public @interface Config {
-
-    String field();
-
-    String path() default "";
-
-    String prefix();
-
-    String hump() default ".";
-
-    boolean removePrefix() default true;
-
-    boolean monitor() default false;
+@Target({ElementType.TYPE, ElementType.FIELD})
+public @interface ConfigFiled {
+
+    /**
+     * @return The key name of the configuration file
+     */
+    String field() default "";
+
+    /**
+     * Note : When reload is true, the class must have a reload method
+     *
+     * @return Whether to reload. This parameter is used when other fields are 
associated
+     */
+    boolean reload() default false;
 }
-
-
-
diff --git 
a/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigInfo.java
 
b/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigInfo.java
index ee3cf3653..39c4624cd 100644
--- 
a/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigInfo.java
+++ 
b/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigInfo.java
@@ -30,46 +30,20 @@ import lombok.NoArgsConstructor;
 @NoArgsConstructor
 public class ConfigInfo {
 
-    public static final String HUPM_SPOT = "spot";
-
-    public static final String HUPM_ROD = "rod";
+    public static final String HUMP_SPOT = "spot";
+    public static final String HUMP_ROD = "rod";
 
     private String path;
-
     private String field;
-
     private String prefix;
+    private String hump;
+    private boolean monitor;
+    private boolean removePrefix;
 
     private Class<?> clazz;
-
     private Object object;
-
     private String filePath;
 
-    private boolean removePrefix;
-
-    private boolean monitor;
-
-    private String hump;
-
     Field objectField;
-
     Object instance;
-
-    protected void setObjectField(Field objectField) {
-        this.objectField = objectField;
-    }
-
-    protected Field getObjectField() {
-        return this.objectField;
-    }
-
-    protected void setInstance(Object instance) {
-        this.instance = instance;
-    }
-
-    protected Object getInstance() {
-        return this.instance;
-    }
-
-}
+}
\ No newline at end of file
diff --git 
a/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigService.java
 
b/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigService.java
index 4b9ec8eca..7ba59d782 100644
--- 
a/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigService.java
+++ 
b/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigService.java
@@ -17,10 +17,14 @@
 
 package org.apache.eventmesh.common.config;
 
+import org.apache.commons.lang3.StringUtils;
+
 import java.io.File;
 import java.lang.reflect.Field;
+import java.util.Locale;
 import java.util.Objects;
 import java.util.Properties;
+import java.util.Set;
 
 
 public class ConfigService {
@@ -29,12 +33,12 @@ public class ConfigService {
 
     private Properties properties = new Properties();
 
-    private ConfigMonitorService configMonitorService = new 
ConfigMonitorService();
+    private final ConfigMonitorService configMonitorService = new 
ConfigMonitorService();
 
     private String configPath;
 
 
-    public static final ConfigService getInstance() {
+    public static ConfigService getInstance() {
         return INSTANCE;
     }
 
@@ -46,32 +50,63 @@ public class ConfigService {
         return this;
     }
 
-    public ConfigService setRootConfig(String path) throws Exception {
+    public void setRootConfig(String path) throws Exception {
         ConfigInfo configInfo = new ConfigInfo();
         configInfo.setPath(path);
         properties = this.getConfig(configInfo);
-        return this;
+    }
+
+    public <T> T getConfig(Class<?> clazz) {
+        Config[] configArray = clazz.getAnnotationsByType(Config.class);
+        if (configArray.length == 0) {
+            try {
+                return this.getConfig(ConfigInfo.builder()
+                        .clazz(clazz)
+                        .hump(ConfigInfo.HUMP_SPOT)
+                        .build());
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+        }
+
+        Config config = configArray[0];
+        try {
+            ConfigInfo configInfo = new ConfigInfo();
+            configInfo.setClazz(clazz);
+            configInfo.setHump(config.hump());
+            configInfo.setPrefix(config.prefix());
+            configInfo.setMonitor(config.monitor());
+
+            return this.getConfig(configInfo);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public void getConfig(Object object) throws Exception {
+        this.getConfig(object, object.getClass());
     }
 
     public void getConfig(Object object, Class<?> clazz) throws Exception {
         Config[] configArray = clazz.getAnnotationsByType(Config.class);
-        if (configArray == null || configArray.length == 0) {
-            //TODO
+        if (configArray.length == 0) {
             return;
         }
+
         for (Config config : configArray) {
             ConfigInfo configInfo = new ConfigInfo();
             configInfo.setField(config.field());
             configInfo.setPath(config.path());
             configInfo.setPrefix(config.prefix());
             configInfo.setHump(config.hump());
-            configInfo.setObject(object);
             configInfo.setMonitor(config.monitor());
+
             Field field = clazz.getDeclaredField(configInfo.getField());
             configInfo.setClazz(field.getType());
             Object configObject = this.getConfig(configInfo);
             field.setAccessible(true);
             field.set(object, configObject);
+
             if (configInfo.isMonitor()) {
                 configInfo.setObjectField(field);
                 configInfo.setInstance(object);
@@ -79,45 +114,35 @@ public class ConfigService {
                 configMonitorService.monitor(configInfo);
             }
         }
-
-    }
-
-    public void getConfig(Object object) throws Exception {
-        this.getConfig(object, object.getClass());
     }
 
-    public <T> T getConfig(Class<?> clazz) {
-        try {
-            return 
this.getConfig(ConfigInfo.builder().clazz(clazz).hump(ConfigInfo.HUPM_SPOT).build());
-        } catch (Exception e) {
-            throw new RuntimeException(e);
-        }
-    }
 
     @SuppressWarnings("unchecked")
     public <T> T getConfig(ConfigInfo configInfo) throws Exception {
         Object object;
-        if (Objects.isNull(configInfo.getPath())) {
+
+        if (Objects.isNull(configInfo.getPath()) || 
StringUtils.isEmpty(configInfo.getPath().trim())) {
             object = FileLoad.getPropertiesFileLoad().getConfig(properties, 
configInfo);
         } else {
             String path = configInfo.getPath();
             String filePath;
             if (path.startsWith("classPath://")) {
-                filePath = ConfigService.class.getResource("/" + 
path.substring(12)).getPath();
+                filePath = 
Objects.requireNonNull(ConfigService.class.getResource("/" + 
path.substring(12))).getPath();
             } else if (path.startsWith("file://")) {
                 filePath = path.substring(7);
             } else {
                 filePath = this.configPath + path;
             }
+
             File file = new File(filePath);
             if (!file.exists()) {
-                throw new RuntimeException("fie is not existis");
+                throw new RuntimeException("fie is not exists");
             }
+
             String suffix = path.substring(path.lastIndexOf('.') + 1);
             configInfo.setFilePath(filePath);
             object = FileLoad.getFileLoad(suffix).getConfig(configInfo);
         }
         return (T) object;
     }
-
-}
+}
\ No newline at end of file
diff --git 
a/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigurationWrapper.java
 
b/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigurationWrapper.java
index a676a260d..16a3a5328 100644
--- 
a/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigurationWrapper.java
+++ 
b/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigurationWrapper.java
@@ -17,38 +17,61 @@
 
 package org.apache.eventmesh.common.config;
 
-import org.apache.eventmesh.common.ThreadPoolFactory;
+import org.apache.eventmesh.common.file.FileChangeContext;
+import org.apache.eventmesh.common.file.FileChangeListener;
+import org.apache.eventmesh.common.file.WatchFileManager;
 
 import org.apache.commons.lang3.StringUtils;
+import org.apache.logging.log4j.util.Strings;
 
 import java.io.BufferedReader;
+import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
+import java.util.Map.Entry;
 import java.util.Properties;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.TimeUnit;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.google.common.base.Preconditions;
 
 public class ConfigurationWrapper {
 
     public Logger logger = LoggerFactory.getLogger(this.getClass());
 
-    private static final long TIME_INTERVAL = 30 * 1000L;
+    private final String directoryPath;
 
-    private String file;
+    private final String fileName;
 
-    private Properties properties = new Properties();
+    private final Properties properties = new Properties();
 
-    private boolean reload;
+    private final String file;
 
-    private ScheduledExecutorService configLoader = 
ThreadPoolFactory.createSingleScheduledExecutor("eventMesh-configLoader-");
+    private final boolean reload;
 
-    public ConfigurationWrapper(String file, boolean reload) {
-        this.file = file;
+    private final FileChangeListener fileChangeListener = new 
FileChangeListener() {
+        @Override
+        public void onChanged(FileChangeContext changeContext) {
+            load();
+        }
+
+        @Override
+        public boolean support(FileChangeContext changeContext) {
+            return 
changeContext.getWatchEvent().context().toString().contains(fileName);
+        }
+    };
+
+    public ConfigurationWrapper(String directoryPath, String fileName, boolean 
reload) {
+        Preconditions.checkArgument(Strings.isNotEmpty(directoryPath), "please 
configure environment variable 'confPath'");
+        this.directoryPath = directoryPath
+                .replace('/', File.separator.charAt(0))
+                .replace('\\', File.separator.charAt(0));
+        this.fileName = fileName;
+        this.file = (directoryPath + File.separator + fileName)
+                .replace('/', File.separator.charAt(0))
+                .replace('\\', File.separator.charAt(0));
         this.reload = reload;
         init();
     }
@@ -56,18 +79,18 @@ public class ConfigurationWrapper {
     private void init() {
         load();
         if (this.reload) {
-            configLoader.scheduleAtFixedRate(this::load, TIME_INTERVAL, 
TIME_INTERVAL, TimeUnit.MILLISECONDS);
+            WatchFileManager.registerFileChangeListener(directoryPath, 
fileChangeListener);
             Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                 logger.info("Configuration reload task closed");
-                configLoader.shutdownNow();
+                WatchFileManager.deregisterFileChangeListener(directoryPath);
             }));
         }
     }
 
     private void load() {
-        try {
+        try (BufferedReader reader = new BufferedReader(new FileReader(file))) 
{
             logger.info("loading config: {}", file);
-            properties.load(new BufferedReader(new FileReader(file)));
+            properties.load(reader);
         } catch (IOException e) {
             logger.error("loading properties [{}] error", file, e);
         }
@@ -82,7 +105,8 @@ public class ConfigurationWrapper {
         if (StringUtils.isEmpty(configValue)) {
             return defaultValue;
         }
-        Preconditions.checkState(StringUtils.isNumeric(configValue), 
String.format("key:%s, value:%s error", configKey, configValue));
+        Preconditions.checkState(StringUtils.isNumeric(configValue),
+                String.format("key:%s, value:%s error", configKey, 
configValue));
         return Integer.parseInt(configValue);
     }
 
@@ -93,4 +117,31 @@ public class ConfigurationWrapper {
         }
         return Boolean.parseBoolean(configValue);
     }
+
+    private String removePrefix(String key, String prefix, boolean 
removePrefix) {
+        return removePrefix ? key.replace(prefix, "") : key;
+    }
+
+    public Properties getPropertiesByConfig(String prefix, boolean 
removePrefix) {
+        Properties properties = new Properties();
+        prefix = prefix.endsWith(".") ? prefix : prefix + ".";
+        for (Entry<Object, Object> entry : this.properties.entrySet()) {
+            String key = (String) entry.getKey();
+            if (key.startsWith(prefix)) {
+                properties.put(removePrefix(key, prefix, removePrefix), 
entry.getValue());
+            }
+        }
+        return properties;
+    }
+
+    @SuppressWarnings("unchecked")
+    public <T> T getPropertiesByConfig(String prefix, Class<?> clazz, boolean 
removePrefix) {
+        ObjectMapper objectMapper = new ObjectMapper();
+        return (T) objectMapper.convertValue(getPropertiesByConfig(prefix, 
removePrefix), clazz);
+    }
+
+    public Properties getProperties() {
+        return this.properties;
+    }
+
 }
\ No newline at end of file
diff --git 
a/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/FileLoad.java
 
b/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/FileLoad.java
index 6f16ed586..99d231cb9 100644
--- 
a/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/FileLoad.java
+++ 
b/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/FileLoad.java
@@ -29,8 +29,7 @@ import java.util.Properties;
 import org.yaml.snakeyaml.Yaml;
 
 /**
- * FileLoad interface
- *
+ * load config from file
  */
 public interface FileLoad {
 
@@ -40,22 +39,26 @@ public interface FileLoad {
 
     public static FileLoad getFileLoad(String fileType) {
         if (Objects.equals("properties", fileType)) {
-            return new PropertiesFileLoad();
+            return PROPERTIES_FILE_LOAD;
         } else if (Objects.equals("yaml", fileType)) {
-            return new YamlFileLoad();
+            return YAML_FILE_LOAD;
         }
-        return new PropertiesFileLoad();
+        return PROPERTIES_FILE_LOAD;
     }
 
     public static PropertiesFileLoad getPropertiesFileLoad() {
         return PROPERTIES_FILE_LOAD;
     }
 
+    public static YamlFileLoad getYamlFileLoad() {
+        return YAML_FILE_LOAD;
+    }
+
     public <T> T getConfig(ConfigInfo configInfo) throws Exception;
 
     class PropertiesFileLoad implements FileLoad {
 
-        private Convert convert = new Convert();
+        private final Convert convert = new Convert();
 
         @SuppressWarnings("unchecked")
         public <T> T getConfig(ConfigInfo configInfo) throws Exception {
@@ -64,6 +67,7 @@ public interface FileLoad {
             if (Objects.isNull(configInfo.getClazz())) {
                 return (T) properties;
             }
+
             return (T) convert.createObject(configInfo, properties);
         }
 
@@ -71,8 +75,6 @@ public interface FileLoad {
         public <T> T getConfig(Properties properties, ConfigInfo configInfo) 
throws Exception {
             return (T) convert.createObject(configInfo, properties);
         }
-
-
     }
 
     class YamlFileLoad implements FileLoad {
@@ -83,6 +85,5 @@ public interface FileLoad {
             Yaml yaml = new Yaml();
             return (T) yaml.loadAs(new BufferedInputStream(new 
FileInputStream(configInfo.getFilePath())), configInfo.getClazz());
         }
-
     }
 }
diff --git 
a/eventmesh-common/src/main/java/org/apache/eventmesh/common/utils/Convert.java 
b/eventmesh-common/src/main/java/org/apache/eventmesh/common/utils/Convert.java
index 21422bec9..c08866be8 100644
--- 
a/eventmesh-common/src/main/java/org/apache/eventmesh/common/utils/Convert.java
+++ 
b/eventmesh-common/src/main/java/org/apache/eventmesh/common/utils/Convert.java
@@ -17,10 +17,12 @@
 
 package org.apache.eventmesh.common.utils;
 
+import org.apache.eventmesh.common.config.ConfigFiled;
 import org.apache.eventmesh.common.config.ConfigInfo;
 import org.apache.eventmesh.common.config.NotNull;
 
 import java.lang.reflect.Field;
+import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
@@ -35,6 +37,7 @@ import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Objects;
@@ -43,14 +46,19 @@ import java.util.TreeMap;
 import java.util.Vector;
 
 import com.google.common.base.Preconditions;
+import com.google.common.base.Splitter;
 
 import lombok.Data;
 
+import inet.ipaddr.AddressStringException;
+import inet.ipaddr.IPAddress;
+import inet.ipaddr.IPAddressString;
+
 public class Convert {
 
-    private Map<Class<?>, ConvertValue<?>> classToConvert = new 
HashMap<Class<?>, ConvertValue<?>>();
+    private final Map<Class<?>, ConvertValue<?>> classToConvert = new 
HashMap<>();
 
-    private ConvertValue<?> convertEnum = new ConvertEnum();
+    private final ConvertValue<?> convertEnum = new ConvertEnum();
 
     {
         this.register(new ConvertCharacter(), Character.class, char.class);
@@ -67,9 +75,10 @@ public class Convert {
         this.register(new ConvertLocalDateTime(), LocalDateTime.class);
         this.register(new ConvertList(), List.class, ArrayList.class, 
LinkedList.class, Vector.class);
         this.register(new ConvertMap(), Map.class, HashMap.class, 
TreeMap.class, LinkedHashMap.class);
-
+        this.register(new ConvertIPAddress(), IPAddress.class);
     }
 
+
     public Object createObject(ConfigInfo configInfo, Properties properties) {
         ConvertInfo convertInfo = new ConvertInfo();
         convertInfo.setConfigInfo(configInfo);
@@ -92,13 +101,18 @@ public class Convert {
         }
     }
 
+    /**
+     * convert convertInfo to obj
+     *
+     * @param <T> obj type
+     */
     public interface ConvertValue<T> {
 
-        public default boolean isNotHandleNullValue() {
+        default boolean isNotHandleNullValue() {
             return true;
         }
 
-        public T convert(ConvertInfo convertInfo);
+        T convert(ConvertInfo convertInfo);
     }
 
     private class ConvertObject implements ConvertValue<Object> {
@@ -118,7 +132,7 @@ public class Convert {
             if (Objects.nonNull(prefix)) {
                 this.prefix = prefix.endsWith(".") ? prefix : prefix + ".";
             }
-            this.hump = Objects.equals(configInfo.getHump(), 
ConfigInfo.HUPM_ROD) ? '_' : '.';
+            this.hump = Objects.equals(configInfo.getHump(), 
ConfigInfo.HUMP_ROD) ? '_' : '.';
             this.clazz = convertInfo.getClazz();
             this.convertInfo.setHump(this.hump);
         }
@@ -130,13 +144,14 @@ public class Convert {
                 this.object = convertInfo.getClazz().newInstance();
                 this.init(convertInfo.getConfigInfo());
                 this.setValue();
-                Class<?> sperclass = convertInfo.getClazz();
+
+                Class<?> superclass = convertInfo.getClazz();
                 for (; ; ) {
-                    sperclass = sperclass.getSuperclass();
-                    if (Objects.equals(sperclass, Object.class) || 
Objects.isNull(sperclass)) {
+                    superclass = superclass.getSuperclass();
+                    if (Objects.equals(superclass, Object.class) || 
Objects.isNull(superclass)) {
                         break;
                     }
-                    this.clazz = sperclass;
+                    this.clazz = superclass;
                     this.setValue();
                 }
 
@@ -147,14 +162,27 @@ public class Convert {
         }
 
         private void setValue() throws Exception {
-            for (Field field : this.clazz.getDeclaredFields()) {
+            boolean needReload = Boolean.FALSE;
 
+            for (Field field : this.clazz.getDeclaredFields()) {
                 if (Modifier.isStatic(field.getModifiers())) {
                     continue;
                 }
                 field.setAccessible(true);
+
                 ConvertInfo convertInfo = this.convertInfo;
-                String key = this.getKey(field.getName(), hump);
+                String key;
+                ConfigFiled configFiled = 
field.getAnnotation(ConfigFiled.class);
+                StringBuilder keyPrefix = new 
StringBuilder(Objects.isNull(prefix) ? "" : prefix);
+                if (configFiled == null || configFiled.field().equals("")) {
+                    key = this.getKey(field.getName(), hump, keyPrefix);
+                } else {
+                    key = keyPrefix.append(configFiled.field()).toString();
+                }
+                if (!needReload && configFiled != null && 
configFiled.reload()) {
+                    needReload = Boolean.TRUE;
+                }
+
                 Class<?> clazz = field.getType();
                 ConvertValue<?> convertValue = classToConvert.get(clazz);
                 if (clazz.isEnum()) {
@@ -198,45 +226,50 @@ public class Convert {
                 }
                 field.set(object, value);
             }
-        }
 
-        public String getKey(String fieldName, char spot) {
-            StringBuffer key = new StringBuffer(Objects.isNull(prefix) ? "" : 
prefix);
+            if (!needReload) {
+                return;
+            }
+            Method method = this.clazz.getDeclaredMethod("reload", null);
+            method.setAccessible(true);
+            method.invoke(this.object, null);
+        }
 
+        public String getKey(String fieldName, char spot, StringBuilder key) {
             boolean currency = false;
-            for (int i = 0; i < fieldName.length(); i++) {
+            int length = fieldName.length();
+            for (int i = 0; i < length; i++) {
                 char c = fieldName.charAt(i);
+                boolean b = i < length - 1 && fieldName.charAt(i + 1) > 96;
+
                 if (currency) {
-                    if (fieldName.length() > (i + 1) && fieldName.charAt(i + 
1) > 96) {
+                    if (b) {
                         key.append(spot);
                         key.append((char) (c + 32));
                         currency = false;
                     } else {
                         key.append(c);
                     }
-                    key.append(c);
                 } else {
                     if (c > 96) {
                         key.append(c);
                     } else {
                         key.append(spot);
-                        if (fieldName.length() > (i + 1) && fieldName.charAt(i 
+ 1) > 96) {
+                        if (b) {
                             key.append((char) (c + 32));
                         } else {
                             key.append(c);
                             currency = true;
                         }
-
                     }
                 }
             }
-            return key.toString();
-        }
-
 
+            return key.toString().toLowerCase(Locale.ROOT);
+        }
     }
 
-    private class ConvertCharacter implements ConvertValue<Character> {
+    private static class ConvertCharacter implements ConvertValue<Character> {
 
         @Override
         public Character convert(ConvertInfo convertInfo) {
@@ -244,18 +277,18 @@ public class Convert {
         }
     }
 
-    private class ConvertBoolean implements ConvertValue<Boolean> {
+    private static class ConvertBoolean implements ConvertValue<Boolean> {
 
         @Override
         public Boolean convert(ConvertInfo convertInfo) {
-            if (Objects.equals(convertInfo.getKey().length(), 1)) {
-                return Objects.equals(convertInfo.getKey(), "1") ? true : 
false;
+            if (Objects.equals(convertInfo.getValue().length(), 1)) {
+                return Objects.equals(convertInfo.getValue(), "1") ? 
Boolean.TRUE : Boolean.FALSE;
             }
-            return Boolean.valueOf(convertInfo.getKey());
+            return Boolean.valueOf(convertInfo.getValue());
         }
     }
 
-    private class ConvertByte implements ConvertValue<Byte> {
+    private static class ConvertByte implements ConvertValue<Byte> {
 
         @Override
         public Byte convert(ConvertInfo convertInfo) {
@@ -263,7 +296,7 @@ public class Convert {
         }
     }
 
-    private class ConvertShort implements ConvertValue<Short> {
+    private static class ConvertShort implements ConvertValue<Short> {
 
         @Override
         public Short convert(ConvertInfo convertInfo) {
@@ -271,7 +304,7 @@ public class Convert {
         }
     }
 
-    private class ConvertInteger implements ConvertValue<Integer> {
+    private static class ConvertInteger implements ConvertValue<Integer> {
 
         @Override
         public Integer convert(ConvertInfo convertInfo) {
@@ -279,7 +312,7 @@ public class Convert {
         }
     }
 
-    private class ConvertLong implements ConvertValue<Long> {
+    private static class ConvertLong implements ConvertValue<Long> {
 
         @Override
         public Long convert(ConvertInfo convertInfo) {
@@ -287,7 +320,7 @@ public class Convert {
         }
     }
 
-    private class ConvertFloat implements ConvertValue<Float> {
+    private static class ConvertFloat implements ConvertValue<Float> {
 
         @Override
         public Float convert(ConvertInfo convertInfo) {
@@ -295,7 +328,7 @@ public class Convert {
         }
     }
 
-    private class ConvertDouble implements ConvertValue<Double> {
+    private static class ConvertDouble implements ConvertValue<Double> {
 
         @Override
         public Double convert(ConvertInfo convertInfo) {
@@ -303,7 +336,7 @@ public class Convert {
         }
     }
 
-    private class ConvertString implements ConvertValue<String> {
+    private static class ConvertString implements ConvertValue<String> {
 
         @Override
         public String convert(ConvertInfo convertInfo) {
@@ -311,7 +344,7 @@ public class Convert {
         }
     }
 
-    private class ConvertDate implements ConvertValue<Date> {
+    private static class ConvertDate implements ConvertValue<Date> {
 
         @Override
         public Date convert(ConvertInfo convertInfo) {
@@ -324,7 +357,7 @@ public class Convert {
         }
     }
 
-    private class ConvertLocalDate implements ConvertValue<LocalDate> {
+    private static class ConvertLocalDate implements ConvertValue<LocalDate> {
 
         @Override
         public LocalDate convert(ConvertInfo convertInfo) {
@@ -333,7 +366,7 @@ public class Convert {
 
     }
 
-    private class ConvertLocalDateTime implements ConvertValue<LocalDateTime> {
+    private static class ConvertLocalDateTime implements 
ConvertValue<LocalDateTime> {
 
         @Override
         public LocalDateTime convert(ConvertInfo convertInfo) {
@@ -342,7 +375,7 @@ public class Convert {
 
     }
 
-    private class ConvertEnum implements ConvertValue<Enum<?>> {
+    private static class ConvertEnum implements ConvertValue<Enum<?>> {
 
         @SuppressWarnings({"unchecked", "rawtypes"})
         @Override
@@ -362,26 +395,28 @@ public class Convert {
         @Override
         public List<Object> convert(ConvertInfo convertInfo) {
             try {
-                String key = convertInfo.getKey() + "[";
+                if (convertInfo.getValue() == null) {
+                    return new ArrayList<>();
+                }
+                List<String> values = 
Splitter.on(",").omitEmptyStrings().trimResults().splitToList(convertInfo.getValue());
                 List<Object> list;
                 if (Objects.equals(convertInfo.getField().getType(), 
List.class)) {
                     list = new ArrayList<>();
                 } else {
                     list = (List<Object>) 
convertInfo.getField().getType().newInstance();
                 }
+
                 Type parameterizedType = ((ParameterizedType) 
convertInfo.getField().getGenericType()).getActualTypeArguments()[0];
                 ConvertValue<?> convert = 
classToConvert.get(parameterizedType);
                 if (Objects.isNull(convert)) {
                     throw new RuntimeException("convert is null");
                 }
-                for (Entry<Object, Object> entry : 
convertInfo.getProperties().entrySet()) {
-                    String propertiesKey = entry.getKey().toString();
-                    if (propertiesKey.startsWith(key)) {
-                        String value = entry.getValue().toString();
-                        convertInfo.setValue(value);
-                        list.add(convert.convert(convertInfo));
-                    }
+
+                for (String value : values) {
+                    convertInfo.setValue(value);
+                    list.add(convert.convert(convertInfo));
                 }
+
                 return list;
             } catch (Exception e) {
                 throw new RuntimeException(e);
@@ -426,15 +461,26 @@ public class Convert {
         }
     }
 
+    private static class ConvertIPAddress implements ConvertValue<IPAddress> {
+
+        @Override
+        public IPAddress convert(ConvertInfo convertInfo) {
+            try {
+                return new IPAddressString(convertInfo.getValue()).toAddress();
+            } catch (AddressStringException e) {
+                throw new RuntimeException(e);
+            }
+        }
+    }
 
     @Data
-    class ConvertInfo {
-        Class<?> clazz;
-        String value;
+    static class ConvertInfo {
+        char hump;
         String key;
-        Properties properties;
         Field field;
+        String value;
+        Class<?> clazz;
+        Properties properties;
         ConfigInfo configInfo;
-        char hump;
     }
 }
diff --git 
a/eventmesh-common/src/test/java/org/apache/eventmesh/common/config/ConfigServiceTest.java
 
b/eventmesh-common/src/test/java/org/apache/eventmesh/common/config/ConfigServiceTest.java
new file mode 100644
index 000000000..e9442dc50
--- /dev/null
+++ 
b/eventmesh-common/src/test/java/org/apache/eventmesh/common/config/ConfigServiceTest.java
@@ -0,0 +1,70 @@
+/*
+ * 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.eventmesh.common.config;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class ConfigServiceTest {
+
+
+    @Test
+    public void testGetConfigForCommonConfiguration() throws Exception {
+        ConfigService configService = ConfigService.getInstance();
+        
configService.setRootConfig("classPath://newConfiguration-common.properties");
+
+        CommonConfiguration config = 
configService.getConfig(CommonConfiguration.class);
+
+        Assert.assertEquals(config.eventMeshEnv, "env-succeed!!!");
+        Assert.assertEquals(config.eventMeshIDC, "idc-succeed!!!");
+        Assert.assertEquals(config.eventMeshCluster, "cluster-succeed!!!");
+        Assert.assertEquals(config.eventMeshName, "name-succeed!!!");
+        Assert.assertEquals(config.sysID, "sysid-succeed!!!");
+        Assert.assertEquals(config.eventMeshConnectorPluginType, 
"connector-succeed!!!");
+        Assert.assertEquals(config.eventMeshSecurityPluginType, 
"security-succeed!!!");
+        Assert.assertEquals(config.eventMeshRegistryPluginType, 
"registry-succeed!!!");
+        Assert.assertEquals(config.eventMeshTracePluginType, 
"trace-succeed!!!");
+        Assert.assertEquals(config.eventMeshServerIp, "hostIp-succeed!!!");
+        Assert.assertEquals(config.eventMeshRegistryPluginUsername, 
"username-succeed!!!");
+        Assert.assertEquals(config.eventMeshRegistryPluginPassword, 
"password-succeed!!!");
+
+        Assert.assertEquals(config.eventMeshRegisterIntervalInMills, 
Integer.valueOf(816));
+        Assert.assertEquals(config.eventMeshFetchRegistryAddrInterval, 
Integer.valueOf(1816));
+
+        List<String> list = new ArrayList<>();
+        list.add("metrics-succeed1!!!");
+        list.add("metrics-succeed2!!!");
+        list.add("metrics-succeed3!!!");
+        Assert.assertEquals(config.eventMeshMetricsPluginType, list);
+
+        List<String> list1 = new ArrayList<>();
+        list1.add("TCP");
+        list1.add("HTTP");
+        list1.add("GRPC");
+        Assert.assertEquals(config.eventMeshProvideServerProtocols, list1);
+
+        Assert.assertEquals(config.eventMeshServerSecurityEnable, 
Boolean.TRUE);
+        Assert.assertEquals(config.eventMeshServerRegistryEnable, 
Boolean.TRUE);
+        Assert.assertEquals(config.eventMeshServerTraceEnable, Boolean.TRUE);
+
+        Assert.assertEquals(config.eventMeshWebhookOrigin, 
"eventmesh.idc-succeed!!!");
+    }
+}
\ No newline at end of file
diff --git 
a/eventmesh-common/src/test/resources/newConfiguration-common.properties 
b/eventmesh-common/src/test/resources/newConfiguration-common.properties
new file mode 100644
index 000000000..4d277cd1b
--- /dev/null
+++ b/eventmesh-common/src/test/resources/newConfiguration-common.properties
@@ -0,0 +1,37 @@
+#
+# 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.
+#
+eventMesh.server.env=env-succeed!!!
+eventMesh.server.idc=idc-succeed!!!
+eventMesh.sysid=sysid-succeed!!!
+eventMesh.server.cluster=cluster-succeed!!!
+eventMesh.server.name=name-succeed!!!
+eventMesh.server.hostIp=hostIp-succeed!!!
+eventMesh.connector.plugin.type=connector-succeed!!!
+eventMesh.security.plugin.type=security-succeed!!!
+eventMesh.registry.plugin.type=registry-succeed!!!
+eventMesh.trace.plugin=trace-succeed!!!
+eventMesh.server.registry.registerIntervalInMills=816
+eventMesh.server.registry.fetchRegistryAddrIntervalInMills=1816
+eventMesh.metrics.plugin=metrics-succeed1!!!,metrics-succeed2!!!,metrics-succeed3!!!
+
+eventMesh.server.security.enabled=true
+eventMesh.server.registry.enabled=true
+eventMesh.server.trace.enabled=true
+
+eventMesh.server.provide.protocols=TCP,HTTP,GRPC
+eventMesh.registry.plugin.username=username-succeed!!!
+eventMesh.registry.plugin.password=password-succeed!!!
diff --git 
a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/consumer/RocketMQConsumerImpl.java
 
b/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/consumer/RocketMQConsumerImpl.java
index 3eb6cf5b0..fe1000b69 100644
--- 
a/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/consumer/RocketMQConsumerImpl.java
+++ 
b/eventmesh-connector-plugin/eventmesh-connector-rocketmq/src/main/java/org/apache/eventmesh/connector/rocketmq/consumer/RocketMQConsumerImpl.java
@@ -20,8 +20,8 @@ package org.apache.eventmesh.connector.rocketmq.consumer;
 import org.apache.eventmesh.api.AbstractContext;
 import org.apache.eventmesh.api.EventListener;
 import org.apache.eventmesh.api.consumer.Consumer;
-import org.apache.eventmesh.common.config.Config;
 import org.apache.eventmesh.common.Constants;
+import org.apache.eventmesh.common.config.Config;
 import org.apache.eventmesh.connector.rocketmq.config.ClientConfiguration;
 
 import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;
@@ -37,7 +37,7 @@ import io.cloudevents.CloudEvent;
 import lombok.extern.slf4j.Slf4j;
 
 @Slf4j
-@Config(field = "clientConfiguration" , prefix = "")
+@Config(field = "clientConfiguration", prefix = "")
 public class RocketMQConsumerImpl implements Consumer {
 
     public Logger messageLogger = LoggerFactory.getLogger("message");
@@ -45,7 +45,7 @@ public class RocketMQConsumerImpl implements Consumer {
     private PushConsumerImpl pushConsumer;
 
     private ClientConfiguration clientConfiguration;
-    
+
     @Override
     public synchronized void init(Properties keyValue) throws Exception {
         clientConfiguration.init();


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]


Reply via email to