wt.util
Class AppStarter

java.lang.Object
  extended bywt.util.AppStarter

public class AppStarter
extends Object

Dynamically instantiates a specified implementation of ExecutableApp and runs it's execute method.The AppStarter, creates a log in the directory specified by the property wt.temp into which all output to System.out and System.err is redirected. The AppStarter also ensures that wt.properties and tools.properties are loaded before instantiating the ExecutableApp.

If a classpath other than the one specified in the environment is desired, the caller be provide the name of a wt.properties or tools.properties property named in the option -classpathProperty. The property value should contain the paths of all elements to be included in the classpath

Examples

An example invocation from Java code looks like:
    String class_name = "com.acme.Foo";
  
    Hashtable options = new Hashtable(5);
    options.put(AppStarter.CLASSPATH_PROPERTY_OPT, "com.acme.foo.classpath");
  
    String[] args = new String[]{ "bar", "baz", "baha" };
  
    new AppStarter(class_name, options, args).startApp();
Invocation from the command line is expected to look like:
    java wt.util.AppStarter [-tee] [-logPrefix prefix] [-logBase baseName] [-append] [-classpathProperty prop-name] class-to-start [args-to-pass-to-app]
 e.g.
    java wt.util.AppStarter -append -classpathProperty com.acme.foo.classpath com.acme.Foo bar baz baha
Process Termination
When executing via main(String[]) it is assumed that the started ExecutableApp will exit the process by calling System.exit(int) or by exiting all non-daemon Threads when it is done.

See Also:
System.exit(int), Thread.setDaemon(boolean), ExecutableApp

Field Summary
private static String APPEND_TO_LOG_OPT
           
private  String[] argsToPass
           
private  String className
           
private static String CLASSPATH_PROPERTY_OPT
          Name of option for providing an alternative classpath specified in the wt.properties or tools.properties files.
private static boolean failOnExit
           
private static String HELP_OPT
           
private static String HIDE_CLASSPATH_OPT
           
private  boolean isLogFileSetup
           
private  boolean isOutputVisibleOnScreen
           
private static String LOG_BASE_OPT
           
private static String LOG_PREFIX_OPT
           
private  String logFileBase
           
private  String logFileName
           
private  String logFilePrefix
           
private  Hashtable options
           
private static String TEE_LOG_OUTPUT_OPT
           
private static String USAGE_MESSAGE
           
 
Constructor Summary
AppStarter(String class_name, Hashtable options, String[] args_to_pass)
          Create an instance that will load and execute an instance of the class specified by class_name.
 
Method Summary
private static String asOptString(String opt_name)
          Returns a string that is '-' followed by the opt name.
private  void buildLogFileName()
          Derive the location of the log file from the property wt.logs.dir and the name of the ExecutableApp class.
private static void displayUsage()
          Issue the usage message to the screen.
static void failOnExit()
          Will trigger System.exit(1) upon application completion.
static void failOnExit(String message)
          Will trigger System.exit(1) upon application completion.
private  ClassLoader getClassLoader(String classpath_prop_name)
          Returns the appropriate class loader for instantiating the ExecutableApp.
private static boolean isOptString(String value, String opt_name)
          Determines if the string value is a command-line options specifier for the supplied option name.
private static boolean looksLikeOptString(String value)
          True if the string value starts with '-'.
static void main(String[] args)
          Main entry point for the AppStarter.
private  void printClasspath(ClassLoader class_loader)
          Prints the classpath being used by the class_loader, to System.out.
private  void printErrorMessage(Throwable t)
          Perform the ugly logic to give the most useful error message...
private static void processOptions(String[] args, Hashtable options, Vector args_to_pass)
          Pick options out of args array.
private  String[] rebuildArgsToPass()
          Create a new String[] containing the command line arguments needed to relaunch the AppStarter in a new classloader.
private  void setupLog()
          Creates a log for the process using the name of the class to launch.
private  void setupProperties()
          Initializes the WTContext and WTProperties including properties in tools.properties.
static void setupProperties(String[] args)
          Initializes the WTContext and WTProperties including properties in tools.properties.
protected  void startApp()
          Loads properties then dynamically load and start the executable app.
private  void startAppWithCurrentClassLoader()
          Dynamically instantiates the class from its name and whatever classloader we were loaded with, ensures that it implements ExecutableApp, casts it and calls its execute() method passing through all necessary arguments.
private  void startAppWithCustomClassLoader(String classpath_prop_name)
          Relaunches AppStarter with a new classloader passing all arguments except those for specifying the classpathProperty.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CLASSPATH_PROPERTY_OPT

private static final String CLASSPATH_PROPERTY_OPT
Name of option for providing an alternative classpath specified in the wt.properties or tools.properties files.

See Also:
Constant Field Values

APPEND_TO_LOG_OPT

private static final String APPEND_TO_LOG_OPT
See Also:
Constant Field Values

LOG_PREFIX_OPT

private static final String LOG_PREFIX_OPT
See Also:
Constant Field Values

LOG_BASE_OPT

private static final String LOG_BASE_OPT
See Also:
Constant Field Values

TEE_LOG_OUTPUT_OPT

private static final String TEE_LOG_OUTPUT_OPT
See Also:
Constant Field Values

HIDE_CLASSPATH_OPT

private static final String HIDE_CLASSPATH_OPT
See Also:
Constant Field Values

