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.apache.tools.ant.taskdefs.optional.script;
|
---|
18 |
|
---|
19 | import org.apache.tools.ant.AntTypeDefinition;
|
---|
20 | import org.apache.tools.ant.ComponentHelper;
|
---|
21 | import org.apache.tools.ant.Project;
|
---|
22 | import org.apache.tools.ant.MagicNames;
|
---|
23 | import org.apache.tools.ant.BuildException;
|
---|
24 | import org.apache.tools.ant.ProjectHelper;
|
---|
25 | import org.apache.tools.ant.taskdefs.DefBase;
|
---|
26 |
|
---|
27 | import java.util.Map;
|
---|
28 | import java.util.HashMap;
|
---|
29 | import java.util.List;
|
---|
30 | import java.util.Locale;
|
---|
31 | import java.util.ArrayList;
|
---|
32 | import java.util.Iterator;
|
---|
33 | import java.util.Set;
|
---|
34 | import java.util.HashSet;
|
---|
35 | import java.io.File;
|
---|
36 |
|
---|
37 | import org.apache.tools.ant.util.ScriptRunner;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Define a task using a script
|
---|
41 | *
|
---|
42 | * @since Ant 1.6
|
---|
43 | */
|
---|
44 | public class ScriptDef extends DefBase {
|
---|
45 | /** Used to run the script */
|
---|
46 | private ScriptRunner runner = new ScriptRunner();
|
---|
47 |
|
---|
48 | /** the name by which this script will be activated */
|
---|
49 | private String name;
|
---|
50 |
|
---|
51 | /** Attributes definitions of this script */
|
---|
52 | private List attributes = new ArrayList();
|
---|
53 |
|
---|
54 | /** Nested Element definitions of this script */
|
---|
55 | private List nestedElements = new ArrayList();
|
---|
56 |
|
---|
57 | /** The attribute names as a set */
|
---|
58 | private Set attributeSet;
|
---|
59 |
|
---|
60 | /** The nested element definitions indexed by their names */
|
---|
61 | private Map nestedElementMap;
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * set the name under which this script will be activated in a build
|
---|
65 | * file
|
---|
66 | *
|
---|
67 | * @param name the name of the script
|
---|
68 | */
|
---|
69 | public void setName(String name) {
|
---|
70 | this.name = name;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Indicates whether the task supports a given attribute name
|
---|
75 | *
|
---|
76 | * @param attributeName the name of the attribute.
|
---|
77 | *
|
---|
78 | * @return true if the attribute is supported by the script.
|
---|
79 | */
|
---|
80 | public boolean isAttributeSupported(String attributeName) {
|
---|
81 | return attributeSet.contains(attributeName);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Class representing an attribute definition
|
---|
86 | */
|
---|
87 | public static class Attribute {
|
---|
88 | /** The attribute name */
|
---|
89 | private String name;
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Set the attribute name
|
---|
93 | *
|
---|
94 | * @param name the attribute name
|
---|
95 | */
|
---|
96 | public void setName(String name) {
|
---|
97 | this.name = name.toLowerCase(Locale.US);
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Add an attribute definition to this script.
|
---|
103 | *
|
---|
104 | * @param attribute the attribute definition.
|
---|
105 | */
|
---|
106 | public void addAttribute(Attribute attribute) {
|
---|
107 | attributes.add(attribute);
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Class to represent a nested element definition
|
---|
112 | */
|
---|
113 | public static class NestedElement {
|
---|
114 | /** The name of the neseted element */
|
---|
115 | private String name;
|
---|
116 |
|
---|
117 | /** The Ant type to which this nested element corresponds. */
|
---|
118 | private String type;
|
---|
119 |
|
---|
120 | /** The class to be created for this nested element */
|
---|
121 | private String className;
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * set the tag name for this nested element
|
---|
125 | *
|
---|
126 | * @param name the name of this nested element
|
---|
127 | */
|
---|
128 | public void setName(String name) {
|
---|
129 | this.name = name.toLowerCase(Locale.US);
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Set the type of this element. This is the name of an
|
---|
134 | * Ant task or type which is to be used when this element is to be
|
---|
135 | * created. This is an alternative to specifying the class name directly
|
---|
136 | *
|
---|
137 | * @param type the name of an Ant type, or task, to use for this nested
|
---|
138 | * element.
|
---|
139 | */
|
---|
140 | public void setType(String type) {
|
---|
141 | this.type = type;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Set the classname of the class to be used for the nested element.
|
---|
146 | * This specifies the class directly and is an alternative to specifying
|
---|
147 | * the Ant type name.
|
---|
148 | *
|
---|
149 | * @param className the name of the class to use for this nested
|
---|
150 | * element.
|
---|
151 | */
|
---|
152 | public void setClassName(String className) {
|
---|
153 | this.className = className;
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Add a nested element definition.
|
---|
159 | *
|
---|
160 | * @param nestedElement the nested element definition.
|
---|
161 | */
|
---|
162 | public void addElement(NestedElement nestedElement) {
|
---|
163 | nestedElements.add(nestedElement);
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Define the script.
|
---|
168 | */
|
---|
169 | public void execute() {
|
---|
170 | if (name == null) {
|
---|
171 | throw new BuildException("scriptdef requires a name attribute to "
|
---|
172 | + "name the script");
|
---|
173 | }
|
---|
174 |
|
---|
175 | if (runner.getLanguage() == null) {
|
---|
176 | throw new BuildException("<scriptdef> requires a language attribute "
|
---|
177 | + "to specify the script language");
|
---|
178 | }
|
---|
179 |
|
---|
180 | attributeSet = new HashSet();
|
---|
181 | for (Iterator i = attributes.iterator(); i.hasNext();) {
|
---|
182 | Attribute attribute = (Attribute) i.next();
|
---|
183 | if (attribute.name == null) {
|
---|
184 | throw new BuildException("scriptdef <attribute> elements "
|
---|
185 | + "must specify an attribute name");
|
---|
186 | }
|
---|
187 |
|
---|
188 | if (attributeSet.contains(attribute.name)) {
|
---|
189 | throw new BuildException("scriptdef <" + name + "> declares "
|
---|
190 | + "the " + attribute.name + " attribute more than once");
|
---|
191 | }
|
---|
192 | attributeSet.add(attribute.name);
|
---|
193 | }
|
---|
194 |
|
---|
195 | nestedElementMap = new HashMap();
|
---|
196 | for (Iterator i = nestedElements.iterator(); i.hasNext();) {
|
---|
197 | NestedElement nestedElement = (NestedElement) i.next();
|
---|
198 | if (nestedElement.name == null) {
|
---|
199 | throw new BuildException("scriptdef <element> elements "
|
---|
200 | + "must specify an element name");
|
---|
201 | }
|
---|
202 | if (nestedElementMap.containsKey(nestedElement.name)) {
|
---|
203 | throw new BuildException("scriptdef <" + name + "> declares "
|
---|
204 | + "the " + nestedElement.name + " nested element more "
|
---|
205 | + "than once");
|
---|
206 | }
|
---|
207 |
|
---|
208 | if (nestedElement.className == null
|
---|
209 | && nestedElement.type == null) {
|
---|
210 | throw new BuildException("scriptdef <element> elements "
|
---|
211 | + "must specify either a classname or type attribute");
|
---|
212 | }
|
---|
213 | if (nestedElement.className != null
|
---|
214 | && nestedElement.type != null) {
|
---|
215 | throw new BuildException("scriptdef <element> elements "
|
---|
216 | + "must specify only one of the classname and type "
|
---|
217 | + "attributes");
|
---|
218 | }
|
---|
219 |
|
---|
220 |
|
---|
221 | nestedElementMap.put(nestedElement.name, nestedElement);
|
---|
222 | }
|
---|
223 |
|
---|
224 | // find the script repository - it is stored in the project
|
---|
225 | Map scriptRepository = null;
|
---|
226 | Project project = getProject();
|
---|
227 | synchronized (project) {
|
---|
228 | scriptRepository =
|
---|
229 | (Map) project.getReference(MagicNames.SCRIPT_REPOSITORY);
|
---|
230 | if (scriptRepository == null) {
|
---|
231 | scriptRepository = new HashMap();
|
---|
232 | project.addReference(MagicNames.SCRIPT_REPOSITORY,
|
---|
233 | scriptRepository);
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | name = ProjectHelper.genComponentName(getURI(), name);
|
---|
238 | scriptRepository.put(name, this);
|
---|
239 | AntTypeDefinition def = new AntTypeDefinition();
|
---|
240 | def.setName(name);
|
---|
241 | def.setClass(ScriptDefBase.class);
|
---|
242 | ComponentHelper.getComponentHelper(
|
---|
243 | getProject()).addDataTypeDefinition(def);
|
---|
244 | }
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Create a nested element to be configured.
|
---|
248 | *
|
---|
249 | * @param elementName the name of the nested element.
|
---|
250 | * @return object representing the element name.
|
---|
251 | */
|
---|
252 | public Object createNestedElement(String elementName) {
|
---|
253 | NestedElement definition
|
---|
254 | = (NestedElement) nestedElementMap.get(elementName);
|
---|
255 | if (definition == null) {
|
---|
256 | throw new BuildException("<" + name + "> does not support "
|
---|
257 | + "the <" + elementName + "> nested element");
|
---|
258 | }
|
---|
259 |
|
---|
260 | Object instance = null;
|
---|
261 | String classname = definition.className;
|
---|
262 | if (classname == null) {
|
---|
263 | instance = getProject().createTask(definition.type);
|
---|
264 | if (instance == null) {
|
---|
265 | instance = getProject().createDataType(definition.type);
|
---|
266 | }
|
---|
267 | } else {
|
---|
268 | /*
|
---|
269 | // try the context classloader
|
---|
270 | ClassLoader loader
|
---|
271 | = Thread.currentThread().getContextClassLoader();
|
---|
272 | */
|
---|
273 | ClassLoader loader = createLoader();
|
---|
274 |
|
---|
275 | Class instanceClass = null;
|
---|
276 | try {
|
---|
277 | instanceClass = Class.forName(classname, true, loader);
|
---|
278 | } catch (Throwable e) {
|
---|
279 | // try normal method
|
---|
280 | try {
|
---|
281 | instanceClass = Class.forName(classname);
|
---|
282 | } catch (Throwable e2) {
|
---|
283 | throw new BuildException("scriptdef: Unable to load "
|
---|
284 | + "class " + classname + " for nested element <"
|
---|
285 | + elementName + ">", e2);
|
---|
286 | }
|
---|
287 | }
|
---|
288 |
|
---|
289 | try {
|
---|
290 | instance = instanceClass.newInstance();
|
---|
291 | } catch (Throwable e) {
|
---|
292 | throw new BuildException("scriptdef: Unable to create "
|
---|
293 | + "element of class " + classname + " for nested "
|
---|
294 | + "element <" + elementName + ">", e);
|
---|
295 | }
|
---|
296 | getProject().setProjectReference(instance);
|
---|
297 | }
|
---|
298 |
|
---|
299 | if (instance == null) {
|
---|
300 | throw new BuildException("<" + name + "> is unable to create "
|
---|
301 | + "the <" + elementName + "> nested element");
|
---|
302 | }
|
---|
303 | return instance;
|
---|
304 | }
|
---|
305 |
|
---|
306 | /**
|
---|
307 | * Execute the script.
|
---|
308 | *
|
---|
309 | * @param attributes collection of attributes
|
---|
310 | *
|
---|
311 | * @param elements a list of nested element values.
|
---|
312 | */
|
---|
313 | public void executeScript(Map attributes, Map elements) {
|
---|
314 | runner.addBean("attributes", attributes);
|
---|
315 | runner.addBean("elements", elements);
|
---|
316 | runner.addBean("project", getProject());
|
---|
317 | runner.executeScript("scriptdef_" + name);
|
---|
318 | }
|
---|
319 |
|
---|
320 |
|
---|
321 | /**
|
---|
322 | * Defines the language (required).
|
---|
323 | *
|
---|
324 | * @param language the scripting language name for the script.
|
---|
325 | */
|
---|
326 | public void setLanguage(String language) {
|
---|
327 | runner.setLanguage(language);
|
---|
328 | }
|
---|
329 |
|
---|
330 | /**
|
---|
331 | * Load the script from an external file ; optional.
|
---|
332 | *
|
---|
333 | * @param file the file containing the script source.
|
---|
334 | */
|
---|
335 | public void setSrc(File file) {
|
---|
336 | runner.setSrc(file);
|
---|
337 | }
|
---|
338 |
|
---|
339 | /**
|
---|
340 | * Set the script text.
|
---|
341 | *
|
---|
342 | * @param text a component of the script text to be added.
|
---|
343 | */
|
---|
344 | public void addText(String text) {
|
---|
345 | runner.addText(text);
|
---|
346 | }
|
---|
347 | }
|
---|
348 |
|
---|