Merge "Bug 1029: Remove dead code: samples/clustersession"
[controller.git] / opendaylight / adsal / 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                 } else {
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);
61                         }
62                     }
63                 }
64
65                 /*
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
69                  * as log to file.
70                  */
71                 UncaughtExceptionHandler handler = DEFAULT_UNCAUGHT_EXCEPTION_POLICY;
72                 final String policy = context.getProperty(UNCAUGHT_EXCEPTION_POLICY_PROP);
73                 if (policy != null) {
74                     try {
75                         handler = UncaughtExceptionPolicy.valueOf(policy.toUpperCase());
76                     } catch (IllegalArgumentException ex) {
77                         log.warn("Invalid policy name \"{}\", defaulting to {}", policy, handler);
78                     }
79                 }
80                 log.trace("Setting uncaught exception policy to {}", handler);
81                 Thread.setDefaultUncaughtExceptionHandler(handler);
82
83                 /*
84                  * Install the Shutdown handler. This will intercept SIGTERM signal and
85                  * close the system bundle. This allows for a graceful  closing of OSGI
86                  * framework.
87                  */
88                 shutdownHandler = new ShutdownHandler(context);
89                 Runtime.getRuntime().addShutdownHook(shutdownHandler);
90             } else {
91                 this.log.error("Cannot register the LogListener because "
92                         + "cannot retrieve LogReaderService");
93             }
94         } else {
95             System.err
96                     .println("Could not initialize the logging bridge subsytem");
97         }
98     }
99
100     @Override
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);
107         }
108         if (this.shutdownHandler != null) {
109             Runtime.getRuntime().removeShutdownHook(this.shutdownHandler);
110         }
111         this.listener = null;
112         this.log = null;
113         this.shutdownHandler = null;
114     }
115
116     private class ShutdownHandler extends Thread {
117         BundleContext bundlecontext;
118         public ShutdownHandler(BundleContext ctxt) {
119                 this.bundlecontext = ctxt;
120         }
121
122         @Override
123         public void run () {
124             try {
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");
131             }
132         }
133     }
134
135 }