Skip to content

Commit

Permalink
Merge 5937413 into 782a127
Browse files Browse the repository at this point in the history
  • Loading branch information
pandaapo committed May 25, 2023
2 parents 782a127 + 5937413 commit 2bb30ef
Show file tree
Hide file tree
Showing 12 changed files with 452 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

package org.apache.eventmesh.common.config;

import org.apache.eventmesh.common.Constants;
import org.apache.eventmesh.common.file.FileLoad;
import org.apache.eventmesh.common.utils.FileUtils;

import org.apache.commons.lang3.StringUtils;

import java.io.File;
Expand Down Expand Up @@ -137,11 +141,17 @@ public <T> T getConfig(ConfigInfo configInfo) throws IOException {
} else {
File file = new File(filePath);
if (!file.exists()) {
throw new RuntimeException("file is not exists");
if (StringUtils.isBlank(filePath = FileUtils.tryToFindOtherPropFile(filePath, file))) {
throw new RuntimeException("file is not exists");

Check warning on line 145 in eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigService.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L145 was not covered by tests
}
// Update the default or old value's suffix of related field in ConfigService and ConfigInfo.
path = path.replace(FileUtils.getExtension(path), FileUtils.getExtension(filePath));
configInfo.setPath(path);
this.rootPath = path;

Check warning on line 150 in eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigService.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L148 - L150 were not covered by tests
}
}

String suffix = path.substring(path.lastIndexOf('.') + 1);
String suffix = path.substring(path.lastIndexOf(Constants.DOT) + 1);
configInfo.setFilePath(filePath);
configInfo.setResourceUrl(resourceUrl);
object = FileLoad.getFileLoad(suffix).getConfig(configInfo);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.file;

import org.apache.eventmesh.common.config.ConfigInfo;
import org.apache.eventmesh.common.config.convert.Convert;

import java.util.Objects;
import java.util.Properties;

/**
* Base class for {@link PropertiesFileLoad}, {@link YamlFileLoad}
*/
public abstract class BaseFileLoad {

protected final Convert convert = new Convert();

@SuppressWarnings("unchecked")
public <T> T getConfig(Properties properties, ConfigInfo configInfo) {
return (T) convert.doConvert(configInfo, properties);
}

protected <T> T convertIfNeed(Properties properties, ConfigInfo configInfo) {
if (Objects.isNull(configInfo.getClazz())) {
return (T) properties;
}
return (T) convert.doConvert(configInfo, properties);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.file;

import org.apache.eventmesh.common.config.ConfigInfo;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.Objects;

/**
* load config from file
*/
public interface FileLoad {

PropertiesFileLoad PROPERTIES_FILE_LOAD = new PropertiesFileLoad();

YamlFileLoad YAML_FILE_LOAD = new YamlFileLoad();

static FileLoad getFileLoad(String fileType) {
if (Objects.equals("properties", fileType)) {
return PROPERTIES_FILE_LOAD;
} else if (StringUtils.equalsAny(fileType, "yaml", "yml")) {
return YAML_FILE_LOAD;

Check warning on line 40 in eventmesh-common/src/main/java/org/apache/eventmesh/common/file/FileLoad.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-common/src/main/java/org/apache/eventmesh/common/file/FileLoad.java#L40

Added line #L40 was not covered by tests
}
return PROPERTIES_FILE_LOAD;

Check warning on line 42 in eventmesh-common/src/main/java/org/apache/eventmesh/common/file/FileLoad.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-common/src/main/java/org/apache/eventmesh/common/file/FileLoad.java#L42

Added line #L42 was not covered by tests
}

static PropertiesFileLoad getPropertiesFileLoad() {
return PROPERTIES_FILE_LOAD;
}

static YamlFileLoad getYamlFileLoad() {
return YAML_FILE_LOAD;

Check warning on line 50 in eventmesh-common/src/main/java/org/apache/eventmesh/common/file/FileLoad.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-common/src/main/java/org/apache/eventmesh/common/file/FileLoad.java#L50

Added line #L50 was not covered by tests
}

<T> T getConfig(ConfigInfo configInfo) throws IOException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.file;

import org.apache.eventmesh.common.Constants;
import org.apache.eventmesh.common.config.ConfigInfo;

import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.Properties;

public class PropertiesFileLoad extends BaseFileLoad implements FileLoad {

@SuppressWarnings("unchecked")
@Override
public <T> T getConfig(ConfigInfo configInfo) throws IOException {
final Properties properties = new Properties();
if (StringUtils.isNotBlank(configInfo.getResourceUrl())) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(
Objects.requireNonNull(getClass().getResourceAsStream(configInfo.getResourceUrl())), Constants.DEFAULT_CHARSET))) {
properties.load(reader);
}
} else {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(Files.newInputStream(Paths.get(configInfo.getFilePath())), Constants.DEFAULT_CHARSET))) {
properties.load(reader);
}
}
return convertIfNeed(properties, configInfo);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.file;

