You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ScriptRunnerHelper.java 8.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. * https://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;
  19. import java.io.File;
  20. import org.apache.tools.ant.ProjectComponent;
  21. import org.apache.tools.ant.types.Path;
  22. import org.apache.tools.ant.types.Reference;
  23. import org.apache.tools.ant.types.ResourceCollection;
  24. import org.apache.tools.ant.types.resources.Union;
  25. /**
  26. * A class to help in creating, setting and getting script runners.
  27. */
  28. public class ScriptRunnerHelper {
  29. private ClasspathUtils.Delegate cpDelegate = null;
  30. private File srcFile;
  31. private String encoding;
  32. private String language;
  33. private String text;
  34. private ScriptManager manager = ScriptManager.auto;
  35. private boolean compiled = false;
  36. private boolean setBeans = true;
  37. private ProjectComponent projectComponent;
  38. private ClassLoader scriptLoader = null;
  39. private Union resources = new Union();
  40. /**
  41. * Set the project component associated with this helper.
  42. * @param component the project component that owns this helper.
  43. */
  44. public void setProjectComponent(ProjectComponent component) {
  45. this.projectComponent = component;
  46. }
  47. /**
  48. * Create and set text on a script.
  49. * @return the created or reused script runner.
  50. */
  51. public ScriptRunnerBase getScriptRunner() {
  52. ScriptRunnerBase runner = getRunner();
  53. runner.setCompiled(compiled);
  54. if (encoding != null) {
  55. // set it first, because runner.setSrc() loads immediately the file
  56. runner.setEncoding(encoding);
  57. }
  58. if (srcFile != null) {
  59. runner.setSrc(srcFile);
  60. }
  61. if (text != null) {
  62. runner.addText(text);
  63. }
  64. if (resources != null) {
  65. runner.loadResources(resources);
  66. }
  67. if (setBeans) {
  68. runner.bindToComponent(projectComponent);
  69. } else {
  70. runner.bindToComponentMinimum(projectComponent);
  71. }
  72. return runner;
  73. }
  74. /**
  75. * Classpath to be used when searching for classes and resources.
  76. *
  77. * @return an empty Path instance to be configured by Ant.
  78. */
  79. public Path createClasspath() {
  80. return getClassPathDelegate().createClasspath();
  81. }
  82. /**
  83. * Set the classpath to be used when searching for classes and resources.
  84. *
  85. * @param classpath an Ant Path object containing the search path.
  86. */
  87. public void setClasspath(Path classpath) {
  88. getClassPathDelegate().setClasspath(classpath);
  89. }
  90. /**
  91. * Set the classpath by reference.
  92. *
  93. * @param r a Reference to a Path instance to be used as the classpath
  94. * value.
  95. */
  96. public void setClasspathRef(Reference r) {
  97. getClassPathDelegate().setClasspathref(r);
  98. }
  99. /**
  100. * Load the script from an external file; optional.
  101. *
  102. * @param file the file containing the script source.
  103. */
  104. public void setSrc(File file) {
  105. this.srcFile = file;
  106. }
  107. /**
  108. * Get the external script file; optional.
  109. * @return the file containing the script source.
  110. * @since Ant 1.10.2
  111. */
  112. public File getSrc() {
  113. return srcFile;
  114. }
  115. /**
  116. * Set the encoding of the script from an external file; optional.
  117. *
  118. * @param encoding the encoding of the file containing the script source.
  119. * @since Ant 1.10.2
  120. */
  121. public void setEncoding(String encoding) {
  122. this.encoding = encoding;
  123. }
  124. /**
  125. * Get the external file encoding.
  126. * @return the encoding of the file containing the script source.
  127. * @since Ant 1.10.2
  128. */
  129. public String getEncoding() {
  130. return encoding;
  131. }
  132. /**
  133. * Add script text.
  134. *
  135. * @param text a component of the script text to be added.
  136. */
  137. public void addText(String text) {
  138. this.text = text;
  139. }
  140. /**
  141. * Defines the script manager - defaults to "auto".
  142. *
  143. * @param manager the scripting manager - "bsf" or "javax" or "auto"
  144. */
  145. @Deprecated
  146. public void setManager(String manager) {
  147. setManager(manager == null ? null : ScriptManager.valueOf(manager));
  148. }
  149. /**
  150. * Set the manager.
  151. * @param manager
  152. */
  153. public void setManager(ScriptManager manager) {
  154. this.manager = manager == null ? ScriptManager.auto : manager;
  155. }
  156. /**
  157. * Defines the language (required).
  158. *
  159. * @param language the scripting language name for the script.
  160. */
  161. public void setLanguage(String language) {
  162. this.language = language;
  163. }
  164. /**
  165. * Get the language.
  166. * @return the scripting language.
  167. */
  168. public String getLanguage() {
  169. return language;
  170. }
  171. /**
  172. * Enable the compilation of the script if possible.
  173. * If this is true and the compilation feature is available in
  174. * the script engine, the script is compiled before the first
  175. * evaluation, and should be cached for future evaluations.
  176. * Otherwise, the script is evaluated each time.
  177. * The default is false.
  178. *
  179. * @param compiled the value to set.
  180. */
  181. public void setCompiled(boolean compiled) {
  182. this.compiled = compiled;
  183. }
  184. /**
  185. * Get the compilation feature.
  186. * @return the compilation feature.
  187. */
  188. public boolean getCompiled() {
  189. return this.compiled;
  190. }
  191. /**
  192. * Set the setbeans attribute.
  193. * If this is true, <script> will create variables in the
  194. * script instance for all
  195. * properties, targets and references of the current project.
  196. * It this is false, only the project and self variables will
  197. * be set.
  198. * The default is true.
  199. * @param setBeans the value to set.
  200. */
  201. public void setSetBeans(boolean setBeans) {
  202. this.setBeans = setBeans;
  203. }
  204. /**
  205. * Used when called by scriptdef.
  206. * @param loader the loader used by scriptdef.
  207. */
  208. public void setClassLoader(ClassLoader loader) {
  209. scriptLoader = loader;
  210. }
  211. private synchronized ClassLoader generateClassLoader() {
  212. if (scriptLoader != null) {
  213. return scriptLoader;
  214. }
  215. if (cpDelegate == null) {
  216. scriptLoader = getClass().getClassLoader();
  217. return scriptLoader;
  218. }
  219. scriptLoader = cpDelegate.getClassLoader();
  220. return scriptLoader;
  221. }
  222. private ClasspathUtils.Delegate getClassPathDelegate() {
  223. if (cpDelegate == null) {
  224. if (projectComponent == null) {
  225. throw new IllegalStateException("Can't access classpath without a project component");
  226. }
  227. cpDelegate = ClasspathUtils.getDelegate(projectComponent);
  228. }
  229. return cpDelegate;
  230. }
  231. /**
  232. * Get a script runner.
  233. */
  234. private ScriptRunnerBase getRunner() {
  235. return new ScriptRunnerCreator(projectComponent.getProject()).createRunner(
  236. manager, language, generateClassLoader());
  237. }
  238. /**
  239. * Add any source resource.
  240. *
  241. * @param resource source of script
  242. * @since Ant 1.7.1
  243. */
  244. public void add(ResourceCollection resource) {
  245. resources.add(resource);
  246. }
  247. }