1 |
|
---|
2 | /*
|
---|
3 | * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
|
---|
4 | *
|
---|
5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
6 | * you may not use this file except in compliance with the License.
|
---|
7 | * 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 | package ise.antelope.tasks;
|
---|
18 |
|
---|
19 | import java.util.Enumeration;
|
---|
20 | import java.util.Hashtable;
|
---|
21 | import java.util.Vector;
|
---|
22 |
|
---|
23 | import ise.library.PrivilegedAccessor;
|
---|
24 |
|
---|
25 | import org.apache.tools.ant.*;
|
---|
26 | import org.apache.tools.ant.taskdefs.condition.Condition;
|
---|
27 | import org.apache.tools.ant.types.EnumeratedAttribute;
|
---|
28 |
|
---|
29 | import ise.antelope.tasks.typedefs.TimeUnit;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Repeatedly executes a set of tasks. Borrowed most of the code from the Limit
|
---|
33 | * task.
|
---|
34 | *
|
---|
35 | * @author Dale Anson
|
---|
36 | * @version $Revision: 1.3 $
|
---|
37 | * @since Ant 1.5
|
---|
38 | */
|
---|
39 | public class Repeat extends Task implements TaskContainer {
|
---|
40 |
|
---|
41 | // storage for nested tasks
|
---|
42 | private Vector tasks = new Vector();
|
---|
43 |
|
---|
44 | // time units, default value is 10 seconds.
|
---|
45 | private long repeatInterval = 10;
|
---|
46 | protected TimeUnit unit = TimeUnit.SECOND_UNIT;
|
---|
47 |
|
---|
48 | // property to set if time limit is reached
|
---|
49 | private String timeoutProperty = null;
|
---|
50 | private String timeoutValue = "true";
|
---|
51 |
|
---|
52 | // should the build fail if any subtasks fail? Default is no.
|
---|
53 | private boolean failOnError = false;
|
---|
54 |
|
---|
55 | private int repeatCount = 1;
|
---|
56 |
|
---|
57 | private BooleanConditionTask condition = null;
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Add a task to repeat.
|
---|
61 | *
|
---|
62 | * @param task A task to execute
|
---|
63 | * @exception BuildException won't happen
|
---|
64 | */
|
---|
65 | public void addTask(Task task) throws BuildException {
|
---|
66 | tasks.addElement(task);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * "until" is the same as a "bool" as used in assert and if. If used, this
|
---|
71 | * task will repeat until either this condition is met or the maximum number
|
---|
72 | * of repetitions has been met, whichever is first.
|
---|
73 | *
|
---|
74 | * @param c The feature to be added to the Until attribute
|
---|
75 | * @exception BuildException Description of Exception
|
---|
76 | */
|
---|
77 | public void addUntil(BooleanConditionTask c) throws BuildException {
|
---|
78 | if (condition == null)
|
---|
79 | condition = c;
|
---|
80 | else
|
---|
81 | throw new BuildException("Can only add one condition.");
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * How long to wait between repeating the nested tasks, default is 10 sec.
|
---|
87 | *
|
---|
88 | * @param wait time between repeats
|
---|
89 | */
|
---|
90 | public void setInterval(int wait) {
|
---|
91 | repeatInterval = wait;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Sets the unit for the time between repeats. Default is seconds.
|
---|
96 | *
|
---|
97 | * @param unit valid values are "millisecond", "second", "minute", "hour",
|
---|
98 | * "day", and "week".
|
---|
99 | */
|
---|
100 | public void setUnit(String unit) {
|
---|
101 | if (unit == null)
|
---|
102 | return;
|
---|
103 | if (unit.equals(TimeUnit.SECOND)) {
|
---|
104 | setRepeatunit(TimeUnit.SECOND_UNIT);
|
---|
105 | return;
|
---|
106 | }
|
---|
107 | if (unit.equals(TimeUnit.MILLISECOND)) {
|
---|
108 | setRepeatunit(TimeUnit.MILLISECOND_UNIT);
|
---|
109 | return;
|
---|
110 | }
|
---|
111 | if (unit.equals(TimeUnit.MINUTE)) {
|
---|
112 | setRepeatunit(TimeUnit.MINUTE_UNIT);
|
---|
113 | return;
|
---|
114 | }
|
---|
115 | if (unit.equals(TimeUnit.HOUR)) {
|
---|
116 | setRepeatunit(TimeUnit.HOUR_UNIT);
|
---|
117 | return;
|
---|
118 | }
|
---|
119 | if (unit.equals(TimeUnit.DAY)) {
|
---|
120 | setRepeatunit(TimeUnit.DAY_UNIT);
|
---|
121 | return;
|
---|
122 | }
|
---|
123 | if (unit.equals(TimeUnit.WEEK)) {
|
---|
124 | setRepeatunit(TimeUnit.WEEK_UNIT);
|
---|
125 | return;
|
---|
126 | }
|
---|
127 |
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Set a number of milliseconds between repeats.
|
---|
132 | *
|
---|
133 | * @param value the number of milliseconds between repeats.
|
---|
134 | */
|
---|
135 | public void setMilliseconds(int value) {
|
---|
136 | setInterval(value);
|
---|
137 | setRepeatunit(TimeUnit.MILLISECOND_UNIT);
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Set a number of seconds between repeats.
|
---|
142 | *
|
---|
143 | * @param value the number of seconds to wait.
|
---|
144 | */
|
---|
145 | public void setSeconds(int value) {
|
---|
146 | setInterval(value);
|
---|
147 | setRepeatunit(TimeUnit.SECOND_UNIT);
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Set a number of minutes between repeats.
|
---|
152 | *
|
---|
153 | * @param value the number of milliseconds to wait.
|
---|
154 | */
|
---|
155 | public void setMinutes(int value) {
|
---|
156 | setInterval(value);
|
---|
157 | setRepeatunit(TimeUnit.MINUTE_UNIT);
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Set a number of hours between repeats.
|
---|
162 | *
|
---|
163 | * @param value the number of hours to wait.
|
---|
164 | */
|
---|
165 | public void setHours(int value) {
|
---|
166 | setInterval(value);
|
---|
167 | setRepeatunit(TimeUnit.HOUR_UNIT);
|
---|
168 | }
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Set a number of days between repeats.
|
---|
172 | *
|
---|
173 | * @param value the number of days to wait.
|
---|
174 | */
|
---|
175 | public void setDays(int value) {
|
---|
176 | setInterval(value);
|
---|
177 | setRepeatunit(TimeUnit.DAY_UNIT);
|
---|
178 | }
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Set a number of weeks between repeats.
|
---|
182 | *
|
---|
183 | * @param value the number of weeks to wait.
|
---|
184 | */
|
---|
185 | public void setWeeks(int value) {
|
---|
186 | setInterval(value);
|
---|
187 | setRepeatunit(TimeUnit.WEEK_UNIT);
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Set the max wait time unit, default is minutes.
|
---|
192 | *
|
---|
193 | * @param unit The new repeatUnit value
|
---|
194 | */
|
---|
195 | public void setRepeatunit(TimeUnit unit) {
|
---|
196 | this.unit = unit;
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Determines whether the build should fail if the time limit has expired on
|
---|
202 | * this task. Default is no.
|
---|
203 | *
|
---|
204 | * @param fail if true, fail the build if the time limit has been reached.
|
---|
205 | */
|
---|
206 | public void setFailonerror(boolean fail) {
|
---|
207 | failOnError = fail;
|
---|
208 | }
|
---|
209 |
|
---|
210 |
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Name the property to set after all repeats are complete.
|
---|
214 | *
|
---|
215 | * @param p name of property
|
---|
216 | */
|
---|
217 | public void setProperty(String p) {
|
---|
218 | timeoutProperty = p;
|
---|
219 | }
|
---|
220 |
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * The value for the property to set after all repeats are complete,
|
---|
224 | * defaults to true.
|
---|
225 | *
|
---|
226 | * @param v value for the property
|
---|
227 | */
|
---|
228 | public void setValue(String v) {
|
---|
229 | timeoutValue = v;
|
---|
230 | }
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * Sets the number of times to repeat, default is 1. Use -1 to repeat
|
---|
234 | * forever.
|
---|
235 | *
|
---|
236 | * @param count The new repeatCount value
|
---|
237 | */
|
---|
238 | public void setCount(int count) {
|
---|
239 | repeatCount = count;
|
---|
240 | }
|
---|
241 |
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * Execute all nested tasks, repeating.
|
---|
245 | *
|
---|
246 | * @exception BuildException Description of the Exception
|
---|
247 | */
|
---|
248 | public void execute() throws BuildException {
|
---|
249 | try {
|
---|
250 | long repeat_interval = repeatInterval * unit.getMultiplier();
|
---|
251 | if (repeat_interval <= 0) {
|
---|
252 | log("Interval is set to 0, will only execute tasks 1 time.");
|
---|
253 | repeatCount = 1;
|
---|
254 | }
|
---|
255 |
|
---|
256 | if (repeatCount >= 1) {
|
---|
257 | for (int i = 0; i < repeatCount; i++) {
|
---|
258 | repeatTasks();
|
---|
259 | if (condition != null && condition.eval()) {
|
---|
260 | break;
|
---|
261 | }
|
---|
262 | if (i + 1 < repeatCount)
|
---|
263 | Thread.currentThread().sleep(repeat_interval);
|
---|
264 | }
|
---|
265 | }
|
---|
266 | else if (repeatCount == -1) {
|
---|
267 | while (true) {
|
---|
268 | repeatTasks();
|
---|
269 | if (condition != null && condition.eval())
|
---|
270 | break;
|
---|
271 | Thread.currentThread().sleep(repeat_interval);
|
---|
272 | }
|
---|
273 | }
|
---|
274 | else if (repeatCount != 0 && repeat_interval == 0) {
|
---|
275 | repeatTasks();
|
---|
276 | }
|
---|
277 | else {
|
---|
278 | // do nothing. To get here, either the repeatCount must be 0, so
|
---|
279 | // don't execute any of the nested tasks, or the repeat_interval
|
---|
280 | // must be less than 0, which is invalid.
|
---|
281 | }
|
---|
282 |
|
---|
283 | if (timeoutProperty != null) {
|
---|
284 | if (timeoutValue == null)
|
---|
285 | timeoutValue = "true";
|
---|
286 | getProject().setUserProperty(timeoutProperty, timeoutValue);
|
---|
287 | }
|
---|
288 | }
|
---|
289 | catch (InterruptedException ie) {
|
---|
290 | throw new BuildException(ie.getMessage());
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | /**
|
---|
295 | * Description of the Method
|
---|
296 | *
|
---|
297 | * @exception BuildException Description of Exception
|
---|
298 | */
|
---|
299 | private void repeatTasks() throws BuildException {
|
---|
300 | try {
|
---|
301 | // executing nested tasks
|
---|
302 | for (int i = 0; i < tasks.size(); i++) {
|
---|
303 | Task currentTask = (Task) tasks.get(i);
|
---|
304 | try {
|
---|
305 | currentTask.perform();
|
---|
306 | }
|
---|
307 | catch (Exception ex) {
|
---|
308 | if (failOnError) {
|
---|
309 | throw ex;
|
---|
310 | }
|
---|
311 | }
|
---|
312 | }
|
---|
313 | }
|
---|
314 | catch (Exception e) {
|
---|
315 | if (failOnError) {
|
---|
316 | throw new BuildException(e.getMessage());
|
---|
317 | }
|
---|
318 | else {
|
---|
319 | log(e.getMessage());
|
---|
320 | }
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | }
|
---|
325 |
|
---|
326 |
|
---|