From 52067da7050106803a62f3445ce5589b63a0cbf2 Mon Sep 17 00:00:00 2001 From: Maurice Qureshi Date: Thu, 25 Apr 2013 10:13:09 -0700 Subject: [PATCH] Add the Shutdown Handler. It gets executed when OS sends SIGTERM signal to JVM Signed-off-by: Maurice Qureshi --- .../logging/bridge/internal/Activator.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/opendaylight/logging/bridge/src/main/java/org/opendaylight/controller/logging/bridge/internal/Activator.java b/opendaylight/logging/bridge/src/main/java/org/opendaylight/controller/logging/bridge/internal/Activator.java index d455a0ae20..6bafee0b2c 100644 --- a/opendaylight/logging/bridge/src/main/java/org/opendaylight/controller/logging/bridge/internal/Activator.java +++ b/opendaylight/logging/bridge/src/main/java/org/opendaylight/controller/logging/bridge/internal/Activator.java @@ -12,6 +12,7 @@ package org.opendaylight.controller.logging.bridge.internal; import org.osgi.service.log.LogEntry; import java.util.Enumeration; import org.osgi.framework.BundleContext; +import org.osgi.framework.BundleException; import org.osgi.framework.BundleActivator; import org.osgi.framework.ServiceRegistration; import org.osgi.framework.ServiceReference; @@ -64,6 +65,14 @@ public class Activator implements BundleActivator { */ Thread.setDefaultUncaughtExceptionHandler(new org.opendaylight. controller.logging.bridge.internal.UncaughtExceptionHandler()); + + /* + * Install the Shutdown handler. This will intercept SIGTERM signal and + * close the system bundle. This allows for a graceful closing of OSGI + * framework. + */ + + Runtime.getRuntime().addShutdownHook(new shutdownHandler(context)); } else { this.log.error("Cannot register the LogListener because " + "cannot retrieve LogReaderService"); @@ -86,4 +95,21 @@ public class Activator implements BundleActivator { this.listener = null; this.log = null; } + + private class shutdownHandler extends Thread { + BundleContext bundlecontext; + public shutdownHandler(BundleContext ctxt) { + this.bundlecontext = ctxt; + } + + public void run () { + try { + this.bundlecontext.getBundle(0).stop(); + log.debug("shutdown handler thread called"); + } catch (BundleException e) { + log.debug("Bundle couldn't be stopped"); + } + } + } + } -- 2.36.6