KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > optional > ScriptRunner


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.optional;
19
20 import org.apache.bsf.BSFException;
21 import org.apache.bsf.BSFManager;
22 import org.apache.bsf.BSFEngine;
23
24
25 import java.util.Iterator JavaDoc;
26 import java.util.Hashtable JavaDoc;
27
28 import org.apache.tools.ant.BuildException;
29 import org.apache.tools.ant.Project;
30
31 import org.apache.tools.ant.util.ReflectUtil;
32 import org.apache.tools.ant.util.ScriptRunnerBase;
33
34 /**
35  * This class is used to run BSF scripts
36  *
37  */

38 public class ScriptRunner extends ScriptRunnerBase {
39     // Register Groovy ourselves, since BSF did not
40
// natively support it in versions previous to 1.2.4.
41
static {
42         BSFManager.registerScriptingEngine(
43             "groovy",
44             "org.codehaus.groovy.bsf.GroovyEngine",
45             new String JavaDoc[] {"groovy", "gy"});
46     }
47
48     private BSFEngine engine;
49     private BSFManager manager;
50
51     /**
52      * Get the name of the manager prefix.
53      * @return "bsf"
54      */

55     public String JavaDoc getManagerName() {
56         return "bsf";
57     }
58
59     /**
60      * Check if bsf supports the language.
61      * @return true if bsf can create an engine for this language.
62      */

63     public boolean supportsLanguage() {
64         Hashtable JavaDoc table = (Hashtable JavaDoc) ReflectUtil.getField(
65             new BSFManager(), "registeredEngines");
66         String JavaDoc engineClassName = (String JavaDoc) table.get(getLanguage());
67         if (engineClassName == null) {
68             getProject().log(
69                 "This is no BSF engine class for language '"
70                 + getLanguage() + "'",
71                 Project.MSG_VERBOSE);
72             return false;
73         }
74         try {
75             getScriptClassLoader().loadClass(engineClassName);
76             return true;
77         } catch (Throwable JavaDoc ex) {
78             getProject().log(
79                 "unable to create BSF engine class for language '"
80                 + getLanguage() + "'",
81                 ex,
82                 Project.MSG_VERBOSE);
83             return false;
84         }
85     }
86
87     /**
88      * Do the work.
89      *
90      * @param execName the name that will be passed to BSF for this script
91      * execution.
92      *
93      * @exception BuildException if someting goes wrong exectuing the script.
94      */

95     public void executeScript(String JavaDoc execName) throws BuildException {
96         checkLanguage();
97         ClassLoader JavaDoc origLoader = replaceContextLoader();
98         try {
99             BSFManager m = createManager();
100             declareBeans(m);
101             // execute the script
102
if (engine == null) {
103                 m.exec(getLanguage(), execName, 0, 0, getScript());
104             } else {
105                 engine.exec(execName, 0, 0, getScript());
106             }
107         } catch (BSFException be) {
108             throwBuildException(be);
109         } finally {
110             restoreContextLoader(origLoader);
111         }
112     }
113
114     /**
115      * Do the work.
116      *
117      * @param execName the name that will be passed to BSF for this script
118      * execution.
119      * @return the result of the evalulation
120      * @exception BuildException if someting goes wrong exectuing the script.
121      */

122     public Object JavaDoc evaluateScript(String JavaDoc execName)
123         throws BuildException {
124         checkLanguage();
125         ClassLoader JavaDoc origLoader = replaceContextLoader();
126         try {
127             BSFManager m = createManager();
128             declareBeans(m);
129             // execute the script
130
if (engine == null) {
131                 return m.eval(getLanguage(), execName, 0, 0, getScript());
132             } else {
133                 return engine.eval(execName, 0, 0, getScript());
134             }
135         } catch (BSFException be) {
136             throwBuildException(be);
137             // NotReached
138
return null;
139         } finally {
140             restoreContextLoader(origLoader);
141         }
142     }
143
144     /**
145      * Throw a buildException in place of a BSFException.
146      * @param be BSFException to convert.
147      * @throws BuildException the conveted exception.
148      */

149     private void throwBuildException(BSFException be) {
150         Throwable JavaDoc t = be;
151         Throwable JavaDoc te = be.getTargetException();
152         if (te != null) {
153             if (te instanceof BuildException) {
154                 throw (BuildException) te;
155             } else {
156                 t = te;
157             }
158         }
159         throw new BuildException(t);
160     }
161
162     private void declareBeans(BSFManager m) throws BSFException {
163         for (Iterator JavaDoc i = getBeans().keySet().iterator(); i.hasNext();) {
164             String JavaDoc key = (String JavaDoc) i.next();
165             Object JavaDoc value = getBeans().get(key);
166             if (value != null) {
167                 m.declareBean(key, value, value.getClass());
168             } else {
169                 // BSF uses a hashtable to store values
170
// so cannot declareBean with a null value
171
// So need to remove any bean of this name as
172
// that bean should not be visible
173
m.undeclareBean(key);
174             }
175         }
176     }
177
178     private BSFManager createManager() throws BSFException {
179         if (manager != null) {
180             return manager;
181         }
182         BSFManager m = new BSFManager();
183         m.setClassLoader(getScriptClassLoader());
184         if (getKeepEngine()) {
185             BSFEngine e = manager.loadScriptingEngine(getLanguage());
186             this.manager = m;
187             this.engine = e;
188         }
189         return m;
190     }
191 }
192
Popular Tags