Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #481] Use YAML file to store configuration #3988

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

import static org.apache.eventmesh.common.utils.ReflectUtils.lookUpField;

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 @@ -138,11 +142,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");
}
// 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;
}
}

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;
}
return PROPERTIES_FILE_LOAD;
}

static PropertiesFileLoad getPropertiesFileLoad() {
return PROPERTIES_FILE_LOAD;
}

static YamlFileLoad getYamlFileLoad() {
return YAML_FILE_LOAD;
}

<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();
if (StringUtils.isNotBlank(configInfo.getResourceUrl())) {
try (InputStream in = getClass().getResourceAsStream(configInfo.getResourceUrl())) {
Object data = yaml.load(in);
flatten("", data, properties);
}
} else {
try (FileInputStream in = new FileInputStream(configInfo.getFilePath())) {
Object data = yaml.load(in);
flatten("", data, properties);
}
}
return convertIfNeed(properties, configInfo);
}

// 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));
} else if (data instanceof List) {
List<Object> list = (List<Object>) data;
for (int i = 0; i < list.size(); i++) {
flatten(prefix + "[" + i + "].", list.get(i), props);
}
} else {
props.put(prefix.substring(0, prefix.length() - 1), data.toString());
}
}
}
Loading