|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectdatabionics.Application
public class Application
Base class for applications. Initializes Log4J, loads *.conf and messages, parses the command line, and offers much more...
Subclasses have to adhere to the following conventions:
app
of their own class typeprotected static MyApp app;
getInstance()
returning this
variablepublic static MyApp getInstance() { return app; }
this.name = "myapp"; this.banner = "MyApp 1.0 (written by me)"; this.msgfile = "databionics.myapp/Messages"; // _en_US.properties
src/databionics/myapp/Messages_en_US.properties
must exist,
though it can be emptyMyApp.getInstance().getMessage("KEY")
define program options in constructor, first boolean flag defines whether
option has an argument, the second one whether it it required:
options.add("a", "augment", false, false, "augment data patterns with 1");
override init()
to set the variable app
and to call super.init()
to pass the command line options
public void init(String[] args) {
super.init(args);
app = this;
}
all program logic should be in the run()
method and will be executed in a Thread
if the program is run without the frontend, this is done in the main method as follows:
public static void main(String[] args) {
MyApp app = new MyApp();
app.init(args);
app.start();
}
System.exit()
should never be called because it would also exit
the frontend, since programs are started in the same JVM
call this.exit(1)
for errors and this.exit(0)
as the last command
in run()
instead
} catch (IOException e) {
log.error("error loading: " + e.getMessage());
this.exit(1);
return;
}
check for interruption by the user occasionaly (and especialy inside long loops)
and automatically exit with:
if (this.exitOnInterruption()) {
return;
}
display progress in percent as 87%
with log.info(...)
console applications:
extends Application
call this.sleep()
instead of Thread.currentThread().sleep()
if neccessary
gui applications:
extends GraphicalApplication
create and show a frame that is a descendant of BaseMainFrame
and store it in the variable frame
in GraphicalApplication
this.frame = new MainFrame(); // MainFrame extends BaseMainFrame
override the method windowClosing(WindowEvent e)
in the frame class
to close the corresponding application.
public void windowClosing(WindowEvent e) {
MyApp.getInstance().exit(0);
}
call sleepAndExit()
after showing the frame
this.frame.show();
this.sleepAndExit();
Field Summary
protected java.lang.String
banner
long program name with version
protected PropertyCommandLine
cmd
interface to command line
protected java.lang.String
conf
name of config file
protected boolean
done
flag whether app is done, is set when calling the exit
method
protected java.lang.String
env
name of envorinment variable
protected int
exitCode
exit code of application, < 0 for not set, 0 for no error
protected static java.lang.String
home
value of environment variable
protected static org.apache.log4j.Logger
log
interface to log4j system
protected java.util.ResourceBundle
messages
localized messages
protected java.lang.String
msgfile
name of properties file with messages
protected java.lang.String
name
short program name
protected java.text.NumberFormat
nf
number format
protected PropertyOptions
options
interface option definition
protected Project
project
project holding files
protected boolean
standalone
flag whether app is run in stand alone mode from own main
method
protected java.lang.Thread
thread
corresponding thread
protected java.lang.String
workdir
current working directory
Constructor Summary
Application()
Standard constructor.
Application(boolean standalone)
Constructor with flag for standalone mode.
Method Summary
void
exit(int exitCode)
Really exit if run in standalone mode, otherwise only set done flag for
frontend.
boolean
exitOnInterruption()
Exit application if current thread is interrupted
java.lang.String
getBanner()
Get long program name with version.
static java.lang.String
getColorGradient(java.lang.String name)
Get path to color gradient.
static java.lang.String
getColorPath()
Get path to color tables
static java.lang.String
getConfigPath()
Get path to configuration files
int
getExitCode()
Get exit code after app is done.
static java.lang.String
getHelpFile()
Get help file.
static java.lang.String
getHome()
Get value of environment variable
static java.lang.String
getLibPath()
Get path to *.jar files
java.lang.String
getMessage(java.lang.String key)
Get localized message from file defined in consructor.
java.lang.String
getName()
Get short program name.
Project
getProject()
Returns the current project holding filename of different types and
optionally the data from the files.
java.lang.Thread
getThread()
java.lang.String
getWorkDir()
Returns the current working directory.
void
init()
Init without command line parameters.
void
init(java.lang.String[] args)
Init Log4J logging system and parse command line.
void
interrupt()
Stop app thread
boolean
interrupted()
Check for interruption of current thread an reset interrupted flag
boolean
isDone()
Get flag whether app is done.
static void
main(java.lang.String[] args)
Main method, only for testing in this class
protected void
printHelp()
Print command line help and exit.
protected void
printVersion()
Print version information and exit
void
run()
Run method for Interface Runnable.
void
setProject(Project project)
Sets the current poject holding filename of different types and
optionally the data from the files.
void
setStandAlone(boolean standalone)
Set flag whether app is run from main
void
setWorkDir(java.lang.String path)
Sets the curent working directory.
void
sleep(long ms)
Call sleep on current thread and handle interruptions
java.lang.Thread
start()
Start app thread
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Field Detail
log
protected static org.apache.log4j.Logger log
- interface to log4j system
thread
protected java.lang.Thread thread
- corresponding thread
home
protected static java.lang.String home
- value of environment variable
messages
protected java.util.ResourceBundle messages
- localized messages
options
protected PropertyOptions options
- interface option definition
cmd
protected PropertyCommandLine cmd
- interface to command line
name
protected java.lang.String name
- short program name
banner
protected java.lang.String banner
- long program name with version
msgfile
protected java.lang.String msgfile
- name of properties file with messages
conf
protected java.lang.String conf
- name of config file
env
protected java.lang.String env
- name of envorinment variable
standalone
protected boolean standalone
- flag whether app is run in stand alone mode from own
main
method
done
protected boolean done
- flag whether app is done, is set when calling the
exit
method
exitCode
protected int exitCode
- exit code of application, < 0 for not set, 0 for no error
nf
protected java.text.NumberFormat nf
- number format
project
protected Project project
- project holding files
workdir
protected java.lang.String workdir
- current working directory
Constructor Detail
Application
public Application()
- Standard constructor. Set names for application and message file and
define command line options.
Application
public Application(boolean standalone)
- Constructor with flag for standalone mode. Set flag to false when
creating the application from within another Java program. Needed to run
programs in from within the frontend in the same JVM.
- Parameters:
standalone
- flag whether app is run in stand alone mode from own main
method
Method Detail
getThread
public java.lang.Thread getThread()
- Returns:
- Returns the thread
getHome
public static java.lang.String getHome()
- Get value of environment variable
- Returns:
- path to home directory including trailing seperator
getConfigPath
public static java.lang.String getConfigPath()
- Get path to configuration files
- Returns:
- path to config files including trailing seperator
getColorPath
public static java.lang.String getColorPath()
- Get path to color tables
- Returns:
- path to color tables files including trailing seperator
getColorGradient
public static java.lang.String getColorGradient(java.lang.String name)
- Get path to color gradient.
- Returns:
- full path to Gradient file
getLibPath
public static java.lang.String getLibPath()
- Get path to *.jar files
- Returns:
- path to *.jar files including trailing seperator
getHelpFile
public static java.lang.String getHelpFile()
- Get help file.
- Returns:
- path to the HTML help file for this application
getMessage
public java.lang.String getMessage(java.lang.String key)
- Get localized message from file defined in consructor. Subclasses of
Application should have a static method
getInstance()
that
returns the instantiated class. Messages can then retrieved via
MyApp.getInstance().getMessage("MY_MSG_KEY")
.
- Returns:
- message for this key in
...Messages_en_US.properties
getName
public java.lang.String getName()
- Get short program name. Should be the same as the corresponding batch
file or shell script in
bin
.
- Returns:
- a short name for the program.
getBanner
public java.lang.String getBanner()
- Get long program name with version. This will be displayed when the
option
-v
is given of the help is displayed.
- Returns:
- a banner with a longer name, the version and the authors
printHelp
protected void printHelp()
- Print command line help and exit.
printVersion
protected void printVersion()
- Print version information and exit
setStandAlone
public void setStandAlone(boolean standalone)
- Set flag whether app is run from main
- Parameters:
standalone
- flag whether app is run in stand alone mode from own main
method
isDone
public boolean isDone()
- Get flag whether app is done. This flag is set when calling the
exit
method.
- Returns:
- true if this application is finished.
getExitCode
public int getExitCode()
- Get exit code after app is done.
- Returns:
- exit code
getProject
public Project getProject()
- Returns the current project holding filename of different types and
optionally the data from the files.
- Returns:
- current project
setProject
public void setProject(Project project)
- Sets the current poject holding filename of different types and
optionally the data from the files.
- Parameters:
project
- project to be set
getWorkDir
public java.lang.String getWorkDir()
- Returns the current working directory.
- Returns:
- the working directory
setWorkDir
public void setWorkDir(java.lang.String path)
- Sets the curent working directory.
- Parameters:
path
- the working directory
init
public void init(java.lang.String[] args)
- Init Log4J logging system and parse command line.
init
public void init()
- Init without command line parameters.
run
public void run()
- Run method for Interface Runnable. All logic in subclasses should go
here.
- Specified by:
run
in interface java.lang.Runnable
interrupted
public boolean interrupted()
- Check for interruption of current thread an reset interrupted flag
exitOnInterruption
public boolean exitOnInterruption()
- Exit application if current thread is interrupted
sleep
public void sleep(long ms)
- Call sleep on current thread and handle interruptions
start
public java.lang.Thread start()
- Start app thread
- Returns:
- created thread
interrupt
public void interrupt()
- Stop app thread
exit
public void exit(int exitCode)
- Really exit if run in standalone mode, otherwise only set done flag for
frontend.
- Parameters:
exitCode
- exit code for System.exit()
in standalone mode.
main
public static void main(java.lang.String[] args)
- Main method, only for testing in this class
Overview
Package
Class
Use
Tree
Deprecated
Index
Help
PREV CLASS
NEXT CLASS
FRAMES
NO FRAMES
All Classes
SUMMARY: NESTED | FIELD | CONSTR | METHOD
DETAIL: FIELD | CONSTR | METHOD
Copyright © 2005-2006 Databionics Research Group. All Rights Reserved.