import org.apache.eventmesh.common.config.ConfigInfo;

import org.apache.commons.lang3.StringUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.yaml.snakeyaml.Yaml;

public class YamlFileLoad extends BaseFileLoad implements FileLoad {

@Override
public <T> T getConfig(ConfigInfo configInfo) throws IOException {
Yaml yaml = new Yaml();
Properties properties = new Properties();

Check warning on line 38 in eventmesh-common/src/main/java/org/apache/eventmesh/common/file/YamlFileLoad.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-common/src/main/java/org/apache/eventmesh/common/file/YamlFileLoad.java#L37-L38

Added lines #L37 - L38 were not covered by tests
if (StringUtils.isNotBlank(configInfo.getResourceUrl())) {
try (InputStream in = getClass().getResourceAsStream(configInfo.getResourceUrl())) {
Object data = yaml.load(in);
flatten("", data, properties);
}

Check warning on line 43 in eventmesh-common/src/main/java/org/apache/eventmesh/common/file/YamlFileLoad.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-common/src/main/java/org/apache/eventmesh/common/file/YamlFileLoad.java#L40-L43

Added lines #L40 - L43 were not covered by tests
} else {
try (FileInputStream in = new FileInputStream(configInfo.getFilePath())) {
Object data = yaml.load(in);
flatten("", data, properties);

Check warning on line 47 in eventmesh-common/src/main/java/org/apache/eventmesh/common/file/YamlFileLoad.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-common/src/main/java/org/apache/eventmesh/common/file/YamlFileLoad.java#L45-L47

Added lines #L45 - L47 were not covered by tests
}
}
return convertIfNeed(properties, configInfo);

Check warning on line 50 in eventmesh-common/src/main/java/org/apache/eventmesh/common/file/YamlFileLoad.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-common/src/main/java/org/apache/eventmesh/common/file/YamlFileLoad.java#L50

Added line #L50 was not covered by tests
}

// Flatten the multi level structure(xxx.yyy.zzz) of the YAML file into a flat properties format
private static void flatten(String prefix, Object data, Properties props) {
if (data instanceof Map) {
Map<String, Object> map = (Map<String, Object>) data;
map.forEach((key, value) -> flatten(prefix + key + ".", value, props));

Check warning on line 57 in eventmesh-common/src/main/java/org/apache/eventmesh/common/file/YamlFileLoad.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-common/src/main/java/org/apache/eventmesh/common/file/YamlFileLoad.java#L56-L57

Added lines #L56 - L57 were not covered by tests
} else if (data instanceof List) {
List<Object> list = (List<Object>) data;

Check warning on line 59 in eventmesh-common/src/main/java/org/apache/eventmesh/common/file/YamlFileLoad.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-common/src/main/java/org/apache/eventmesh/common/file/YamlFileLoad.java#L59

Added line #L59 was not covered by tests
for (int i = 0; i < list.size(); i++) {
flatten(prefix + "[" + i + "].", list.get(i), props);

Check warning on line 61 in eventmesh-common/src/main/java/org/apache/eventmesh/common/file/YamlFileLoad.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-common/src/main/java/org/apache/eventmesh/common/file/YamlFileLoad.java#L61

Added line #L61 was not covered by tests
}
} else {
props.put(prefix.substring(0, prefix.length() - 1), data.toString());

Check warning on line 64 in eventmesh-common/src/main/java/org/apache/eventmesh/common/file/YamlFileLoad.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-common/src/main/java/org/apache/eventmesh/common/file/YamlFileLoad.java#L63-L64

Added lines #L63 - L64 were not covered by tests
}
}

Check warning on line 66 in eventmesh-common/src/main/java/org/apache/eventmesh/common/file/YamlFileLoad.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-common/src/main/java/org/apache/eventmesh/common/file/YamlFileLoad.java#L66

Added line #L66 was not covered by tests
}
Loading

0 comments on commit 2bb30ef

Please sign in to comment.