From 93622c2baf328a90b122dd749b63676c72c839f5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Hannu=20Niemist=C3=B6?= Date: Mon, 2 Jul 2018 11:24:40 +0300 Subject: [PATCH] Force platform shutdown after some delay when workbench is closed gitlab #37 Change-Id: I6ab788e55824dbc3b2dd656cbb6ead407e35938a --- .../SimanticsWorkbenchApplication.java | 37 +++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/bundles/org.simantics.workbench/src/org/simantics/workbench/internal/SimanticsWorkbenchApplication.java b/bundles/org.simantics.workbench/src/org/simantics/workbench/internal/SimanticsWorkbenchApplication.java index d31914fd0..aaacddc46 100644 --- a/bundles/org.simantics.workbench/src/org/simantics/workbench/internal/SimanticsWorkbenchApplication.java +++ b/bundles/org.simantics.workbench/src/org/simantics/workbench/internal/SimanticsWorkbenchApplication.java @@ -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) { -- 2.47.1