Revert "Checkstyle enforcer"
[controller.git] / opendaylight / logging / bridge / src / main / java / org / opendaylight / controller / logging / bridge / internal / Activator.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
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
8  */
9
10 package org.opendaylight.controller.logging.bridge.internal;
11
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;
23
24 public class Activator implements BundleActivator {
25     private LogListenerImpl listener = null;
26     private Logger log = null;
27
28     @Override
29     public void start(BundleContext context) {
30         // Lets trigger the resolution of the slf4j logger factory
31         ILoggerFactory f = LoggerFactory.getILoggerFactory();
32
33         // Now retrieve a logger for the bridge
34         log = f
35                 .getLogger("org.opendaylight.controller.logging.bridge.OSGI2SLF4J");
36
37         if (this.log != null) {
38             this.listener = new LogListenerImpl(log);
39
40             ServiceReference service = null;
41             service = context.getServiceReference(LogReaderService.class
42                     .getName());
43             if (service != null) {
44                 LogReaderService reader = (LogReaderService) context
45                         .getService(service);
46                 if (reader == null) {
47                     this.log.error("Cannot register the LogListener because "
48                             + "cannot retrieve LogReaderService");
49                 }
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);
57                     }
58                 }
59                 
60                 /*
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
64                  * as log to file.
65                  */
66                 Thread.setDefaultUncaughtExceptionHandler(new org.opendaylight.
67                         controller.logging.bridge.internal.UncaughtExceptionHandler());
68                 
69                 /*
70                  * Install the Shutdown handler. This will intercept SIGTERM signal and
71                  * close the system bundle. This allows for a graceful  closing of OSGI
72                  * framework.
73                  */
74                 
75                 Runtime.getRuntime().addShutdownHook(new shutdownHandler(context));
76             } else {
77                 this.log.error("Cannot register the LogListener because "
78                         + "cannot retrieve LogReaderService");
79             }
80         } else {
81             System.err
82                     .println("Could not initialize the logging bridge subsytem");
83         }
84     }
85
86     @Override
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);
93         }
94
95         this.listener = null;
96         this.log = null;
97     }
98     
99     private class shutdownHandler extends Thread {
100         BundleContext bundlecontext;
101         public shutdownHandler(BundleContext ctxt) {
102                 this.bundlecontext = ctxt;
103         }
104         
105         public void run () {
106             try {
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             }
112         }   
113     }
114
115 }