1 18 package org.apache.tools.ant.taskdefs; 19 20 import org.apache.tools.ant.BuildException; 21 import org.apache.tools.ant.Project; 22 import org.apache.tools.ant.Task; 23 24 41 42 public class Sleep extends Task { 43 46 private boolean failOnError = true; 47 48 51 private int seconds = 0; 52 53 56 private int hours = 0; 57 60 private int minutes = 0; 61 62 65 private int milliseconds = 0; 66 67 68 69 72 public Sleep() { 73 } 74 75 76 81 public void setSeconds(int seconds) { 82 this.seconds = seconds; 83 } 84 85 86 91 public void setHours(int hours) { 92 this.hours = hours; 93 } 94 95 96 101 public void setMinutes(int minutes) { 102 this.minutes = minutes; 103 } 104 105 106 111 public void setMilliseconds(int milliseconds) { 112 this.milliseconds = milliseconds; 113 } 114 115 116 121 public void doSleep(long millis) { 122 try { 123 Thread.sleep(millis); 124 } catch (InterruptedException ie) { 125 } 127 } 128 129 130 135 public void setFailOnError(boolean failOnError) { 136 this.failOnError = failOnError; 137 } 138 139 140 145 146 private long getSleepTime() { 147 return ((((long) hours * 60) + minutes) * 60 + seconds) * 1000 148 + milliseconds; 149 } 150 151 152 157 public void validate() 158 throws BuildException { 159 if (getSleepTime() < 0) { 160 throw new BuildException("Negative sleep periods are not " 161 + "supported"); 162 } 163 } 164 165 166 172 public void execute() 173 throws BuildException { 174 try { 175 validate(); 176 long sleepTime = getSleepTime(); 177 log("sleeping for " + sleepTime + " milliseconds", 178 Project.MSG_VERBOSE); 179 doSleep(sleepTime); 180 } catch (Exception e) { 181 if (failOnError) { 182 throw new BuildException(e); 183 } else { 184 String text = e.toString(); 185 log(text, Project.MSG_ERR); 186 } 187 } 188 } 189 190 } 191 192 | Popular Tags |