]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics/src/org/simantics/internal/UnhandledExceptionServiceImpl.java
Simantics Console
[simantics/platform.git] / bundles / org.simantics / src / org / simantics / internal / UnhandledExceptionServiceImpl.java
1 /*******************************************************************************
2  * Copyright (c) 2019 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  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.internal;
13
14 import java.util.ArrayList;
15 import java.util.Hashtable;
16 import java.util.List;
17 import java.util.function.Consumer;
18
19 import org.osgi.framework.BundleContext;
20 import org.osgi.framework.ServiceRegistration;
21 import org.simantics.UnhandledExceptionService;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * @author Antti Villberg
27  */
28 public class UnhandledExceptionServiceImpl implements UnhandledExceptionService {
29
30     private static final Logger LOGGER = LoggerFactory.getLogger(UnhandledExceptionServiceImpl.class);
31     private static ServiceRegistration<?> service = null;
32
33     private final List<Consumer<Throwable>>       handlers   = new ArrayList<>();
34
35     /**
36      * Invoked by the bundle activator to initialize the cache service.
37      * 
38      * @param context the bundle context to register the service with
39      */
40     @SuppressWarnings({ "unchecked", "rawtypes" })
41     public synchronized static void initialize(BundleContext context) {
42         if (service == null) {
43             service = context.registerService(UnhandledExceptionService.class.getName(), new UnhandledExceptionServiceImpl(), new Hashtable());
44         }
45     }
46
47     /**
48      * Invoked by the bundle activator to close the cache service.
49      */
50     public synchronized static void close() {
51         if (service != null) {
52             service.unregister();
53             service = null;
54         }
55     }
56
57     @Override
58     public synchronized void registerHandler(Consumer<Throwable> handler) {
59         handlers.add(handler);
60     }
61
62     @Override
63     public synchronized void handle(Throwable t) {
64         for (Consumer<Throwable> handler : handlers) {
65             try {
66                 handler.accept(t);
67             } catch (Exception e) {
68                 handleException(handler, e);
69             } catch (LinkageError e) {
70                 handleException(handler, e);
71             } catch (AssertionError e) {
72                 handleException(handler, e);
73             }
74         }
75     }
76
77     protected void handleException(Object source, Throwable t) {
78         LOGGER.error("{}: Unhandled exception handler {} caused unexpected exception", getClass().getSimpleName(), source, t);
79     }
80
81 }