3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.application.process;
22 import org.sonar.application.FileSystem;
23 import org.sonar.application.Scheduler;
24 import org.sonar.application.config.AppSettings;
25 import org.sonar.process.DefaultProcessCommands;
26 import org.sonar.process.ProcessCommands;
27 import org.sonar.process.ProcessId;
28 import org.sonar.process.ProcessProperties;
30 public class StopRequestWatcherImpl extends Thread implements StopRequestWatcher {
32 private static final long DEFAULT_WATCHER_DELAY_MS = 500L;
34 private final ProcessCommands commands;
35 private final Scheduler scheduler;
36 private final AppSettings settings;
37 private long delayMs = DEFAULT_WATCHER_DELAY_MS;
39 StopRequestWatcherImpl(AppSettings settings, Scheduler scheduler, ProcessCommands commands) {
40 super("StopRequestWatcherImpl");
41 this.settings = settings;
42 this.commands = commands;
43 this.scheduler = scheduler;
45 // safeguard, do not block the JVM if thread is not interrupted
46 // (method stopWatching() never called).
50 public static StopRequestWatcherImpl create(AppSettings settings, Scheduler scheduler, FileSystem fs) {
51 DefaultProcessCommands commands = DefaultProcessCommands.secondary(fs.getTempDir(), ProcessId.APP.getIpcIndex());
52 return new StopRequestWatcherImpl(settings, scheduler, commands);
59 void setDelayMs(long delayMs) {
60 this.delayMs = delayMs;
67 if (commands.askedForStop()) {
68 scheduler.terminate();
71 Thread.sleep(delayMs);
73 } catch (InterruptedException e) {
74 Thread.currentThread().interrupt();
75 // stop watching the commands
80 public void startWatching() {
81 if (settings.getProps().valueAsBoolean(ProcessProperties.ENABLE_STOP_COMMAND)) {
87 public void stopWatching() {
88 // does nothing is not started