Filtration Class Loader Implementation

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); // load our java framework to this class loader _internalLoader = new URLClassLoader(...) } public Class<?> loadClass(String name) throws ClassNotFoundException { // first, try to load from our internal class loader // that only sees the java framework if that works, load the class // from the system class loader and return that. otherwise, the class // should be filtered out and the call to loadClass will throw as expected _internalLoader.loadClass(name); Class<?> retClazz = super.loadClass(name); return retClazz; } } 

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?

+7
java classloader
source share
2 answers

How do you solve this problem?

The OSGi Alliance has already done. The Wikipedia article on the OSGi framework can give you some ideas.

You might want to look at the source code for Eclipse and see how they implemented the plug-in download.

+2
source share

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.

Put your code in the class loader, which is the peer node of the plugin class loader, both with the interface code class loader and with the parent.

+2
source share

All Articles