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 = (LogEntry) entries.nextElement();
60 this.listener.logged(entry);
65 * Install the default exception handler so that the uncaught
66 * exceptions are handled by our customized handler. This new
67 * handler will display the exceptions to OSGI console as well
70 UncaughtExceptionHandler handler = DEFAULT_UNCAUGHT_EXCEPTION_POLICY;
71 final String policy = context.getProperty(UNCAUGHT_EXCEPTION_POLICY_PROP);
74 handler = UncaughtExceptionPolicy.valueOf(policy.toUpperCase());
75 } catch (IllegalArgumentException ex) {
76 log.warn("Invalid policy name \"{}\", defaulting to {}", policy, handler);
79 log.info("Setting uncaught exception policy to {}", handler);
80 Thread.setDefaultUncaughtExceptionHandler(handler);
83 * Install the Shutdown handler. This will intercept SIGTERM signal and
84 * close the system bundle. This allows for a graceful closing of OSGI
87 shutdownHandler = new ShutdownHandler(context);
88 Runtime.getRuntime().addShutdownHook(shutdownHandler);
90 this.log.error("Cannot register the LogListener because "
91 + "cannot retrieve LogReaderService");
95 .println("Could not initialize the logging bridge subsytem");
100 public void stop(BundleContext context) {
101 ServiceReference service = null;
102 service = context.getServiceReference(LogReaderService.class.getName());
103 if (service != null) {
104 LogReaderService reader = (LogReaderService) service;
105 reader.removeLogListener(this.listener);
107 if (this.shutdownHandler != null) {
108 Runtime.getRuntime().removeShutdownHook(this.shutdownHandler);
110 this.listener = null;
112 this.shutdownHandler = null;
115 private class ShutdownHandler extends Thread {
116 BundleContext bundlecontext;
117 public ShutdownHandler(BundleContext ctxt) {
118 this.bundlecontext = ctxt;
124 this.bundlecontext.getBundle(0).stop();
125 log.debug("shutdown handler thread called");
126 } catch (BundleException e) {
127 log.debug("Bundle couldn't be stopped");
128 } catch (Exception e) {
129 log.debug("Unhandled exception");