-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: root config path by default resolved #289 * test: add null test
- Loading branch information
1 parent
e502bc6
commit d0a8dfd
Showing
11 changed files
with
253 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
config/src/main/java/com/megaease/easeagent/config/ConfigLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright (c) 2021, MegaEase | ||
* All rights reserved. | ||
* | ||
* Licensed 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 com.megaease.easeagent.config; | ||
|
||
import com.megaease.easeagent.config.yaml.YamlReader; | ||
import com.megaease.easeagent.log4j2.Logger; | ||
import com.megaease.easeagent.log4j2.LoggerFactory; | ||
import org.yaml.snakeyaml.parser.ParserException; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
public class ConfigLoader { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigLoader.class); | ||
|
||
private static boolean checkYaml(String filename) { | ||
return filename.endsWith(".yaml") || filename.endsWith(".yml"); | ||
} | ||
|
||
static GlobalConfigs loadFromFile(File file) { | ||
try (FileInputStream in = new FileInputStream(file)) { | ||
return ConfigLoader.loadFromStream(in, file.getAbsolutePath()); | ||
} catch (IOException e) { | ||
LOGGER.warn("Load config file failure: {}", file.getAbsolutePath()); | ||
} | ||
return new GlobalConfigs(Collections.emptyMap()); | ||
} | ||
|
||
static GlobalConfigs loadFromStream(InputStream in, String filename) throws IOException { | ||
if (in != null) { | ||
Map<String, String> map; | ||
if (checkYaml(filename)) { | ||
try { | ||
map = new YamlReader().load(in).compress(); | ||
} catch (ParserException e) { | ||
LOGGER.warn("Wrong Yaml format, load config file failure: {}", filename); | ||
map = Collections.emptyMap(); | ||
} | ||
} else { | ||
map = extractPropsMap(in); | ||
} | ||
return new GlobalConfigs(map); | ||
} else { | ||
return new GlobalConfigs(Collections.emptyMap()); | ||
} | ||
} | ||
|
||
private static HashMap<String, String> extractPropsMap(InputStream in) throws IOException { | ||
Properties properties = new Properties(); | ||
properties.load(in); | ||
HashMap<String, String> map = new HashMap<>(); | ||
for (String one : properties.stringPropertyNames()) { | ||
map.put(one, properties.getProperty(one)); | ||
} | ||
return map; | ||
} | ||
|
||
static GlobalConfigs loadFromClasspath(ClassLoader classLoader, String file) { | ||
try (InputStream in = classLoader.getResourceAsStream(file)) { | ||
return ConfigLoader.loadFromStream(in, file); | ||
} catch (IOException e) { | ||
LOGGER.warn("Load config file:{} by classloader:{} failure: {}", file, classLoader.toString(), e); | ||
} | ||
|
||
return new GlobalConfigs(Collections.emptyMap()); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
config/src/main/java/com/megaease/easeagent/config/JarFileConfigLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright (c) 2021, MegaEase | ||
* All rights reserved. | ||
* | ||
* Licensed 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 com.megaease.easeagent.config; | ||
|
||
import com.megaease.easeagent.log4j2.Logger; | ||
import com.megaease.easeagent.log4j2.LoggerFactory; | ||
import com.megaease.easeagent.plugin.api.config.ConfigConst; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.jar.JarFile; | ||
import java.util.zip.ZipEntry; | ||
|
||
public class JarFileConfigLoader { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(JarFileConfigLoader.class); | ||
|
||
static GlobalConfigs load(String file) { | ||
String agentJarPath = System.getProperty(ConfigConst.AGENT_JAR_PATH); | ||
if (agentJarPath == null) { | ||
return null; | ||
} | ||
try { | ||
JarFile jarFile = new JarFile(new File(agentJarPath)); | ||
ZipEntry zipEntry = jarFile.getEntry(file); | ||
if (zipEntry == null) { | ||
return null; | ||
} | ||
try (InputStream in = jarFile.getInputStream(zipEntry)) { | ||
return ConfigLoader.loadFromStream(in, file); | ||
} catch (IOException e) { | ||
LOGGER.debug("Load config file:{} failure: {}", file, e); | ||
} | ||
} catch (IOException e) { | ||
LOGGER.debug("create JarFile:{} failure: {}", agentJarPath, e); | ||
} | ||
return null; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
config/src/test/java/com/megaease/easeagent/config/JarFileConfigLoaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright (c) 2021, MegaEase | ||
* All rights reserved. | ||
* | ||
* Licensed 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 com.megaease.easeagent.config; | ||
|
||
import com.megaease.easeagent.plugin.api.config.ConfigConst; | ||
import org.junit.After; | ||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
import java.net.URISyntaxException; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class JarFileConfigLoaderTest { | ||
|
||
@After | ||
public void after() { | ||
System.clearProperty(ConfigConst.AGENT_JAR_PATH); | ||
} | ||
|
||
|
||
@Test | ||
public void load() throws URISyntaxException { | ||
String fileName = "agent.properties"; | ||
assertNull(JarFileConfigLoader.load(null)); | ||
assertNull(JarFileConfigLoader.load(fileName)); | ||
String jarPath = new File(this.getClass().getClassLoader().getResource("easeagent_config.jar").toURI()).getPath(); | ||
System.setProperty(ConfigConst.AGENT_JAR_PATH, jarPath); | ||
|
||
GlobalConfigs config = JarFileConfigLoader.load(fileName); | ||
assertNotNull(config); | ||
assertEquals("demo-jar-service", config.getString("name")); | ||
assertEquals("demo-system", config.getString("system")); | ||
|
||
config = JarFileConfigLoader.load("agent.yaml"); | ||
assertNotNull(config); | ||
assertEquals("test-jar-service", config.getString("name")); | ||
assertEquals("demo-system", config.getString("system")); | ||
|
||
config = JarFileConfigLoader.load("agentaaaa.yaml"); | ||
assertNull(config); | ||
|
||
|
||
String nullJarPath = new File("easeagent_config_null.jar").getPath(); | ||
System.setProperty(ConfigConst.AGENT_JAR_PATH, nullJarPath); | ||
|
||
config = JarFileConfigLoader.load("agent.yaml"); | ||
assertNull(config); | ||
|
||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
mock/config-mock/src/main/java/com/megaease/easeagent/config/MockConfigLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright (c) 2021, MegaEase | ||
* All rights reserved. | ||
* | ||
* Licensed 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 com.megaease.easeagent.config; | ||
|
||
import java.io.File; | ||
|
||
public class MockConfigLoader { | ||
public static GlobalConfigs loadFromFile(File file) { | ||
return ConfigLoader.loadFromFile(file); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.