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