com.github.rwhogg.git_vcr.review.ProcessBasedReviewTool.java Source code

Java tutorial

Introduction

Here is the source code for com.github.rwhogg.git_vcr.review.ProcessBasedReviewTool.java

Source

/*
ProcessBasedReviewTool.java: Review tool that invokes an external process
    
Copyright  2015 Bob W. Hogg. All Rights Reserved.
       
This file is part of Git-VCR.
       
Git-VCR 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 3 of the License, or
(at your option) any later version.
       
Git-VCR 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 Git-VCR.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.github.rwhogg.git_vcr.review;

import java.io.*;
import java.util.*;
import org.apache.commons.exec.*;

/**
 * Abstract class representing a review tool that uses an external process.
 * Any class deriving from this class automatically recognizes the config
 * option "args", which allows the setting of arbitrary command-line flags.
 */
public abstract class ProcessBasedReviewTool extends AbstractReviewTool {
    /**
     * Retrieve the name of the executable associated with this review tool
     * @return The name of the executable file we will use
     */
    public abstract String getExecutableName();

    /**
     * Runs the command string specified by commandLine, then returns the output.
     * Note that any command-line arguments from the configuration are automatically added.
     * @param commands The rest of the command line, excluding any arguments
     * @return The output of the command
     */
    protected List<String> runProcess(String commands) throws ExecuteException, IOException {
        DefaultExecutor executor = new DefaultExecutor();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        executor.setStreamHandler(streamHandler);
        executor.setExitValues(null);
        String arguments = configuration.getString("args", "");
        executor.execute(CommandLine.parse(getExecutableName() + " " + arguments + " " + commands));
        return Arrays.asList(outputStream.toString("UTF-8").split("\n"));
    }
}