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 java.io.Closeable;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.concurrent.TimeUnit;
26 import javax.annotation.Nullable;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.sonar.process.ProcessId;
31 abstract class AbstractProcessMonitor implements ProcessMonitor {
33 private static final Logger LOG = LoggerFactory.getLogger(AbstractProcessMonitor.class);
34 private static final int EXPECTED_EXIT_VALUE = 0;
36 protected final Process process;
37 private final ProcessId processId;
39 protected AbstractProcessMonitor(Process process, ProcessId processId) {
40 this.process = process;
41 this.processId = processId;
44 public InputStream getInputStream() {
45 return process.getInputStream();
48 public InputStream getErrorStream() {
49 return process.getErrorStream();
52 public void closeStreams() {
53 closeQuietly(process.getInputStream());
54 closeQuietly(process.getOutputStream());
55 closeQuietly(process.getErrorStream());
58 private static void closeQuietly(@Nullable Closeable closeable) {
60 if (closeable != null) {
63 } catch (IOException ignored) {
68 public boolean isAlive() {
69 return process.isAlive();
72 public void destroyForcibly() {
73 process.destroyForcibly();
76 public void waitFor() throws InterruptedException {
77 int exitValue = process.waitFor();
78 if (exitValue != EXPECTED_EXIT_VALUE) {
79 LOG.warn("Process exited with exit value [{}]: {}", processId.getKey(), exitValue);
81 LOG.debug("Process exited with exit value [{}]: {}", processId.getKey(), exitValue);
85 public void waitFor(long timeout, TimeUnit unit) throws InterruptedException {
86 process.waitFor(timeout, unit);