KickJava   Java API By Example, From Geeks To Geeks.

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


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
19 package org.apache.tools.ant.taskdefs;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.Project;
25 import org.apache.tools.ant.taskdefs.condition.Condition;
26 import org.apache.tools.ant.taskdefs.condition.ConditionBase;
27 import org.apache.tools.ant.types.EnumeratedAttribute;
28
29 /**
30  * Wait for an external event to occur.
31  *
32  * Wait for an external process to start or to complete some
33  * task. This is useful with the <code>parallel</code> task to
34  * synchronize the execution of tests with server startup.
35  *
36  * The following attributes can be specified on a waitfor task:
37  * <ul>
38  * <li>maxwait - maximum length of time to wait before giving up</li>
39  * <li>maxwaitunit - The unit to be used to interpret maxwait attribute</li>
40  * <li>checkevery - amount of time to sleep between each check</li>
41  * <li>checkeveryunit - The unit to be used to interpret checkevery attribute</li>
42  * <li>timeoutproperty - name of a property to set if maxwait has been exceeded.</li>
43  * </ul>
44  *
45  * The maxwaitunit and checkeveryunit are allowed to have the following values:
46  * millisecond, second, minute, hour, day and week. The default is millisecond.
47  *
48  * For programmatic use/subclassing, there are two methods that may be overrridden,
49  * <code>processSuccess</code> and <code>processTimeout</code>
50  * @since Ant 1.5
51  *
52  * @ant.task category="control"
53  */

54 public class WaitFor extends ConditionBase {
55     /** default max wait time */
56     private long maxWaitMillis = 1000L * 60L * 3L;
57     private long maxWaitMultiplier = 1L;
58     private long checkEveryMillis = 500L;
59     private long checkEveryMultiplier = 1L;
60     private String JavaDoc timeoutProperty;
61
62     /**
63      * Constructor, names this task "waitfor".
64      */

65     public WaitFor() {
66         super("waitfor");
67     }
68
69     /**
70      * Set the maximum length of time to wait.
71      * @param time a <code>long</code> value
72      */

73     public void setMaxWait(long time) {
74         maxWaitMillis = time;
75     }
76
77     /**
78      * Set the max wait time unit
79      * @param unit an enumerated <code>Unit</code> value
80      */

81     public void setMaxWaitUnit(Unit unit) {
82         maxWaitMultiplier = unit.getMultiplier();
83     }
84
85     /**
86      * Set the time between each check
87      * @param time a <code>long</code> value
88      */

89     public void setCheckEvery(long time) {
90         checkEveryMillis = time;
91     }
92
93     /**
94      * Set the check every time unit
95      * @param unit an enumerated <code>Unit</code> value
96      */

97     public void setCheckEveryUnit(Unit unit) {
98         checkEveryMultiplier = unit.getMultiplier();
99     }
100
101     /**
102      * Name the property to set after a timeout.
103      * @param p the property name
104      */

105     public void setTimeoutProperty(String JavaDoc p) {
106         timeoutProperty = p;
107     }
108
109     /**
110      * Check repeatedly for the specified conditions until they become
111      * true or the timeout expires.
112      * @throws BuildException on error
113      */

114     public void execute() throws BuildException {
115         if (countConditions() > 1) {
116             throw new BuildException("You must not nest more than one "
117                                      + "condition into "
118                                      + getTaskName());
119         }
120         if (countConditions() < 1) {
121             throw new BuildException("You must nest a condition into "
122                                      + getTaskName());
123         }
124         Condition c = (Condition) getConditions().nextElement();
125
126         long savedMaxWaitMillis = maxWaitMillis;
127         long savedCheckEveryMillis = checkEveryMillis;
128         try {
129             maxWaitMillis *= maxWaitMultiplier;
130             checkEveryMillis *= checkEveryMultiplier;
131             long start = System.currentTimeMillis();
132             long end = start + maxWaitMillis;
133
134             while (System.currentTimeMillis() < end) {
135                 if (c.eval()) {
136                     processSuccess();
137                     return;
138                 }
139                 try {
140                     Thread.sleep(checkEveryMillis);
141                 } catch (InterruptedException JavaDoc e) {
142                     // ignore
143
}
144             }
145             processTimeout();
146         } finally {
147             maxWaitMillis = savedMaxWaitMillis;
148             checkEveryMillis = savedCheckEveryMillis;
149         }
150     }
151
152     /**
153      * Actions to be taken on a successful waitfor.
154      * This is an override point. The base implementation does nothing.
155      * @since Ant1.7
156      */

157     protected void processSuccess() {
158         log(getTaskName() + ": condition was met", Project.MSG_VERBOSE);
159     }
160
161     /**
162      * Actions to be taken on an unsuccessful wait.
163      * This is an override point. It is where the timeout processing takes place.
164      * The base implementation sets the timeoutproperty if there was a timeout
165      * and the property was defined.
166      * @since Ant1.7
167      */

168     protected void processTimeout() {
169         log(getTaskName() + ": timeout", Project.MSG_VERBOSE);
170         if (timeoutProperty != null) {
171             getProject().setNewProperty(timeoutProperty, "true");
172         }
173     }
174
175     /**
176      * The enumeration of units:
177      * millisecond, second, minute, hour, day, week
178      * @todo we use timestamps in many places, why not factor this out
179      */

180     public static class Unit extends EnumeratedAttribute {
181
182         /** millisecond string */
183         public static final String JavaDoc MILLISECOND = "millisecond";
184         /** second string */
185         public static final String JavaDoc SECOND = "second";
186         /** minute string */
187         public static final String JavaDoc MINUTE = "minute";
188         /** hour string */
189         public static final String JavaDoc HOUR = "hour";
190         /** day string */
191         public static final String JavaDoc DAY = "day";
192         /** week string */
193         public static final String JavaDoc WEEK = "week";
194
195         private static final String JavaDoc[] UNITS = {
196             MILLISECOND, SECOND, MINUTE, HOUR, DAY, WEEK
197         };
198
199         private Map JavaDoc timeTable = new HashMap JavaDoc();
200
201         /** Constructor the Unit enumerated type. */
202         public Unit() {
203             timeTable.put(MILLISECOND, new Long JavaDoc(1L));
204             timeTable.put(SECOND, new Long JavaDoc(1000L));
205             timeTable.put(MINUTE, new Long JavaDoc(1000L * 60L));
206             timeTable.put(HOUR, new Long JavaDoc(1000L * 60L * 60L));
207             timeTable.put(DAY, new Long JavaDoc(1000L * 60L * 60L * 24L));
208             timeTable.put(WEEK, new Long JavaDoc(1000L * 60L * 60L * 24L * 7L));
209         }
210
211         /**
212          * Convert the value to a multipler (millisecond to unit).
213          * @return a multipler (a long value)
214          */

215         public long getMultiplier() {
216             String JavaDoc key = getValue().toLowerCase();
217             Long JavaDoc l = (Long JavaDoc) timeTable.get(key);
218             return l.longValue();
219         }
220
221         /**
222          * @see EnumeratedAttribute#getValues()
223          */

224         /** {@inheritDoc} */
225         public String JavaDoc[] getValues() {
226             return UNITS;
227         }
228     }
229 }
230
Popular Tags