Monday, August 11, 2014

TomEE - OpenEJB SystemInstance class

The SystemInstance  class is one of the most important class in TomEE. It acts as a centralized repository which holds all the important properties and components of the entire TomEE system. Further it fires events to its observers whenever a component is registered/unregistered.

This class follows the singleton pattern. Which means that there can be only one instance per any given classloader and its child classloaders. Further, it is the only static in the entire TomEE system.

The SystemInstance.init(Properties) method initializes the SystemInstance. That method guarantees one time initialization by  "if (initialized) return;" statement at the beginning. Next it loads properties from various sources.

Who Initializes the SystemInstance?


There are many places that calls this method. In TomEE (jars in the tomcat/lib), the TomcatLoader calls this method.

In the dropin-war deployment, this init method calls twise. One on the mini-webapp classloader by TomcatHook.hook() method and the other one on the Tomcat common class loader by TomcatLoader.

Reading Properties


The class has the following methods for read/write properties.

// Read all the properties
public Properties getProperties(){ ... }

// Read a property value by passing the property key.
public String getProperty(String key) { ... }

As a Component Repository


The SystemInstance class maintains a HashMap to hold the components of various classes. The key is the class and the actual Object is kept as the value.

Here are the two methods that put values into the above HashMap.

// Simply sets the input as key value pair.
// Publish the "ComponentAdded" or "ComponentRemved" event to Observers
public T setComponent(final Class type, final T value) 

// Look for an existing value in the HashMap
// If found
//     return it
// else
//     Create an instance of the given class using reflxion
//     Store that in the HashMap
//     Fire the "ComponentAdded" event
//     Return that instance.
public T getComponent(final Class type)

No comments:

Post a Comment