changeset 92:7302985efe99

Log output of external processes Review thread: http://icedtea.classpath.org/pipermail/thermostat/2012-February/000123.html
author Jon VanAlten <jon.vanalten@redhat.com>
date Wed, 29 Feb 2012 10:42:36 -0500
parents fdcb4ee647cf
children 9a0db610e395
files agent/src/main/java/com/redhat/thermostat/agent/Main.java client/src/main/java/com/redhat/thermostat/client/MongoConnection.java common/src/main/java/com/redhat/thermostat/common/utils/LoggedExternalProcess.java
diffstat 3 files changed, 93 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/agent/src/main/java/com/redhat/thermostat/agent/Main.java	Wed Feb 29 10:29:43 2012 -0500
+++ b/agent/src/main/java/com/redhat/thermostat/agent/Main.java	Wed Feb 29 10:42:36 2012 -0500
@@ -51,6 +51,7 @@
 import com.redhat.thermostat.common.LaunchException;
 import com.redhat.thermostat.common.storage.MongoStorage;
 import com.redhat.thermostat.common.storage.Storage;
+import com.redhat.thermostat.common.utils.LoggedExternalProcess;
 import com.redhat.thermostat.common.utils.LoggingUtils;
 
 public final class Main {
@@ -88,8 +89,7 @@
             try {
                 logger.fine("Starting private mongod instance.");
                 logger.finest("Mongo launch script at: " + mongoScript);
-                Process mongodStarter = Runtime.getRuntime().exec(mongoScript + " start");
-                int result = mongodStarter.waitFor();
+                int result = new LoggedExternalProcess(new String[] { mongoScript, "start" }).runAndReturnResult();
                 if (result != 0) {
                     logger.severe("Error starting local mongod instance.");
                     System.exit(Constants.EXIT_UNABLE_TO_CONNECT_TO_DATABASE);
@@ -143,8 +143,7 @@
         if (config.getLocalMode()) {
             logger.fine("Stopping private mongod instance.");
             try {
-                Process mongodStopper = Runtime.getRuntime().exec(mongoScript + " stop");
-                int result = mongodStopper.waitFor();
+                int result = new LoggedExternalProcess(new String[] { mongoScript, "stop" }).runAndReturnResult();
                 if (result != 0) {
                     logger.severe("Error stopping local mongod instance.");
                 }
--- a/client/src/main/java/com/redhat/thermostat/client/MongoConnection.java	Wed Feb 29 10:29:43 2012 -0500
+++ b/client/src/main/java/com/redhat/thermostat/client/MongoConnection.java	Wed Feb 29 10:42:36 2012 -0500
@@ -49,6 +49,7 @@
 import com.redhat.thermostat.common.Constants;
 import com.redhat.thermostat.common.NotImplementedException;
 import com.redhat.thermostat.common.storage.StorageConstants;
+import com.redhat.thermostat.common.utils.LoggedExternalProcess;
 import com.redhat.thermostat.common.utils.LoggingUtils;
 
 public class MongoConnection extends Connection {
@@ -111,8 +112,8 @@
     private void startLocalAgent() throws LocalAgentException {
         int status = 0;
         try {
-            String agentCommand = props.getProperty(Constants.CLIENT_PROPERTY_AGENT_LAUNCH_SCRIPT) + " --local";
-            localAgentProcess = Runtime.getRuntime().exec(agentCommand);
+            String agentScript = props.getProperty(Constants.CLIENT_PROPERTY_AGENT_LAUNCH_SCRIPT);
+            localAgentProcess = new LoggedExternalProcess(new String[] { agentScript, "--local" }).runAndReturnProcess();
             // Allow some time for things to get started.
             try {
                 // TODO provide some UI feedback here instead of just seeming dead.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/src/main/java/com/redhat/thermostat/common/utils/LoggedExternalProcess.java	Wed Feb 29 10:42:36 2012 -0500
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2012 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.common.utils;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.Arrays;
+import java.util.logging.Logger;
+
+public class LoggedExternalProcess extends Thread {
+
+    private static final Logger logger = LoggingUtils.getLogger(LoggedExternalProcess.class);
+    private BufferedReader reader;
+    private String[] commands;
+
+    public LoggedExternalProcess(String[] commands) {
+        this.commands = Arrays.copyOf(commands, commands.length);
+        setDaemon(true);
+    }
+
+    public int runAndReturnResult() throws IOException, InterruptedException {
+        Process p = runAndReturnProcess();
+        return p.waitFor();
+    }
+
+    public Process runAndReturnProcess() throws IOException {
+        ProcessBuilder b = new ProcessBuilder(commands);
+        b.redirectErrorStream(true);
+        Process p = b.start();
+        reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
+        this.start();
+        return p;
+    }
+
+    @Override
+    public void run() {
+        try {
+            String output = null;
+            while ((output = reader.readLine()) != null) {
+                logger.info(output);
+            }
+        } catch (IOException ex) {
+            logger.warning("Error reading external program output.");
+        } finally {
+            try {
+                reader.close();
+            } catch (IOException ex) {
+                logger.warning("Error closing stream from external program.");
+            }
+        }
+    }
+}
\ No newline at end of file