com.marringtons.util
Class Application

java.lang.Object
  extended bycom.marringtons.util.Application

public class Application
extends Object

A class of static data and methods designed to be used application wide - including properties, operating system, configurationd details and thread control. A start() routine must be called when an application starts. After this initialisation, properties, including application name and command line are available. These properties come from system.properties.txt on the classpath and default Java properties. Independant of the application, the home, user and temp directories are made available.

 
  
   	public static void main(String[] args)
      {
   	    Application.start( "Adept", args);
   	    ...
      }
   
  
 

A list of paths are available and used to find files like the properties files. This comes from the Java classpath, the command line or existing properties files as property data.path . The property file.base is added to each file in the path when searching. In this way a file can be in com/marringtons/MyProject/test.txt and MyProject/test.txt will file the same file if the file base is com/marringtons. This path includes directories and zip files. As more files are added, the previous paths are searched, so zip files on the existing path can be found and processed.

The directory to write files to should be retrieved with getPrimaryPath().

Any class that retains resources other than memory has a finalize() method that makes sure the resource was freed before the memory is to be reclaimed. If this requirement fails they call garbageCollectionError() that sets garbageCollectionErrors for the unit tests to check.

 public void finalize()
 	{
 		if (fileHandle != null)
 			{
 				fileHandle.close();
 				Application
 						.garbageCollectionError("An active myClass was garbaged collected");
 			}
 	}
 

Lastly there is thread management support including snooze and sleep.

 int tenSeconds = 10000;
 if (!snooze(tenSeconds)) Application.log("10 second sleep interrupted");
 sleep(tenSeconds); // will sleep 10 seconds not matter what
 
 
 

Author:
Paul Marrington

Field Summary
static String[] commandLine
          String array passed into the program with command line arguments.
static Properties configuration
          Application configuration - properties change by the user to reflect customisation of the application.
static String defaultDatabase
          Many applications keep all persistent data in a single database.
static boolean garbageCollectionErrors
          Set true by any routine that implements finalize() and finds that garbage collection has occurred on an object that still holds resources.
static boolean isWindows
          return true if running on a MS Windows platform.
static boolean isWindows16
          return true if running on a MS Windows 16-bit based (95, 98 or ME) platform.
static String name
          Name of the currently running application.
static String osName
          Name of the Operating System (Windows, Linux, etc).
static Properties properties
          Application and system properties - typically created when the application was installed - or retrieved from the system at run-time.
 
Method Summary
static void changeConfiguration(String key, String value)
          Application functionality can be modified with properties loaded from properties files.
static void garbageCollectionError(String message)
          Any class that retains resources other than memory has a finalize() method that makes sure the resource was freed before the memory is to be reclaimed.
static boolean sleep(int milliseconds)
          Let this thread sleep (lightly) for the required time - but drop out if interrupted.
static void sleepSoundly(int milliseconds)
          Let this thread sleep for the required time - independant of interrupts.
static void start(Object object)
          Units tests don't have a command line to enter - and their idea of a current directory is coloured by the IDE that is running it.
static void start(String applicationName, String[] arguments)
          Called by every application when it starts with the name of the application and the command line arguments.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

name

public static String name
Name of the currently running application.


commandLine

public static String[] commandLine
String array passed into the program with command line arguments.


properties

public static Properties properties
Application and system properties - typically created when the application was installed - or retrieved from the system at run-time.


configuration

public static Properties configuration
Application configuration - properties change by the user to reflect customisation of the application. Saved when updated.


defaultDatabase

public static String defaultDatabase
Many applications keep all persistent data in a single database.


osName

public static final String osName
Name of the Operating System (Windows, Linux, etc).


isWindows

public static final boolean isWindows
return true if running on a MS Windows platform.


isWindows16

public static final boolean isWindows16
return true if running on a MS Windows 16-bit based (95, 98 or ME) platform.


garbageCollectionErrors

public static boolean garbageCollectionErrors
Set true by any routine that implements finalize() and finds that garbage collection has occurred on an object that still holds resources. The matching unit test should call System.gc() and check this boolean.

Method Detail

start

public static void start(String applicationName,
                         String[] arguments)
Called by every application when it starts with the name of the application and the command line arguments. The name can be a simple name or it can be an absolute or relative path. ~/mdir will use a directory off your home directory for output. Firstly the output directory is set. Then any archives in directories named after the application and under the user directory or the current working directory are processed. These archives are added to the data path and their properties and path bases set from their META-INF directories. Next, directories with the same name as supplied to the application under the current working directory and the user home directory are added if they exist. As before the META-INF is processed. When the application was installed a system.properties.txt file was created on the data path. It is now processed. Next properties on the command line are processed. Command line properties are either name or name=value as space separated entries. The final property load is from user.configuration.properties.txt in the output directory. It contains configuration information that can be dynamically updated during program operation - under user control. The last part of preparations is to process properties for global significance. Data paths are updated, the zip cache site is fixed and the log file redirected.

Parameters:
applicationName - can be a combination of output path and name (i.e. MyDir/MyAppName)
arguments - on command line.
See Also:
Output.set(String)

start

public static void start(Object object)
Units tests don't have a command line to enter - and their idea of a current directory is coloured by the IDE that is running it. To that end this start places output files/directories under the home directory in a folder called Marringtons with a name matching the test class name.

Parameters:
object - normally this for the test class

changeConfiguration

public static void changeConfiguration(String key,
                                       String value)
                                throws FileNotFoundException
Application functionality can be modified with properties loaded from properties files. These properties can be changed also on-the-fly by user interaction with the program. Calling this method will add to the current properties and also save it in a user configuration file for later runs. This configuration takes precedence over install level properties files. It is saved in a file called user.configuration.properties.txt in the output directory.

Parameters:
key - of the configuration item to add or change.
value - to change the configuration item to.
Throws:
FileNotFoundException - if the file cannot be created (i.e. is a directory)

garbageCollectionError

public static void garbageCollectionError(String message)
Any class that retains resources other than memory has a finalize() method that makes sure the resource was freed before the memory is to be reclaimed. If this requirement fails they call this method. The message and a stack dump are written to the log and a flag is set that can be checked by unit tests.

Parameters:
message - to record in the log.

sleep

public static boolean sleep(int milliseconds)
Let this thread sleep (lightly) for the required time - but drop out if interrupted.

Parameters:
milliseconds - to sleep for.
Returns:
boolean true if time expired, false if interrupted

sleepSoundly

public static void sleepSoundly(int milliseconds)
Let this thread sleep for the required time - independant of interrupts.

Parameters:
milliseconds - to sleep for.


Copyright © 2005 Paul Marrington http://library.marringtons.com