Force platform shutdown after some delay when workbench is closed 93/1893/2
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Mon, 2 Jul 2018 08:24:40 +0000 (11:24 +0300)
committerHannu Niemistö <hannu.niemisto@semantum.fi>
Mon, 2 Jul 2018 12:13:45 +0000 (15:13 +0300)
gitlab #37

Change-Id: I6ab788e55824dbc3b2dd656cbb6ead407e35938a

bundles/org.simantics.workbench/src/org/simantics/workbench/internal/SimanticsWorkbenchApplication.java

index d31914fd00351c59d1c42f3f5e735f18cdefc712..aaacddc460addfea176ea7f9ef45794cb7e7ee8e 100644 (file)
@@ -33,8 +33,6 @@ import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.osgi.service.datalocation.Location;
 import org.eclipse.osgi.util.NLS;
 import org.eclipse.swt.SWT;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.MessageBox;
 import org.eclipse.swt.widgets.Shell;
@@ -58,6 +56,8 @@ import org.simantics.db.management.SessionContextProvider;
 import org.simantics.db.management.SingleSessionContextProviderSource;
 import org.simantics.ui.SimanticsUI;
 import org.simantics.utils.ui.BundleUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 
 /**
@@ -67,6 +67,7 @@ import org.simantics.utils.ui.BundleUtils;
  */
 public class SimanticsWorkbenchApplication implements IApplication, IExecutableExtension {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(SimanticsWorkbenchApplication.class);
     /**
      * The name of the folder containing metadata information for the workspace.
      */
@@ -79,6 +80,9 @@ public class SimanticsWorkbenchApplication implements IApplication, IExecutableE
     private static final String WORKSPACE_VERSION_VALUE = "1"; //$NON-NLS-1$
 
     private static final String PROP_EXIT_CODE = "eclipse.exitcode"; //$NON-NLS-1$
+    
+    private static final String PROP_SHUTDOWN_GRACE_PERIOD = "simantics.shutdownGracePeriod"; //$NON-NLS-1$
+    private static final long DEFAULT_SHUTDOWN_GRACE_PERIOD = 5000L;
 
     /**
      * A special return code that will be recognized by the launcher and used to
@@ -151,18 +155,26 @@ public class SimanticsWorkbenchApplication implements IApplication, IExecutableE
             // PlatformUI.getWorkbench() or AbstractUIPlugin.getWorkbench()
             int returnCode = PlatformUI.createAndRunWorkbench(display,
                     createWorkbenchAdvisor(args, processor));
+            
+            Long shutdownGracePeriodPropValue = Long.getLong(PROP_SHUTDOWN_GRACE_PERIOD);
+            long shutdownGracePeriod = shutdownGracePeriodPropValue == null 
+                    ? DEFAULT_SHUTDOWN_GRACE_PERIOD
+                    : shutdownGracePeriodPropValue;
 
             // the workbench doesn't support relaunch yet (bug 61809) so
             // for now restart is used, and exit data properties are checked
             // here to substitute in the relaunch return code if needed
             if (returnCode != PlatformUI.RETURN_RESTART) {
+                delayedShutdown(EXIT_OK, shutdownGracePeriod);
                 return EXIT_OK;
             }
 
             // if the exit code property has been set to the relaunch code, then
             // return that code now, otherwise this is a normal restart
-            return EXIT_RELAUNCH.equals(Integer.getInteger(PROP_EXIT_CODE)) ? EXIT_RELAUNCH
+            int exitCode = EXIT_RELAUNCH.equals(Integer.getInteger(PROP_EXIT_CODE)) ? EXIT_RELAUNCH
                     : EXIT_RESTART;
+            delayedShutdown(exitCode, shutdownGracePeriod);
+            return exitCode;
         } finally {
             if (display != null) {
                 display.dispose();
@@ -173,6 +185,25 @@ public class SimanticsWorkbenchApplication implements IApplication, IExecutableE
         }
     }
 
+    private void delayedShutdown(int exitCode, long delayMs) {
+        LOGGER.info("Started delayed shutdown with delay {} ms.", delayMs);
+        Thread shutdownThread = new Thread() {
+            @Override
+            public void run() {
+                try {
+                    Thread.sleep(delayMs);
+                    LOGGER.warn("Delayed shutdown forced the application to exit with code {}.", exitCode);
+                    System.exit(exitCode);
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                }
+            }
+        };
+        shutdownThread.setDaemon(true);
+        shutdownThread.setName("delayed-shutdown");
+        shutdownThread.start();
+    }
+
     /*************************************************************************/
 
     private IArguments parseArguments(String[] args) {