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:
- The default preference node is based upon the implementing application class.
-
-
Field Summary
-
Fields inherited from interface com.globalmentor.application.Application
NO_ARGUMENTS
-
-
Constructor Summary
Constructors Constructor Description AbstractApplication()No-arguments constructor.AbstractApplication(java.lang.String[] args)Arguments constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected booleancanExit()Determines whether the application can exit.booleancanStart()Checks requirements, permissions, and expirations before starting.voiddisplayError(java.lang.String message)Displays the given error to the uservoiddisplayError(java.lang.String message, java.lang.Throwable throwable)Displays an error message to the user for an exception.voidexit(int status)Exits the application with the given status.java.lang.String[]getArgs()java.util.Optional<com.globalmentor.net.Authenticable>getAuthenticator()protected static java.lang.StringgetDisplayErrorMessage(java.lang.Throwable throwable)Constructs a user-presentable error message based on an exception.java.util.Optional<java.time.LocalDate>getExpirationDate()java.util.prefs.PreferencesgetPreferences()Returns the application user preferences.voidinitialize()Initializes the application.protected voidperformExit(int status)Exits the application with the given status without checking to see if exit should be performed.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.-
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
exit, getVersion, isDebug, start
-
-
-
-
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.
-
canStart
public boolean canStart()
Description copied from interface:ApplicationChecks requirements, permissions, and expirations before starting.- Specified by:
canStartin interfaceApplication- Returns:
trueif the checks succeeded.
-
displayError
public void displayError(@Nonnull java.lang.String message, @Nonnull java.lang.Throwable throwable)Description copied from interface:ApplicationDisplays an error message to the user for an exception.- Specified by:
displayErrorin interfaceApplication- Parameters:
message- The message to display.throwable- The condition that caused the error.
-
displayError
public void displayError(java.lang.String message)
Description copied from interface:ApplicationDisplays the given error to the user- Specified by:
displayErrorin interfaceApplication- Parameters:
message- The error to display.
-
getDisplayErrorMessage
protected static java.lang.String getDisplayErrorMessage(java.lang.Throwable throwable)
Constructs a user-presentable error message based on an exception. In most cases this isThrowable.getMessage().- Parameters:
throwable- The condition that caused the error.- Returns:
- The error message.
- See Also:
Throwable.getMessage()
-
canExit
protected boolean canExit()
Determines whether the application can exit. This method may query the user. If the application has been modified, the configuration is saved if possible.- Returns:
trueif the application can exit, elsefalse.
-
exit
public final void exit(int status)
Exits the application with the given status. This method first checks to see if exit can occur.- Specified by:
exitin interfaceApplication- API Note:
- To add to exit functionality,
performExit(int)should be overridden rather than this method. - Parameters:
status- The exit status.- See Also:
canExit(),performExit(int)
-
performExit
protected void performExit(int status) throws java.lang.ExceptionExits the application with the given status without checking to see if exit should be performed.- Parameters:
status- The exit status.- Throws:
java.lang.Exception- Thrown if anything goes wrong.
-
-