KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > Sleep


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

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 /**
25  * Sleep, or pause, for a period of time.
26  *
27  * A task for sleeping a short period of time, useful when a
28  * build or deployment process requires an interval between tasks.
29  *<p>
30  * A negative value can be supplied to any of attributes provided the total sleep time
31  * is positive, pending fundamental changes in physics and JVM
32  * execution times</p>
33  * Note that sleep times are always hints to be interpreted by the OS how it feels
34  * small times may either be ignored or rounded up to a minimum timeslice. Note
35  * also that the system clocks often have a fairly low granularity too, which complicates
36  * measuring how long a sleep actually took.</p>
37  *
38  * @since Ant 1.4
39  * @ant.task category="utility"
40  */

41
42 public class Sleep extends Task {
43     /**
44      * failure flag
45      */

46     private boolean failOnError = true;
47
48     /**
49      * sleep seconds
50      */

51     private int seconds = 0;
52
53     /**
54      * sleep hours
55      */

56     private int hours = 0;
57     /**
58      * sleep minutes
59      */

60     private int minutes = 0;
61
62     /**
63      * sleep milliseconds
64      */

65     private int milliseconds = 0;
66
67
68
69     /**
70      * Creates new instance
71      */

72     public Sleep() {
73     }
74
75
76     /**
77      * seconds to add to the sleep time
78      *
79      * @param seconds The new Seconds value
80      */

81     public void setSeconds(int seconds) {
82         this.seconds = seconds;
83     }
84
85
86     /**
87      * hours to add to the sleep time.
88      *
89      * @param hours The new Hours value
90      */

91     public void setHours(int hours) {
92         this.hours = hours;
93     }
94
95
96     /**
97      * minutes to add to the sleep time
98      *
99      * @param minutes The new Minutes value
100      */

101     public void setMinutes(int minutes) {
102         this.minutes = minutes;
103     }
104
105
106     /**
107      * milliseconds to add to the sleep time
108      *
109      * @param milliseconds The new Milliseconds value
110      */

111     public void setMilliseconds(int milliseconds) {
112         this.milliseconds = milliseconds;
113     }
114
115
116     /**
117      * sleep for a period of time
118      *
119      * @param millis time to sleep
120      */

121     public void doSleep(long millis) {
122         try {
123             Thread.sleep(millis);
124         } catch (InterruptedException JavaDoc ie) {
125             // Ignore Exception
126
}
127     }
128
129
130     /**
131      * flag controlling whether to break the build on an error.
132      *
133      * @param failOnError The new FailOnError value
134      */

135     public void setFailOnError(boolean failOnError) {
136         this.failOnError = failOnError;
137     }
138
139
140     /**
141      * return time to sleep
142      *
143      * @return sleep time. if below 0 then there is an error
144      */

145
146     private long getSleepTime() {
147         return ((((long) hours * 60) + minutes) * 60 + seconds) * 1000
148             + milliseconds;
149     }
150
151
152     /**
153      * verify parameters
154      *
155      * @throws BuildException if something is invalid
156      */

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     /**
167      * Executes this build task. Throws org.apache.tools.ant.BuildException
168      * if there is an error during task execution.
169      *
170      * @exception BuildException Description of Exception
171      */

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 JavaDoc e) {
181             if (failOnError) {
182                 throw new BuildException(e);
183             } else {
184                 String JavaDoc text = e.toString();
185                 log(text, Project.MSG_ERR);
186             }
187         }
188     }
189
190 }
191
192
Popular Tags