view src/org/thermostat/qa2/framework/NativeProcess.java @ 170:0d34a379de82

- Added new framework (package org.thermosta.qa2.framework) - Removed hardcoded display in org.thermostat.qa.framework.GuiRobot class
author Zdenek Zambersky <zzambers@redhat.com>
date Tue, 10 Mar 2015 13:38:36 +0100
parents
children a74aa91ab44c
line wrap: on
line source

/*
 ThermostatQA - test framework for Thermostat Monitoring Tool

 Copyright 2015 Red Hat, Inc.

 This file is part of ThermostatQA

 ThermostatQA is distributed under the GNU General Public License,
 version 2 or any later version (with a special exception described
 below, commonly known as the "Classpath Exception").

 A copy of GNU General Public License (GPL) is included in this
 distribution, in the file COPYING.

 Linking ThermostatQA code with other modules is making a combined work
 based on ThermostatQA.  Thus, the terms and conditions of the GPL
 cover the whole combination.

 As a special exception, the copyright holders of ThermostatQA 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 ThermostatQA code.  If you modify ThermostatQA, you may
 extend this exception to your version of the software, but you are
 not obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version.
 */
package org.thermostat.qa2.framework;

import org.thermostat.qa2.framework.streamprocessors.StreamProcessor;
import org.thermostat.qa2.framework.streamprocessors.BufferingStreamProcessor;
import org.thermostat.qa2.framework.streamprocessors.LoggingStreamProcessor;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Zdeněk Žamberský
 */
public class NativeProcess {

    public static final int MODE_NONE = 0;
    public static final int MODE_CLOSE = 1;
    public static final int MODE_DISCARD = 2;
    public static final int MODE_LOG = 4;
    public static final int MODE_BUFFER = 8;
    public static final int MODE_LOG_AND_BUFFER = 4 | 8;

    ProcessBuilder processBuilder;
    public java.lang.Process process;
    String[] commands;

    public Thread stdoutProcessingThread = null;
    public int stdoutMode = MODE_LOG;
    public StreamProcessor stdoutStreamProcessor;

    public Thread stderrProcessingThread = null;
    public int stderrMode = MODE_LOG;
    public StreamProcessor stderrStreamProcessor;

    public PrintStream output = System.out;
    SimpleLogger logger = new SimpleLogger(output);

    String label = null;

    public NativeProcess(String command) {
        commands = new String[]{command};
        processBuilder = new ProcessBuilder(command);
    }

    public NativeProcess(String... command) {
        commands = command;
        processBuilder = new ProcessBuilder(command);
    }

    public void setWorkingDirectory(String s) {
        processBuilder.directory(new File(s));
    }

    public void setWorkingDirectory(File f) {
        processBuilder.directory(f);
        if (label != null) {
            logger.setPrefix(label + " (workdir setup)");
            logger.log(f.getPath());
        }
    }

    public void setEnvironmentVariable(String name, String value) {
        processBuilder.environment().put(name, value);
        if (label != null) {
            logger.setPrefix(label + " (environment add)");
            logger.log(name + "=" + value);
        }
    }

    public void setLabel(String label) {
        this.label = label;

    }

    public void start() throws IOException {
        if (label == null) {
            setLabel(getName());
        } else {
            logger.setPrefix(label);
        }
        logger.log(commands);
        process = processBuilder.start();
        startLogging();
    }

    public void destroy() {
        try {
            process.getOutputStream().close();
//            process.getInputStream().close();
//            process.getErrorStream().close();
        } catch (IOException ex) {
            Logger.getLogger(NativeProcess.class.getName()).log(Level.SEVERE, null, ex);
        }
        process.destroy();
    }

    public String getName() {
        return commands[0];
    }

    private StreamProcessor initStreamProcessor(InputStream is, PrintStream out, String postfix, int mode, InputStream comparator) throws IOException {
        StreamProcessor sp;
        switch (mode) {
            case MODE_NONE:
                sp = null;
                break;
            case MODE_CLOSE:
                sp = null;
                is.close();
                break;
            case MODE_DISCARD:
                sp = new StreamProcessor(is);
                break;
            case MODE_LOG:
                sp = new LoggingStreamProcessor(is, new SimpleLogger(out, label + postfix));
                break;
            case MODE_BUFFER:
                sp = new BufferingStreamProcessor(is, null, false);
                break;
            case MODE_LOG_AND_BUFFER:
                sp = new BufferingStreamProcessor(is, new SimpleLogger(out, label + postfix), true);
                break;
            default:
                throw new IllegalArgumentException("Unknown stream processing mode");
        }
        return sp;
    }

    private void startLogging() throws IOException {

        stdoutStreamProcessor = initStreamProcessor(process.getInputStream(), System.out, " (stdout)", stdoutMode, null);
        if (stdoutStreamProcessor != null) {
            stdoutProcessingThread = new Thread(stdoutStreamProcessor);
            stdoutProcessingThread.setDaemon(true);
            stdoutProcessingThread.start();
        }

        stderrStreamProcessor = initStreamProcessor(process.getErrorStream(), System.out, " (stderr)", stderrMode, null);
        if (stderrStreamProcessor != null) {
            stderrProcessingThread = new Thread(stderrStreamProcessor);
            stderrProcessingThread.setDaemon(true);
            stderrProcessingThread.start();
        }

    }

    public void setStdOutMode(int mode) {
        stdoutMode = mode;
    }

    public void setStdErrMode(int mode) {
        stderrMode = mode;
    }

    public StreamProcessor getStdoutProcessor() {
        return stdoutStreamProcessor;
    }

    public List<String> getStdoutLines() throws IOException {
        return ((BufferingStreamProcessor) getStdoutProcessor()).getLineList();
    }

    public StreamProcessor getStderrProcessor() {
        return stderrStreamProcessor;
    }

    public List<String> getStderrLines() throws IOException {
        return ((BufferingStreamProcessor) getStderrProcessor()).getLineList();
    }

    public int waitForRaw() throws InterruptedException {
        int exitCode;
        for (;;) {
            try {
                exitCode = process.waitFor();
            } catch (InterruptedException e) {
                continue;
            }
            break;
        }
        if (stdoutProcessingThread != null) {
            stdoutProcessingThread.join();
        }
        if (stderrProcessingThread != null) {
            stderrProcessingThread.join();
        }
        logger.setPrefix(label + " (exit code)");
        logger.log(exitCode);
        return exitCode;
    }

    public void waitFor() throws Exception {
        int exitCode = waitForRaw();
        if (exitCode != 0) {
            throw new Exception("non zero exit code: " + exitCode);
        }
    }

}