From 423d725841896dfc7f676cf367e88a950a81b865 Mon Sep 17 00:00:00 2001 From: eight-nines <1748526511@qq.com> Date: Mon, 19 Dec 2022 23:05:28 +0800 Subject: [PATCH] [1] test code --- .../common/config/CommonConfiguration.java | 79 +++++++-- .../eventmesh/common/config/Config.java | 2 +- .../eventmesh/common/config/ConfigFiled.java | 40 +++++ .../eventmesh/common/config/ConfigInfo.java | 38 +---- .../common/config/ConfigService.java | 73 ++++++--- .../common/config/ConfigurationWrapper.java | 81 ++++++++-- .../eventmesh/common/config/FileLoad.java | 19 +-- .../eventmesh/common/utils/Convert.java | 150 ++++++++++++------ .../common/config/ConfigServiceTest.java | 70 ++++++++ .../newConfiguration-common.properties | 37 +++++ .../consumer/RocketMQConsumerImpl.java | 6 +- 11 files changed, 446 insertions(+), 149 deletions(-) create mode 100644 eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigFiled.java create mode 100644 eventmesh-common/src/test/java/org/apache/eventmesh/common/config/ConfigServiceTest.java create mode 100644 eventmesh-common/src/test/resources/newConfiguration-common.properties 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 8646c01665..d34d181acc 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 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 eventMeshProvideServerProtocols; - public String sysID = "5477"; - public String eventMeshConnectorPluginType = "rocketmq"; - public String eventMeshSecurityPluginType = "security"; - public String eventMeshRegistryPluginType = "namesrv"; - public List 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 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 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 3c91978477..45f65a0e14 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 @@ @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/ConfigFiled.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigFiled.java new file mode 100644 index 0000000000..d12b464edc --- /dev/null +++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigFiled.java @@ -0,0 +1,40 @@ +/* + * 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.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@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 ee3cf3653e..39c4624cdc 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 @@ @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 4b9ec8ecaa..7ba59d7828 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 ConfigService setConfigPath(String configPath) { 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 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 void getConfig(Object object, Class clazz) throws Exception { configMonitorService.monitor(configInfo); } } - - } - - public void getConfig(Object object) throws Exception { - this.getConfig(object, object.getClass()); } - public 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 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 a676a260d8..16a3a53286 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 ConfigurationWrapper(String file, boolean reload) { 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 int getIntProp(String configKey, int defaultValue) { 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 boolean getBoolProp(String configKey, boolean defaultValue) { } 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 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 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 6f16ed586c..99d231cb90 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 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 getConfig(ConfigInfo configInfo) throws Exception; class PropertiesFileLoad implements FileLoad { - private Convert convert = new Convert(); + private final Convert convert = new Convert(); @SuppressWarnings("unchecked") public T getConfig(ConfigInfo configInfo) throws Exception { @@ -64,6 +67,7 @@ public T getConfig(ConfigInfo configInfo) throws Exception { if (Objects.isNull(configInfo.getClazz())) { return (T) properties; } + return (T) convert.createObject(configInfo, properties); } @@ -71,8 +75,6 @@ public T getConfig(ConfigInfo configInfo) throws Exception { public T getConfig(Properties properties, ConfigInfo configInfo) throws Exception { return (T) convert.createObject(configInfo, properties); } - - } class YamlFileLoad implements FileLoad { @@ -83,6 +85,5 @@ public T getConfig(ConfigInfo configInfo) throws Exception { 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 21422bec91..c08866be88 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.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.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, ConvertValue> classToConvert = new HashMap, ConvertValue>(); + private final Map, 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 void register(ConvertValue convertValue, Class... clazzs) { } } + /** + * convert convertInfo to obj + * + * @param obj type + */ public interface ConvertValue { - public default boolean isNotHandleNullValue() { + default boolean isNotHandleNullValue() { return true; } - public T convert(ConvertInfo convertInfo); + T convert(ConvertInfo convertInfo); } private class ConvertObject implements ConvertValue { @@ -118,7 +132,7 @@ private void init(ConfigInfo configInfo) { 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 Object convert(ConvertInfo convertInfo) { 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 Object convert(ConvertInfo convertInfo) { } 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 @@ private void setValue() throws Exception { } 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 { + private static class ConvertCharacter implements ConvertValue { @Override public Character convert(ConvertInfo convertInfo) { @@ -244,18 +277,18 @@ public Character convert(ConvertInfo convertInfo) { } } - private class ConvertBoolean implements ConvertValue { + private static class ConvertBoolean implements ConvertValue { @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 { + private static class ConvertByte implements ConvertValue { @Override public Byte convert(ConvertInfo convertInfo) { @@ -263,7 +296,7 @@ public Byte convert(ConvertInfo convertInfo) { } } - private class ConvertShort implements ConvertValue { + private static class ConvertShort implements ConvertValue { @Override public Short convert(ConvertInfo convertInfo) { @@ -271,7 +304,7 @@ public Short convert(ConvertInfo convertInfo) { } } - private class ConvertInteger implements ConvertValue { + private static class ConvertInteger implements ConvertValue { @Override public Integer convert(ConvertInfo convertInfo) { @@ -279,7 +312,7 @@ public Integer convert(ConvertInfo convertInfo) { } } - private class ConvertLong implements ConvertValue { + private static class ConvertLong implements ConvertValue { @Override public Long convert(ConvertInfo convertInfo) { @@ -287,7 +320,7 @@ public Long convert(ConvertInfo convertInfo) { } } - private class ConvertFloat implements ConvertValue { + private static class ConvertFloat implements ConvertValue { @Override public Float convert(ConvertInfo convertInfo) { @@ -295,7 +328,7 @@ public Float convert(ConvertInfo convertInfo) { } } - private class ConvertDouble implements ConvertValue { + private static class ConvertDouble implements ConvertValue { @Override public Double convert(ConvertInfo convertInfo) { @@ -303,7 +336,7 @@ public Double convert(ConvertInfo convertInfo) { } } - private class ConvertString implements ConvertValue { + private static class ConvertString implements ConvertValue { @Override public String convert(ConvertInfo convertInfo) { @@ -311,7 +344,7 @@ public String convert(ConvertInfo convertInfo) { } } - private class ConvertDate implements ConvertValue { + private static class ConvertDate implements ConvertValue { @Override public Date convert(ConvertInfo convertInfo) { @@ -324,7 +357,7 @@ public Date convert(ConvertInfo convertInfo) { } } - private class ConvertLocalDate implements ConvertValue { + private static class ConvertLocalDate implements ConvertValue { @Override public LocalDate convert(ConvertInfo convertInfo) { @@ -333,7 +366,7 @@ public LocalDate convert(ConvertInfo convertInfo) { } - private class ConvertLocalDateTime implements ConvertValue { + private static class ConvertLocalDateTime implements ConvertValue { @Override public LocalDateTime convert(ConvertInfo convertInfo) { @@ -342,7 +375,7 @@ public LocalDateTime convert(ConvertInfo convertInfo) { } - private class ConvertEnum implements ConvertValue> { + private static class ConvertEnum implements ConvertValue> { @SuppressWarnings({"unchecked", "rawtypes"}) @Override @@ -362,26 +395,28 @@ public boolean isNotHandleNullValue() { @Override public List convert(ConvertInfo convertInfo) { try { - String key = convertInfo.getKey() + "["; + if (convertInfo.getValue() == null) { + return new ArrayList<>(); + } + List values = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(convertInfo.getValue()); List list; if (Objects.equals(convertInfo.getField().getType(), List.class)) { list = new ArrayList<>(); } else { list = (List) 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 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 Map convert(ConvertInfo convertInfo) { } } + private static class ConvertIPAddress implements ConvertValue { + + @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 0000000000..e9442dc503 --- /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 list = new ArrayList<>(); + list.add("metrics-succeed1!!!"); + list.add("metrics-succeed2!!!"); + list.add("metrics-succeed3!!!"); + Assert.assertEquals(config.eventMeshMetricsPluginType, list); + + List 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 0000000000..4d277cd1b7 --- /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 3eb6cf5b0d..fe1000b696 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 @@ 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 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();