whoops
[mir.git] / source / mir / producer / ScriptCallingProducerNode.java
1 /*\r
2  * Copyright (C) 2001, 2002 The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\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
10  *\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
15  *\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
19  *\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
29  */\r
30 package mir.producer;\r
31 \r
32 import java.util.Map;\r
33 \r
34 import mir.log.LoggerWrapper;\r
35 import mir.util.ParameterExpander;\r
36 \r
37 public class ScriptCallingProducerNode implements ProducerNode  {\r
38   private String scriptExpression;\r
39   private String maxDurationExpression;\r
40 \r
41   public ScriptCallingProducerNode(String aScriptExpression, String aMaxDurationExpression) {\r
42     scriptExpression = aScriptExpression;\r
43     maxDurationExpression = aMaxDurationExpression;\r
44   }\r
45 \r
46   public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger) throws ProducerFailure {\r
47     String script;\r
48     long maxDuration;\r
49     Process process;\r
50     int returnValue;\r
51 \r
52     try {\r
53       script = ParameterExpander.expandExpression(aValueMap, scriptExpression);\r
54       maxDuration = ParameterExpander.evaluateIntegerExpressionWithDefault(aValueMap, maxDurationExpression, 0);\r
55 \r
56       ProcessRunner runner = new ProcessRunner(Runtime.getRuntime().exec(script));\r
57       runner.start();\r
58       synchronized (runner) {\r
59         runner.wait(maxDuration);\r
60       }\r
61       runner.interrupt();\r
62 \r
63       if (runner.getFinished()) {\r
64         aLogger.info(script + " terminated successfully, return value = " + runner.getReturnValue() + ".");\r
65       }\r
66       else {\r
67         aLogger.info(script + " interrupted prematurely after " + maxDuration + "ms.");\r
68       }\r
69     }\r
70     catch (Throwable e) {\r
71       aLogger.error(scriptExpression + " failed to execute: " + e.getMessage());\r
72     }\r
73   }\r
74 \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
79 \r
80     public ProcessRunner(Process aProcess) {\r
81       process = aProcess;\r
82     }\r
83 \r
84     public boolean getFinished() {\r
85       return finished;\r
86     }\r
87 \r
88     public int getReturnValue() {\r
89       return returnValue;\r
90     }\r
91 \r
92     public void run() {\r
93       try {\r
94         returnValue = process.waitFor();\r
95         finished = true;\r
96         synchronized (this) {\r
97           this.notify();\r
98         }\r
99       }\r
100       catch (InterruptedException e) {\r
101         process.destroy();\r
102       }\r
103     }\r
104   }\r
105 \r
106 }\r