001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs;
019:
020: import org.apache.tools.ant.BuildException;
021: import org.apache.tools.ant.Project;
022: import org.apache.tools.ant.Task;
023:
024: /**
025: * Sleep, or pause, for a period of time.
026: *
027: * A task for sleeping a short period of time, useful when a
028: * build or deployment process requires an interval between tasks.
029: *<p>
030: * A negative value can be supplied to any of attributes provided the total sleep time
031: * is positive, pending fundamental changes in physics and JVM
032: * execution times</p>
033: * Note that sleep times are always hints to be interpreted by the OS how it feels
034: * small times may either be ignored or rounded up to a minimum timeslice. Note
035: * also that the system clocks often have a fairly low granularity too, which complicates
036: * measuring how long a sleep actually took.</p>
037: *
038: * @since Ant 1.4
039: * @ant.task category="utility"
040: */
041:
042: public class Sleep extends Task {
043: /**
044: * failure flag
045: */
046: private boolean failOnError = true;
047:
048: /**
049: * sleep seconds
050: */
051: private int seconds = 0;
052:
053: /**
054: * sleep hours
055: */
056: private int hours = 0;
057: /**
058: * sleep minutes
059: */
060: private int minutes = 0;
061:
062: /**
063: * sleep milliseconds
064: */
065: private int milliseconds = 0;
066:
067: /**
068: * Creates new instance
069: */
070: public Sleep() {
071: }
072:
073: /**
074: * seconds to add to the sleep time
075: *
076: * @param seconds The new Seconds value
077: */
078: public void setSeconds(int seconds) {
079: this .seconds = seconds;
080: }
081:
082: /**
083: * hours to add to the sleep time.
084: *
085: * @param hours The new Hours value
086: */
087: public void setHours(int hours) {
088: this .hours = hours;
089: }
090:
091: /**
092: * minutes to add to the sleep time
093: *
094: * @param minutes The new Minutes value
095: */
096: public void setMinutes(int minutes) {
097: this .minutes = minutes;
098: }
099:
100: /**
101: * milliseconds to add to the sleep time
102: *
103: * @param milliseconds The new Milliseconds value
104: */
105: public void setMilliseconds(int milliseconds) {
106: this .milliseconds = milliseconds;
107: }
108:
109: /**
110: * sleep for a period of time
111: *
112: * @param millis time to sleep
113: */
114: public void doSleep(long millis) {
115: try {
116: Thread.sleep(millis);
117: } catch (InterruptedException ie) {
118: // Ignore Exception
119: }
120: }
121:
122: /**
123: * flag controlling whether to break the build on an error.
124: *
125: * @param failOnError The new FailOnError value
126: */
127: public void setFailOnError(boolean failOnError) {
128: this .failOnError = failOnError;
129: }
130:
131: /**
132: * return time to sleep
133: *
134: * @return sleep time. if below 0 then there is an error
135: */
136:
137: private long getSleepTime() {
138: return ((((long) hours * 60) + minutes) * 60 + seconds) * 1000
139: + milliseconds;
140: }
141:
142: /**
143: * verify parameters
144: *
145: * @throws BuildException if something is invalid
146: */
147: public void validate() throws BuildException {
148: if (getSleepTime() < 0) {
149: throw new BuildException("Negative sleep periods are not "
150: + "supported");
151: }
152: }
153:
154: /**
155: * Executes this build task. Throws org.apache.tools.ant.BuildException
156: * if there is an error during task execution.
157: *
158: * @exception BuildException Description of Exception
159: */
160: public void execute() throws BuildException {
161: try {
162: validate();
163: long sleepTime = getSleepTime();
164: log("sleeping for " + sleepTime + " milliseconds",
165: Project.MSG_VERBOSE);
166: doSleep(sleepTime);
167: } catch (Exception e) {
168: if (failOnError) {
169: throw new BuildException(e);
170: } else {
171: String text = e.toString();
172: log(text, Project.MSG_ERR);
173: }
174: }
175: }
176:
177: }
|