KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2003-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.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.bsf.BSFException;
24 import org.apache.bsf.BSFManager;
25 import org.apache.tools.ant.BuildException;
26
27 /**
28  * This class is used to run BSF scripts
29  *
30  */

31 public class ScriptRunner {
32
33     // Register Groovy ourselves, since BSF does not
34
// natively support it (yet).
35
// This "hack" can be removed once BSF has been
36
// modified to support Groovy or more dynamic
37
// registration.
38
static {
39         BSFManager.registerScriptingEngine(
40             "groovy",
41             "org.codehaus.groovy.bsf.GroovyEngine",
42             new String JavaDoc[] {"groovy", "gy"});
43     }
44
45     /** Script language */
46     private String JavaDoc language;
47
48     /** Script content */
49     private String JavaDoc script = "";
50
51     /** Beans to be provided to the script */
52     private Map JavaDoc beans = new HashMap JavaDoc();
53
54
55     /**
56      * Add a list of named objects to the list to be exported to the script
57      *
58      * @param dictionary a map of objects to be placed into the script context
59      * indexed by String names.
60      */

61     public void addBeans(Map JavaDoc dictionary) {
62         for (Iterator JavaDoc i = dictionary.keySet().iterator(); i.hasNext();) {
63             String JavaDoc key = (String JavaDoc) i.next();
64             try {
65                 Object JavaDoc val = dictionary.get(key);
66                 addBean(key, val);
67             } catch (BuildException ex) {
68                 // The key is in the dictionary but cannot be retrieved
69
// This is usually due references that refer to tasks
70
// that have not been taskdefed in the current run.
71
// Ignore
72
}
73         }
74     }
75
76     /**
77      * Add a single object into the script context.
78      *
79      * @param key the name in the context this object is to stored under.
80      * @param bean the object to be stored in the script context.
81      */

82     public void addBean(String JavaDoc key, Object JavaDoc bean) {
83         boolean isValid = key.length() > 0
84             && Character.isJavaIdentifierStart(key.charAt(0));
85
86         for (int i = 1; isValid && i < key.length(); i++) {
87             isValid = Character.isJavaIdentifierPart(key.charAt(i));
88         }
89
90         if (isValid) {
91             beans.put(key, bean);
92         }
93     }
94
95     /**
96      * Do the work.
97      *
98      * @param execName the name that will be passed to BSF for this script
99      * execution.
100      *
101      * @exception BuildException if someting goes wrong exectuing the script.
102      */

103     public void executeScript(String JavaDoc execName) throws BuildException {
104         if (language == null) {
105             throw new BuildException("script language must be specified");
106         }
107
108         try {
109             BSFManager manager = new BSFManager ();
110
111             for (Iterator JavaDoc i = beans.keySet().iterator(); i.hasNext();) {
112                 String JavaDoc key = (String JavaDoc) i.next();
113                 Object JavaDoc value = beans.get(key);
114                 if (value != null) {
115                     manager.declareBean(key, value, value.getClass());
116                 } else {
117                     // BSF uses a hashtable to store values
118
// so cannot declareBean with a null value
119
// So need to remove any bean of this name as
120
// that bean should not be visible
121
manager.undeclareBean(key);
122                 }
123             }
124
125             // execute the script
126
manager.exec(language, execName, 0, 0, script);
127         } catch (BSFException be) {
128             Throwable JavaDoc t = be;
129             Throwable JavaDoc te = be.getTargetException();
130             if (te != null) {
131                 if (te instanceof BuildException) {
132                     throw (BuildException) te;
133                 } else {
134                     t = te;
135                 }
136             }
137             throw new BuildException(t);
138         }
139     }
140
141     /**
142      * Defines the language (required).
143      *
144      * @param language the scripting language name for the script.
145      */

146     public void setLanguage(String JavaDoc language) {
147         this.language = language;
148     }
149
150     /**
151      * Get the script language
152      *
153      * @return the script language
154      */

155     public String JavaDoc getLanguage() {
156         return language;
157     }
158
159     
160     public String JavaDoc getScript() {
161         return script;
162     }
163     
164     public void setScript(String JavaDoc script) {
165         this.script = script;
166     }
167 }
168
Popular Tags