Clazzy

Overview

Clazzy is a basic classloading framework that was made necessary due to a limitation in the JDK's URLClassLoader class, which does not provide a hook to cleanly close underlying jar files (when accessing classes and resources in such files). Under Windows, this forbids deletion of said jar files while the corresponding VM is running - a bugging limitation if you are implementing hot-deployment.

Design

Get the following and you're off:

  • The Loader interface abstracts sources of bytes and "resources". The framework provides such loader implementation that load bytes from the file system or from jar files.
  • The BaseClassLoader provides basic classloading behavior. It supports the JDK's delegation model, and can optionally proceed otherwise if desired (delegating to child classloaders before delegating to the parent classloader).
  • The most useful class in the framework is probably the CompositeClassLoader class. An instance thereof is "composed" of multiple Loaders that are searched sequentially.

Learning by Example

The example below should prove a thorough enough introduction. For more details, see the javadoc and the test cases.

import org.sapia.clazzy.*

...

  LoaderSelector selector = new DefaultLoaderSelector();
  
  // the path below can be delimited by ':' or ';'
  String classpath = "/some/path/classes:some/path/lib/some.jar");
  
  CompositeClassLoader loader = 
    CompositeClassLoaderBuilder.parseClassPath(
                                  null, 
                                  new DefaultLoaderSelector(), 
				  classpath
				);
  Class myClass = loader.loadClass("com.foo.myclass");
  
  // do not forget the following when you're finished with
  // the classloader: it releases all underlying resources...
  
  loader.close();
..

Note that if you are using java.net.URL instances in conjunction with this framework, make sure you define the java.protocol.handler.pkgs system property, whose value should be org.sapia. See the javadoc for more details.