HELP_OPT

private static final String HELP_OPT
See Also:
Constant Field Values

USAGE_MESSAGE

private static final String USAGE_MESSAGE

options

private Hashtable options

className

private String className

argsToPass

private String[] argsToPass

isOutputVisibleOnScreen

private boolean isOutputVisibleOnScreen

isLogFileSetup

private boolean isLogFileSetup

logFilePrefix

private String logFilePrefix

logFileBase

private String logFileBase

logFileName

private String logFileName

failOnExit

private static boolean failOnExit
Constructor Detail

AppStarter

public AppStarter(String class_name,
                  Hashtable options,
                  String[] args_to_pass)
Create an instance that will load and execute an instance of the class specified by class_name. The options will be used to configure the classpath, and the args_to_pass will be passed to the ExecutableApp when started.

Method Detail

startApp

protected void startApp()
                 throws Exception
Loads properties then dynamically load and start the executable app.

If the option -classpathProperty is specified, the property will be looked up, a class loader will be created from the paths specified in the property value, and the AppStarter will be reinvoked sans the -classpathProperty option using the new classloader rather than the primordial classloader.

Throws:
Exception - if the class cannot found, loaded, cast to an ExecutableApp, or if it throws an unhandled exception when executed

setupProperties

private void setupProperties()
                      throws Exception
Initializes the WTContext and WTProperties including properties in tools.properties.

Throws:
Exception

setupLog

private void setupLog()
               throws Exception
Creates a log for the process using the name of the class to launch. The log will be in the wt.logs.dir.All output to STDOUT and STDERR is tee'd to the log and the log is overwritten for each start of the application.

Throws:
Exception

buildLogFileName

private void buildLogFileName()
Derive the location of the log file from the property wt.logs.dir and the name of the ExecutableApp class. Assigns the value to logFileName.


startAppWithCustomClassLoader

private void startAppWithCustomClassLoader(String classpath_prop_name)
                                    throws IOException,
                                           ClassNotFoundException,
                                           NoSuchMethodException,
                                           IllegalAccessException,
                                           InvocationTargetException
Relaunches AppStarter with a new classloader passing all arguments except those for specifying the classpathProperty.

Throws:
Exception - if the class cannot be found, loaded, cast to an ExecutableApp, or if it throws an unhandled exception
IOException
ClassNotFoundException
NoSuchMethodException
IllegalAccessException
InvocationTargetException

startAppWithCurrentClassLoader

private void startAppWithCurrentClassLoader()
                                     throws ClassNotFoundException,
                                            IllegalAccessException,
                                            InstantiationException,
                                            WTException
Dynamically instantiates the class from its name and whatever classloader we were loaded with, ensures that it implements ExecutableApp, casts it and calls its execute() method passing through all necessary arguments.

Throws:
ClassNotFoundException
IllegalAccessException
InstantiationException
WTException

getClassLoader

private ClassLoader getClassLoader(String classpath_prop_name)
                            throws IOException,
                                   MalformedURLException
Returns the appropriate class loader for instantiating the ExecutableApp. If the -classPathProperty is not specified then the classloader that loaded this class(AppStarter.class)is used; if AppStarter is launched via the java command on the command line, then this will be the primordial classloader.Otherwise, the option value is treated as the name of a property in the wt.properties or tools.properties files that contains an alternative classpath.If the property value is found, then an URLClassLoader with each of the classpath entries in the property value will be returned.

Returns:
a non-null ClassLoader
Throws:
IOException
MalformedURLException

rebuildArgsToPass

private String[] rebuildArgsToPass()
Create a new String[] containing the command line arguments needed to relaunch the AppStarter in a new classloader. This means include all arguments except the classpath property option.

Returns:
a non-null String[]

printClasspath

private void printClasspath(ClassLoader class_loader)
Prints the classpath being used by the class_loader, to System.out.


printErrorMessage

private void printErrorMessage(Throwable t)
Perform the ugly logic to give the most useful error message...


main

public static void main(String[] args)
Main entry point for the AppStarter. See class documentation for usage.

See Also:
AppStarter

processOptions

private static void processOptions(String[] args,
                                   Hashtable options,
                                   Vector args_to_pass)
Pick options out of args array. Options are expected options up to but not including the first non-option which is presumed to be the class name of the Executable app.

Throws:
WTRuntimeException - if help was asked for or if an unexpected option was encountered before the class name

isOptString

private static boolean isOptString(String value,
                                   String opt_name)
Determines if the string value is a command-line options specifier for the supplied option name.

Returns:
true if value is opt_name preceeded by one or two dashes

looksLikeOptString

private static boolean looksLikeOptString(String value)
True if the string value starts with '-'.


asOptString

private static String asOptString(String opt_name)
Returns a string that is '-' followed by the opt name. If the opt_name already starts with -, it is return unchanged.


displayUsage

private static void displayUsage()
Issue the usage message to the screen.


setupProperties

public static void setupProperties(String[] args)
                            throws Exception
Initializes the WTContext and WTProperties including properties in tools.properties.

Throws:
Exception

failOnExit

public static void failOnExit()
Will trigger System.exit(1) upon application completion.


failOnExit

public static void failOnExit(String message)
Will trigger System.exit(1) upon application completion. Will immediately print the message to the standard output stream.