source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/WaitFor.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 5.8 KB
Line 
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
18package org.apache.tools.ant.taskdefs;
19
20import java.util.Hashtable;
21import org.apache.tools.ant.BuildException;
22import org.apache.tools.ant.taskdefs.condition.Condition;
23import org.apache.tools.ant.taskdefs.condition.ConditionBase;
24import org.apache.tools.ant.types.EnumeratedAttribute;
25
26/**
27 * Wait for an external event to occur.
28 *
29 * Wait for an external process to start or to complete some
30 * task. This is useful with the <code>parallel</code> task to
31 * synchronize the execution of tests with server startup.
32 *
33 * The following attributes can be specified on a waitfor task:
34 * <ul>
35 * <li>maxwait - maximum length of time to wait before giving up</li>
36 * <li>maxwaitunit - The unit to be used to interpret maxwait attribute</li>
37 * <li>checkevery - amount of time to sleep between each check</li>
38 * <li>checkeveryunit - The unit to be used to interpret checkevery attribute</li>
39 * <li>timeoutproperty - name of a property to set if maxwait has been exceeded.</li>
40 * </ul>
41 *
42 * The maxwaitunit and checkeveryunit are allowed to have the following values:
43 * millisecond, second, minute, hour, day and week. The default is millisecond.
44 *
45 * @since Ant 1.5
46 *
47 * @ant.task category="control"
48 */
49
50public class WaitFor extends ConditionBase {
51 /** default max wait time */
52 private long maxWaitMillis = 1000L * 60L * 3L;
53 private long maxWaitMultiplier = 1L;
54 private long checkEveryMillis = 500L;
55 private long checkEveryMultiplier = 1L;
56 private String timeoutProperty;
57
58 /**
59 * Set the maximum length of time to wait
60 */
61 public void setMaxWait(long time) {
62 maxWaitMillis = time;
63 }
64
65 /**
66 * Set the max wait time unit
67 */
68 public void setMaxWaitUnit(Unit unit) {
69 maxWaitMultiplier = unit.getMultiplier();
70 }
71
72 /**
73 * Set the time between each check
74 */
75 public void setCheckEvery(long time) {
76 checkEveryMillis = time;
77 }
78
79 /**
80 * Set the check every time unit
81 */
82 public void setCheckEveryUnit(Unit unit) {
83 checkEveryMultiplier = unit.getMultiplier();
84 }
85
86 /**
87 * Name the property to set after a timeout.
88 */
89 public void setTimeoutProperty(String p) {
90 timeoutProperty = p;
91 }
92
93 /**
94 * Check repeatedly for the specified conditions until they become
95 * true or the timeout expires.
96 */
97 public void execute() throws BuildException {
98 if (countConditions() > 1) {
99 throw new BuildException("You must not nest more than one "
100 + "condition into <waitfor>");
101 }
102 if (countConditions() < 1) {
103 throw new BuildException("You must nest a condition into "
104 + "<waitfor>");
105 }
106 Condition c = (Condition) getConditions().nextElement();
107
108 long savedMaxWaitMillis = maxWaitMillis;
109 long savedCheckEveryMillis = checkEveryMillis;
110 try {
111 maxWaitMillis *= maxWaitMultiplier;
112 checkEveryMillis *= checkEveryMultiplier;
113 long start = System.currentTimeMillis();
114 long end = start + maxWaitMillis;
115
116 while (System.currentTimeMillis() < end) {
117 if (c.eval()) {
118 return;
119 }
120 try {
121 Thread.sleep(checkEveryMillis);
122 } catch (InterruptedException e) {
123 // ignore
124 }
125 }
126
127 if (timeoutProperty != null) {
128 getProject().setNewProperty(timeoutProperty, "true");
129 }
130 } finally {
131 maxWaitMillis = savedMaxWaitMillis;
132 checkEveryMillis = savedCheckEveryMillis;
133 }
134 }
135
136 /**
137 * The enumeration of units:
138 * millisecond, second, minute, hour, day, week
139 * @todo we use timestamps in many places, why not factor this out
140 */
141 public static class Unit extends EnumeratedAttribute {
142
143 private static final String MILLISECOND = "millisecond";
144 private static final String SECOND = "second";
145 private static final String MINUTE = "minute";
146 private static final String HOUR = "hour";
147 private static final String DAY = "day";
148 private static final String WEEK = "week";
149
150 private static final String[] units = {
151 MILLISECOND, SECOND, MINUTE, HOUR, DAY, WEEK
152 };
153
154 private Hashtable timeTable = new Hashtable();
155
156 public Unit() {
157 timeTable.put(MILLISECOND, new Long(1L));
158 timeTable.put(SECOND, new Long(1000L));
159 timeTable.put(MINUTE, new Long(1000L * 60L));
160 timeTable.put(HOUR, new Long(1000L * 60L * 60L));
161 timeTable.put(DAY, new Long(1000L * 60L * 60L * 24L));
162 timeTable.put(WEEK, new Long(1000L * 60L * 60L * 24L * 7L));
163 }
164
165 public long getMultiplier() {
166 String key = getValue().toLowerCase();
167 Long l = (Long) timeTable.get(key);
168 return l.longValue();
169 }
170
171 public String[] getValues() {
172 return units;
173 }
174 }
175}
Note: See TracBrowser for help on using the repository browser.