1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs;
  18. import org.apache.tools.ant.BuildException;
  19. import org.apache.tools.ant.Project;
  20. import org.apache.tools.ant.Task;
  21. /**
  22. * Sleep, or pause, for a period of time.
  23. *
  24. * A task for sleeping a short period of time, useful when a
  25. * build or deployment process requires an interval between tasks.
  26. *<p>
  27. * A negative value can be supplied to any of attributes provided the total sleep time
  28. * is positive, pending fundamental changes in physics and JVM
  29. * execution times</p>
  30. * Note that sleep times are always hints to be interpreted by the OS how it feels
  31. * small times may either be ignored or rounded up to a minimum timeslice. Note
  32. * also that the system clocks often have a fairly low granularity too, which complicates
  33. * measuring how long a sleep actually took.</p>
  34. *
  35. * @since Ant 1.4
  36. * @ant.task category="utility"
  37. */
  38. public class Sleep extends Task {
  39. /**
  40. * failure flag
  41. */
  42. private boolean failOnError = true;
  43. /**
  44. * sleep seconds
  45. */
  46. private int seconds = 0;
  47. /**
  48. * sleep hours
  49. */
  50. private int hours = 0;
  51. /**
  52. * sleep minutes
  53. */
  54. private int minutes = 0;
  55. /**
  56. * sleep milliseconds
  57. */
  58. private int milliseconds = 0;
  59. /**
  60. * Creates new instance
  61. */
  62. public Sleep() {
  63. }
  64. /**
  65. * seconds to add to the sleep time
  66. *
  67. * @param seconds The new Seconds value
  68. */
  69. public void setSeconds(int seconds) {
  70. this.seconds = seconds;
  71. }
  72. /**
  73. * hours to add to the sleep time.
  74. *
  75. * @param hours The new Hours value
  76. */
  77. public void setHours(int hours) {
  78. this.hours = hours;
  79. }
  80. /**
  81. * minutes to add to the sleep time
  82. *
  83. * @param minutes The new Minutes value
  84. */
  85. public void setMinutes(int minutes) {
  86. this.minutes = minutes;
  87. }
  88. /**
  89. * milliseconds to add to the sleep time
  90. *
  91. * @param milliseconds The new Milliseconds value
  92. */
  93. public void setMilliseconds(int milliseconds) {
  94. this.milliseconds = milliseconds;
  95. }
  96. /**
  97. * sleep for a period of time
  98. *
  99. * @param millis time to sleep
  100. */
  101. public void doSleep(long millis) {
  102. try {
  103. Thread.sleep(millis);
  104. } catch (InterruptedException ie) {
  105. }
  106. }
  107. /**
  108. * flag controlling whether to break the build on an error.
  109. *
  110. * @param failOnError The new FailOnError value
  111. */
  112. public void setFailOnError(boolean failOnError) {
  113. this.failOnError = failOnError;
  114. }
  115. /**
  116. * return time to sleep
  117. *
  118. * @return sleep time. if below 0 then there is an error
  119. */
  120. private long getSleepTime() {
  121. return ((((long) hours * 60) + minutes) * 60 + seconds) * 1000
  122. + milliseconds;
  123. }
  124. /**
  125. * verify parameters
  126. *
  127. * @throws BuildException if something is invalid
  128. */
  129. public void validate()
  130. throws BuildException {
  131. if (getSleepTime() < 0) {
  132. throw new BuildException("Negative sleep periods are not "
  133. + "supported");
  134. }
  135. }
  136. /**
  137. * Executes this build task. Throws org.apache.tools.ant.BuildException
  138. * if there is an error during task execution.
  139. *
  140. * @exception BuildException Description of Exception
  141. */
  142. public void execute()
  143. throws BuildException {
  144. try {
  145. validate();
  146. long sleepTime = getSleepTime();
  147. log("sleeping for " + sleepTime + " milliseconds",
  148. Project.MSG_VERBOSE);
  149. doSleep(sleepTime);
  150. } catch (Exception e) {
  151. if (failOnError) {
  152. throw new BuildException(e);
  153. } else {
  154. String text = e.toString();
  155. log(text, Project.MSG_ERR);
  156. }
  157. }
  158. }
  159. }