3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.logging.bridge.internal;
12 import org.osgi.service.log.LogEntry;
13 import java.util.Enumeration;
14 import org.osgi.framework.BundleContext;
15 import org.osgi.framework.BundleException;
16 import org.osgi.framework.BundleActivator;
17 import org.osgi.framework.ServiceReference;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import org.slf4j.ILoggerFactory;
21 import org.osgi.service.log.LogReaderService;
23 public class Activator implements BundleActivator {
24 private LogListenerImpl listener = null;
25 private Logger log = null;
28 public void start(BundleContext context) {
29 // Lets trigger the resolution of the slf4j logger factory
30 ILoggerFactory f = LoggerFactory.getILoggerFactory();
32 // Now retrieve a logger for the bridge
34 .getLogger("org.opendaylight.controller.logging.bridge.OSGI2SLF4J");
36 if (this.log != null) {
37 this.listener = new LogListenerImpl(log);
39 ServiceReference service = null;
40 service = context.getServiceReference(LogReaderService.class
42 if (service != null) {
43 LogReaderService reader = (LogReaderService) context
46 this.log.error("Cannot register the LogListener because "
47 + "cannot retrieve LogReaderService");
49 reader.addLogListener(this.listener);
50 // Now lets walk all the exiting messages
51 Enumeration<LogEntry> entries = reader.getLog();
52 if (entries != null) {
53 while (entries.hasMoreElements()) {
54 LogEntry entry = (LogEntry) entries.nextElement();
55 this.listener.logged(entry);
60 * Install the default exception handler so that the uncaught
61 * exceptions are handled by our customized handler. This new
62 * handler will display the exceptions to OSGI console as well
65 Thread.setDefaultUncaughtExceptionHandler(new org.opendaylight.
66 controller.logging.bridge.internal.UncaughtExceptionHandler());
69 * Install the Shutdown handler. This will intercept SIGTERM signal and
70 * close the system bundle. This allows for a graceful closing of OSGI
74 Runtime.getRuntime().addShutdownHook(new shutdownHandler(context));
76 this.log.error("Cannot register the LogListener because "
77 + "cannot retrieve LogReaderService");
81 .println("Could not initialize the logging bridge subsytem");
86 public void stop(BundleContext context) {
87 ServiceReference service = null;
88 service = context.getServiceReference(LogReaderService.class.getName());
89 if (service != null) {
90 LogReaderService reader = (LogReaderService) service;
91 reader.removeLogListener(this.listener);
98 private class shutdownHandler extends Thread {
99 BundleContext bundlecontext;
100 public shutdownHandler(BundleContext ctxt) {
101 this.bundlecontext = ctxt;
107 this.bundlecontext.getBundle(0).stop();
108 log.debug("shutdown handler thread called");
109 } catch (BundleException e) {
110 log.debug("Bundle couldn't be stopped");
111 } catch (Exception e) {
112 log.debug("Unhandled exception");