# HG changeset patch # User Mario Torre # Date 1302200588 -7200 # Node ID 46e4c0258c33beaf17d97f8dd052d5973d693738 # Parent 1f7a868038de15a3a1fe806d076effee21ada21f # This patch file was generated by NetBeans IDE # Following Index: paths are relative to: /home/giulio/Documenti/Progetti/ika-main # This patch can be applied using context Tools: Patch action on respective folder. # It uses platform neutral UTF-8 encoding and \n newlines. # Above lines and this line are ignored by the patching process. diff -r 1f7a868038de -r 46e4c0258c33 main/java/org/icedrobot/ika/runtime/RuntimeCommand.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/java/org/icedrobot/ika/runtime/RuntimeCommand.java Thu Apr 07 20:23:08 2011 +0200 @@ -0,0 +1,144 @@ +/* + * 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 . + */ + +package org.icedrobot.ika.runtime; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; +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 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( Arrays.asList(commandLine) ); + + StatusKeeper.getInstance().registerWorker(taskName ); + } + + + /** + * Processes 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 process(String output) { + return new DefaultWorkerStatus(0f, output); + } + + /** + * Starts the external process + * @param path Working directory + * @return Newly started process. + */ + protected Process runCommand(File path) { + return IkaRuntime.exec( path, commandLine.toArray(typeSample) ); + } + + /** + * Starts the external process + * @param path Working directory + */ + public void execute(File path) { + proc = runCommand(path); + + BufferedReader input = new BufferedReader( + new InputStreamReader( + proc.getInputStream() )); + + String line; + WorkerStatus status; + + try { + + while ((line = input.readLine()) != null) { + status = process(line); + StatusKeeper.getInstance().reportStatus(taskName, status); + } + + } catch (IOException ex) { + Logger.getLogger( RuntimeCommand.class.getName() ).log( + Level.SEVERE, taskName + ": error on the input stream", ex ); + } + + do { + try { + + /* + * If the process terminates abnormally, leave its status there + * so that it can be clearly seen. + */ + if (proc.waitFor() == 0) { + StatusKeeper.getInstance().deregisterWorker(taskName); + } + + proc = null; + + } catch (InterruptedException ex) { + Logger.getLogger( RuntimeCommand.class.getName() ) + .log( Level.INFO, + "Thread was interrupted while waiting " + + "for process termination", ex); + } + + } while (proc != null); + + } + + @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(); + } +}