1 /*******************************************************************************
2 * Copyright (c) 2019 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 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.internal;
14 import java.util.ArrayList;
15 import java.util.Hashtable;
16 import java.util.List;
17 import java.util.function.Consumer;
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;
26 * @author Antti Villberg
28 public class UnhandledExceptionServiceImpl implements UnhandledExceptionService {
30 private static final Logger LOGGER = LoggerFactory.getLogger(UnhandledExceptionServiceImpl.class);
31 private static ServiceRegistration<?> service = null;
33 private final List<Consumer<Throwable>> handlers = new ArrayList<>();
36 * Invoked by the bundle activator to initialize the cache service.
38 * @param context the bundle context to register the service with
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());
48 * Invoked by the bundle activator to close the cache service.
50 public synchronized static void close() {
51 if (service != null) {
58 public synchronized void registerHandler(Consumer<Throwable> handler) {
59 handlers.add(handler);
63 public synchronized void handle(Throwable t) {
64 for (Consumer<Throwable> handler : handlers) {
67 } catch (Exception e) {
68 handleException(handler, e);
69 } catch (LinkageError e) {
70 handleException(handler, e);
71 } catch (AssertionError e) {
72 handleException(handler, e);
77 protected void handleException(Object source, Throwable t) {
78 LOGGER.error("{}: Unhandled exception handler {} caused unexpected exception", getClass().getSimpleName(), source, t);