Search in sources :

Example 6 with ClassLoaderInterface

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;
    }
}
Also used : ClassLoaderInterfaceDelegate(com.opensymphony.xwork2.util.finder.ClassLoaderInterfaceDelegate) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ClassLoaderInterface(com.opensymphony.xwork2.util.finder.ClassLoaderInterface) Properties(java.util.Properties) URL(java.net.URL)

Aggregations

ClassLoaderInterface (com.opensymphony.xwork2.util.finder.ClassLoaderInterface)4 ClassLoaderInterfaceDelegate (com.opensymphony.xwork2.util.finder.ClassLoaderInterfaceDelegate)3 URL (java.net.URL)3 ActionContext (com.opensymphony.xwork2.ActionContext)2 FileManager (com.opensymphony.xwork2.FileManager)1 FileManagerFactory (com.opensymphony.xwork2.FileManagerFactory)1 ReloadingClassLoader (com.opensymphony.xwork2.util.classloader.ReloadingClassLoader)1 UrlSet (com.opensymphony.xwork2.util.finder.UrlSet)1 DefaultFileManager (com.opensymphony.xwork2.util.fs.DefaultFileManager)1 DefaultFileManagerFactory (com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory)1 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Properties (java.util.Properties)1 Servlet (javax.servlet.Servlet)1 ServletContext (javax.servlet.ServletContext)1 JspPage (javax.servlet.jsp.JspPage)1 DiagnosticCollector (javax.tools.DiagnosticCollector)1