view src/main/java/org/icedrobot/ika/runtime/RuntimeCommand.java @ 29:3b0d6002605a

Add patch to output commands names. Contributed by Giulio Franco.
author Mario Torre <neugens.limasoftware@gmail.com>
date Fri, 15 Apr 2011 17:13:03 +0200
parents
children
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.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import org.icedrobot.ika.output.DefaultWorkerStatus;
import org.icedrobot.ika.output.StatusKeeper;
import org.icedrobot.ika.output.WorkerStatus;


/**
 * Executable command that will report its status to the StatusKeeper.
 */
public class RuntimeCommand {

    private final String[] typeSample = new String[0];


    private Process proc;
    protected final String taskName;
    protected List<String> commandLine;


    /**
     * Initializes a RuntimeCommand
     * @param name          Command name. No other active commands must exist.
     * @param commandLine   Command line used to execute the command.
     */
    public RuntimeCommand(String name, String... commandLine) { 
        taskName = name;
        this.commandLine = new LinkedList<String>(Arrays.asList(commandLine));

        StatusKeeper.getInstance().registerWorker(taskName);
    }


    /**
     * Parses a line of output provided by the external process.
     * @param output    Output of the external process
     * @return          WorkerStatus expressing the same information as output.
     */
    protected WorkerStatus parse(String output) {
        //Remove escape sequences to avoid troubles
        output = output.replaceAll("\33\\[=?\\d*(;\\d*)*\\w", "").trim();
        return (output.isEmpty()) ? null
                                :new DefaultWorkerStatus(taskName, 0f, output);
    }

    /**
     * Processes a line of output provided by the external process.
     */
    protected void process(String output) {
        WorkerStatus status = parse(output);
        if (status != null) {
            StatusKeeper.getInstance().reportStatus(status);
        }
    }

    public String[] getCommandLine() {
        return commandLine.toArray(typeSample);
    }

    public String getName() {
        return taskName;
    }

    @Override
    public String toString() {
        StringBuilder bdr = new StringBuilder(getClass().getSimpleName());

        bdr.append('(');

        for (String s : commandLine) {
            bdr.append(s);
            bdr.append(' ');
        }

        bdr.setCharAt(bdr.length() - 1, ')');

        return bdr.toString();
    }
}