com.isti.util
Class ProgramSingleton

java.lang.Object
  extended by com.isti.util.ProgramSingleton

public class ProgramSingleton
extends java.lang.Object

Class ProgramSingleton manages a "program singleton" file used to determine if another instance of the given program is already running. The name of the "program singleton" file will be the program name given to the constructor plus the extension ".psf". While the file-manager thread created by this class is running, the file will be written to once every 30 seconds. The standard usage of this class is to call the 'initialize()' method in the main method of the program (aborting if the method returns false) and call the 'close()' method from the shutdown-hook for the program (setup via 'Runtime.getRuntime().addShutdownHook()').


Nested Class Summary
protected  class ProgramSingleton.FileManagerThread
          Class FileManagerThread defines the "program singleton" file managing thread.
 
Field Summary
protected  ProgramSingleton.FileManagerThread fileManagerThreadObj
           
static java.lang.String SINGLETON_FILE_EXT
          The singleton file extension.
protected  java.io.RandomAccessFile singletonFileAccessObj
           
protected  java.io.File singletonFileObj
           
protected  int UPDATE_INTERVAL_MS
           
 
Constructor Summary
ProgramSingleton(java.io.File singletonFileObj)
          Creates a "program singleton" file manager.
ProgramSingleton(java.io.File parent, java.lang.String programNameStr)
          Creates a "program singleton" file manager.
ProgramSingleton(java.lang.String programNameStr)
          Creates a "program singleton" file manager.
ProgramSingleton(java.lang.String parent, java.lang.String programNameStr)
          Creates a "program singleton" file manager.
 
Method Summary
 void close()
          Terminates the "program singleton" file manager thread and closes and deletes the "program singleton" file.
 boolean initialize()
          Checks to see if another instance of the program is running and, if not, initializes the "program singleton" file manager thread.
 boolean isRunning()
          Determines if another instance of the program is running or not.
 boolean waitForClose(int millis)
          Wait for the program close.
 boolean waitForClose(int millis, int retry)
          Wait for the program close.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SINGLETON_FILE_EXT

public static final java.lang.String SINGLETON_FILE_EXT
The singleton file extension.

See Also:
Constant Field Values

singletonFileObj

protected final java.io.File singletonFileObj

UPDATE_INTERVAL_MS

protected final int UPDATE_INTERVAL_MS
See Also:
Constant Field Values

fileManagerThreadObj

protected ProgramSingleton.FileManagerThread fileManagerThreadObj

singletonFileAccessObj

protected java.io.RandomAccessFile singletonFileAccessObj
Constructor Detail

ProgramSingleton

public ProgramSingleton(java.lang.String programNameStr)
Creates a "program singleton" file manager.

Parameters:
programNameStr - the program name to be used.

ProgramSingleton

public ProgramSingleton(java.lang.String parent,
                        java.lang.String programNameStr)
Creates a "program singleton" file manager.

Parameters:
parent - the parent pathname string.
programNameStr - the program name to be used.

ProgramSingleton

public ProgramSingleton(java.io.File parent,
                        java.lang.String programNameStr)
Creates a "program singleton" file manager.

Parameters:
parent - the parent file.
programNameStr - the program name to be used.

ProgramSingleton

public ProgramSingleton(java.io.File singletonFileObj)
Creates a "program singleton" file manager.

Parameters:
singletonFileObj - the singleton file object.
Method Detail

isRunning

public boolean isRunning()
Determines if another instance of the program is running or not.

Returns:
true if another instance is running, false otherwise.

initialize

public boolean initialize()
Checks to see if another instance of the program is running and, if not, initializes the "program singleton" file manager thread.

Returns:
true if successful, false if another instance of the program is already running.

close

public void close()
Terminates the "program singleton" file manager thread and closes and deletes the "program singleton" file.


waitForClose

public boolean waitForClose(int millis)
Wait for the program close.

Parameters:
millis - Waits at most millis milliseconds for this program to close. A timeout of 0 means to wait forever.
Returns:
true if the program closed, false otherwise.

waitForClose

public boolean waitForClose(int millis,
                            int retry)
Wait for the program close.

Parameters:
millis - Waits at most millis milliseconds for this program to close. A timeout of 0 means to wait forever.
retry - the number of millis to wait between retries.
Returns:
true if the program closed, false otherwise.