Package com.globalmentor.application
Class AbstractApplication
- java.lang.Object
-
- com.globalmentor.application.AbstractApplication
-
- All Implemented Interfaces:
Application,com.globalmentor.model.Named<java.lang.String>,io.clogr.Clogged,java.lang.Runnable
- Direct Known Subclasses:
BaseCliApplication
public abstract class AbstractApplication extends java.lang.Object implements Application
An abstract implementation of an application that by default is a console application.- Author:
- Garret Wilson
- Implementation Specification:
- Errors are written in simple form to
System.err., The default preference node is based upon the implementing application class.
-
-
Field Summary
-
Fields inherited from interface com.globalmentor.application.Application
EXIT_CODE_CONTINUE, EXIT_CODE_OK, EXIT_CODE_SOFTWARE, EXIT_CODE_USAGE, NO_ARGUMENTS
-
-
Constructor Summary
Constructors Constructor Description AbstractApplication()No-arguments constructor.AbstractApplication(java.lang.String[] args)Arguments constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected booleancanEnd()Determines whether the application can end.protected booleancanStart()Checks requirements, permissions, and expirations before starting.voidend(int status)Ends the application with the given status.protected intexecute()Main execution implementation.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.voidinitialize()Initializes the application.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.protected voidsetAuthenticator(com.globalmentor.net.Authenticable authenticable)Sets the authenticator object used to retrieve client authentication.protected voidsetExpirationDate(java.time.LocalDate newExpirationDate)Sets the expiration date of the application.intstart()Starts the application if it can be started.protected java.lang.StringtoErrorMessage(java.lang.Throwable throwable)Constructs a user-presentable error message based on an exception.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.globalmentor.application.Application
end, exit, getVersion, isDebug
-
-
-
-
Method Detail
-
getAuthenticator
public java.util.Optional<com.globalmentor.net.Authenticable> getAuthenticator()
- Specified by:
getAuthenticatorin interfaceApplication- Returns:
- The authenticator object used to retrieve client authentication.
-
setAuthenticator
protected void setAuthenticator(@Nullable com.globalmentor.net.Authenticable authenticable)Sets the authenticator object used to retrieve client authentication.- Parameters:
authenticable- The object to retrieve authentication information regarding a client.
-
getArgs
public java.lang.String[] getArgs()
- Specified by:
getArgsin interfaceApplication- Returns:
- The command-line arguments of the application.
-
getPreferences
public java.util.prefs.Preferences getPreferences() throws java.lang.SecurityExceptionDescription copied from interface:ApplicationReturns the application user preferences.- Specified by:
getPreferencesin interfaceApplication- Returns:
- The default user preferences for this application.
- Throws:
java.lang.SecurityException- if a security manager is present and it deniesRuntimePermission("preferences").
-
getExpirationDate
public java.util.Optional<java.time.LocalDate> getExpirationDate()
- Specified by:
getExpirationDatein interfaceApplication- Returns:
- The expiration date of the application, if there is one.
-
setExpirationDate
protected void setExpirationDate(@Nullable java.time.LocalDate newExpirationDate)Sets the expiration date of the application.- Parameters:
newExpirationDate- The new expiration date, ornullif there is no expiration.
-
initialize
public void initialize() throws java.lang.ExceptionInitializes the application. This method is called after construction but before application execution.- Specified by:
initializein interfaceApplication- Implementation Specification:
- This version does nothing.
- Throws:
java.lang.Exception- if anything goes wrong.
-
start
public int start()
Starts the application if it can be started.- Specified by:
startin interfaceApplication- API Note:
- To change how the main application is executed, normally
execute()should be overridden and not this method. - Implementation Specification:
- The default implementation delegates calls
canStart()and, if it returnstrue, delegates toexecute(). - Returns:
- The application status:
- See Also:
canStart(),execute()
-
execute
protected int execute()
Main execution implementation.- Implementation Specification:
- The default implementation delegates to
Runnable.run()and returns a status code of 0. - Implementation Note:
- Normally this method delegates to
Runnable.run()for default functionality, but may delegate directly to other methods, e.g. representing CLI commands. - Returns:
- The application status:
-
canStart
protected boolean canStart()
Checks requirements, permissions, and expirations before starting.- Returns:
trueif the checks succeeded.
-
end
public final void end(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.- Specified by:
endin interfaceApplication- Implementation Specification:
- This method first calls
canEnd()to see if exit can occur., If exit is allowed to occur, this method will exit even if there was an error in callingApplication.exit(int). - Parameters:
status- The exit status.- See Also:
canEnd(),Application.exit(int)
-
canEnd
protected boolean canEnd()
Determines whether the application can end. This method may query the user. If the application has been modified, the configuration is saved if possible.- Returns:
trueif the application can end, elsefalse.
-
reportError
public void reportError(java.lang.Throwable throwable)
Reports an error condition to the user. A message will be added as appropriate.- Specified by:
reportErrorin interfaceApplication- Implementation Specification:
- This version delegates to
reportError(String, Throwable)using the message determined bytoErrorMessage(Throwable). - Parameters:
throwable- The condition that caused the error.
-
reportError
public void reportError(@Nonnull java.lang.String message, @Nonnull java.lang.Throwable throwable)Reports an error message to the user related to an exception.- Specified by:
reportErrorin interfaceApplication- Implementation Specification:
- This implementation calls
reportError(String)and then prints a stack trace toSystem.err. - Parameters:
message- The message to display.throwable- The condition that caused the error.- See Also:
Throwable.printStackTrace(PrintStream)
-
reportError
public void reportError(java.lang.String message)
Reports the given error message to the user- Specified by:
reportErrorin interfaceApplication- Implementation Specification:
- This implementation writes the message to
System.err. - Parameters:
message- The error to display.
-
toErrorMessage
@Nonnull protected java.lang.String toErrorMessage(java.lang.Throwable throwable)
Constructs a user-presentable error message based on an exception.- Implementation Specification:
- This version returns constructed messages for exceptions known not to contain useful information. In most cases it returns
Throwable.getMessage(). - Parameters:
throwable- The condition that caused the error.- Returns:
- The error message.
- See Also:
Throwable.getMessage()
-
-