in dubbo-common/src/main/java/org/apache/dubbo/common/utils/ConfigUtils.java [211:307]
public static Properties loadProperties(
Set<ClassLoader> classLoaders, String fileName, boolean allowMultiFile, boolean optional) {
Properties properties = new Properties();
// add scene judgement in windows environment Fix 2557
if (checkFileNameExist(fileName)) {
try {
FileInputStream input = new FileInputStream(fileName);
try {
properties.load(input);
} finally {
input.close();
}
} catch (Throwable e) {
logger.warn(
COMMON_IO_EXCEPTION,
"",
"",
"Failed to load " + fileName + " file from " + fileName + "(ignore this file): "
+ e.getMessage(),
e);
}
return properties;
}
Set<java.net.URL> set = null;
try {
List<ClassLoader> classLoadersToLoad = new LinkedList<>();
classLoadersToLoad.add(ClassUtils.getClassLoader());
classLoadersToLoad.addAll(classLoaders);
set = ClassLoaderResourceLoader.loadResources(fileName, classLoadersToLoad).values().stream()
.reduce(new LinkedHashSet<>(), (a, i) -> {
a.addAll(i);
return a;
});
} catch (Throwable t) {
logger.warn(COMMON_IO_EXCEPTION, "", "", "Fail to load " + fileName + " file: " + t.getMessage(), t);
}
if (CollectionUtils.isEmpty(set)) {
if (!optional) {
logger.warn(COMMON_IO_EXCEPTION, "", "", "No " + fileName + " found on the class path.");
}
return properties;
}
if (!allowMultiFile) {
if (set.size() > 1) {
String errMsg = String.format(
"only 1 %s file is expected, but %d dubbo.properties files found on class path: %s",
fileName, set.size(), set);
logger.warn(COMMON_IO_EXCEPTION, "", "", errMsg);
}
// fall back to use method getResourceAsStream
try {
properties.load(ClassUtils.getClassLoader().getResourceAsStream(fileName));
} catch (Throwable e) {
logger.warn(
COMMON_IO_EXCEPTION,
"",
"",
"Failed to load " + fileName + " file from " + fileName + "(ignore this file): "
+ e.getMessage(),
e);
}
return properties;
}
logger.info("load " + fileName + " properties file from " + set);
for (java.net.URL url : set) {
try {
Properties p = new Properties();
InputStream input = url.openStream();
if (input != null) {
try {
p.load(input);
properties.putAll(p);
} finally {
try {
input.close();
} catch (Throwable t) {
}
}
}
} catch (Throwable e) {
logger.warn(
COMMON_IO_EXCEPTION,
"",
"",
"Fail to load " + fileName + " file from " + url + "(ignore this file): " + e.getMessage(),
e);
}
}
return properties;
}