Improve logging of package io.moquette.server.config
This commit is contained in:
@@ -4,8 +4,13 @@ import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class ClasspathResourceLoader implements IResourceLoader {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ClasspathResourceLoader.class);
|
||||
|
||||
private final String defaultResource;
|
||||
private final ClassLoader classLoader;
|
||||
|
||||
@@ -29,6 +34,7 @@ public class ClasspathResourceLoader implements IResourceLoader {
|
||||
|
||||
@Override
|
||||
public Reader loadResource(String relativePath) {
|
||||
LOG.info("Loading resource. RelativePath = {}.", relativePath);
|
||||
InputStream is = this.classLoader.getResourceAsStream(relativePath);
|
||||
return is != null ? new InputStreamReader(is) : null;
|
||||
}
|
||||
|
||||
@@ -5,8 +5,13 @@ import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.Reader;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class FileResourceLoader implements IResourceLoader {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(FileResourceLoader.class);
|
||||
|
||||
private final File defaultFile;
|
||||
private final String parentPath;
|
||||
|
||||
@@ -42,12 +47,15 @@ public class FileResourceLoader implements IResourceLoader {
|
||||
}
|
||||
|
||||
public Reader loadResource(File f) {
|
||||
LOG.info("Loading file. Path = {}.", f.getAbsolutePath());
|
||||
if (f.isDirectory()) {
|
||||
LOG.error("The given file is a directory. Path = {}.", f.getAbsolutePath());
|
||||
throw new ResourceIsDirectoryException("File \"" + f + "\" is a directory!");
|
||||
}
|
||||
try {
|
||||
return new FileReader(f);
|
||||
} catch (FileNotFoundException e) {
|
||||
LOG.error("The file does not exist. Path = {}.", f.getAbsolutePath());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,19 +38,38 @@ public class ResourceLoaderConfig extends IConfig {
|
||||
|
||||
public ResourceLoaderConfig(IResourceLoader resourceLoader, String configName) {
|
||||
this.resourceLoader = resourceLoader;
|
||||
Reader configReader = configName != null ? resourceLoader.loadResource(configName) : resourceLoader.loadDefaultResource();
|
||||
if (configReader == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"Can't locate " + resourceLoader.getName() + " \"" + configName + "\"");
|
||||
|
||||
/*
|
||||
* If we use a conditional operator, the loadResource() and the
|
||||
* loadDefaultResource() methods will be always called. This makes the
|
||||
* log traces confusing.
|
||||
*/
|
||||
|
||||
Reader configReader;
|
||||
if (configName != null) {
|
||||
configReader = resourceLoader.loadResource(configName);
|
||||
} else {
|
||||
configReader = resourceLoader.loadDefaultResource();
|
||||
}
|
||||
|
||||
if (configReader == null) {
|
||||
LOG.error("The resource loader returned no configuration reader. ResourceLoader = {}, configName = {}.",
|
||||
resourceLoader.getName(), configName);
|
||||
throw new IllegalArgumentException("Can't locate " + resourceLoader.getName() + " \"" + configName + "\"");
|
||||
}
|
||||
|
||||
LOG.info("Parsing configuration properties. ResourceLoader = {}, configName = {}.", resourceLoader.getName(),
|
||||
configName);
|
||||
ConfigurationParser confParser = new ConfigurationParser();
|
||||
m_properties = confParser.getProperties();
|
||||
assignDefaults();
|
||||
try {
|
||||
confParser.parse(configReader);
|
||||
} catch (ParseException pex) {
|
||||
LOG.warn("An error occurred in parsing configuration, fallback on default configuration", pex);
|
||||
}
|
||||
try {
|
||||
confParser.parse(configReader);
|
||||
} catch (ParseException pex) {
|
||||
LOG.warn(
|
||||
"Unable to parse configuration properties. Using default configuration. ResourceLoader = {}, configName = {}, cause = {}, errorMessage = {}.",
|
||||
resourceLoader.getName(), configName, pex.getCause(), pex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user