]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/WorkbenchShutdownServiceImpl.java
Replace System.err and System.out with SLF4J Logging
[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 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * @author Tuukka Lehtonen
25  */
26 public class WorkbenchShutdownServiceImpl implements WorkbenchShutdownService {
27
28     private static final Logger LOGGER = LoggerFactory.getLogger(WorkbenchShutdownServiceImpl.class);
29     private static ServiceRegistration service = null;
30
31     private final List<Runnable>       hooks   = new ArrayList<Runnable>();
32
33     /**
34      * Invoked by the bundle activator to initialize the cache service.
35      * 
36      * @param context the bundle context to register the service with
37      */
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());
42         }
43     }
44
45     /**
46      * Invoked by the bundle activator to close the cache service.
47      */
48     public synchronized static void close() {
49         if (service != null) {
50             service.unregister();
51             service = null;
52         }
53     }
54
55     @Override
56     public synchronized void registerShutdownHook(Runnable hook) {
57         hooks.add(hook);
58     }
59
60     @Override
61     public synchronized void doShutdown() {
62         Runnable[] rs = hooks.toArray(new Runnable[0]);
63         hooks.clear();
64         for (Runnable hook : rs) {
65             try {
66                 hook.run();
67             } catch (Exception e) {
68                 handleException(hook, e);
69             } catch (LinkageError e) {
70                 handleException(hook, e);
71             } catch (AssertionError e) {
72                 handleException(hook, e);
73             }
74         }
75     }
76
77     protected void handleException(Object source, Throwable t) {
78         LOGGER.error(getClass().getSimpleName() + ": workbench shutdown hook " + source + " caused unexpected exception:", t);
79     }
80
81 }