]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/WorkbenchShutdownServiceImpl.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / WorkbenchShutdownServiceImpl.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.ui.workbench;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Hashtable;\r
16 import java.util.List;\r
17 \r
18 import org.osgi.framework.BundleContext;\r
19 import org.osgi.framework.ServiceRegistration;\r
20 \r
21 /**\r
22  * @author Tuukka Lehtonen\r
23  */\r
24 public class WorkbenchShutdownServiceImpl implements WorkbenchShutdownService {\r
25 \r
26     private static ServiceRegistration service = null;\r
27 \r
28     private final List<Runnable>       hooks   = new ArrayList<Runnable>();\r
29 \r
30     /**\r
31      * Invoked by the bundle activator to initialize the cache service.\r
32      * \r
33      * @param context the bundle context to register the service with\r
34      */\r
35     @SuppressWarnings({ "unchecked", "rawtypes" })\r
36     public synchronized static void initialize(BundleContext context) {\r
37         if (service == null) {\r
38             service = context.registerService(WorkbenchShutdownService.class.getName(), new WorkbenchShutdownServiceImpl(), new Hashtable());\r
39         }\r
40     }\r
41 \r
42     /**\r
43      * Invoked by the bundle activator to close the cache service.\r
44      */\r
45     public synchronized static void close() {\r
46         if (service != null) {\r
47             service.unregister();\r
48             service = null;\r
49         }\r
50     }\r
51 \r
52     @Override\r
53     public synchronized void registerShutdownHook(Runnable hook) {\r
54         hooks.add(hook);\r
55     }\r
56 \r
57     @Override\r
58     public synchronized void doShutdown() {\r
59         Runnable[] rs = hooks.toArray(new Runnable[0]);\r
60         hooks.clear();\r
61         for (Runnable hook : rs) {\r
62             try {\r
63                 hook.run();\r
64             } catch (Exception e) {\r
65                 handleException(hook, e);\r
66             } catch (LinkageError e) {\r
67                 handleException(hook, e);\r
68             } catch (AssertionError e) {\r
69                 handleException(hook, e);\r
70             }\r
71         }\r
72     }\r
73 \r
74     protected void handleException(Object source, Throwable t) {\r
75         System.err.println(getClass().getSimpleName() + ": workbench shutdown hook " + source + " caused unexpected exception:");\r
76         t.printStackTrace();\r
77     }\r
78 \r
79 }\r