/plugins/AntFarm/tags/release-1.7/antfarm/TargetRunner.java

#
Java | 355 lines | 256 code | 61 blank | 38 comment | 21 complexity | a1016fce3e00a5838378725a21e9c912 MD5 | raw file

✨ Summary
  1. /*
  2. * TargetRunner.java - Plugin for running Ant builds from jEdit.
  3. * Copyright (C) 2001 Brian Knowles
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package antfarm;
  20. import java.io.File;
  21. import java.io.PrintStream;
  22. import java.util.Enumeration;
  23. import java.util.Properties;
  24. import javax.swing.JOptionPane;
  25. import org.apache.tools.ant.BuildEvent;
  26. import org.apache.tools.ant.DefaultLogger;
  27. import org.apache.tools.ant.Main;
  28. import org.apache.tools.ant.Project;
  29. import org.apache.tools.ant.Target;
  30. import org.gjt.sp.jedit.View;
  31. import org.gjt.sp.jedit.jEdit;
  32. import org.gjt.sp.jedit.GUIUtilities;
  33. import org.gjt.sp.jedit.io.VFSManager;
  34. import org.gjt.sp.util.Log;
  35. import console.Console;
  36. import console.ConsolePlugin;
  37. import console.Output;
  38. import console.Shell;
  39. public class TargetRunner extends Thread
  40. {
  41. PrintStream _out = System.out;
  42. Project runner = new Project();
  43. Target runAnt = new Target();
  44. PrintStream _err = System.err;
  45. DefaultLogger _buildLogger = new DefaultLogger();
  46. Throwable _error = null;
  47. Target _target;
  48. Project _project;
  49. File _buildFile;
  50. View _view;
  51. Output _output;
  52. Properties _userProperties;
  53. PrintStream _consoleOut;
  54. PrintStream _consoleErr;
  55. public TargetRunner(Target target, File buildFile, View view, Output output,
  56. Properties userProperties)
  57. {
  58. init(target, buildFile, view, output, userProperties);
  59. }
  60. public TargetRunner(Project project, File buildFile, View view, Output output,
  61. Properties userProperties)
  62. {
  63. Target target = (Target) project.getTargets().get(project.getDefaultTarget());
  64. init(target, buildFile, view, output, userProperties);
  65. }
  66. public void run()
  67. {
  68. AntFarmPlugin.getErrorSource().clear();
  69. if (jEdit.getBooleanProperty(AntFarmPlugin.OPTION_PREFIX + "save-on-execute"))
  70. {
  71. jEdit.saveAllBuffers(_view, false);
  72. // Have our Ant run wait for any IO to finish up.
  73. VFSManager.waitForRequests();
  74. runAntTarget();
  75. }
  76. else
  77. {
  78. runAntTarget();
  79. }
  80. }
  81. void resetLogging()
  82. {
  83. _consoleErr.flush();
  84. _consoleOut.flush();
  85. System.setOut(_out);
  86. System.setErr(_err);
  87. _project.removeBuildListener(_buildLogger);
  88. }
  89. private void setOutputStreams()
  90. {
  91. System.setOut(_consoleOut);
  92. System.setErr(_consoleErr);
  93. }
  94. private void runAntTarget()
  95. {
  96. boolean useSameJvm = jEdit.getBooleanProperty(AntFarmPlugin.OPTION_PREFIX
  97. + "use-same-jvm");
  98. String antCommand = jEdit.getProperty(AntFarmPlugin.OPTION_PREFIX + "command");
  99. if (!useSameJvm && antCommand == null)
  100. promptForAntCommand();
  101. useSameJvm = jEdit.getBooleanProperty(AntFarmPlugin.OPTION_PREFIX + "use-same-jvm");
  102. // assume we have the console open at this point since it called
  103. // us.
  104. Console console = (Console) _view.getDockableWindowManager().getDockable("console");
  105. if (useSameJvm)
  106. {
  107. // Check if AntFarm is installed
  108. if (jEdit.getPlugin("ant.AntPlugin") == null) {
  109. GUIUtilities.error(_view, "ant-plugin-not-installed", null);
  110. return;
  111. }
  112. setOutputStreams();
  113. loadProjectProperties();
  114. AntFarmPlugin.loadCustomClasspath();
  115. try
  116. {
  117. _project.addBuildListener(_buildLogger);
  118. fireBuildStarted();
  119. _project.executeTarget(_target.getName());
  120. }
  121. catch (RuntimeException exc)
  122. {
  123. _error = exc;
  124. }
  125. catch (Error e)
  126. {
  127. _error = e;
  128. }
  129. finally
  130. {
  131. fireBuildFinished();
  132. resetLogging();
  133. cleanup();
  134. }
  135. }
  136. else
  137. {
  138. String command = " -buildfile ";
  139. command += "\"";
  140. command += _buildFile.getAbsolutePath();
  141. command += "\"";
  142. command += " " + _target.getName();
  143. runAntCommand(command);
  144. }
  145. cleanup();
  146. }
  147. private void cleanup()
  148. {
  149. System.gc();
  150. _output.commandDone();
  151. _view.getTextArea().requestFocus();
  152. }
  153. private void loadProjectProperties()
  154. {
  155. // re-init the project so that system properties are re-loaded.
  156. _project.init();
  157. _project.setUserProperty("ant.version", Main.getAntVersion());
  158. // set user-define properties
  159. Enumeration e = _userProperties.keys();
  160. while (e.hasMoreElements())
  161. {
  162. String arg = (String) e.nextElement();
  163. String value = (String) _userProperties.get(arg);
  164. value = ConsolePlugin.expandSystemShellVariables(_view, value);
  165. _project.setUserProperty(arg, value);
  166. }
  167. _project.setUserProperty("ant.file", _buildFile.getAbsolutePath());
  168. }
  169. private void init(Target target, File buildFile, View view, Output output,
  170. Properties userProperties)
  171. {
  172. _target = target;
  173. // _project = _target.getProject();
  174. _buildFile = buildFile;
  175. _view = view;
  176. _output = output;
  177. _userProperties = userProperties;
  178. //_view.getDockableWindowManager().addDockableWindow("antfarm");
  179. //AntFarm antFarm = (AntFarm) _view.getDockableWindowManager().getDockable("antfarm");
  180. try
  181. {
  182. _project = AntFarmPlugin.parseBuildFile(buildFile.getAbsolutePath());
  183. }
  184. catch (Exception e)
  185. {
  186. Log.log(Log.WARNING, this, "Cannot parse build file: " + e);
  187. }
  188. _consoleErr = new AntPrintStream(System.out, _view, _output);
  189. _consoleOut = new AntPrintStream(System.out, _view, _output);
  190. configureBuildLogger();
  191. // set so jikes prints emacs style errors
  192. _userProperties.setProperty("build.compiler.emacs", "true");
  193. // add in the global properties
  194. addGlobalProperties();
  195. // fire it up
  196. this.start();
  197. }
  198. private void addGlobalProperties()
  199. {
  200. String name = null;
  201. int counter = 1;
  202. while ((name = jEdit.getProperty(PropertiesOptionPane.PROPERTY + counter
  203. + PropertiesOptionPane.NAME)) != null)
  204. {
  205. String value = jEdit.getProperty(PropertiesOptionPane.PROPERTY + counter
  206. + PropertiesOptionPane.VALUE);
  207. _userProperties.setProperty(name, value);
  208. counter++;
  209. }
  210. }
  211. private void runAntCommand(String args)
  212. {
  213. String command = jEdit.getProperty(AntFarmPlugin.OPTION_PREFIX + "command");
  214. if (command == null || command.equals(""))
  215. Log.log(Log.WARNING, this,
  216. "Please set the path to the Ant script you wish to use.");
  217. if (command != null)
  218. {
  219. command = "\"" + command + "\"";
  220. command += AntFarmShell.getAntCommandFragment(_userProperties);
  221. if (jEdit.getBooleanProperty(AntFarmPlugin.OPTION_PREFIX + "output-emacs"))
  222. {
  223. command += " -emacs ";
  224. }
  225. command += args;
  226. Console console = AntFarmPlugin.getConsole(_view);
  227. Shell antShell = console.getShell();
  228. Shell systemShell = ConsolePlugin.getSystemShell();
  229. console.run(systemShell, _output, command);
  230. // Bring the Ant Console to the front.
  231. // AntFarmPlugin.getConsole( _view, true );
  232. console.setShell(antShell);
  233. // Wait for and stop system shell animation.
  234. // ConsolePlugin.getSystemShell().waitFor(console);
  235. // console.setShell(ConsolePlugin.getSystemShell());
  236. // console.getOutput().commandDone();
  237. systemShell.waitFor(console);
  238. console.getOutput().commandDone();
  239. // Bring the Ant Console to the front.
  240. // AntFarmPlugin.getConsole( _view, true );
  241. console.setShell(antShell);
  242. }
  243. }
  244. private void configureBuildLogger()
  245. {
  246. _buildLogger.setEmacsMode(jEdit.getBooleanProperty(AntFarmPlugin.OPTION_PREFIX
  247. + "output-emacs"));
  248. _buildLogger.setOutputPrintStream(_consoleOut);
  249. _buildLogger.setErrorPrintStream(_consoleErr);
  250. _buildLogger.setMessageOutputLevel(jEdit
  251. .getIntegerProperty(AntFarmPlugin.OPTION_PREFIX + "logging-level",
  252. LogLevelEnum.INFO.getValue()));
  253. }
  254. private void fireBuildStarted()
  255. {
  256. BuildEvent event = new BuildEvent(_project);
  257. event.setMessage("Running target: " + _target, Project.MSG_INFO);
  258. _buildLogger.buildStarted(event);
  259. _buildLogger.messageLogged(event);
  260. }
  261. private void fireBuildFinished()
  262. {
  263. BuildEvent event = new BuildEvent(_project);
  264. event.setException(_error);
  265. _buildLogger.buildFinished(event);
  266. }
  267. private String promptForAntCommand()
  268. {
  269. Object[] options = {
  270. jEdit.getProperty(AntFarmPlugin.OPTION_PREFIX + "select-path-button"),
  271. jEdit.getProperty(AntFarmPlugin.OPTION_PREFIX + "use-jvm-button") };
  272. int yesOrNo = JOptionPane.showOptionDialog(_view, jEdit
  273. .getProperty(AntFarmPlugin.OPTION_PREFIX + "prompt"), jEdit
  274. .getProperty(AntFarmPlugin.OPTION_PREFIX + "prompt-dialog-title"),
  275. JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options,
  276. options[0]);
  277. if (yesOrNo == JOptionPane.YES_OPTION)
  278. {
  279. String scriptFilePath = AntFarmOptionPane.promptForAntScript(_view);
  280. jEdit.setProperty(AntFarmPlugin.OPTION_PREFIX + "command", scriptFilePath);
  281. jEdit.setBooleanProperty(AntFarmPlugin.OPTION_PREFIX + "use-same-jvm",
  282. false);
  283. jEdit.propertiesChanged();
  284. return scriptFilePath;
  285. }
  286. else if (yesOrNo == JOptionPane.NO_OPTION)
  287. {
  288. jEdit.setBooleanProperty(
  289. AntFarmPlugin.OPTION_PREFIX + "use-same-jvm", true);
  290. jEdit.propertiesChanged();
  291. }
  292. return null;
  293. }
  294. }