Fixed inappropriate uses of log level INFO
[controller.git] / opendaylight / logging / bridge / src / main / java / org / opendaylight / controller / logging / bridge / internal / Activator.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.logging.bridge.internal;
9
10 import org.osgi.service.log.LogEntry;
11
12 import java.lang.Thread.UncaughtExceptionHandler;
13 import java.util.Enumeration;
14
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;
23
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;
27
28     private LogListenerImpl listener = null;
29     private ShutdownHandler shutdownHandler = null;
30     private Logger log = null;
31
32     @Override
33     public void start(BundleContext context) {
34         // Lets trigger the resolution of the slf4j logger factory
35         ILoggerFactory f = LoggerFactory.getILoggerFactory();
36
37         // Now retrieve a logger for the bridge
38         log = f
39                 .getLogger("org.opendaylight.controller.logging.bridge.OSGI2SLF4J");
40
41         if (this.log != null) {
42             this.listener = new LogListenerImpl(log);
43
44             ServiceReference service = null;
45             service = context.getServiceReference(LogReaderService.class
46                     .getName());
47             if (service != null) {
48                 LogReaderService reader = (LogReaderService) context
49                         .getService(service);
50                 if (reader == null) {
51                     this.log.error("Cannot register the LogListener because "
52                             + "cannot retrieve LogReaderService");
53                 }
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);
61                     }
62                 }
63
64                 /*
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
68                  * as log to file.
69                  */
70                 UncaughtExceptionHandler handler = DEFAULT_UNCAUGHT_EXCEPTION_POLICY;
71                 final String policy = context.getProperty(UNCAUGHT_EXCEPTION_POLICY_PROP);
72                 if (policy != null) {
73                     try {
74                         handler = UncaughtExceptionPolicy.valueOf(policy.toUpperCase());
75                     } catch (IllegalArgumentException ex) {
76                         log.warn("Invalid policy name \"{}\", defaulting to {}", policy, handler);
77                     }
78                 }
79                 log.trace("Setting uncaught exception policy to {}", handler);
80                 Thread.setDefaultUncaughtExceptionHandler(handler);
81
82                 /*
83                  * Install the Shutdown handler. This will intercept SIGTERM signal and
84                  * close the system bundle. This allows for a graceful  closing of OSGI
85                  * framework.
86                  */
87                 shutdownHandler = new ShutdownHandler(context);
88                 Runtime.getRuntime().addShutdownHook(shutdownHandler);
89             } else {
90                 this.log.error("Cannot register the LogListener because "
91                         + "cannot retrieve LogReaderService");
92             }
93         } else {
94             System.err
95                     .println("Could not initialize the logging bridge subsytem");
96         }
97     }
98
99     @Override
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);
106         }
107         if (this.shutdownHandler != null) {
108             Runtime.getRuntime().removeShutdownHook(this.shutdownHandler);
109         }
110         this.listener = null;
111         this.log = null;
112         this.shutdownHandler = null;
113     }
114
115     private class ShutdownHandler extends Thread {
116         BundleContext bundlecontext;
117         public ShutdownHandler(BundleContext ctxt) {
118                 this.bundlecontext = ctxt;
119         }
120
121         @Override
122         public void run () {
123             try {
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");
130             }
131         }
132     }
133
134 }