wf.frk.tilde.sblauncher.SauerbratenExecutor.java Source code

Java tutorial

Introduction

Here is the source code for wf.frk.tilde.sblauncher.SauerbratenExecutor.java

Source

package wf.frk.tilde.sblauncher;

import java.io.File;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.Scanner;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.PumpStreamHandler;

import wf.frk.tilde.sblauncher.plugins.Plugin;

/**
 * ##################
 * # Tilde Launcher #
 * ##################
 * 
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @author Riccardo B.
 * @version 0.1
 */
public class SauerbratenExecutor extends Thread {
    protected Scanner SAUER_OUTPUT;
    protected boolean STOP = false;

    public void startThread(String executable, String[] params, String working_dir)
            throws ExecuteException, IOException {

        setDaemon(true);
        startThread();

        CommandLine cmdLine = CommandLine.parse(executable);
        for (String p : params) {
            System.out.println(p);

            cmdLine.addArgument(p);
        }

        DefaultExecutor executor = new DefaultExecutor();

        PipedOutputStream output = new PipedOutputStream();
        PumpStreamHandler streamHandler = new PumpStreamHandler(output, System.err);
        SAUER_OUTPUT = new Scanner(new PipedInputStream(output));

        executor.setStreamHandler(streamHandler);
        executor.setWorkingDirectory(new File(working_dir));
        executor.execute(cmdLine);

    }

    @Override
    public void start() {
        STOP = false;
        super.start();
    }

    public void startThread() {
        start();
    }

    public void stopThread() {
        STOP = true;
    }

    @Override
    public void run() {
        while (!STOP) {
            if (SAUER_OUTPUT != null && SAUER_OUTPUT.hasNextLine()) {
                String line = SAUER_OUTPUT.nextLine();
                boolean r = false;
                for (Plugin p : Plugins.getPlugins().values()) {
                    r = p.onSauerOutput(line, r);
                    //               if(r)break;
                }
                if (!r)
                    System.out.println(line);
            } else {
                try {
                    Thread.sleep(60);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}