/plugins/AntFarm/tags/antfarm-0-4-2/TargetExecutor.java

# · Java · 240 lines · 127 code · 29 blank · 84 comment · 10 complexity · 7d245bf2f2d0d54c4ae0fe9a94d33d3e MD5 · raw file

  1. /**
  2. * TargetExecutor.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. /**
  59. @author Chris Scott, Rick Gibbs
  60. */
  61. import java.awt.Color;
  62. import java.io.*;
  63. import java.util.*;
  64. import org.apache.tools.ant.*;
  65. public class TargetExecutor
  66. {
  67. /** Our current message output status. Follows Project.MSG_XXX */
  68. private int msgOutputLevel = Project.MSG_INFO;
  69. /** The build targets */
  70. private Vector targets = new Vector(5);
  71. private File buildFile;
  72. private AntFarmPlugin farm;
  73. private AntFarm window;
  74. private String targetName;
  75. private boolean debug = false;
  76. public TargetExecutor( AntFarmPlugin farm, AntFarm window, File buildFile )
  77. {
  78. this( farm, window, buildFile, null, false );
  79. }
  80. public TargetExecutor( AntFarmPlugin farm, AntFarm window, File buildFile, String targetName )
  81. {
  82. this(farm, window, buildFile, targetName, false );
  83. }
  84. public TargetExecutor( AntFarmPlugin farm, AntFarm window, File buildFile, String targetName, boolean debug )
  85. {
  86. this.buildFile = buildFile;
  87. this.farm = farm;
  88. this.window = window;
  89. this.targetName = targetName;
  90. this.debug = debug;
  91. }
  92. public void execute() throws Exception
  93. {
  94. // Add Timer for the build process.
  95. Date startTime = new Date();
  96. farm.handleBuildMessage(window,
  97. new BuildMessage( "Building at " + startTime + " ..."), Color.blue
  98. );
  99. PrintStream out = System.out;
  100. PrintStream err = System.err;
  101. if ( !debug )
  102. {
  103. System.setOut( this.getStdOut() );
  104. System.setErr( this.getStdErr() );
  105. }
  106. if (msgOutputLevel >= Project.MSG_INFO)
  107. {
  108. farm.handleBuildMessage( window, new BuildMessage( "Buildfile: " + buildFile) );
  109. }
  110. Project project = new Project();
  111. Throwable error = null;
  112. AntFarmLogger logger = null;
  113. try
  114. {
  115. logger = new AntFarmLogger( farm, window );
  116. logger.setOutputPrintStream( this.getStdOut() );
  117. logger.setErrorPrintStream( this.getStdErr() );
  118. project.addBuildListener( logger );
  119. project.init();
  120. // set user-define properties
  121. //Enumeration e = definedProps.keys();
  122. //while (e.hasMoreElements()) {
  123. // String arg = (String)e.nextElement();
  124. // String value = (String)definedProps.get(arg);
  125. // project.setUserProperty(arg, value);
  126. //}
  127. project.setUserProperty( "ant.file" , buildFile.getAbsolutePath() );
  128. // first use the ProjectHelper to create the project object
  129. // from the given build file.
  130. try
  131. {
  132. Class.forName("javax.xml.parsers.SAXParserFactory");
  133. ProjectHelper.configureProject(project, buildFile);
  134. }
  135. catch (NoClassDefFoundError ncdfe)
  136. {
  137. throw new BuildException("No JAXP compliant XML parser found. See http://java.sun.com/xml for the\nreference implementation.", ncdfe);
  138. }
  139. catch (ClassNotFoundException cnfe)
  140. {
  141. throw new BuildException("No JAXP compliant XML parser found. See http://java.sun.com/xml for the\nreference implementation.", cnfe);
  142. }
  143. catch (NullPointerException npe)
  144. {
  145. throw new BuildException("No JAXP compliant XML parser found. See http://java.sun.com/xml for the\nreference implementation.", npe);
  146. }
  147. // make sure that we have a target to execute
  148. if (targetName == null || targetName.length() == 0)
  149. {
  150. targetName = project.getDefaultTarget();
  151. }
  152. project.executeTarget(targetName);
  153. //assume that if we are here, then the build was successful
  154. Date finishTime = new Date();
  155. long difference = finishTime.getTime() - startTime.getTime();
  156. farm.handleBuildMessage( window, new BuildMessage("BUILD SUCCESSFUL: Completed in " +
  157. formatTime(difference)), Color.blue);
  158. }
  159. catch (BuildException be)
  160. {
  161. System.err.println(be.getMessage());
  162. farm.handleBuildMessage( window, new BuildMessage( "\nBUILD FAILED: " +
  163. be.toString()), Color.red );
  164. }
  165. catch (Throwable exc)
  166. {
  167. farm.handleBuildMessage( window, new BuildMessage( exc.toString() ));
  168. exc.printStackTrace(logger.getErrorPrintStream());
  169. }
  170. finally
  171. {
  172. System.setOut( out );
  173. System.setErr( err );
  174. }
  175. }
  176. /**
  177. * Returns a writer in which external classes can send
  178. * <code>String</code> to make them being displayed in the
  179. * console as standard output.
  180. */
  181. public PrintStream getStdOut()
  182. {
  183. return new BuildStream( farm, window );
  184. }
  185. /**
  186. * Returns a writer in which external classes can send
  187. * <code>String</code> to make them being displayed in the
  188. * console as error output.
  189. */
  190. public PrintStream getStdErr()
  191. {
  192. return new BuildStream( farm, window );
  193. }
  194. private static String formatTime(long millis)
  195. {
  196. long seconds = millis / 1000;
  197. long minutes = seconds / 60;
  198. if (minutes > 0)
  199. {
  200. return Long.toString(minutes) + " minute"
  201. + (minutes == 1 ? " " : "s ")
  202. + Long.toString(seconds % 60) + " second"
  203. + (seconds % 60 == 1 ? "" : "s");
  204. }
  205. else
  206. {
  207. return Long.toString(seconds) + " second"
  208. + (seconds % 60 == 1 ? "" : "s");
  209. }
  210. }
  211. }