Assume, I have a relatively large and complex application (100 MB .war
file) with multiple dependencies. Some part of the code is defining an object factory, where similar objects (all inheriting from the same base class) are instantiated.
My goal is to convert these hard coded objects to plugins that can be loaded dynamically at run-time.
So, I wrote a plugin loader:
String localPath = "...";
String pluginName = "...";
File jarFile = new File(localPath);
ClassLoader pluginLoader = URLClassLoader.newInstance(new URL[]{jarFile.toURL()});
pluginLoader.loadClass(pluginName).newInstance();
I then went on to actually write the plugins, which is when I realized that each potential plugin has a large number of dependencies and creating a jar file for each of them would result in plugins of roughly 50MB in size.
I'm worried about the memory requirements this would entail, when loading 100 of these plugins.
Is there a way I can load plugins that depend on classes inside the main app without bundling them into the plugin itself?
Or how would I best share such common resources?
[UPDATE]
For now I figured I could expose the already loaded classes to the ClassLoader
, which I was told works like this:
ClassLoader mainLoader = PluginManager.class.getClassLoader();`
ClassLoader pluginLoader = URLClassLoader.newInstance(
new URL[]{jarFile.toURL()},
mainLoader);
The downside to this is obviously that any change in the main application can make the plugins fail. This will, however, help with my memory concerns and with proper error handling it will be easy to find no longer working plugins.