Commit 26cc1705 by nobodyiam

load config file(apollo-env.properties) from working directory first, so that it…

load config file(apollo-env.properties) from working directory first, so that it behaves the same as spring boot config files
parent bd1832bf
......@@ -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