org.eodisp.util
Class RootApp

java.lang.Object
  extended by org.eodisp.util.RootApp

public class RootApp
extends java.lang.Object

The root application is holds registered application modules and configurations.

As of now there can only be one RootApp instance in one JVM. This instance is registered with the AppRegistry at the time an instance of this class is created. An assertion exception will be thrown if you try to create two instances of this class.

This class is not thread safe. It is assumed that the registerXXX methods are not called concurrently from different Threads.

Most of the functionality of a Java application should be implemented in separate application modules. Only very few core functionality is directly implemented in this class:

Version:
$Id: RootApp.java 4154 2006-10-26 13:33:47Z ibirrer $
Author:
ibirrer
See Also:
AppModule, Configuration

Nested Class Summary
static class RootApp.RunState
           
 
Field Summary
static java.lang.String WORKING_DIR_SYSTEM_PROPERTY
           
 
Constructor Summary
RootApp(java.lang.String name, java.lang.String description, java.io.File defaultWorkingDir, java.lang.Class mainClass)
          Creates a new root application and registers it with the AppManager.
 
Method Summary
 void execute(java.lang.String[] commandLineArgs)
          Executes the root application.
 void executeAdditionalAppModules()
           
 AppModule getAppModule(java.lang.String appModuleId)
           
 Configuration getConfiguration(java.lang.String configurationId)
          Returns the configuration identified by the configuration id parameter.
 java.io.File getConfigurationDir()
          Returns the standard configuration path for this root application
 java.io.File getDataDir()
          Returns the standard data path for this root application
protected  java.io.File getDefaultWorkingDir()
          The default working directory of this root application.
 java.lang.String getDescription()
           
 java.lang.String getName()
           
 RootApp.RunState getRunState()
          Returns the state of this application.
 java.io.File getWorkingDir()
          The working directory.
protected  void initJsap()
          Creates the JSAP instance and adds the following options: --help --working-dir --delete-lock
protected  void initLogger(java.lang.String[] args)
          Initializes the log4j framework
protected  void initWorkingDirs(java.lang.String[] args)
          Determines the working directory of this root application.
protected  void loadConfigurationsFromCommandLine()
          Processes the command line arguments.
protected  void loadConfigurationsFromFile()
           
 void registerAppModule(AppModule appModule)
          Registers and application module with the root application.
 Configuration registerConfiguration(Configuration configuration)
          Registers a Configuration.
 Configuration registerConfiguration(Configuration configuration, CommandlineMapper commandlineMapper)
          Registers a Configuration with a associated command line mapper.
 Configuration registerConfiguration(Configuration configuration, CommandlineMapper commandlineMapper, SystemPropertyMapper systemPropertyMapper)
          Registers a Configuration with a associated command line mapper and system property mapper.
 void shutdown()
          Shuts down the root the root application.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

WORKING_DIR_SYSTEM_PROPERTY

public static final java.lang.String WORKING_DIR_SYSTEM_PROPERTY
See Also:
Constant Field Values
Constructor Detail

RootApp

public RootApp(java.lang.String name,
               java.lang.String description,
               java.io.File defaultWorkingDir,
               java.lang.Class mainClass)
Creates a new root application and registers it with the AppManager.

Parameters:
defaultWorkingDir - The default working directory of the root application. The given file path must be absolute.
name - The name of the application. Used for customizing some of the help texts. Can be accessed by the application modules and may be shown to the user.
mainClass - The class that contains the main method from which the JVM was originally started from. Used for command line usage help output.
Method Detail

execute

public void execute(java.lang.String[] commandLineArgs)
Executes the root application.

Parameters:
commandLineArgs - The arguments given on the command line. Normally this should be the args variable from main( String[] args ).

executeAdditionalAppModules

public void executeAdditionalAppModules()

shutdown

public void shutdown()
Shuts down the root the root application.


getWorkingDir

public java.io.File getWorkingDir()
The working directory. It is initially not set (returns null) and only when invoking execute(String[]) it is set. It is unset (null) after shutting down the root application.

Returns:
the working directory

getConfigurationDir

public java.io.File getConfigurationDir()
Returns the standard configuration path for this root application

Returns:
the standard configuration path for this root application

getDataDir

public java.io.File getDataDir()
Returns the standard data path for this root application

