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.ServiceRegistration;
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 LogListenerImpl listener = null;
26 private Logger log = null;
29 public void start(BundleContext context) {
30 // Lets trigger the resolution of the slf4j logger factory
31 ILoggerFactory f = LoggerFactory.getILoggerFactory();
33 // Now retrieve a logger for the bridge
35 .getLogger("org.opendaylight.controller.logging.bridge.OSGI2SLF4J");
37 if (this.log != null) {
38 this.listener = new LogListenerImpl(log);
40 ServiceReference service = null;
41 service = context.getServiceReference(LogReaderService.class
43 if (service != null) {
44 LogReaderService reader = (LogReaderService) context
47 this.log.error("Cannot register the LogListener because "
48 + "cannot retrieve LogReaderService");
50 reader.addLogListener(this.listener);
51 // Now lets walk all the exiting messages
52 Enumeration<LogEntry> entries = reader.getLog();
53 if (entries != null) {
54 while (entries.hasMoreElements()) {
55 LogEntry entry = (LogEntry) entries.nextElement();
56 this.listener.logged(entry);
61 * Install the default exception handler so that the uncaught
62 * exceptions are handled by our customized handler. This new
63 * handler will display the exceptions to OSGI console as well
66 Thread.setDefaultUncaughtExceptionHandler(new org.opendaylight.
67 controller.logging.bridge.internal.UncaughtExceptionHandler());
70 * Install the Shutdown handler. This will intercept SIGTERM signal and
71 * close the system bundle. This allows for a graceful closing of OSGI
75 Runtime.getRuntime().addShutdownHook(new shutdownHandler(context));
77 this.log.error("Cannot register the LogListener because "
78 + "cannot retrieve LogReaderService");
82 .println("Could not initialize the logging bridge subsytem");
87 public void stop(BundleContext context) {
88 ServiceReference service = null;
89 service = context.getServiceReference(LogReaderService.class.getName());
90 if (service != null) {
91 LogReaderService reader = (LogReaderService) service;
92 reader.removeLogListener(this.listener);
99 private class shutdownHandler extends Thread {
100 BundleContext bundlecontext;
101 public shutdownHandler(BundleContext ctxt) {
102 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");