1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.ui.workbench;
14 import java.util.ArrayList;
15 import java.util.Hashtable;
16 import java.util.List;
18 import org.osgi.framework.BundleContext;
19 import org.osgi.framework.ServiceRegistration;
22 * @author Tuukka Lehtonen
24 public class WorkbenchShutdownServiceImpl implements WorkbenchShutdownService {
26 private static ServiceRegistration service = null;
28 private final List<Runnable> hooks = new ArrayList<Runnable>();
31 * Invoked by the bundle activator to initialize the cache service.
33 * @param context the bundle context to register the service with
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());
43 * Invoked by the bundle activator to close the cache service.
45 public synchronized static void close() {
46 if (service != null) {
53 public synchronized void registerShutdownHook(Runnable hook) {
58 public synchronized void doShutdown() {
59 Runnable[] rs = hooks.toArray(new Runnable[0]);
61 for (Runnable hook : rs) {
64 } catch (Exception e) {
65 handleException(hook, e);
66 } catch (LinkageError e) {
67 handleException(hook, e);
68 } catch (AssertionError e) {
69 handleException(hook, e);
74 protected void handleException(Object source, Throwable t) {
75 System.err.println(getClass().getSimpleName() + ": workbench shutdown hook " + source + " caused unexpected exception:");