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 |
|
---|
19 | import org.apache.tools.ant.BuildException;
|
---|
20 | import org.apache.tools.ant.Project;
|
---|
21 | import org.apache.tools.ant.Task;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Sleep, or pause, for a period of time.
|
---|
25 | *
|
---|
26 | * A task for sleeping a short period of time, useful when a
|
---|
27 | * build or deployment process requires an interval between tasks.
|
---|
28 | *<p>
|
---|
29 | * A negative value can be supplied to any of attributes provided the total sleep time
|
---|
30 | * is positive, pending fundamental changes in physics and JVM
|
---|
31 | * execution times</p>
|
---|
32 | * Note that sleep times are always hints to be interpreted by the OS how it feels
|
---|
33 | * small times may either be ignored or rounded up to a minimum timeslice. Note
|
---|
34 | * also that the system clocks often have a fairly low granularity too, which complicates
|
---|
35 | * measuring how long a sleep actually took.</p>
|
---|
36 | *
|
---|
37 | * @since Ant 1.4
|
---|
38 | * @ant.task category="utility"
|
---|
39 | */
|
---|
40 |
|
---|
41 | public class Sleep extends Task {
|
---|
42 | /**
|
---|
43 | * failure flag
|
---|
44 | */
|
---|
45 | private boolean failOnError = true;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * sleep seconds
|
---|
49 | */
|
---|
50 | private int seconds = 0;
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * sleep hours
|
---|
54 | */
|
---|
55 | private int hours = 0;
|
---|
56 | /**
|
---|
57 | * sleep minutes
|
---|
58 | */
|
---|
59 | private int minutes = 0;
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * sleep milliseconds
|
---|
63 | */
|
---|
64 | private int milliseconds = 0;
|
---|
65 |
|
---|
66 |
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Creates new instance
|
---|
70 | */
|
---|
71 | public Sleep() {
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * seconds to add to the sleep time
|
---|
77 | *
|
---|
78 | * @param seconds The new Seconds value
|
---|
79 | */
|
---|
80 | public void setSeconds(int seconds) {
|
---|
81 | this.seconds = seconds;
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * hours to add to the sleep time.
|
---|
87 | *
|
---|
88 | * @param hours The new Hours value
|
---|
89 | */
|
---|
90 | public void setHours(int hours) {
|
---|
91 | this.hours = hours;
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * minutes to add to the sleep time
|
---|
97 | *
|
---|
98 | * @param minutes The new Minutes value
|
---|
99 | */
|
---|
100 | public void setMinutes(int minutes) {
|
---|
101 | this.minutes = minutes;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * milliseconds to add to the sleep time
|
---|
107 | *
|
---|
108 | * @param milliseconds The new Milliseconds value
|
---|
109 | */
|
---|
110 | public void setMilliseconds(int milliseconds) {
|
---|
111 | this.milliseconds = milliseconds;
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * sleep for a period of time
|
---|
117 | *
|
---|
118 | * @param millis time to sleep
|
---|
119 | */
|
---|
120 | public void doSleep(long millis) {
|
---|
121 | try {
|
---|
122 | Thread.sleep(millis);
|
---|
123 | } catch (InterruptedException ie) {
|
---|
124 | // Ignore Exception
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * flag controlling whether to break the build on an error.
|
---|
131 | *
|
---|
132 | * @param failOnError The new FailOnError value
|
---|
133 | */
|
---|
134 | public void setFailOnError(boolean failOnError) {
|
---|
135 | this.failOnError = failOnError;
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * return time to sleep
|
---|
141 | *
|
---|
142 | * @return sleep time. if below 0 then there is an error
|
---|
143 | */
|
---|
144 |
|
---|
145 | private long getSleepTime() {
|
---|
146 | return ((((long) hours * 60) + minutes) * 60 + seconds) * 1000
|
---|
147 | + milliseconds;
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * verify parameters
|
---|
153 | *
|
---|
154 | * @throws BuildException if something is invalid
|
---|
155 | */
|
---|
156 | public void validate()
|
---|
157 | throws BuildException {
|
---|
158 | if (getSleepTime() < 0) {
|
---|
159 | throw new BuildException("Negative sleep periods are not "
|
---|
160 | + "supported");
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Executes this build task. Throws org.apache.tools.ant.BuildException
|
---|
167 | * if there is an error during task execution.
|
---|
168 | *
|
---|
169 | * @exception BuildException Description of Exception
|
---|
170 | */
|
---|
171 | public void execute()
|
---|
172 | throws BuildException {
|
---|
173 | try {
|
---|
174 | validate();
|
---|
175 | long sleepTime = getSleepTime();
|
---|
176 | log("sleeping for " + sleepTime + " milliseconds",
|
---|
177 | Project.MSG_VERBOSE);
|
---|
178 | doSleep(sleepTime);
|
---|
179 | } catch (Exception e) {
|
---|
180 | if (failOnError) {
|
---|
181 | throw new BuildException(e);
|
---|
182 | } else {
|
---|
183 | String text = e.toString();
|
---|
184 | log(text, Project.MSG_ERR);
|
---|
185 | }
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | }
|
---|
190 |
|
---|