Bundle Class Loader

Simple custom Java Bundle Class Loader for runtime class loading from various sources.

Bundle class loader can load classes from designed files, URLs or directly from binary content provided. All loaded resources must be JAR or CLASS files or content loaded from such files.

Bundle class loader bundles Java class native loaders together with a custom local class loader which is used to load runtime class resources. This local loader is only overridden by customised boot loader which handles the loading of classes from Java native and other configurable boot level packages.

Native class loaders are used for loading classes not added as bundle loader resource, but provided by class path from main application.

Bundle class loader can be configured to automatically create jdk proxies for objects created by it when created object class implements any interface. This enables casting loaded objects into interfaces even when the interface is loaded by different class loader than bundle class loader.

Project sources and documentation are available from GitLab.

Maven dependency:

<dependency>
     <groupId>net.relaysoft.commons</groupId>
     <artifactId>bundle-class-loader</artifactId>
     <version>1.0.0</version>
</dependency>

Example usage:

// Create new bundle class loader instance
BundleClassLoader loader = BundleClassLoaderFactory.getInstance().create("myBundleClassLoader");

// Add resource to loader
loader.addResource(new File("/path/to/file1.jar"));
// Or multiple resources at the same time
loader.addResources(Arrays.asList(new File("/path/to/file2.jar"), new File("/path/to/file3.jar")));

// Create new objects from added resources
// With default constructor
Object object1 = loader.create("path.to.MyClass");
// And with parameters
Object object2 = loader.create("path.to.MyClass", new Object[] {"test"});
Object object3 = loader.create("path.to.MyClass", new Object[] {"test", 10}, new Class[] {String.class, Integer.class});

// It is also possible to use factory methods inside the bundle from loaded resources
Object object4 = loader.create("path.to.MyFactoryClass", "methodName");
// And with parameters
Object object5 = loader.create("path.to.MyFactoryClass", "methodName", new Object[] {"test"});
Object object6 = loader.create("path.to.MyFactoryClass", "methodName", new Object[] {"test", 10}, new Class[] {String.class, Integer.class});

// If application class loader introduces interface which is implemented by the classes in loaded resources, then classes created with bundle loader can
// be cast into that interface. If automatic proxies are enabled casting can be made directly. Otherwise proxy utility can be used.
MyInterface myInt1 = (MyInterface) loader.create("path.to.MyClass");
MyInterface myInt2 = (MyInterface) ProxyUtil.toCastableProxy(loader.create("path.to.MyClass"));

Using Bundle Class Loader factory to store multiple bundle class loader instances:

BundleClassLoaderFactory factory = BundleClassLoaderFactory.getInstance();
factory.create("myBundleClassLoader");
factory.createObject("myBundleClassLoader", "newInstance");