Unverified Commit fce7ddb3 by Jason Song Committed by GitHub

Merge pull request #804 from nobodyiam/refactor-resource-utils

refactor resource utils a little bit
parents a86dd3e0 dca803c2
package com.ctrip.framework.apollo.core.utils; package com.ctrip.framework.apollo.core.utils;
import java.net.URL;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -43,8 +44,8 @@ public class ResourceUtils { ...@@ -43,8 +44,8 @@ public class ResourceUtils {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (String sropertyName : props.stringPropertyNames()) { for (String propertyName : props.stringPropertyNames()) {
sb.append(sropertyName).append('=').append(props.getProperty(sropertyName)).append('\n'); sb.append(propertyName).append('=').append(props.getProperty(propertyName)).append('\n');
} }
if (sb.length() > 0) { if (sb.length() > 0) {
...@@ -58,6 +59,7 @@ public class ResourceUtils { ...@@ -58,6 +59,7 @@ public class ResourceUtils {
private static InputStream loadConfigFileFromDefaultSearchLocations(String configPath) { private static InputStream loadConfigFileFromDefaultSearchLocations(String configPath) {
try { try {
// load from default search locations
for (String searchLocation : DEFAULT_FILE_SEARCH_LOCATIONS) { for (String searchLocation : DEFAULT_FILE_SEARCH_LOCATIONS) {
File candidate = Paths.get(searchLocation, configPath).toFile(); File candidate = Paths.get(searchLocation, configPath).toFile();
if (candidate.exists() && candidate.isFile() && candidate.canRead()) { if (candidate.exists() && candidate.isFile() && candidate.canRead()) {
...@@ -66,22 +68,35 @@ public class ResourceUtils { ...@@ -66,22 +68,35 @@ public class ResourceUtils {
} }
} }
InputStream in = ClassLoaderUtil.getLoader().getResourceAsStream(configPath); // load from classpath
URL url = ClassLoaderUtil.getLoader().getResource(configPath);
if (url != null) {
InputStream in = getResourceAsStream(url);
if (in != null) { if (in != null) {
logger.debug("Reading config from resource {}", ClassLoaderUtil.getLoader().getResource(configPath).getPath()); logger.debug("Reading config from resource {}", url.getPath());
return in; return in;
} else { }
}
// load outside resource under current user path // load outside resource under current user path
File candidate = new File(System.getProperty("user.dir") + configPath); File candidate = new File(System.getProperty("user.dir"), configPath);
if (candidate.exists() && candidate.isFile() && candidate.canRead()) { if (candidate.exists() && candidate.isFile() && candidate.canRead()) {
logger.debug("Reading config from resource {}", candidate.getAbsolutePath()); logger.debug("Reading config from resource {}", candidate.getAbsolutePath());
return new FileInputStream(candidate); return new FileInputStream(candidate);
} }
}
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
//ignore //ignore
} }
return null; return null;
} }
private static InputStream getResourceAsStream(URL url) {
try {
return url != null ? url.openStream() : null;
} catch (IOException e) {
return null;
}
}
} }
...@@ -9,11 +9,11 @@ ...@@ -9,11 +9,11 @@
</Async> </Async>
</appenders> </appenders>
<loggers> <loggers>
<logger name="com.ctrip.framework.apollo" additivity="false" level="trace"> <logger name="com.ctrip.framework.apollo" additivity="false" level="INFO">
<AppenderRef ref="Async" level="INFO"/> <AppenderRef ref="Async"/>
</logger> </logger>
<logger name="com.ctrip.framework.apollo.demo" additivity="false" level="trace"> <logger name="com.ctrip.framework.apollo.demo" additivity="false" level="DEBUG">
<AppenderRef ref="Async" level="DEBUG"/> <AppenderRef ref="Async"/>
</logger> </logger>
<root level="INFO"> <root level="INFO">
<AppenderRef ref="Async"/> <AppenderRef ref="Async"/>
......
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