/plugins/AntFarm/tags/pre_AntBrowser_import/SimpleAntBridge.java

# · Java · 314 lines · 122 code · 42 blank · 150 comment · 12 complexity · 524031e93438d3f70c1fe166674d334d MD5 · raw file

  1. /*
  2. * AntFarmLogger.java - Ant build utility plugin for jEdit
  3. * Copyright (C) 2000 Chris Scott
  4. * Other contributors: Rick Gibbs
  5. *
  6. * The Apache Software License, Version 1.1
  7. *
  8. * Copyright (c) 1999, 2000 The Apache Software Foundation. All rights
  9. * reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in
  20. * the documentation and/or other materials provided with the
  21. * distribution.
  22. *
  23. * 3. The end-user documentation included with the redistribution, if
  24. * any, must include the following acknowlegement:
  25. * "This product includes software developed by the
  26. * Apache Software Foundation (http://www.apache.org/)."
  27. * Alternately, this acknowlegement may appear in the software itself,
  28. * if and wherever such third-party acknowlegements normally appear.
  29. *
  30. * 4. The names "The Jakarta Project", "Ant", and "Apache Software
  31. * Foundation" must not be used to endorse or promote products derived
  32. * from this software without prior written permission. For written
  33. * permission, please contact apache@apache.org.
  34. *
  35. * 5. Products derived from this software may not be called "Apache"
  36. * nor may "Apache" appear in their names without prior written
  37. * permission of the Apache Group.
  38. *
  39. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  40. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  41. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  42. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  45. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  46. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  47. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  48. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  49. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This software consists of voluntary contributions made by many
  54. * individuals on behalf of the Apache Software Foundation. For more
  55. * information on the Apache Software Foundation, please see
  56. * <http://www.apache.org/>.
  57. */
  58. import java.io.*;
  59. import java.util.*;
  60. import org.apache.tools.ant.*;
  61. import org.gjt.sp.jedit.jEdit;
  62. import org.gjt.sp.util.Log;
  63. /**
  64. * Description of the Class
  65. *
  66. *@author steinbeck
  67. *@created 10. August 2001
  68. */
  69. public class SimpleAntBridge
  70. implements AntBridge
  71. {
  72. private boolean debug = true;
  73. private Project project;
  74. private String buildFile;
  75. private AntFarmPlugin plugin;
  76. private AntFarmLogger logger;
  77. /**
  78. * Create a new <code>SimpleAntBridge</code> .
  79. *
  80. *@since
  81. */
  82. public SimpleAntBridge()
  83. {
  84. project = null;
  85. }
  86. /**
  87. * Set the {@link AntFarmPlugin}.
  88. *
  89. *@param aPlugin The new Plugin value
  90. *@since
  91. */
  92. public void setPlugin(AntFarmPlugin aPlugin)
  93. {
  94. plugin = aPlugin;
  95. logger = new AntFarmLogger(plugin, plugin.getAntFarm());
  96. logger.setOutputPrintStream(getStdOut());
  97. logger.setErrorPrintStream(getStdErr());
  98. }
  99. /**
  100. * Returns the currently used build file.
  101. *
  102. *@return The BuildFile value
  103. *@since
  104. */
  105. public String getBuildFile()
  106. {
  107. return buildFile;
  108. }
  109. /**
  110. * Returns an array of target names.
  111. *
  112. *@return The Targets value
  113. *@since
  114. */
  115. public String[] getTargets()
  116. {
  117. if (project == null)
  118. {
  119. return new String[0];
  120. }
  121. Map targets = project.getTargets();
  122. List targetNames = new ArrayList(targets.size());
  123. for (Iterator i = targets.keySet().iterator(); i.hasNext(); )
  124. {
  125. targetNames.add(i.next());
  126. }
  127. Collections.sort(targetNames);
  128. String[] arr = new String[targetNames.size()];
  129. return (String[]) targetNames.toArray(arr);
  130. }
  131. /**
  132. * Gets the DefaultTarget attribute of the TargetParser object
  133. *
  134. *@return The DefaultTarget value
  135. */
  136. public String getDefaultTarget()
  137. {
  138. if (project == null)
  139. {
  140. return null;
  141. }
  142. return project.getDefaultTarget();
  143. }
  144. /**
  145. * Load the specified build file.
  146. *
  147. *@param buildFile Description of Parameter
  148. *@since
  149. */
  150. public void loadBuildFile(String buildFile)
  151. {
  152. this.buildFile = buildFile;
  153. File buildFileObject = new File(buildFile);
  154. if (!buildFileObject.exists() || buildFileObject.isDirectory())
  155. {
  156. // TODO: Log to AntFarmLogger.
  157. return;
  158. }
  159. project = new Project();
  160. try
  161. {
  162. project.addBuildListener(logger);
  163. project.init();
  164. project.setUserProperty("ant.file", buildFileObject.getAbsolutePath());
  165. //project.setProperty("ant.home", jEdit.getProperty("ant.home"));
  166. ProjectHelper.configureProject(project, buildFileObject);
  167. // TODO: Notify that build file was loaded successfully.
  168. }
  169. catch (Throwable t)
  170. {
  171. // TODO: Log to AntFarmLogger.
  172. Log.log(Log.WARNING, this, t);
  173. project = null;
  174. }
  175. }
  176. /**
  177. * Execute the specified target.
  178. *
  179. *@param targetName Description of Parameter
  180. *@since
  181. */
  182. public void executeTarget(String targetName)
  183. {
  184. PrintStream out = System.out;
  185. PrintStream err = System.err;
  186. if (!debug)
  187. {
  188. System.setOut(getStdOut());
  189. System.setErr(getStdErr());
  190. }
  191. Throwable error = null;
  192. try
  193. {
  194. fireBuildStarted();
  195. if (targetName == null || targetName.trim().length() == 0)
  196. {
  197. targetName = project.getDefaultTarget();
  198. }
  199. project.executeTarget(targetName);
  200. }
  201. catch (Throwable t)
  202. {
  203. error = t;
  204. Log.log(Log.WARNING, this, t);
  205. }
  206. finally
  207. {
  208. fireBuildFinished(error);
  209. }
  210. /*
  211. * farm.handleBuildMessage( new BuildMessage( "\nBUILD FAILED: " +
  212. * be.getMessage()), Color.red );
  213. *
  214. * } catch(Throwable exc) {
  215. * error = exc;
  216. * farm.handleBuildMessage( new BuildMessage( exc.getMessage() ));
  217. *
  218. * } finally {
  219. * project.fireBuildFinished( error );
  220. * System.setOut( out );
  221. * System.setErr( err );
  222. * }
  223. */
  224. }
  225. /**
  226. * Fire the build started event to the {@link AntFarmLogger}. This is hack
  227. * since <code>Project</code> 's <code>fireBuildXXX</code> are declared as <b>
  228. * protected</b> .
  229. *
  230. *@since
  231. */
  232. protected void fireBuildStarted()
  233. {
  234. logger.buildStarted(new BuildEvent(project));
  235. }
  236. /**
  237. * Returns a writer in which external classes can send <code>String</code> to
  238. * make them being displayed in the console as standard output.
  239. *
  240. *@return The StdOut value
  241. *@since
  242. */
  243. private PrintStream getStdOut()
  244. {
  245. return new BuildStream(plugin, plugin.getAntFarm());
  246. }
  247. /**
  248. * Returns a writer in which external classes can send <code>String</code> to
  249. * make them being displayed in the console as error output.
  250. *
  251. *@return The StdErr value
  252. *@since
  253. */
  254. private PrintStream getStdErr()
  255. {
  256. return new BuildStream(plugin, plugin.getAntFarm());
  257. }
  258. /**
  259. * Fire the build finished event to the {@link AntFarmLogger}. This is hack
  260. * since <code>Project</code> 's <code>fireBuildXXX</code> are declared as <b>
  261. * protected</b> .
  262. *
  263. *@param exception Description of Parameter
  264. *@since
  265. */
  266. private void fireBuildFinished(Throwable exception)
  267. {
  268. BuildEvent event = new BuildEvent(project);
  269. event.setException(exception);
  270. logger.buildFinished(event);
  271. }
  272. }