abstract class ClientApplication extends Application
This base class provides its own run
method. Particular
applications are implemented by overriding the coreLogic
method of this class. The run
method in this class wraps the
main application logic in a try...catch
statement. The
application logic need not concern itself with error codes or printing error
messages. If the coreLogic
method throws an exception, an error
message is printed to standard error, and the exit code is non-zero.
This class also provides an exception aggregation mechanism. If an
application repeats certain work on each of several arguments, instead of
the first exception terminating the whole program, it is possible to
accumulate exceptions from each argument, and display all of them only after
all arguments have been processed. This is done using the
report
methods. An application which wishes to process multiple
arguments in this way should call report
with no arguments
after each argument that is processed successfully, and report
with the exception as an argument after each argument that causes a failure
during processing.
Calling fatal
during processing causes the application to quit
at the next call to report
. Fatal errors are those in which
resources which must be released to continue running the application safely
cannot be released.
Modifier and Type | Field and Description |
---|---|
private java.util.LinkedList<ApplicationFailure> |
failures
Aggregated list of application error messages.
|
private java.lang.String |
fatal_error_message
Fatal error message, if a fatal error has occurred.
|
EXIT_FAILURE, EXIT_SUCCESS
Constructor and Description |
---|
ClientApplication() |
Modifier and Type | Method and Description |
---|---|
protected abstract void |
coreLogic(java.lang.String[] arguments)
Called to execute the main body of the application.
|
protected void |
fatal(java.lang.String message)
Indicates that a fatal error has occurred.
|
private void |
printErrorsAndExit()
Prints all errors and terminates the process.
|
protected void |
report()
Reports that an argument has been processed successfully, for an
application that processes multiple arguments.
|
protected void |
report(ApplicationFailure failure)
Reports that an argument has been processed, but the processing has
caused a failure.
|
void |
run(java.lang.String[] arguments)
Wraps
coreLogic in an exception handler. |
private java.lang.String fatal_error_message
private java.util.LinkedList<ApplicationFailure> failures
protected abstract void coreLogic(java.lang.String[] arguments) throws ApplicationFailure
arguments
- Command line arguments passed to the application.ApplicationFailure
- When an error occurs. If this exception is
thrown, its message string is printed to the
standard error stream, and the application's
exit status is non-zero.java.lang.Throwable
- If the application failed to handle an error by
responding to it, or by translating it to
ApplicationFailure
.public void run(java.lang.String[] arguments)
coreLogic
in an exception handler.run
in class Application
arguments
- Command line arguments.protected void report()
The primary purpose of this method is to act as a checkpoint. Fatal
errors are sometimes the result of code that is run in
finally
blocks. Such code typically avoids raising
exceptions when it fails, in order to avoid masking a primary exception.
Thus, fatal errors are signalled instead by calling the
fatal
method. A method which called fatal
, but
had no other errors, will appear to have returned successfully.
report
must be called after such a method is run to ensure
that the fatal error causes termination of the program.
protected void report(ApplicationFailure failure)
The failure is added to a list of application errors, and will be printed on application exit. If a fatal error has been signalled, the application exits immediately.
failure
- Application failure.private void printErrorsAndExit()
The exit status is zero if there are no errors, and non-zero otherwise.
protected void fatal(java.lang.String message)
message
- Description of the error.