Mercurial > hg > icedrobot > ika
view main/java/org/icedrobot/ika/runtime/RuntimeCommand.java @ 24:46e4c0258c33
# 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.
author | Mario Torre <neugens.limasoftware@gmail.com> |
---|---|
date | Thu, 07 Apr 2011 20:23:08 +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.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<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 ); } /** * 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(); } }