|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.openmdx.base.application.control.Manageable
org.openmdx.base.application.control.Application
public class Application
The application class represents an executable Java application.
The most simple application looks like:
import org.openmdx.base.application.Application;
import org.openmdx.base.application.ApplicationController_1_0;
public class SimpApp extends Application
{
public SimpApp()
{
super("SimpApp", "1.0", "Simple App", null);
}
protected void run()
throws Exception
{
System.out.println("Hello World");
}
public static void main(String args[])
{
ApplicationController_1_0 controller;
controller = new ApplicationController_1_0(args);
controller.registerApplication(new SimpApp());
controller.run();
}
}
A full featured application using
import org.openmdx.base.application.CmdLineOption;
import org.openmdx.base.application.Application;
import org.openmdx.base.application.ApplicationController_1_0;
import org.openmdx.base.application.ExceptionEvent;
import org.openmdx.base.application.ExceptionListener;
import org.openmdx.base.log.generic.AppLog;
import org.openmdx.base.exception.StackedException;
import myPackage.MyException;
public class SimpApp extends Application
implements ExceptionListener
{
public SimpApp()
{
super("SimpApp", "1.0",
"A sample to demonstrate the Application features",
createCmdLineOptions());
}
protected void init()
throws Exception
{
this.msg = getCmdLineArgs().getFirstValue("msg"));
this.print = getCmdLineArgs().hasArg("print"));
}
protected void run()
throws Exception
{
if (this.print) System.out.println(this.msg);
}
protected void release()
throws Exception
{
AppLog.trace("Done");
setExitCode(1);
}
// ExceptionEvent listener
public void exception(ExceptionEvent e)
{
// handle your own exceptions, that org.openmdx.base is not
// aware of
Throwable th = e.getException();
if (ex instanceof StackedException) {
AppLog.error("",((StackedException)ex).toString());
}else if (ex instanceof MyException) {
AppLog.error("",((MyException)ex).toString());
}else{
AppLog.error("", ex);
}
}
// CmdLineEvent listener
public void cmdLineBadArgs(CmdLineEvent e)
{
System.out.println("Command line parsing failed!");
System.out.println();
System.out.println(e.getInfo());
exit(Application.EXIT_CODE_FAILED);
}
// CmdLineEvent listener
public void cmdLineHelpRequest(CmdLineEvent e)
{
System.out.println(e.getInfo());
exit(Application.EXIT_CODE_FAILED);
}
// CmdLineEvent listener
public void cmdLineVersionRequest(CmdLineEvent e)
{
System.out.println(e.getInfo());
exit(Application.EXIT_CODE_OK);
}
// CmdLineEvent listener
public void cmdLineTrace(CmdLineEvent e)
{
System.out.println(e.getInfo());
}
private static ArrayList createCmdLineOptions()
{
ArrayList options = new ArrayList();
options.add(new CmdLineOption("msg" , "A message to be printed", 1, 1));
options.add(new CmdLineOption("print", "Enable message printing"));
return options;
}
public static void main(String args[])
{
ApplicationController_1_0 controller;
controller = new ApplicationController_1_0(args);
controller.initLogging("simpapp", "SimpApp");
controller.registerApplication(new SimpApp());
controller.run();
}
private boolean print;
private String msg;
}
| Field Summary | |
|---|---|
static int |
EXIT_CODE_FAILED
|
static int |
EXIT_CODE_OK
Predefined application exit codes |
| Constructor Summary | |
|---|---|
Application()
Creates an Application object. |
|
Application(String name,
String version,
String helpText,
List cmdLineOptions)
Creates an Application object. |
|
Application(String name,
String version,
String helpText,
List cmdLineOptions,
CmdLineFreeArgOption cmdLineFreeArgOption)
Creates an Application object. |
|
| Method Summary | |
|---|---|
void |
exit()
Terminates the currently running application. |
void |
exit(int exitCode)
Terminates the currently running application. |
static Application |
getApp()
Returns the application object |
CmdLineFreeArgOption |
getCmdLineFreeArgOptions()
Requests the command line free argument option specification |
List |
getCmdLineOptions()
Requests the command line options specification |
int |
getExitCode()
Returns the application's exit code |
String |
getHelpText()
Returns the application's version |
String |
getName()
Returns the application's name |
String |
getVersion()
Returns the application's version |
boolean |
hasFailed()
Checks if the application has failed. |
protected void |
init()
Initializes the application. |
protected static List |
mergeCmdLineOptions(List opts1,
List opts2)
Merges all command line options from opst2 into
opts1 |
protected void |
release()
The default application release. |
protected void |
run()
The default application run. |
protected void |
setExitCode(int exitCode)
Sets the applications exit code. |
| Methods inherited from class org.openmdx.base.application.control.Manageable |
|---|
getCmdLineArgs, getController, getRawArgs, setController |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final int EXIT_CODE_OK
public static final int EXIT_CODE_FAILED
| Constructor Detail |
|---|
public Application()
Application object. The application name
is the class name and the application version is set to 1.0
public Application(String name,
String version,
String helpText,
List cmdLineOptions)
Application object.
A note on help text:
Requests the application overview help text. Pass a short text that describes what the application is doing. The application name and the version are implicitely added.
Do not format the text using control characters as ('\r', '\n', ...) nor pass any command option description here. The Help text is appropriately formatted when outputted.
name - The application's nameversion - The application's versionhelpText - The application's helpcmdLineOptions - The application's command line options. A list of
CmdLineOption objects
public Application(String name,
String version,
String helpText,
List cmdLineOptions,
CmdLineFreeArgOption cmdLineFreeArgOption)
Application object.
A note on help text:
Requests the application overview help text. Pass a short text that describes what the application is doing. The application name and the version are implicitely added.
Do not format the text using control characters as ('\r', '\n', ...) nor pass any command option description here. The Help text is appropriately formatted when outputted.
name - The application's nameversion - The application's versionhelpText - The application's helpcmdLineOptions - The application's command line options. A list of
CmdLineOption objectscmdLineFreeArgOption - The application's command line free arg option| Method Detail |
|---|
public final String getName()
public final String getVersion()
public final String getHelpText()
public final List getCmdLineOptions()
getCmdLineOptions in class Manageablepublic final CmdLineFreeArgOption getCmdLineFreeArgOptions()
public final int getExitCode()
public final void exit()
public final void exit(int exitCode)
public final boolean hasFailed()
public static Application getApp()
protected void init()
throws Exception
init in class ManageableException
protected void run()
throws Exception
Exception
protected void release()
throws Exception
release in class ManageableExceptionprotected final void setExitCode(int exitCode)
protected static List mergeCmdLineOptions(List opts1,
List opts2)
opst2 into
opts1
- Parameters:
opts1 - opts2 -
- Returns:
- List The merged option list
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||