Refactored config file resource loading to make it easier to load config files from class path when embedding.

This commit is contained in:
Neeme Praks
2016-12-31 11:46:32 +01:00
committed by andsel
parent c36ebc648b
commit ac93f04dde
12 changed files with 373 additions and 167 deletions
@@ -0,0 +1,41 @@
package io.moquette.server.config;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public class ClasspathResourceLoader implements IResourceLoader {
private final String defaultResource;
private final ClassLoader classLoader;
public ClasspathResourceLoader() {
this(IConfig.DEFAULT_CONFIG);
}
public ClasspathResourceLoader(String defaultResource) {
this(defaultResource, Thread.currentThread().getContextClassLoader());
}
public ClasspathResourceLoader(String defaultResource, ClassLoader classLoader) {
this.defaultResource = defaultResource;
this.classLoader = classLoader;
}
@Override
public Reader loadDefaultResource() {
return loadResource(defaultResource);
}
@Override
public Reader loadResource(String relativePath) {
InputStream is = this.classLoader.getResourceAsStream(relativePath);
return is != null ? new InputStreamReader(is) : null;
}
@Override
public String getName() {
return "classpath resource";
}
}