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.

Execute.java 6.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.taskdefs;
  55. import java.io.IOException;
  56. import java.io.InputStream;
  57. import java.io.OutputStream;
  58. /**
  59. * Runs an external program.
  60. *
  61. * @author thomas.haas@softwired-inc.com
  62. */
  63. public class Execute {
  64. /** Invalid exit code. **/
  65. public final static int INVALID = Integer.MAX_VALUE;
  66. private String[] cmdl = null;
  67. private String[] env = null;
  68. private int exitValue = INVALID;
  69. private ExecuteStreamHandler streamHandler;
  70. private ExecuteWatchdog watchdog;
  71. /**
  72. * Creates a new execute object using <code>PumpStreamHandler</code> for
  73. * stream handling.
  74. */
  75. public Execute() {
  76. this(new PumpStreamHandler(), null);
  77. }
  78. /**
  79. * Creates a new execute object.
  80. *
  81. * @param streamHandler the stream handler used to handle the input and
  82. * output streams of the subprocess.
  83. * @param watchdog a watchdog for the subprocess or <code>null</code> to
  84. * to disable a timeout for the subprocess.
  85. */
  86. public Execute(ExecuteStreamHandler streamHandler, ExecuteWatchdog watchdog) {
  87. this.streamHandler = streamHandler;
  88. this.watchdog = watchdog;
  89. }
  90. /**
  91. * Returns the commandline used to create a subprocess.
  92. *
  93. * @return the commandline used to create a subprocess
  94. */
  95. public String[] getCommandline() {
  96. return cmdl;
  97. }
  98. /**
  99. * Sets the commandline of the subprocess to launch.
  100. *
  101. * @param commandline the commandline of the subprocess to launch
  102. */
  103. public void setCommandline(String[] commandline) {
  104. cmdl = commandline;
  105. }
  106. /**
  107. * Returns the commandline used to create a subprocess.
  108. *
  109. * @return the commandline used to create a subprocess
  110. */
  111. public String[] getEnvironment() {
  112. return env;
  113. }
  114. /**
  115. * Sets the commandline of the subprocess to launch.
  116. *
  117. * @param commandline the commandline of the subprocess to launch
  118. */
  119. public void setEnvironment(String[] env) {
  120. this.env = env;
  121. }
  122. /**
  123. * Runs a process defined by the command line and returns its exit status.
  124. *
  125. * @return the exit status of the subprocess or <code>INVALID</code>
  126. * @exception java.io.IOExcpetion The exception is thrown, if launching
  127. * of the subprocess failed
  128. */
  129. public int execute() throws IOException {
  130. final Process process = exec();
  131. try {
  132. streamHandler.setProcessInputStream(process.getOutputStream());
  133. streamHandler.setProcessOutputStream(process.getInputStream());
  134. streamHandler.setProcessErrorStream(process.getErrorStream());
  135. } catch (IOException e) {
  136. process.destroy();
  137. throw e;
  138. }
  139. streamHandler.start();
  140. if (watchdog != null) watchdog.start(process);
  141. waitFor(process);
  142. if (watchdog != null) watchdog.stop();
  143. streamHandler.stop();
  144. if (watchdog != null) watchdog.checkException();
  145. return getExitValue();
  146. }
  147. protected Process exec() throws IOException {
  148. return Runtime.getRuntime().exec(getCommandline(), getEnvironment());
  149. }
  150. protected void waitFor(Process process) {
  151. try {
  152. process.waitFor();
  153. setExitValue(process.exitValue());
  154. } catch (InterruptedException e) {}
  155. }
  156. protected void setExitValue(int value) {
  157. exitValue = value;
  158. }
  159. protected int getExitValue() {
  160. return exitValue;
  161. }
  162. }