2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with any library licensed under the Apache Software License,
\r
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
\r
23 * (or with modified versions of the above that use the same license as the above),
\r
24 * and distribute linked combinations including the two. You must obey the
\r
25 * GNU General Public License in all respects for all of the code used other than
\r
26 * the above mentioned libraries. If you modify this file, you may extend this
\r
27 * exception to your version of the file, but you are not obligated to do so.
\r
28 * If you do not wish to do so, delete this exception statement from your version.
\r
30 package mir.producer;
\r
32 import java.util.Map;
\r
34 import mir.log.LoggerWrapper;
\r
35 import mir.util.ParameterExpander;
\r
37 public class ScriptCallingProducerNode implements ProducerNode {
\r
38 private String scriptExpression;
\r
39 private String maxDurationExpression;
\r
41 public ScriptCallingProducerNode(String aScriptExpression, String aMaxDurationExpression) {
\r
42 scriptExpression = aScriptExpression;
\r
43 maxDurationExpression = aMaxDurationExpression;
\r
46 public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger) throws ProducerFailure {
\r
53 script = ParameterExpander.expandExpression(aValueMap, scriptExpression);
\r
54 maxDuration = ParameterExpander.evaluateIntegerExpressionWithDefault(aValueMap, maxDurationExpression, 0);
\r
56 ProcessRunner runner = new ProcessRunner(Runtime.getRuntime().exec(script));
\r
58 synchronized (runner) {
\r
59 runner.wait(maxDuration);
\r
63 if (runner.getFinished()) {
\r
64 aLogger.info(script + " terminated successfully, return value = " + runner.getReturnValue() + ".");
\r
67 aLogger.info(script + " interrupted prematurely after " + maxDuration + "ms.");
\r
70 catch (Throwable e) {
\r
71 aLogger.error(scriptExpression + " failed to execute: " + e.getMessage());
\r
75 private static class ProcessRunner extends Thread {
\r
76 private Process process;
\r
77 private boolean finished = false;
\r
78 private int returnValue = 0;
\r
80 public ProcessRunner(Process aProcess) {
\r
84 public boolean getFinished() {
\r
88 public int getReturnValue() {
\r
94 returnValue = process.waitFor();
\r
96 synchronized (this) {
\r
100 catch (InterruptedException e) {
\r