We are expanding our Java application to support plugins. Some of them include maintaining plugins isolated from our own classes, so each plugin will live in its own class loader.
We also plan to provide plugins with the java framework for work, so it should be available for plugins. This java infrastructure also contains classes that should be available from our own java code, so it should also be available for our own java code.
The problem is that if the java infrastructure lives in the system class loader (where our own Java code lives), we cannot provide the plugins with the necessary isolation. If we decide to separate the java structure to another class loader and use it as the parent class of the plugin class loader, the java infrastructure will not be visible to our own classes.
The current solution that I had in mind is to implement a filter class loader. The java framework will work in the system class loader, but this class loader will filter everything from the system class loader except for the java framework, and I will use this class loader as the parent class loader for plugins.
Here's a rough implementation:
public class FilteringClassLoader extends ClassLoader { private URLClassLoader _internalLoader; public FilteringClassLoader(ClassLoader parent) { super(parent);
However, this has several problems, as I see it:
- Using a separate URLClassLoader just to see if a class should be filtered seems to be hacked.
- When the plugin loads the class, the loader of the parent class of this class will be the loader of the system class, which obviously defeats the goals that I'm trying to achieve.
How do you solve this problem?
java classloader
Idan k
source share