2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.logging.bridge.internal;
10 import org.osgi.service.log.LogEntry;
12 import java.lang.Thread.UncaughtExceptionHandler;
13 import java.util.Enumeration;
15 import org.osgi.framework.BundleContext;
16 import org.osgi.framework.BundleException;
17 import org.osgi.framework.BundleActivator;
18 import org.osgi.framework.ServiceReference;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.slf4j.ILoggerFactory;
22 import org.osgi.service.log.LogReaderService;
24 public class Activator implements BundleActivator {
25 private static final String UNCAUGHT_EXCEPTION_POLICY_PROP = "controller.uncaughtExceptionPolicy";
26 private static final UncaughtExceptionPolicy DEFAULT_UNCAUGHT_EXCEPTION_POLICY = UncaughtExceptionPolicy.IGNORE;
28 private LogListenerImpl listener = null;
29 private ShutdownHandler shutdownHandler = null;
30 private Logger log = null;
33 public void start(BundleContext context) {
34 // Lets trigger the resolution of the slf4j logger factory
35 ILoggerFactory f = LoggerFactory.getILoggerFactory();
37 // Now retrieve a logger for the bridge
39 .getLogger("org.opendaylight.controller.logging.bridge.OSGI2SLF4J");
41 if (this.log != null) {
42 this.listener = new LogListenerImpl(log);
44 ServiceReference service = null;
45 service = context.getServiceReference(LogReaderService.class
47 if (service != null) {
48 LogReaderService reader = (LogReaderService) context
51 this.log.error("Cannot register the LogListener because "
52 + "cannot retrieve LogReaderService");
54 reader.addLogListener(this.listener);
55 // Now lets walk all the exiting messages
56 Enumeration<LogEntry> entries = reader.getLog();
57 if (entries != null) {
58 while (entries.hasMoreElements()) {
59 LogEntry entry = entries.nextElement();
60 this.listener.logged(entry);
66 * Install the default exception handler so that the uncaught
67 * exceptions are handled by our customized handler. This new
68 * handler will display the exceptions to OSGI console as well
71 UncaughtExceptionHandler handler = DEFAULT_UNCAUGHT_EXCEPTION_POLICY;
72 final String policy = context.getProperty(UNCAUGHT_EXCEPTION_POLICY_PROP);
75 handler = UncaughtExceptionPolicy.valueOf(policy.toUpperCase());
76 } catch (IllegalArgumentException ex) {
77 log.warn("Invalid policy name \"{}\", defaulting to {}", policy, handler);
80 log.trace("Setting uncaught exception policy to {}", handler);
81 Thread.setDefaultUncaughtExceptionHandler(handler);
84 * Install the Shutdown handler. This will intercept SIGTERM signal and
85 * close the system bundle. This allows for a graceful closing of OSGI
88 shutdownHandler = new ShutdownHandler(context);
89 Runtime.getRuntime().addShutdownHook(shutdownHandler);
91 this.log.error("Cannot register the LogListener because "
92 + "cannot retrieve LogReaderService");
96 .println("Could not initialize the logging bridge subsytem");
101 public void stop(BundleContext context) {
102 ServiceReference serviceRef = context.getServiceReference(
103 LogReaderService.class.getName());
104 if (serviceRef != null) {
105 LogReaderService reader = (LogReaderService) context.getService(serviceRef);
106 reader.removeLogListener(this.listener);
108 if (this.shutdownHandler != null) {
109 Runtime.getRuntime().removeShutdownHook(this.shutdownHandler);
111 this.listener = null;
113 this.shutdownHandler = null;
116 private class ShutdownHandler extends Thread {
117 BundleContext bundlecontext;
118 public ShutdownHandler(BundleContext ctxt) {
119 this.bundlecontext = ctxt;
125 this.bundlecontext.getBundle(0).stop();
126 log.debug("shutdown handler thread called");
127 } catch (BundleException e) {
128 log.debug("Bundle couldn't be stopped");
129 } catch (Exception e) {
130 log.debug("Unhandled exception");