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;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * @author Tuukka Lehtonen
26 public class WorkbenchShutdownServiceImpl implements WorkbenchShutdownService {
28 private static final Logger LOGGER = LoggerFactory.getLogger(WorkbenchShutdownServiceImpl.class);
29 private static ServiceRegistration service = null;
31 private final List<Runnable> hooks = new ArrayList<Runnable>();
34 * Invoked by the bundle activator to initialize the cache service.
36 * @param context the bundle context to register the service with
38 @SuppressWarnings({ "unchecked", "rawtypes" })
39 public synchronized static void initialize(BundleContext context) {
40 if (service == null) {
41 service = context.registerService(WorkbenchShutdownService.class.getName(), new WorkbenchShutdownServiceImpl(), new Hashtable());
46 * Invoked by the bundle activator to close the cache service.
48 public synchronized static void close() {
49 if (service != null) {
56 public synchronized void registerShutdownHook(Runnable hook) {
61 public synchronized void doShutdown() {
62 Runnable[] rs = hooks.toArray(new Runnable[0]);
64 for (Runnable hook : rs) {
67 } catch (Exception e) {
68 handleException(hook, e);
69 } catch (LinkageError e) {
70 handleException(hook, e);
71 } catch (AssertionError e) {
72 handleException(hook, e);
77 protected void handleException(Object source, Throwable t) {
78 LOGGER.error(getClass().getSimpleName() + ": workbench shutdown hook " + source + " caused unexpected exception:", t);