2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.yang.shutdown.impl;
10 import com.google.common.base.Optional;
11 import org.opendaylight.controller.config.shutdown.ShutdownService;
12 import org.osgi.framework.Bundle;
13 import org.osgi.framework.BundleException;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
17 import java.lang.management.ManagementFactory;
18 import java.lang.management.ThreadInfo;
20 public class ShutdownServiceImpl implements ShutdownService, AutoCloseable {
21 private final ShutdownService impl;
22 private final ShutdownRuntimeRegistration registration;
24 public ShutdownServiceImpl(String secret, Bundle systemBundle,
25 ShutdownRuntimeRegistrator rootRuntimeBeanRegistratorWrapper) {
27 throw new IllegalArgumentException("Secret cannot be null");
29 impl = new Impl(secret, systemBundle);
30 registration = rootRuntimeBeanRegistratorWrapper.register(new MXBeanImpl(impl));
34 public void shutdown(String inputSecret, Long maxWaitTime, Optional<String> reason) {
35 impl.shutdown(inputSecret, maxWaitTime, reason);
44 class Impl implements ShutdownService {
45 private static final Logger logger = LoggerFactory.getLogger(Impl.class);
46 private final String secret;
47 private final Bundle systemBundle;
49 Impl(String secret, Bundle systemBundle) {
51 this.systemBundle = systemBundle;
55 public void shutdown(String inputSecret, Long maxWaitTime, Optional<String> reason) {
56 logger.warn("Shutdown issued with secret {} and reason {}", inputSecret, reason);
58 Thread.sleep(1000); // prevent brute force attack
59 } catch (InterruptedException e) {
60 Thread.currentThread().interrupt();
61 logger.warn("Shutdown process interrupted", e);
63 if (this.secret.equals(inputSecret)) {
64 logger.info("Server is shutting down");
67 Thread stopSystemBundleThread = new StopSystemBundleThread(systemBundle);
68 stopSystemBundleThread.start();
69 if (maxWaitTime != null && maxWaitTime > 0) {
70 Thread systemExitThread = new CallSystemExitThread(maxWaitTime);
71 logger.debug("Scheduling {}", systemExitThread);
72 systemExitThread.start();
76 logger.warn("Unauthorized attempt to shut down server");
77 throw new IllegalArgumentException("Invalid secret");
83 class StopSystemBundleThread extends Thread {
84 private static final Logger logger = LoggerFactory.getLogger(StopSystemBundleThread.class);
85 private final Bundle systemBundle;
87 StopSystemBundleThread(Bundle systemBundle) {
88 super("stop-system-bundle");
89 this.systemBundle = systemBundle;
95 // wait so that JMX response is received
97 logger.debug("Stopping system bundle");
99 } catch (BundleException e) {
100 logger.warn("Can not stop OSGi server", e);
101 } catch (InterruptedException e) {
102 logger.warn("Shutdown process interrupted", e);
107 class CallSystemExitThread extends Thread {
108 private static final Logger logger = LoggerFactory.getLogger(CallSystemExitThread.class);
109 private final long maxWaitTime;
110 CallSystemExitThread(long maxWaitTime) {
111 super("call-system-exit-daemon");
113 if (maxWaitTime <= 0){
114 throw new IllegalArgumentException("Cannot schedule to zero or negative time:" + maxWaitTime);
116 this.maxWaitTime = maxWaitTime;
120 public String toString() {
121 return "CallSystemExitThread{" +
122 "maxWaitTime=" + maxWaitTime +
129 // wait specified time
130 Thread.sleep(maxWaitTime);
131 logger.error("Since some threads are still running, server is going to shut down via System.exit(1) !");
133 ThreadInfo[] threads = ManagementFactory.getThreadMXBean().dumpAllThreads(true, true);
134 StringBuffer sb = new StringBuffer();
135 for(ThreadInfo info : threads) {
139 logger.warn("Thread dump:{}", sb);
141 } catch (InterruptedException e) {
142 logger.warn("Interrupted, not going to call System.exit(1)");
148 class MXBeanImpl implements ShutdownRuntimeMXBean {
149 private final ShutdownService impl;
151 MXBeanImpl(ShutdownService impl) {
156 public void shutdown(String inputSecret, Long maxWaitTime, String nullableReason) {
157 Optional<String> optionalReason;
158 if (nullableReason == null) {
159 optionalReason = Optional.absent();
161 optionalReason = Optional.of(nullableReason);
163 impl.shutdown(inputSecret, maxWaitTime, optionalReason);