KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > script > ScriptJob


1 /*
2  * Copyright 2000-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package org.oddjob.script;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.oddjob.arooa.ArooaContext;
28 import org.oddjob.arooa.ArooaConstants;
29 import org.oddjob.arooa.ObjectFactory;
30 import org.oddjob.framework.SimpleJob;
31 import org.oddjob.util.IO;
32 import org.oddjob.util.OddjobConfigException;
33
34 /**
35  * @oddjob.description Execute a script. The script can be in any
36  * language supported by <a HREF="http://jakarta.apache.org/bsf/">Apache
37  * Bean Scripting Framework</a>. Oddjob currently comes bundled with
38  * the <a HREF="http://groovy.codehaus.org/">Groovy</a> libraries but
39  * others can easily be added.
40  * <p>
41  * The named beans property allow values to be passed to and from the
42  * script.
43  * <p>
44  * The result of this job can altered by the script setting this jobs
45  * result property. 0 will complete, any other value will flag not
46  * complete.
47  *
48  * @oddjob.example
49  *
50  * Hello World.
51  *
52  * <pre>
53  * &lt;script language="groovy"&gt;
54  * &lt;text&gt;
55  * printlnt "Hello World"
56  * &lt;/text&gt;
57  * &lt;/script&gt;
58  * </pre>
59  *
60  * @oddjob.example
61  *
62  * Setting todays date as a variable.
63  * <pre>
64  * &lt;sequential&gt;
65  * &lt;script language="groovy"&gt;
66  * &lt;text&gt;
67  * vars.today=new Date()
68  * &lt;/text&gt;
69  * &lt;beans&gt;
70  * &lt;value name="vars" value="${vars}"/&gt;
71  * &lt;/beans&gt;
72  * &lt;/script&gt;
73  * &lt;variables id="vars"/>
74  * &lt;sequential&gt;
75  * </pre>
76  *
77  * @oddjob.example
78  *
79  * Setting the script job to not complete.
80  * <pre>
81  * &lt;script id="s" language="groovy"&gt;
82  * &lt;text&gt;
83  * s.result = -1
84  * &lt;/text&gt;
85  * &lt;beans&gt;
86  * &lt;value name="s" value="${s}"/&gt;
87  * &lt;/beans&gt;
88  * &lt;/script&gt;
89  * </pre>
90  *
91  * @author Rob Gordon - Based on the original from Ant.
92  */

