KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > ScriptRunnerBase


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

18 package org.apache.tools.ant.util;
19
20 import java.io.BufferedReader JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileReader JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.ProjectComponent;
27 import org.apache.tools.ant.Project;
28
29 import java.util.Map JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 /**
34  * This is a common abstract base case for script runners.
35  * These classes need to implement executeScript, evaluateScript
36  * and supportsLanguage.
37  * @since Ant 1.7.0
38  */

39 public abstract class ScriptRunnerBase {
40     /** Whether to keep the engine between calls to execute/eval */
41     private boolean keepEngine = false;
42
43     /** Script language */
44     private String JavaDoc language;
45
46     /** Script content */
47     private String JavaDoc script = "";
48
49     /** Project this runner is used in */
50     private Project project;
51
52     /** Classloader to be used when running the script. */
53     private ClassLoader JavaDoc scriptLoader;
54
55     /** Beans to be provided to the script */
56     private Map JavaDoc beans = new HashMap JavaDoc();
57
58     /**
59      * Add a list of named objects to the list to be exported to the script
60      *
61      * @param dictionary a map of objects to be placed into the script context
62      * indexed by String names.
63      */

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

85     public void addBean(String JavaDoc key, Object JavaDoc bean) {
86         boolean isValid = key.length() > 0
87             && Character.isJavaIdentifierStart(key.charAt(0));
88
89         for (int i = 1; isValid && i < key.length(); i++) {
90             isValid = Character.isJavaIdentifierPart(key.charAt(i));
91         }
92
93         if (isValid) {
94             beans.put(key, bean);
95         }
96     }
97
98     /**
99      * Get the beans used for the script.
100      * @return the map of beans.
101      */

102     protected Map JavaDoc getBeans() {
103         return beans;
104     }
105
106     /**
107      * Do the work.
108      * @param execName the name that will be passed to BSF for this script
109      * execution.
110      */

111     public abstract void executeScript(String JavaDoc execName);
112
113     /**
114      * Evalulate the script.
115      * @param execName the name that will be passed to BSF for this script
116      * execution.
117      * @return the result of evalulating the script.
118      */

119     public abstract Object JavaDoc evaluateScript(String JavaDoc execName);
120
121     /**
122      * Check if a script engine can be created for
123      * this language.
124      * @return true if a script engine can be created, false
125      * otherwise.
126      */

127     public abstract boolean supportsLanguage();
128
129     /**
130      * Get the name of the manager prefix used for this
131      * scriptrunner.
132      * @return the prefix string.
133      */

134     public abstract String JavaDoc getManagerName();
135
136     /**
137      * Defines the language (required).
138      * @param language the scripting language name for the script.
139      */

140     public void setLanguage(String JavaDoc language) {
141         this.language = language;
142     }
143
144     /**
145      * Get the script language
146      * @return the script language
147      */

148     public String JavaDoc getLanguage() {
149         return language;
150     }
151
152     /**
153      * Set the script classloader.
154      * @param classLoader the classloader to use.
155      */

156     public void setScriptClassLoader(ClassLoader JavaDoc classLoader) {
157         this.scriptLoader = classLoader;
158     }
159
160     /**
161      * Get the classloader used to load the script engine.
162      * @return the classloader.
163      */

164     protected ClassLoader JavaDoc getScriptClassLoader() {
165         return scriptLoader;
166     }
167
168     /**
169      * Whether to keep the script engine between calls.
170      * @param keepEngine if true, keep the engine.
171      */

172     public void setKeepEngine(boolean keepEngine) {
173         this.keepEngine = keepEngine;
174     }
175
176     /**
177      * Get the keep engine attribute.
178      * @return the attribute.
179      */

180     public boolean getKeepEngine() {
181         return keepEngine;
182     }
183
184     /**
185      * Load the script from an external file; optional.
186      * @param file the file containing the script source.
187      */

188     public void setSrc(File JavaDoc file) {
189         if (!file.exists()) {
190             throw new BuildException("file " + file.getPath() + " not found.");
191         }
192         BufferedReader JavaDoc in = null;
193         try {
194             in = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
195             script += FileUtils.readFully(in);
196         } catch (IOException JavaDoc ex) {
197             throw new BuildException(ex);
198         } finally {
199             FileUtils.close(in);
200         }
201     }
202
203     /**
204      * Set the script text.
205      *
206      * @param text a component of the script text to be added.
207      */

208     public void addText(String JavaDoc text) {
209         this.script += text;
210     }
211
212     /**
213      * Get the current script text content.
214      * @return the script text.
215      */

216     public String JavaDoc getScript() {
217         return script;
218     }
219
220     /**
221      * Clear the current script text content.
222      */

223     public void clearScript() {
224         this.script = "";
225     }
226
227     /**
228      * Set the project for this runner.
229      * @param project the project.
230      */

231     public void setProject(Project project) {
232         this.project = project;
233     }
234
235     /**
236      * Get the project for this runner.
237      * @return the project.
238      */

239     public Project getProject() {
240         return project;
241     }
242
243     /**
244      * Bind the runner to a project component.
245      * Properties, targets and references are all added as beans;
246      * project is bound to project, and self to the component.
247      * @param component to become <code>self</code>
248      */

249     public void bindToComponent(ProjectComponent component) {
250         project = component.getProject();
251         addBeans(project.getProperties());
252         addBeans(project.getUserProperties());
253         addBeans(project.getTargets());
254         addBeans(project.getReferences());
255         addBean("project", project);
256         addBean("self", component);
257     }
258
259     /**
260      * Bind the runner to a project component.
261      * The project and self are the only beans set.
262      * @param component to become <code>self</code>
263      */

264     public void bindToComponentMinimum(ProjectComponent component) {
265         project = component.getProject();
266         addBean("project", project);
267         addBean("self", component);
268     }
269
270     /**
271      * Check if the language attribute is set.
272      * @throws BuildException if it is not.
273      */

274     protected void checkLanguage() {
275         if (language == null) {
276             throw new BuildException(
277                 "script language must be specified");
278         }
279     }
280
281     /**
282      * Replace the current context classloader with the
283      * script context classloader.
284      * @return the current context classloader.
285      */

286     protected ClassLoader JavaDoc replaceContextLoader() {
287         ClassLoader JavaDoc origContextClassLoader =
288             Thread.currentThread().getContextClassLoader();
289         if (getScriptClassLoader() == null) {
290             setScriptClassLoader(getClass().getClassLoader());
291         }
292         Thread.currentThread().setContextClassLoader(getScriptClassLoader());
293         return origContextClassLoader;
294     }
295
296     /**
297      * Restore the context loader with the original context classloader.
298      *
299      * script context loader.
300      * @param origLoader the original context classloader.
301      */

302     protected void restoreContextLoader(ClassLoader JavaDoc origLoader) {
303         Thread.currentThread().setContextClassLoader(
304                  origLoader);
305     }
306
307 }
308
Popular Tags