-
Notifications
You must be signed in to change notification settings - Fork 625
Commit
- Loading branch information
There are no files selected for viewing
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(); | ||
Check warning on line 38 in eventmesh-common/src/main/java/org/apache/eventmesh/common/file/YamlFileLoad.java
|
||
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
|
||
} 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
|
||
} | ||
} | ||
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)); | ||
Check warning on line 57 in eventmesh-common/src/main/java/org/apache/eventmesh/common/file/YamlFileLoad.java
|
||
} 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()); | ||
Check warning on line 64 in eventmesh-common/src/main/java/org/apache/eventmesh/common/file/YamlFileLoad.java
|
||
} | ||
} | ||
} |