Invalid cast results in HTTP 500 error returned by the Neutron REST APIs.
[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.ServiceReference;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import org.slf4j.ILoggerFactory;
21 import org.osgi.service.log.LogReaderService;
22
23 public class Activator implements BundleActivator {
24     private LogListenerImpl listener = null;
25     private Logger log = null;
26
27     @Override
28     public void start(BundleContext context) {
29         // Lets trigger the resolution of the slf4j logger factory
30         ILoggerFactory f = LoggerFactory.getILoggerFactory();
31
32         // Now retrieve a logger for the bridge
33         log = f
34                 .getLogger("org.opendaylight.controller.logging.bridge.OSGI2SLF4J");
35
36         if (this.log != null) {
37             this.listener = new LogListenerImpl(log);
38
39             ServiceReference service = null;
40             service = context.getServiceReference(LogReaderService.class
41                     .getName());
42             if (service != null) {
43                 LogReaderService reader = (LogReaderService) context
44                         .getService(service);
45                 if (reader == null) {
46                     this.log.error("Cannot register the LogListener because "
47                             + "cannot retrieve LogReaderService");
48                 }
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);
56                     }
57                 }
58
59                 /*
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
63                  * as log to file.
64                  */
65                 Thread.setDefaultUncaughtExceptionHandler(new org.opendaylight.
66                         controller.logging.bridge.internal.UncaughtExceptionHandler());
67
68                 /*
69                  * Install the Shutdown handler. This will intercept SIGTERM signal and
70                  * close the system bundle. This allows for a graceful  closing of OSGI
71                  * framework.
72                  */
73
74                 Runtime.getRuntime().addShutdownHook(new shutdownHandler(context));
75             } else {
76                 this.log.error("Cannot register the LogListener because "
77                         + "cannot retrieve LogReaderService");
78             }
79         } else {
80             System.err
81                     .println("Could not initialize the logging bridge subsytem");
82         }
83     }
84
85     @Override
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);
92         }
93
94         this.listener = null;
95         this.log = null;
96     }
97
98     private class shutdownHandler extends Thread {
99         BundleContext bundlecontext;
100         public shutdownHandler(BundleContext ctxt) {
101                 this.bundlecontext = ctxt;
102         }
103
104         @Override
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             } catch (Exception e) {
112                 log.debug("Unhandled exception");
113             }
114         }
115     }
116
117 }