In order to load external JAR files that are not placed in WEB-INF/lib
folder of my web application I have written a custom loader class by extending org.apache.catalina.loader.WebappLoader
class.
What it does is on startup it scans the folder (in my case it is cataline_home\plugins\
) and adds all the classes/jars in this folder to the classpath of my web application.
This all is working fine, it loads all the available classes and I can even execute them from my application; but for some reason these classes can't access the properties files within the JAR's.
Say I have JAR called pluginsms.jar
, inside this JAR I have a properties file called sms.properties
and I am using ResourceBundle resource = ResourceBundle.getBundle("sms");
to read this file, it works perfectly fine when I run it as standalone but when I try to load it in my web application (via the custom WebappLoader) it throws: Resource Not Found Exception
.
Given below is the source for my WebappLoader:
public class PluginLoader extends WebappLoader {
@Override
public void setContainer(Container container) {
StandardContext ctx = (StandardContext) container;
try {
File pluginFolder = new File(System.getProperty("catalina.home"),
"plugins");
for (File file : pluginFolder.listFiles()) {
if (file.isDirectory()) {
continue;
}
if (file.getName().endsWith(".jar")) {
addRepository(file.toURI().toString());
}
}
} catch (Exception e) {
e.printStackTrace();
}
super.setContainer(container);
}
}