view src/main/java/org/icedrobot/ika/runtime/IkaRuntime.java @ 14:8a417ad3e271

Sanitise output. Contributed by Giulio Franco.
author Mario Torre <neugens.limasoftware@gmail.com>
date Fri, 01 Apr 2011 00:37:47 +0200
parents 5fa4fe467471
children 1366f29ac6fa
line wrap: on
line source

/*
 * IKA - IcedRobot Kiosk Application
 * Copyright (C) 2011  IcedRobot team
 *
 * 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/>.
 */
package org.icedrobot.ika.runtime;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.icedrobot.ika.plugins.IkaPluginException;

public class IkaRuntime {

    /**
     * Executes the given command on the passed path.
     */
    public static void exec(File path, String... command) {
        try {
            System.err.println("command: " + Arrays.toString(command) +
                               ", path: " + path);

            ProcessBuilder pb = new ProcessBuilder(command);
            pb.redirectErrorStream(true);
            pb.directory(path);
            Process process = pb.start();

            InputStream is = process.getInputStream();
            InputStreamReader reader = new InputStreamReader(is);

            BufferedReader br = new BufferedReader(reader);

            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }

        } catch (IOException ex) {
            Logger.getLogger(IkaRuntime.class.getName()).
                log(Level.SEVERE, "cannot execute: " + Arrays.toString(command), ex);
            throw new IkaPluginException("cannot execute: " + Arrays.toString(command), ex);
        }
    }
}