X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmir%2Fproducer%2FScriptCallingProducerNode.java;h=75c51918f3622b324a20b19cd3b87f2083b89eef;hb=8b91e8d8bf4a31a88440a404e83238dcf32f8f4a;hp=61a1fb1c90a9951a52fb1cf2a551cbb1b4ddfd44;hpb=e42ecea2db8ff123f268edf48190d6b9b28fcedb;p=mir.git diff --git a/source/mir/producer/ScriptCallingProducerNode.java b/source/mir/producer/ScriptCallingProducerNode.java index 61a1fb1c..75c51918 100755 --- a/source/mir/producer/ScriptCallingProducerNode.java +++ b/source/mir/producer/ScriptCallingProducerNode.java @@ -1,37 +1,106 @@ -package mir.producer; - -import java.io.*; -import java.util.*; -import mir.util.*; - -// ML: needs to be tested! - -public class ScriptCallingProducerNode implements ProducerNode { - String scriptExpression; - - public ScriptCallingProducerNode(String aScriptExpression) { - scriptExpression = aScriptExpression; - } - - public void produce(Map aValueMap, String aVerb, PrintWriter aLogger) throws ProducerFailure { - String script; - Process process; - int returnValue; - - try { - script = ParameterExpander.expandExpression( aValueMap, scriptExpression ); - aLogger.println("Executing " + script + ":"); - - process = Runtime.getRuntime().exec(script); - returnValue = process.waitFor(); - aLogger.println("Terminated successfully, return value = " + returnValue + "."); - } - catch (Throwable e) { - throw new ProducerFailure("Executing script failed: " + e.getMessage(), e); - } - } - - public Set buildVerbSet() { - return new HashSet(); - } -} +/* + * Copyright (C) 2001, 2002 The Mir-coders group + * + * This file is part of Mir. + * + * Mir 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 2 of the License, or + * (at your option) any later version. + * + * Mir 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 Mir; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * In addition, as a special exception, The Mir-coders gives permission to link + * the code of this program with any library licensed under the Apache Software License, + * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library + * (or with modified versions of the above that use the same license as the above), + * and distribute linked combinations including the two. You must obey the + * GNU General Public License in all respects for all of the code used other than + * the above mentioned libraries. If you modify this file, you may extend this + * exception to your version of the file, but you are not obligated to do so. + * If you do not wish to do so, delete this exception statement from your version. + */ +package mir.producer; + +import java.util.Map; + +import mir.log.LoggerWrapper; +import mir.util.ParameterExpander; + +public class ScriptCallingProducerNode implements ProducerNode { + private String scriptExpression; + private String maxDurationExpression; + + public ScriptCallingProducerNode(String aScriptExpression, String aMaxDurationExpression) { + scriptExpression = aScriptExpression; + maxDurationExpression = aMaxDurationExpression; + } + + public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger) throws ProducerFailure { + String script; + long maxDuration; + Process process; + int returnValue; + + try { + script = ParameterExpander.expandExpression(aValueMap, scriptExpression); + maxDuration = ParameterExpander.evaluateIntegerExpressionWithDefault(aValueMap, maxDurationExpression, 0); + + ProcessRunner runner = new ProcessRunner(Runtime.getRuntime().exec(script)); + runner.start(); + synchronized (runner) { + runner.wait(maxDuration); + } + runner.interrupt(); + + if (runner.getFinished()) { + aLogger.info(script + " terminated successfully, return value = " + runner.getReturnValue() + "."); + } + else { + aLogger.info(script + " interrupted prematurely after " + maxDuration + "ms."); + } + } + catch (Throwable e) { + aLogger.error(scriptExpression + " failed to execute: " + e.getMessage()); + } + } + + private static class ProcessRunner extends Thread { + private Process process; + private boolean finished = false; + private int returnValue = 0; + + public ProcessRunner(Process aProcess) { + process = aProcess; + } + + public boolean getFinished() { + return finished; + } + + public int getReturnValue() { + return returnValue; + } + + public void run() { + try { + returnValue = process.waitFor(); + finished = true; + synchronized (this) { + this.notify(); + } + } + catch (InterruptedException e) { + process.destroy(); + } + } + } + +}