use of com.opensymphony.xwork2.util.finder.ClassLoaderInterface in project struts by apache.
the class BaseOsgiHost method findPropertiesFileInRelativePath.
/**
* Attempt to read a properties file from the relative path of the current classloader.
* Intended as an alternate configuration fallback for special scenarios where the default lookup
* is not functional (such as for unit tests).
*
* @param fileName the filename (relative path) of the properties file.
* @return a Properties bundle loaded from the provided fileName.
* @throws IOException if the properties file does not exist or cannot be loaded.
*/
protected Properties findPropertiesFileInRelativePath(String fileName) throws IOException {
if (fileName == null || fileName.toLowerCase().endsWith(".class") || fileName.toLowerCase().endsWith(".jar")) {
throw new IllegalArgumentException("Provided file name cannot be null, nor should it be a class or jar file");
}
final ClassLoaderInterface classLoaderInterface = new ClassLoaderInterfaceDelegate(Thread.currentThread().getContextClassLoader());
final URL fileUrl = classLoaderInterface.getResource(fileName);
try (InputStream reader = new BufferedInputStream(fileUrl.openStream())) {
Properties properties = new Properties();
properties.load(reader);
return properties;
}
}
Aggregations