Package com.globalmentor.application
Interface Application
-
- All Superinterfaces:
io.clogr.Clogged,com.globalmentor.model.Named<java.lang.String>,java.lang.Runnable
- All Known Implementing Classes:
AbstractApplication,BaseCliApplication
public interface Application extends java.lang.Runnable, com.globalmentor.model.Named<java.lang.String>, io.clogr.CloggedA general application.To start an application, call the static
start(Application)method, passing it an application instance.- Author:
- Garret Wilson
- API Note:
- Although an application implements
Runnable, it should usually be started usingstart(), which will eventually (depending on the implementation) callRunnable.run(). Thestart(Application)takes care of calling the correct entry point.
-
-
Field Summary
Fields Modifier and Type Field Description static intEXIT_CODE_CONTINUEPseudo exit code indicating that the application should not exit immediately, e.g.static intEXIT_CODE_OKExit code indicating a successful termination.static intEXIT_CODE_SOFTWAREExit code indicating execution failure.static intEXIT_CODE_USAGEExit code indicating incorrect command-line usage.static java.lang.String[]NO_ARGUMENTSAn array containing no arguments.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default voidend()Ends the application with a status of success.voidend(int status)Ends the application with the given status.default voidexit(int status)Exits the application immediately with the given status without checking to see if exit should be performed.java.lang.String[]getArgs()java.util.Optional<com.globalmentor.net.Authenticable>getAuthenticator()java.util.Optional<java.time.LocalDate>getExpirationDate()java.util.prefs.PreferencesgetPreferences()Returns the application user preferences.java.lang.StringgetVersion()voidinitialize()Initializes the application.booleanisDebug()Returns whether debug mode is enabled.voidreportError(java.lang.String message)Reports the given error message to the uservoidreportError(java.lang.String message, java.lang.Throwable throwable)Reports an error message to the user related to an exception.voidreportError(java.lang.Throwable throwable)Reports an error condition to the user.intstart()Starts the application if it can be started.static voidstart(Application application)Starts an application.
-
-
-
Field Detail
-
EXIT_CODE_OK
static final int EXIT_CODE_OK
Exit code indicating a successful termination.- See Also:
- Constant Field Values
-
EXIT_CODE_SOFTWARE
static final int EXIT_CODE_SOFTWARE
Exit code indicating execution failure.
-
EXIT_CODE_USAGE
static final int EXIT_CODE_USAGE
Exit code indicating incorrect command-line usage.
-
EXIT_CODE_CONTINUE
static final int EXIT_CODE_CONTINUE
Pseudo exit code indicating that the application should not exit immediately, e.g. for a GUI or daemon application.- See Also:
- Constant Field Values
-
NO_ARGUMENTS
static final java.lang.String[] NO_ARGUMENTS
An array containing no arguments.
-
-
Method Detail
-
getAuthenticator
java.util.Optional<com.globalmentor.net.Authenticable> getAuthenticator()
- Returns:
- The authenticator object used to retrieve client authentication.
-
getArgs
java.lang.String[] getArgs()
- Returns:
- The command-line arguments of the application.
-
getVersion
java.lang.String getVersion()
- Returns:
- The application version string .
-
isDebug
boolean isDebug()
Returns whether debug mode is enabled.- API Note:
- Debug mode enables debug level logging and may also enable other debug functionality.
- Returns:
- The state of debug mode.
-
getPreferences
java.util.prefs.Preferences getPreferences() throws java.lang.SecurityExceptionReturns the application user preferences.- Returns:
- The default user preferences for this application.
- Throws:
java.lang.SecurityException- if a security manager is present and it deniesRuntimePermission("preferences").
-
getExpirationDate
java.util.Optional<java.time.LocalDate> getExpirationDate()
- Returns:
- The expiration date of the application, if there is one.
-
initialize
void initialize() throws java.lang.ExceptionInitializes the application. This method is called after construction but before application execution.- Throws:
java.lang.Exception- if anything goes wrong.
-
start
int start()
Starts the application if it can be started.
-
start
static void start(@Nonnull Application application)Starts an application. If this method returns, the program is still running.- Calls
initialize(). - Calls
start(), which eventually callsRunnable.run(). If a non-zero exit code is returned, the application will end. An exit code of-1indicates that the application should not exit after running. - Normally
start()delegates toRunnable.run()for default functionality, but may delegate directly to other methods, e.g. representing CLI commands. - If a positive exit code was given, calls
end(int)to exit the application. (A status of -1 indicates that the program continues to run.)
- API Note:
- Except for GUI programs and daemons, this method will never return, as it will eventually call
end(int)which will immediately exit the program. If the program chooses to continue running, it should callend(int)at some point when it is ready to stop so that all needed shutdown activities will occur. - Parameters:
application- The application to start.
- Calls
-
end
default void end()
Ends the application with a status of success.
-
end
void end(@Nonnegative int status)Ends the application with the given status. This method first checks to see if the program can end. If the status is not 0, the application will then exit immediately.- API Note:
- This method normally will never return., To add to exit functionality,
exit(int)should be overridden rather than this method., This method explicitly does not accept -1, as continuing and ending contradictory concepts. - Implementation Note:
- This method should eventually delegate to
exit(int). - Parameters:
status- The exit status, which must not be negative.- Throws:
java.lang.IllegalArgumentException- if the given status is negative.- See Also:
exit(int)
-
exit
default void exit(int status)
Exits the application immediately with the given status without checking to see if exit should be performed.- API Note:
- This method normally will never return., Normally this method is never called directly by the application. To end the application, calling
end(int)is preferred. - Implementation Specification:
- The default implementation delegates to
System.exit(int). - Parameters:
status- The exit status.- Throws:
java.lang.SecurityException- if a security manager exists and itsSecurityManager.checkExit(int)method doesn't allow exit with the specified status.- See Also:
System.exit(int)
-
reportError
void reportError(@Nonnull java.lang.Throwable throwable)Reports an error condition to the user. A message will be added as appropriate.- Parameters:
throwable- The condition that caused the error.
-
reportError
void reportError(@Nonnull java.lang.String message, @Nonnull java.lang.Throwable throwable)Reports an error message to the user related to an exception.- Parameters:
message- The message to display.throwable- The condition that caused the error.
-
reportError
void reportError(@Nonnull java.lang.String message)Reports the given error message to the user- Parameters:
message- The error to display.
-
-