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.apache.tools.ant.util;
|
---|
18 |
|
---|
19 | import java.io.File;
|
---|
20 | import java.io.FileInputStream;
|
---|
21 | import java.io.IOException;
|
---|
22 | import org.apache.bsf.BSFException;
|
---|
23 | import org.apache.bsf.BSFManager;
|
---|
24 | import org.apache.tools.ant.BuildException;
|
---|
25 | import java.util.Map;
|
---|
26 | import java.util.HashMap;
|
---|
27 | import java.util.Iterator;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * This class is used to run BSF scripts
|
---|
31 | *
|
---|
32 | */
|
---|
33 | public class ScriptRunner {
|
---|
34 |
|
---|
35 | // Register Groovy ourselves, since BSF does not
|
---|
36 | // natively support it (yet).
|
---|
37 | // This "hack" can be removed once BSF has been
|
---|
38 | // modified to support Groovy or more dynamic
|
---|
39 | // registration.
|
---|
40 | static {
|
---|
41 | BSFManager.registerScriptingEngine(
|
---|
42 | "groovy",
|
---|
43 | "org.codehaus.groovy.bsf.GroovyEngine",
|
---|
44 | new String[] {"groovy", "gy"});
|
---|
45 | }
|
---|
46 |
|
---|
47 | /** Script language */
|
---|
48 | private String language;
|
---|
49 |
|
---|
50 | /** Script content */
|
---|
51 | private String script = "";
|
---|
52 |
|
---|
53 | /** Beans to be provided to the script */
|
---|
54 | private Map beans = new HashMap();
|
---|
55 |
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Add a list of named objects to the list to be exported to the script
|
---|
59 | *
|
---|
60 | * @param dictionary a map of objects to be placed into the script context
|
---|
61 | * indexed by String names.
|
---|
62 | */
|
---|
63 | public void addBeans(Map dictionary) {
|
---|
64 | for (Iterator i = dictionary.keySet().iterator(); i.hasNext();) {
|
---|
65 | String key = (String) i.next();
|
---|
66 | try {
|
---|
67 | Object val = dictionary.get(key);
|
---|
68 | addBean(key, val);
|
---|
69 | } catch (BuildException ex) {
|
---|
70 | // The key is in the dictionary but cannot be retrieved
|
---|
71 | // This is usually due references that refer to tasks
|
---|
72 | // that have not been taskdefed in the current run.
|
---|
73 | // Ignore
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Add a single object into the script context.
|
---|
80 | *
|
---|
81 | * @param key the name in the context this object is to stored under.
|
---|
82 | * @param bean the object to be stored in the script context.
|
---|
83 | */
|
---|
84 | public void addBean(String key, Object bean) {
|
---|
85 | boolean isValid = key.length() > 0
|
---|
86 | && Character.isJavaIdentifierStart(key.charAt(0));
|
---|
87 |
|
---|
88 | for (int i = 1; isValid && i < key.length(); i++) {
|
---|
89 | isValid = Character.isJavaIdentifierPart(key.charAt(i));
|
---|
90 | }
|
---|
91 |
|
---|
92 | if (isValid) {
|
---|
93 | beans.put(key, bean);
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Do the work.
|
---|
99 | *
|
---|
100 | * @param execName the name that will be passed to BSF for this script
|
---|
101 | * execution.
|
---|
102 | *
|
---|
103 | * @exception BuildException if someting goes wrong exectuing the script.
|
---|
104 | */
|
---|
105 | public void executeScript(String execName) throws BuildException {
|
---|
106 | if (language == null) {
|
---|
107 | throw new BuildException("script language must be specified");
|
---|
108 | }
|
---|
109 |
|
---|
110 | try {
|
---|
111 | BSFManager manager = new BSFManager ();
|
---|
112 |
|
---|
113 | for (Iterator i = beans.keySet().iterator(); i.hasNext();) {
|
---|
114 | String key = (String) i.next();
|
---|
115 | Object value = beans.get(key);
|
---|
116 | if (value != null) {
|
---|
117 | manager.declareBean(key, value, value.getClass());
|
---|
118 | } else {
|
---|
119 | // BSF uses a hashtable to store values
|
---|
120 | // so cannot declareBean with a null value
|
---|
121 | // So need to remove any bean of this name as
|
---|
122 | // that bean should not be visible
|
---|
123 | manager.undeclareBean(key);
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | // execute the script
|
---|
128 | manager.exec(language, execName, 0, 0, script);
|
---|
129 | } catch (BSFException be) {
|
---|
130 | Throwable t = be;
|
---|
131 | Throwable te = be.getTargetException();
|
---|
132 | if (te != null) {
|
---|
133 | if (te instanceof BuildException) {
|
---|
134 | throw (BuildException) te;
|
---|
135 | } else {
|
---|
136 | t = te;
|
---|
137 | }
|
---|
138 | }
|
---|
139 | throw new BuildException(t);
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Defines the language (required).
|
---|
145 | *
|
---|
146 | * @param language the scripting language name for the script.
|
---|
147 | */
|
---|
148 | public void setLanguage(String language) {
|
---|
149 | this.language = language;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Get the script language
|
---|
154 | *
|
---|
155 | * @return the script language
|
---|
156 | */
|
---|
157 | public String getLanguage() {
|
---|
158 | return language;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Load the script from an external file ; optional.
|
---|
163 | *
|
---|
164 | * @param file the file containing the script source.
|
---|
165 | */
|
---|
166 | public void setSrc(File file) {
|
---|
167 | if (!file.exists()) {
|
---|
168 | throw new BuildException("file " + file.getPath() + " not found.");
|
---|
169 | }
|
---|
170 |
|
---|
171 | int count = (int) file.length();
|
---|
172 | byte[] data = new byte[count];
|
---|
173 |
|
---|
174 | try {
|
---|
175 | FileInputStream inStream = new FileInputStream(file);
|
---|
176 | inStream.read(data);
|
---|
177 | inStream.close();
|
---|
178 | } catch (IOException e) {
|
---|
179 | throw new BuildException(e);
|
---|
180 | }
|
---|
181 |
|
---|
182 | script += new String(data);
|
---|
183 | }
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Set the script text.
|
---|
187 | *
|
---|
188 | * @param text a component of the script text to be added.
|
---|
189 | */
|
---|
190 | public void addText(String text) {
|
---|
191 | this.script += text;
|
---|
192 | }
|
---|
193 | }
|
---|