Using initializers in Aspire

Aspire during startup can load classes implementing the "IApplicationInitializer" interface. These classes will be loaded as per the factory specification. This means the loaded class can be a single instance or a multi-instance. Once these classes are loaded their "initialize method gets called".

Aspire will look for a config entry with the following name


Aspire.startup.initializers=init1,init2

Where init1 and init2 are request names that are going to be executed at startup. These requests can be defined as follows


request.init1.classname=myclass
request.init2.classname=anotherClass

Where myclass can be defined as follows


public class myclass implements IApplicationInitializer
{
   public boolean initialize(IConfig cfg, ILog log, IFactory factory)
   {
   	//do the initialization work here
   }
}

com.ai.application.defaultpkg.SystemPropertiesInitializer: An example

This initializer adds java system properties at start up to the java VM. This is useful for configuring and enabling such APIs as JAXP. Java vm uses system properties as a way to load factory classes. Here is a section of the Aspire properties file that does this using this class


#
# Identify the symbolic name of all the initializers
# Only one is identified in this example
#
Aspire.startup.initializers=SystemPropertiesInitializer

#Identify a class implementation for the identified initializer
request.SystemPropertiesInitializer.className=com.ai.application.defaultpkg.SystemPropertiesInitializer

# The nature of the SystemPropertiesInitializer dictates the rest of the config file
# The following line identifies Various properties that needs to be setup
# These property names are only symbolic at this point
# 
Aspire.systemProperties=Xerces-DBF,XSLTransformFactory,Xerces-SPF

# For each property identified specify its name and value
# as identified to the java vm
# 
# For instance the follow two pairs is equivalent to
#
# jaca -Djavax.xml.parsers.SAXParserFactory=org.apache.xerces.jaxp.SAXParserFactoryImpl
#
#Xerces-SPF
Aspire.systemProperties.Xerces-SPF.key=javax.xml.parsers.SAXParserFactory
Aspire.systemProperties.Xerces-SPF.value=org.apache.xerces.jaxp.SAXParserFactoryImpl

#Xerces-DBF
Aspire.systemProperties.Xerces-DBF.key=javax.xml.parsers.DocumentBuilderFactory
Aspire.systemProperties.Xerces-DBF.value=org.apache.xerces.jaxp.DocumentBuilderFactoryImpl

#SAXParserFactory
Aspire.systemProperties.SAXParserFactory.key=javax.xml.parsers.SAXParserFactory
Aspire.systemProperties.SAXParserFactory.value=org.apache.crimson.jaxp.SAXParserFactoryImpl

#XMLParserFactory
Aspire.systemProperties.XMLParserFactory.key=javax.xml.parsers.DocumentBuilderFactory
Aspire.systemProperties.XMLParserFactory.value=org.apache.crimson.jaxp.DocumentBuilderFactoryImpl

#XSL Transform Factory
Aspire.systemProperties.XSLTransformFactory.key=javax.xml.transform.TransformerFactory
Aspire.systemProperties.XSLTransformFactory.value=org.apache.xalan.processor.TransformerFactoryImpl

Source code of SystemPropertiesInitializer


package com.ai.application.defaultpkg;
import com.ai.application.interfaces.*;
import com.ai.application.utils.*;
import java.util.*;
/**
 * Manifests
 * Aspire.SystemProperties=prop1,prop2,prop3
 *
 * Aspire.SystemProperties.prop1.key=
 * Aspire.SystemProperties.prop1.value=
 *
 *
 */
public class SystemPropertiesInitializer implements IApplicationInitializer
{
   public static String PROPERTIES_STRING="Aspire.SystemProperties";
   
   public boolean initialize(IConfig cfg, ILog log, IFactory factory)
   {
      String properties = cfg.getValue(this.PROPERTIES_STRING,null);
      if (properties == null)
      {
         AppObjects.log("Info: No system properties identified by " + this.PROPERTIES_STRING);
         return true;
      }
      // properties available
      Vector v = com.ai.common.Tokenizer.tokenize(properties,",");
      for(Enumeration e=v.elements();e.hasMoreElements();)
      {
         String property = (String)e.nextElement();
         String key=cfg.getValue(this.PROPERTIES_STRING + "." + property + ".key","");
         String value=cfg.getValue(this.PROPERTIES_STRING + "." + property + ".value","");
         if (key.equals(""))
         {
            AppObjects.log("Warn: Key for Specified System property not found: " + property);
         }
         else
         {
            // key found, look for value
            if (value.equals(""))
            {
               AppObjects.log("Warn: Value for key " + key + " is empty");
            }
            else
            {
               // value found as well
               // set key and value in system
               AppObjects.log("Info: Placing " + key + " in System Properties as " + value);
               System.setProperty(key,value);
            }
         }//else
      }// for each property
      return true;
   }
} 

Related interfaces/classes

IApplicationInitializer
ApplicationHolder
SystemPropertiesInitializer

IApplicationInitializer


package com.ai.application.interfaces;

public interface IApplicationInitializer 
{
   public boolean initialize(IConfig cfg, ILog log, IFactory factory);
} 

Further reading

1. How to use factories to control object instances