Skip to content

Commit 2bb30ef

Browse files
authored
Merge 5937413 into 782a127
2 parents 782a127 + 5937413 commit 2bb30ef

File tree

12 files changed

+452
-114
lines changed

12 files changed

+452
-114
lines changed

eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigService.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717

1818
package org.apache.eventmesh.common.config;
1919

20+
import org.apache.eventmesh.common.Constants;
21+
import org.apache.eventmesh.common.file.FileLoad;
22+
import org.apache.eventmesh.common.utils.FileUtils;
23+
2024
import org.apache.commons.lang3.StringUtils;
2125

2226
import java.io.File;
@@ -137,11 +141,17 @@ public <T> T getConfig(ConfigInfo configInfo) throws IOException {
137141
} else {
138142
File file = new File(filePath);
139143
if (!file.exists()) {
140-
throw new RuntimeException("file is not exists");
144+
if (StringUtils.isBlank(filePath = FileUtils.tryToFindOtherPropFile(filePath, file))) {
145+
throw new RuntimeException("file is not exists");
146+
}
147+
// Update the default or old value's suffix of related field in ConfigService and ConfigInfo.
148+
path = path.replace(FileUtils.getExtension(path), FileUtils.getExtension(filePath));
149+
configInfo.setPath(path);
150+
this.rootPath = path;
141151
}
142152
}
143153

144-
String suffix = path.substring(path.lastIndexOf('.') + 1);
154+
String suffix = path.substring(path.lastIndexOf(Constants.DOT) + 1);
145155
configInfo.setFilePath(filePath);
146156
configInfo.setResourceUrl(resourceUrl);
147157
object = FileLoad.getFileLoad(suffix).getConfig(configInfo);

eventmesh-common/src/main/java/org/apache/eventmesh/common/config/FileLoad.java

Lines changed: 0 additions & 106 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.eventmesh.common.file;
19+
20+
import org.apache.eventmesh.common.config.ConfigInfo;
21+
import org.apache.eventmesh.common.config.convert.Convert;
22+
23+
import java.util.Objects;
24+
import java.util.Properties;
25+
26+
/**
27+
* Base class for {@link PropertiesFileLoad}, {@link YamlFileLoad}
28+
*/
29+
public abstract class BaseFileLoad {
30+
31+
protected final Convert convert = new Convert();
32+
33+
@SuppressWarnings("unchecked")
34+
public <T> T getConfig(Properties properties, ConfigInfo configInfo) {
35+
return (T) convert.doConvert(configInfo, properties);
36+
}
37+
38+
protected <T> T convertIfNeed(Properties properties, ConfigInfo configInfo) {
39+
if (Objects.isNull(configInfo.getClazz())) {
40+
return (T) properties;
41+
}
42+
return (T) convert.doConvert(configInfo, properties);
43+
}
44+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.eventmesh.common.file;
19+
20+
import org.apache.eventmesh.common.config.ConfigInfo;
21+
22+
import org.apache.commons.lang3.StringUtils;
23+
24+
import java.io.IOException;
25+
import java.util.Objects;
26+
27+
/**
28+
* load config from file
29+
*/
30+
public interface FileLoad {
31+
32+
PropertiesFileLoad PROPERTIES_FILE_LOAD = new PropertiesFileLoad();
33+
34+
YamlFileLoad YAML_FILE_LOAD = new YamlFileLoad();
35+
36+
static FileLoad getFileLoad(String fileType) {
37+
if (Objects.equals("properties", fileType)) {
38+
return PROPERTIES_FILE_LOAD;
39+
} else if (StringUtils.equalsAny(fileType, "yaml", "yml")) {
40+
return YAML_FILE_LOAD;
41+
}
42+
return PROPERTIES_FILE_LOAD;
43+
}
44+
45+
static PropertiesFileLoad getPropertiesFileLoad() {
46+
return PROPERTIES_FILE_LOAD;
47+
}
48+
49+
static YamlFileLoad getYamlFileLoad() {
50+
return YAML_FILE_LOAD;
51+
}
52+
53+
<T> T getConfig(ConfigInfo configInfo) throws IOException;
54+
55+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.eventmesh.common.file;
19+
20+
import org.apache.eventmesh.common.Constants;
21+
import org.apache.eventmesh.common.config.ConfigInfo;
22+
23+
import org.apache.commons.lang3.StringUtils;
24+
25+
import java.io.BufferedReader;
26+
import java.io.IOException;
27+
import java.io.InputStreamReader;
28+
import java.nio.file.Files;
29+
import java.nio.file.Paths;
30+
import java.util.Objects;
31+
import java.util.Properties;
32+
33+
public class PropertiesFileLoad extends BaseFileLoad implements FileLoad {
34+
35+
@SuppressWarnings("unchecked")
36+
@Override
37+
public <T> T getConfig(ConfigInfo configInfo) throws IOException {
38+
final Properties properties = new Properties();
39+
if (StringUtils.isNotBlank(configInfo.getResourceUrl())) {
40+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(
41+
Objects.requireNonNull(getClass().getResourceAsStream(configInfo.getResourceUrl())), Constants.DEFAULT_CHARSET))) {
42+
properties.load(reader);
43+
}
44+
} else {
45+
try (BufferedReader reader = new BufferedReader(
46+
new InputStreamReader(Files.newInputStream(Paths.get(configInfo.getFilePath())), Constants.DEFAULT_CHARSET))) {
47+
properties.load(reader);
48+
}
49+
}
50+
return convertIfNeed(properties, configInfo);
51+
}
52+
53+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.eventmesh.common.file;
19+
20+
import org.apache.eventmesh.common.config.ConfigInfo;
21+
22+
import org.apache.commons.lang3.StringUtils;
23+
24+
import java.io.FileInputStream;
25+
import java.io.IOException;
26+
import java.io.InputStream;
27+
import java.util.List;
28+
import java.util.Map;
29+
import java.util.Properties;
30+
31+
import org.yaml.snakeyaml.Yaml;
32+
33+
public class YamlFileLoad extends BaseFileLoad implements FileLoad {
34+
35+
@Override
36+
public <T> T getConfig(ConfigInfo configInfo) throws IOException {
37+
Yaml yaml = new Yaml();
38+
Properties properties = new Properties();
39+
if (StringUtils.isNotBlank(configInfo.getResourceUrl())) {
40+
try (InputStream in = getClass().getResourceAsStream(configInfo.getResourceUrl())) {
41+
Object data = yaml.load(in);
42+
flatten("", data, properties);
43+
}
44+
} else {
45+
try (FileInputStream in = new FileInputStream(configInfo.getFilePath())) {
46+
Object data = yaml.load(in);
47+
flatten("", data, properties);
48+
}
49+
}
50+
return convertIfNeed(properties, configInfo);
51+
}
52+
53+
// Flatten the multi level structure(xxx.yyy.zzz) of the YAML file into a flat properties format
54+
private static void flatten(String prefix, Object data, Properties props) {
55+
if (data instanceof Map) {
56+
Map<String, Object> map = (Map<String, Object>) data;
57+
map.forEach((key, value) -> flatten(prefix + key + ".", value, props));
58+
} else if (data instanceof List) {
59+
List<Object> list = (List<Object>) data;
60+
for (int i = 0; i < list.size(); i++) {
61+
flatten(prefix + "[" + i + "].", list.get(i), props);
62+
}
63+
} else {
64+
props.put(prefix.substring(0, prefix.length() - 1), data.toString());
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)