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