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