Class 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.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected boolean canExit()
      Determines whether the application can exit.
      boolean canStart()
      Checks requirements, permissions, and expirations before starting.
      void displayError​(java.lang.String message)
      Displays the given error to the user
      void displayError​(java.lang.String message, java.lang.Throwable throwable)
      Displays an error message to the user for an exception.
      void exit​(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.String getDisplayErrorMessage​(java.lang.Throwable throwable)
      Constructs a user-presentable error message based on an exception.
      java.util.Optional<java.time.LocalDate> getExpirationDate()  
      java.util.prefs.Preferences getPreferences()
      Returns the application user preferences.
      void initialize()
      Initializes the application.
      protected void performExit​(int status)
      Exits the application with the given status without checking to see if exit should be performed.
      protected void setAuthenticator​(com.globalmentor.net.Authenticable authenticable)
      Sets the authenticator object used to retrieve client authentication.
      protected void setExpirationDate​(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 io.clogr.Clogged

        getLogger
      • Methods inherited from interface com.globalmentor.model.Named

        getName
      • Methods inherited from interface java.lang.Runnable

        run
    • Constructor Detail

      • AbstractApplication

        public AbstractApplication()
        No-arguments constructor.
      • AbstractApplication

        public AbstractApplication​(@Nonnull
                                   java.lang.String[] args)
        Arguments constructor.
        Parameters:
        args - The command line arguments.
    • Method Detail

      • getAuthenticator

        public java.util.Optional<com.globalmentor.net.Authenticable> getAuthenticator()
        Specified by:
        getAuthenticator in interface Application
        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:
        getArgs in interface Application
        Returns:
        The command-line arguments of the application.
      • getPreferences

        public java.util.prefs.Preferences getPreferences()
                                                   throws java.lang.SecurityException
        Description copied from interface: Application
        Returns the application user preferences.
        Specified by:
        getPreferences in interface Application
        Returns:
        The default user preferences for this application.
        Throws:
        java.lang.SecurityException - if a security manager is present and it denies RuntimePermission("preferences").
      • getExpirationDate

        public java.util.Optional<java.time.LocalDate> getExpirationDate()
        Specified by:
        getExpirationDate in interface Application
        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, or null if there is no expiration.
      • initialize

        public void initialize()
                        throws java.lang.Exception
        Initializes the application. This method is called after construction but before application execution.
        Specified by:
        initialize in interface Application
        Implementation Specification:
        This version does nothing.
        Throws:
        java.lang.Exception - if anything goes wrong.
      • canStart

        public boolean canStart()
        Description copied from interface: Application
        Checks requirements, permissions, and expirations before starting.
        Specified by:
        canStart in interface Application
        Returns:
        true if the checks succeeded.
      • displayError

        public void displayError​(@Nonnull
                                 java.lang.String message,
                                 @Nonnull
                                 java.lang.Throwable throwable)
        Description copied from interface: Application
        Displays an error message to the user for an exception.
        Specified by:
        displayError in interface Application
        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: Application
        Displays the given error to the user
        Specified by:
        displayError in interface Application
        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 is Throwable.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:
        true if the application can exit, else false.
      • 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:
        exit in interface Application
        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.Exception
        Exits 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.