Package jmri.managers

Class DefaultShutDownManager

java.lang.Object
jmri.beans.UnboundBean
jmri.beans.Bean
jmri.managers.DefaultShutDownManager
All Implemented Interfaces:
BeanInterface, PropertyChangeFirer, PropertyChangeProvider, ShutDownManager

public class DefaultShutDownManager
extends Bean
implements ShutDownManager
The default implementation of ShutDownManager. This implementation makes the following assumptions:
  • The shutdown() and restart() methods are called on the application's main thread.
  • If the application has a graphical user interface, the application's main thread is the event dispatching thread.
  • Application windows may contain code that should be run within a registered ShutDownTask.run() method, but are not. A side effect of this assumption is that all displayable application windows are closed by this implementation when shutdown() or restart() is called and a ShutDownTask has not aborted the shutdown or restart.
  • It is expected that SIGINT and SIGTERM should trigger a clean application exit.

If another implementation of ShutDownManager has not been registered with the InstanceManager, an instance of this implementation will be automatically registered as the ShutDownManager.

Developers other applications that cannot accept the above assumptions are recommended to create their own implementations of ShutDownManager that integrates with their application's lifecycle and register that implementation with the InstanceManager as soon as possible in their application.

  • Field Details

  • Constructor Details

  • Method Details

    • register

      public void register​(ShutDownTask s)
      Register a task object for later execution. An attempt to register an already registered task will be silently ignored.
      Specified by:
      register in interface ShutDownManager
      Parameters:
      s - the task to execute
    • register

      public void register​(Callable<Boolean> task)
      Register a task for verification that JMRI should stop. An attempt to register an already register task will be silently ignored.
      Specified by:
      register in interface ShutDownManager
      Parameters:
      task - the verification task
    • register

      public void register​(Runnable task)
      Register a task that runs when JMRI is stopping. An attempt to register an already register task will be silently ignored.
      Specified by:
      register in interface ShutDownManager
      Parameters:
      task - the execution task
    • deregister

      public void deregister​(ShutDownTask s)
      Deregister a task. Attempts to deregister a task that is not registered are silently ignored.
      Specified by:
      deregister in interface ShutDownManager
      Parameters:
      s - the task not to execute
    • deregister

      public void deregister​(Callable<Boolean> task)
      Deregister a task. Attempts to deregister a task that is not registered are silently ignored.
      Specified by:
      deregister in interface ShutDownManager
      Parameters:
      task - the task not to call
    • deregister

      public void deregister​(Runnable task)
      Deregister a task. Attempts to deregister a task that is not registered are silently ignored.
      Specified by:
      deregister in interface ShutDownManager
      Parameters:
      task - the task not to run
    • tasks

      public List<ShutDownTask> tasks()
      Provide access to the current registered shutdown tasks.

      Note that implementations are free to provide a copy of the list of registered tasks and do not need to provide modifiable live access to the internal list of registered tasks.

      Specified by:
      tasks in interface ShutDownManager
      Returns:
      the list of shutdown tasks or an empty list if no shutdown tasks are registered
    • getCallables

      Specified by:
      getCallables in interface ShutDownManager
    • getRunnables

      Specified by:
      getRunnables in interface ShutDownManager
    • shutdown

      public boolean shutdown()
      Run the shutdown tasks, and then terminate the program with status 0 if not aborted. Does not return under normal circumstances. Returns false if the shutdown was aborted by the user, in which case the program should continue to operate.

      NOTE If the macOS application->quit menu item is used, this must return false to abort the shutdown.

      Specified by:
      shutdown in interface ShutDownManager
      Returns:
      false if any shutdown task aborts the shutdown or if anything goes wrong.
    • restart

      public boolean restart()
      Run the shutdown tasks, and then terminate the program with status 210 if not aborted. Does not return under normal circumstances. Returns false if the shutdown was aborted by the user, in which case the program should continue to operate.

      By exiting the program with status 210, the batch file (MS Windows) or shell script (Linux/macOS/UNIX) can catch the exit status and tell the operating system to restart.

      Specified by:
      restart in interface ShutDownManager
      Returns:
      false if any shutdown task aborts restarting the application
    • restartOS

      public boolean restartOS()
      Run the shutdown tasks, and then terminate the program with status 100 if not aborted. Does not return under normal circumstances. Returns false if the shutdown was aborted by the user, in which case the program should continue to operate.

      By exiting the program with status 100, the batch file (MS Windows) or shell script (Linux/macOS/UNIX) can catch the exit status and restart the java program.

      NOTE If the macOS application->quit menu item is used, this must return false to abort the shutdown.

      Specified by:
      restartOS in interface ShutDownManager
      Returns:
      false if any shutdown task aborts restarting the application
    • shutdownOS

      public boolean shutdownOS()
      Run the shutdown tasks, and then terminate the program with status 200 if not aborted. Does not return under normal circumstances. Returns false if the shutdown was aborted by the user, in which case the program should continue to operate.

      By exiting the program with status 200, the batch file (MS Windows) or shell script (Linux/macOS/UNIX) can catch the exit status and shutdown the OS

      NOTE If the macOS application->quit menu item is used, this must return false to abort the shutdown.

      Specified by:
      shutdownOS in interface ShutDownManager
      Returns:
      false if any shutdown task aborts restarting the application
    • shutdown

      protected boolean shutdown​(int status, boolean exit)
      First asks the shutdown tasks if shutdown is allowed. If not return false.

      Then run the shutdown tasks, and then terminate the program with status 0 if not aborted. Does not return under normal circumstances. Does return false if the shutdown was aborted by the user, in which case the program should continue to operate.

      Executes all registered ShutDownTasks before closing any displayable windows.

      Parameters:
      status - integer status on program exit
      exit - true if System.exit() should be called if all tasks are executed correctly; false otherwise
      Returns:
      false if shutdown or restart failed
    • isShuttingDown

      public boolean isShuttingDown()
      Allow components that normally request confirmation to shutdown to determine if the shutdown is already underway so as not to request confirmation.
      Specified by:
      isShuttingDown in interface ShutDownManager
      Returns:
      true if shutting down or restarting
    • setShuttingDown

      protected void setShuttingDown​(boolean state)
      This method is static so that if multiple DefaultShutDownManagers are registered, they are all aware of this state.
      Parameters:
      state - true if shutting down; false otherwise