93 public class ScriptJob extends SimpleJob {
94
95     /**
96      * @oddjob.property
97      * @oddjob.description The name of the language the script
98      * is in.
99      * @oddjob.required Yes.
100      */

101     private transient String JavaDoc language;
102     
103     /**
104      * @oddjob.property
105      * @oddjob.description The script as text
106      * @oddjob.required No if the script is provided by another means.
107      */

108     private transient String JavaDoc text;
109
110     /**
111      * @oddjob.property
112      * @oddjob.description A file containing a script.
113      * @oddjob.required No if the script is provided by another means.
114      */

115     private transient File JavaDoc file;
116     
117     /**
118      * @oddjob.property
119      * @oddjob.description A named bean which is made available to
120      * the script. The bean can be any type supported by
121      * {@link org.oddjob.values.types.MapType}.
122      * @oddjob.required No.
123      */

124     private transient Map JavaDoc beans;
125     
126     /**
127      * @oddjob.property
128      * @oddjob.description The script provided as input.
129      * @oddjob.required No if the script is provided by another means.
130      */

131     private transient InputStream JavaDoc input;
132
133     /**
134      * @oddjob.property
135      * @oddjob.description The component factory exposed as a property
136      * so that it can be passed back into the script if required.
137      * @oddjob.required Read only.
138      */

139     private transient ObjectFactory componentFactory;
140     
141     /**
142      * @oddjob.property
143      * @oddjob.description The value factory exposed as a property
144      * so that it can be passed back into the script if required.
145      * @oddjob.required Read only.
146      */

147     private transient ObjectFactory valueFactory;
148
149     /**
150      * @oddjob.property
151      * @oddjob.description The value factory exposed as a property
152      * so that it can be passed back into the script if required.
153      * @oddjob.required No, If not supplied this job will always complete.
154      */

155     private int result;
156     
157     /*
158      * (non-Javadoc)
159      * @see org.oddjob.framework.BaseComponent#setContext(org.oddjob.arooa.ArooaXMLContext)
160      */

161     public boolean setContext(ArooaContext context) {
162         this.componentFactory = (ObjectFactory) context.get(ArooaConstants.COMPONENT_FACTORY);
163         this.valueFactory = (ObjectFactory) context.get(ArooaConstants.VALUE_FACTORY);
164         return super.setContext(context);
165     }
166     
167     /*
168      * (non-Javadoc)
169      * @see org.oddjob.framework.SimpleJob#execute()
170      */

171     protected int execute() throws IOException JavaDoc {
172         ScriptRunner runner = new ScriptRunner();
173         if (language != null) {
174             runner.setLanguage(language);
175         }
176         
177         if (text != null) {
178             runner.setScript(text);
179         }
180         else if (file != null) {
181             input = new FileInputStream JavaDoc(file);
182         }
183         else if (input == null) {
184             throw new OddjobConfigException("No script provided!");
185         }
186             
187         if (input != null) {
188             ByteArrayOutputStream JavaDoc script = new ByteArrayOutputStream JavaDoc();
189             IO.copy(input, script);
190             runner.setScript(script.toString());
191         }
192
193         if (beans != null) {
194             runner.addBeans(beans);
195         }
196
197         runner.executeScript("ODDJOB");
198         
199         return result;
200     }
201
202     /**
203      * Defines the language (required).
204      *
205      * @param language the scripting language name for the script.
206      */

207     public void setLanguage(String JavaDoc language) {
208         this.language = language;
209     }
210     
211     /**
212      * Get the language.
213      *
214      * @return The language.
215      */

216     public String JavaDoc getLanguage() {
217         return language;
218     }
219     
220     /**
221      * Get the named bean.
222      *
223      * @param name The name of the bean
224      * @return The bean or null if it doesn't exist.
225      */

226     public Object JavaDoc getBeans(String JavaDoc name) {
227         if (beans == null) {
228             return null;
229         }
230         return beans.get(name);
231     }
232
233     /**
234      * Add a named bean.
235      *
236      * @param name The name of the bean.
237      * @param value The bean.
238      */

239     public void setBeans(String JavaDoc name, Object JavaDoc value) {
240         if (beans == null) {
241             beans = new HashMap JavaDoc();
242         }
243         logger().debug("Adding bean (" + name
244                 + ", [" + value + "]");
245         beans.put(name, value);
246     }
247     
248     /**
249      * Get the script file.
250      *
251      * @return The script file, null if none used.
252      */

253     public File JavaDoc getFile() {
254         return file;
255     }
256     
257     /**
258      * Set the script file.
259      *
260      * @param file The script file.
261      */

262     public void setFile(File JavaDoc file) {
263         this.file = file;
264     }
265     
266     /**
267      * Get the input.
268      *
269      * @return The input.
270      */

271     public InputStream JavaDoc getInput() {
272         return input;
273     }
274     
275     /**
276      * Set the input.
277      *
278      * @param input The input.
279      */

280     public void setInput(InputStream JavaDoc input) {
281         this.input = input;
282     }
283     
284     /**
285      * Get the sciprt text.
286      *
287      * @return The script text.
288      */

289     public String JavaDoc getText() {
290         return text;
291     }
292     
293     /**
294      * Set the script text.
295      *
296      * @param text The script text.
297      */

298     public void setText(String JavaDoc text) {
299         this.text = text;
300     }
301         
302     /**
303      * Get the component factory.
304      *
305      * @return The component factory.
306      */

307     public ObjectFactory getComponentFactory() {
308         return componentFactory;
309     }
310         
311     /**
312      * Get the result.
313      *
314      * @return The result.
315      */

316     public int getResult() {
317         return result;
318     }
319     
320     /**
321      * Set the result.
322      *
323      * @param result The result.
324      */

325     public void setResult(int result) {
326         this.result = result;
327     }
328     
329     /**
330      * Get the value factory.
331      *
332      * @return The value factory.
333      */

334     public ObjectFactory getValueFactory() {
335         return valueFactory;
336     }
337 }
338
Popular Tags