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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.ui.workbench;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Hashtable;
\r
16 import java.util.List;
\r
18 import org.osgi.framework.BundleContext;
\r
19 import org.osgi.framework.ServiceRegistration;
\r
22 * @author Tuukka Lehtonen
\r
24 public class WorkbenchShutdownServiceImpl implements WorkbenchShutdownService {
\r
26 private static ServiceRegistration service = null;
\r
28 private final List<Runnable> hooks = new ArrayList<Runnable>();
\r
31 * Invoked by the bundle activator to initialize the cache service.
\r
33 * @param context the bundle context to register the service with
\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
43 * Invoked by the bundle activator to close the cache service.
\r
45 public synchronized static void close() {
\r
46 if (service != null) {
\r
47 service.unregister();
\r
53 public synchronized void registerShutdownHook(Runnable hook) {
\r
58 public synchronized void doShutdown() {
\r
59 Runnable[] rs = hooks.toArray(new Runnable[0]);
\r
61 for (Runnable hook : rs) {
\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
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