Returns:
the standard data path for this root application

getDefaultWorkingDir

protected java.io.File getDefaultWorkingDir()
The default working directory of this root application. This is not necessarily the same as the working directory. The working directory can be set on the command line or through the Java system properties.

Returns:
the default working directory (always absolute)

registerAppModule

public void registerAppModule(AppModule appModule)
Registers and application module with the root application. The startup sequence of the application modules is specified by the sequence of calls to this method. The application module that is registered first is also the first that is being started. This method must only be called before the #execute() method is invoked. AppModule#initConfiguration()


getAppModule

public AppModule getAppModule(java.lang.String appModuleId)

registerConfiguration

public Configuration registerConfiguration(Configuration configuration)
Registers a Configuration. No mapping of configuration entries to command line parameters is done.

This method must only be called during the configuration initialization phase, i.e. in implementation of the method AppModule#initConfiguration()

Parameters:
configuration - The configuration to be registered. Each configuration registered must have a distinct id
Returns:
the newly registered configuration
Throws:
java.lang.IllegalArgumentException - if a configuration with the same name is already registered.
java.lang.NullPointerException - if given configuration is null

registerConfiguration

public Configuration registerConfiguration(Configuration configuration,
                                           CommandlineMapper commandlineMapper)
Registers a Configuration with a associated command line mapper. The command line mapper is used to generate a command line parser that parses command line options given by a user. This method must only be called during the configuration initialization phase, i.e. in implementation of the method AppModule.registerConfiguration(RootApp)

Parameters:
configuration - The configuration to be registered. Each configuration registered must have a distinct id
commandlineMapper -
Returns:
the newly registered configuration
Throws:
java.lang.IllegalArgumentException - if a configuration with the same name is already registered.
java.lang.NullPointerException - if any of the arguments is null. Use CommandlineMapper.NULL_COMMAND_LINE_MAPPER instead of null.

registerConfiguration

public Configuration registerConfiguration(Configuration configuration,
                                           CommandlineMapper commandlineMapper,
                                           SystemPropertyMapper systemPropertyMapper)
Registers a Configuration with a associated command line mapper and system property mapper. The command line mapper is used to generate a command line parser that parses command line options given by a user. If a mapping to a system property exists, the configuration is updated at startup by reading the system property.

The order in which configuration are read is as follows

  1. Configuration File
  2. System Property
  3. Command line arguments
This method must only be called during the configuration initialization phase, i.e. in implementation of the method AppModule#initConfiguration()

Parameters:
configuration - The configuration to be registered. Each configuration registered must have a distinct id
commandlineMapper -
systemPropertyMapper -
Returns:
the newly registered configuration
Throws:
java.lang.IllegalArgumentException - if a configuration with the same name is already registered.
java.lang.NullPointerException - if any of the arguments is null. Use CommandlineMapper.NULL_COMMAND_LINE_MAPPER and SystemPropertyMapper.NULL_SYSTEM_PROPERTY_MAPPER instead of null.

getConfiguration

public Configuration getConfiguration(java.lang.String configurationId)
Returns the configuration identified by the configuration id parameter.

Parameters:
configurationId - The id of the configuration to be retrieved.
Returns:
the configuration identified by the given configuration id parameter or null if no configuration with the given name exists.

getRunState

public RootApp.RunState getRunState()
Returns the state of this application.

Returns:
the state of this application.

initJsap

protected void initJsap()
Creates the JSAP instance and adds the following options:


initWorkingDirs

protected void initWorkingDirs(java.lang.String[] args)
                        throws java.io.IOException
Determines the working directory of this root application. Precedence is as follows:
  1. The path that was given to the command line option --working-dir
  2. The value that is set for the org.eodisp.working-dir system property
  3. Default value given to the constructor of the root application

Parameters:
args -
Throws:
java.io.IOException

loadConfigurationsFromFile

protected void loadConfigurationsFromFile()

initLogger

protected void initLogger(java.lang.String[] args)
Initializes the log4j framework

Parameters:
args - Command line parameters from main method.

loadConfigurationsFromCommandLine

protected void loadConfigurationsFromCommandLine()
Processes the command line arguments. Exits and prints error message if wrong parameters were given.

Parameters:
args - Command line parameters from main method.

getName

public java.lang.String getName()

getDescription

public java.lang.String getDescription()