-
/*
* Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
*
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.controller.logging.bridge.internal;
import org.osgi.service.log.LogEntry;
+
+import java.lang.Thread.UncaughtExceptionHandler;
import java.util.Enumeration;
+
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.BundleActivator;
import org.osgi.service.log.LogReaderService;
public class Activator implements BundleActivator {
+ private static final String UNCAUGHT_EXCEPTION_POLICY_PROP = "controller.uncaughtExceptionPolicy";
+ private static final UncaughtExceptionPolicy DEFAULT_UNCAUGHT_EXCEPTION_POLICY = UncaughtExceptionPolicy.IGNORE;
+
private LogListenerImpl listener = null;
+ private ShutdownHandler shutdownHandler = null;
private Logger log = null;
@Override
* handler will display the exceptions to OSGI console as well
* as log to file.
*/
- Thread.setDefaultUncaughtExceptionHandler(new org.opendaylight.
- controller.logging.bridge.internal.UncaughtExceptionHandler());
+ UncaughtExceptionHandler handler = DEFAULT_UNCAUGHT_EXCEPTION_POLICY;
+ final String policy = context.getProperty(UNCAUGHT_EXCEPTION_POLICY_PROP);
+ if (policy != null) {
+ try {
+ handler = UncaughtExceptionPolicy.valueOf(policy.toUpperCase());
+ } catch (IllegalArgumentException ex) {
+ log.warn("Invalid policy name \"{}\", defaulting to {}", policy, handler);
+ }
+ }
+ log.info("Setting uncaught exception policy to {}", handler);
+ Thread.setDefaultUncaughtExceptionHandler(handler);
/*
* Install the Shutdown handler. This will intercept SIGTERM signal and
* close the system bundle. This allows for a graceful closing of OSGI
* framework.
*/
-
- Runtime.getRuntime().addShutdownHook(new shutdownHandler(context));
+ shutdownHandler = new ShutdownHandler(context);
+ Runtime.getRuntime().addShutdownHook(shutdownHandler);
} else {
this.log.error("Cannot register the LogListener because "
+ "cannot retrieve LogReaderService");
LogReaderService reader = (LogReaderService) service;
reader.removeLogListener(this.listener);
}
-
+ if (this.shutdownHandler != null) {
+ Runtime.getRuntime().removeShutdownHook(this.shutdownHandler);
+ }
this.listener = null;
this.log = null;
+ this.shutdownHandler = null;
}
- private class shutdownHandler extends Thread {
+ private class ShutdownHandler extends Thread {
BundleContext bundlecontext;
- public shutdownHandler(BundleContext ctxt) {
+ public ShutdownHandler(BundleContext ctxt) {
this.bundlecontext = ctxt;
}
--- /dev/null
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.logging.bridge.internal;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+enum UncaughtExceptionPolicy implements Thread.UncaughtExceptionHandler {
+ ABORT {
+ public static final int EXIT_CODE = 1;
+
+ @Override
+ public void uncaughtException(final Thread t, final Throwable e) {
+ log.error("Thread {} died because of an uncaught exception, forcing virtual machine shutdown", t, e);
+ System.exit(EXIT_CODE);
+ }
+ },
+ IGNORE {
+ @Override
+ public void uncaughtException(final Thread t, final Throwable e) {
+ log.error("Thread {} died because of an uncaught exception", t, e);
+ }
+ };
+
+ private static final Logger log = LoggerFactory.getLogger(UncaughtExceptionPolicy.class);
+}