Commit 74a39156 by 张乐 Committed by GitHub

Merge pull request #607 from nobodyiam/refine-apollo-env-search

load config file from working directory first
parents 0e8163e5 26cc1705
......@@ -9,16 +9,18 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.Properties;
public class ResourceUtils {
private static final Logger logger = LoggerFactory.getLogger(ResourceUtils.class);
private static final String[] DEFAULT_FILE_SEARCH_LOCATIONS = new String[]{"./config/", "./"};
@SuppressWarnings("unchecked")
public static Properties readConfigFile(String configPath, Properties defaults) {
InputStream in = ClassLoaderUtil.getLoader().getResourceAsStream(configPath);
InputStream in = loadConfigFileFromDefaultSearchLocations(configPath);
logger.debug("Reading config from resource {}", configPath);
Properties props = new Properties();
try {
......@@ -65,4 +67,19 @@ public class ResourceUtils {
}
return props;
}
private static InputStream loadConfigFileFromDefaultSearchLocations(String configPath) {
for (String searchLocation : DEFAULT_FILE_SEARCH_LOCATIONS) {
try {
File candidate = Paths.get(searchLocation, configPath).toFile();
if (candidate.exists() && candidate.isFile() && candidate.canRead()) {
return new FileInputStream(candidate);
}
} catch (Throwable ex) {
//ignore
}
}
return ClassLoaderUtil.getLoader().getResourceAsStream(configPath);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment