HK2: Hundred Kilobyte Kernel

The micro Kernel 

Resources

Glassfish V3

 HK2

 

Glassfish version 3, is based upon HK2  (Hundred Kilobyte Kernel).

HK2, as the name suggest has a micro kernel design. 

HK2 consists of:

  • Module subsystem
  • Component Model

HK2 Modules

HK2 Modules are jar files with added Manifest file information containing: key, name, version, imports and exports. The concept of module has evolved from Netbeans module design. Click here for details.

Module life-cycle consists of load (once), start and stop (multiple times) and finally garbage collected.

The HK2 Module package source can be found here.

There is a circular relationship between Module   & ModulesRegistry, this (I feel) is a design flaw, that can be rectified by placing an Module interface class between the two to break the circularity. Also, ideally ReposatoryFactory abstract class should have been an Interface.

How HK2 (Hundred Kilobyte) kernel Bootstraps? 

The Module starts up through its Main.java in bootstrap sub-package. The main starts a new Thread. within the new thread, it first figures out the jar file which contains this class. It then finds the main module name from the jar manifest. thereafter, it instantiates a new ModulesRegistry. Next, a  Repository is created from the lib sub directory and added to the Repository.  This is an excellent approach that eliminates the Tomcat flaw,which needed a script file to add all the jars into the CLASSPATH.

Finally, the launch() method of the Main class is called, which creates a startup context, creates a Habitat instance from the registry; then finds the startup component, using the name extracted from the manifest, and its run() method is called.

To summarize, the bootstrap.jar contains a name of the ModuleStratup instance (which extends Runnable)  in the manifest file. Which is instantiated and its run() method called. All jar files contained in the lib sub-directory are added to the class path by the boot-strapper.

HK2 Component Model