Debian Codegraph @

src/main/java/net/sf/antcontrib/process/Limit.java

raw symbols SCIP
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 net.sf.antcontrib.process;
18
19
20 import java.util.Enumeration;
21 import java.util.Hashtable;
22 import java.util.Vector;
23
24
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.Task;
27 import org.apache.tools.ant.TaskContainer;
28 import org.apache.tools.ant.types.EnumeratedAttribute;
29
30
31 /**
32 * Limits the amount of time that a task or set of tasks can run. This is useful
33 * for tasks that may "hang" or otherwise not complete in a timely fashion. This
34 * task is done when either the maxwait time has expired or all nested tasks are
35 * complete, whichever is first.
36 *
37 * <p>Developed for use with Antelope, migrated to ant-contrib Oct 2003.
38 *
39 * @author Dale Anson
40 * @author Robert D. Rice
41 * @version $Revision: 1.6 $
42 * @since Ant 1.5
43 */
44 public class Limit extends Task implements TaskContainer {
45
46
47 // storage for nested tasks
48 private Vector tasks = new Vector();
49
50
51 // time units, default value is 3 minutes.
52 private long maxwait = 180;
53 protected TimeUnit unit = TimeUnit.SECOND_UNIT;
54
55 // property to set if time limit is reached
56 private String timeoutProperty = null;
57 private String timeoutValue = "true";
58
59
60 // storage for task currently executing
61 private Task currentTask = null;
62
63
64 // used to control thread stoppage
65 private Thread taskRunner = null;
66
67
68 // should the build fail if the time limit has expired? Default is no.
69 private boolean failOnError = false;
70
71
72 private Exception exception = null;
73
74
75
76
77 /**
78 * Add a task to wait on.
79 *
80 * @param task A task to execute
81 * @exception BuildException won't happen
82 */
83 public void addTask( Task task ) throws BuildException {
84 tasks.addElement( task );
85 }
86
87
88
89
90 /**
91 * How long to wait for all nested tasks to complete, in units.
92 * Default is to wait 3 minutes.
93 *
94 * @param wait time to wait, set to 0 to wait forever.
95 */
96 public void setMaxwait( int wait ) {
97 maxwait = wait;
98 }
99
100 /**
101 * Sets the unit for the max wait. Default is minutes.
102
103 * @param unit valid values are "millisecond", "second", "minute", "hour", "day", and "week".
104
105 */
106 public void setUnit( String unit ) {
107 if ( unit == null )
108 return ;
109 if ( unit.equals( TimeUnit.SECOND ) ) {
110 setMaxWaitUnit( TimeUnit.SECOND_UNIT );
111 return ;
112 }
113 if ( unit.equals( TimeUnit.MILLISECOND ) ) {
114 setMaxWaitUnit( TimeUnit.MILLISECOND_UNIT );
115 return ;
116 }
117 if ( unit.equals( TimeUnit.MINUTE ) ) {
118 setMaxWaitUnit( TimeUnit.MINUTE_UNIT );
119 return ;
120 }
121 if ( unit.equals( TimeUnit.HOUR ) ) {
122 setMaxWaitUnit( TimeUnit.HOUR_UNIT );
123 return ;
124 }
125 if ( unit.equals( TimeUnit.DAY ) ) {
126 setMaxWaitUnit( TimeUnit.DAY_UNIT );
127 return ;
128 }
129 if ( unit.equals( TimeUnit.WEEK ) ) {
130 setMaxWaitUnit( TimeUnit.WEEK_UNIT );
131 return ;
132 }
133
134 }
135
136 /**
137 * Set a millisecond wait value.
138 * @param value the number of milliseconds to wait.
139 */
140 public void setMilliseconds( int value ) {
141 setMaxwait( value );
142 setMaxWaitUnit( TimeUnit.MILLISECOND_UNIT );
143 }
144
145 /**
146 * Set a second wait value.
147 * @param value the number of seconds to wait.
148 */
149 public void setSeconds( int value ) {
150 setMaxwait( value );
151 setMaxWaitUnit( TimeUnit.SECOND_UNIT );
152 }
153
154 /**
155 * Set a minute wait value.
156 * @param value the number of milliseconds to wait.
157 */
158 public void setMinutes( int value ) {
159 setMaxwait( value );
160 setMaxWaitUnit( TimeUnit.MINUTE_UNIT );
161 }
162
163 /**
164 * Set an hours wait value.
165 * @param value the number of hours to wait.
166 */
167 public void setHours( int value ) {
168 setMaxwait( value );
169 setMaxWaitUnit( TimeUnit.HOUR_UNIT );
170 }
171
172 /**
173 * Set a day wait value.
174 * @param value the number of days to wait.
175 */
176 public void setDays( int value ) {
177 setMaxwait( value );
178 setMaxWaitUnit( TimeUnit.DAY_UNIT );
179 }
180
181 /**
182 * Set a week wait value.
183 * @param value the number of weeks to wait.
184 */
185 public void setWeeks( int value ) {
186 setMaxwait( value );
187 setMaxWaitUnit( TimeUnit.WEEK_UNIT );
188 }
189
190 /**
191 * Set the max wait time unit, default is minutes.
192 */
193 public void setMaxWaitUnit( TimeUnit unit ) {
194 this.unit = unit;
195 }
196
197
198 /**
199 * Determines whether the build should fail if the time limit has
200 * expired on this task.
201 * Default is no.
202 *
203 * @param fail if true, fail the build if the time limit has been reached.
204 */
205 public void setFailonerror( boolean fail ) {
206 failOnError = fail;
207 }
208
209
210 /**
211 * Name the property to set after a timeout.
212 *
213 * @param p of property to set if the time limit has been reached.
214 */
215 public void setProperty( String p ) {
216 timeoutProperty = p;
217 }
218
219
220 /**
221 * The value for the property to set after a timeout, defaults to true.
222 *
223 * @param v for the property to set if the time limit has been reached.
224 */
225 public void setValue( String v ) {
226 timeoutValue = v;
227 }
228
229
230 /**
231 * Execute all nested tasks, but stopping execution of nested tasks after
232 * maxwait or when all tasks are done, whichever is first.
233 *
234 * @exception BuildException Description of the Exception
235 */
236 public void execute() throws BuildException {
237 try {
238 // start executing nested tasks
239 final Thread runner =
240 new Thread() {
241 public void run() {
242 Enumeration e = tasks.elements();
243 while ( e.hasMoreElements() ) {
244 if ( taskRunner != this ) {
245 break;
246 }
247 currentTask = ( Task ) e.nextElement();
248 try {
249 currentTask.perform();
250 }
251 catch ( Exception ex ) {
252 if ( failOnError ) {
253 exception = ex;
254 return ;
255 }
256 else {
257 exception = ex;
258 }
259 }
260 }
261 }
262 };
263 taskRunner = runner;
264 runner.start();
265 runner.join( unit.toMillis( maxwait ) );
266
267
268 // stop executing the nested tasks
269 if ( runner.isAlive() ) {
270 taskRunner = null;
271 runner.interrupt();
272 int index = tasks.indexOf( currentTask );
273 StringBuffer not_ran = new StringBuffer();
274 for ( int i = index + 1; i < tasks.size(); i++ ) {
275 not_ran.append( '<' ).append( ( ( Task ) tasks.get( i ) ).getTaskName() ).append( '>' );
276 if ( i < tasks.size() - 1 ) {
277 not_ran.append( ", " );
278 }
279 }
280
281
282 // maybe set timeout property
283 if ( timeoutProperty != null ) {
284 getProject().setNewProperty( timeoutProperty, timeoutValue );
285 }
286
287
288 // create output message
289 StringBuffer msg = new StringBuffer();
290 msg.append( "Interrupted task <" )
291 .append( currentTask.getTaskName() )
292 .append( ">. Waited " )
293 .append( ( maxwait ) ).append( " " ).append( unit.getValue() )
294 .append( ", but this task did not complete." )
295 .append( ( not_ran.length() > 0 ?
296 " The following tasks did not execute: " + not_ran.toString() + "." :
297 "" ) );
298
299
300 // deal with it
301 if ( failOnError ) {
302 throw new BuildException( msg.toString() );
303 }
304 else {
305 log( msg.toString() );
306 }
307 }
308 else if ( failOnError && exception != null ) {
309 throw new BuildException( exception );
310 }
311 }
312 catch ( Exception e ) {
313 throw new BuildException( e );
314 }
315 }
316
317
318 /**
319 * The enumeration of units:
320 * millisecond, second, minute, hour, day, week
321 * Todo: we use timestamps in many places, why not factor this out
322 */
323 public static class TimeUnit extends EnumeratedAttribute {
324
325 public static final String MILLISECOND = "millisecond";
326 public static final String SECOND = "second";
327 public static final String MINUTE = "minute";
328 public static final String HOUR = "hour";
329 public static final String DAY = "day";
330 public static final String WEEK = "week";
331
332 /** static unit objects, for use as sensible defaults */
333 public static final TimeUnit MILLISECOND_UNIT =
334 new TimeUnit( MILLISECOND );
335 public static final TimeUnit SECOND_UNIT =
336 new TimeUnit( SECOND );
337 public static final TimeUnit MINUTE_UNIT =
338 new TimeUnit( MINUTE );
339 public static final TimeUnit HOUR_UNIT =
340 new TimeUnit( HOUR );
341 public static final TimeUnit DAY_UNIT =
342 new TimeUnit( DAY );
343 public static final TimeUnit WEEK_UNIT =
344 new TimeUnit( WEEK );
345
346
347 private static final String[] units = {
348 MILLISECOND, SECOND, MINUTE, HOUR, DAY, WEEK
349 };
350
351 private Hashtable timeTable = new Hashtable();
352
353 public TimeUnit() {
354 timeTable.put( MILLISECOND, new Long( 1L ) );
355 timeTable.put( SECOND, new Long( 1000L ) );
356 timeTable.put( MINUTE, new Long( 1000L * 60L ) );
357 timeTable.put( HOUR, new Long( 1000L * 60L * 60L ) );
358 timeTable.put( DAY, new Long( 1000L * 60L * 60L * 24L ) );
359 timeTable.put( WEEK, new Long( 1000L * 60L * 60L * 24L * 7L ) );
360 }
361
362 /**
363 * private constructor
364 * used for static construction of TimeUnit objects.
365 * @param value String representing the value.
366 */
367 private TimeUnit( String value ) {
368 this( );
369 setValueProgrammatically( value );
370 }
371
372 /**
373 * set the inner value programmatically.
374 * @param value to set
375 */
376 protected void setValueProgrammatically( String value ) {
377 this.value = value;
378 }
379
380 public long getMultiplier() {
381 String key = getValue().toLowerCase();
382 Long l = ( Long ) timeTable.get( key );
383 return l.longValue();
384 }
385
386 public String[] getValues() {
387 return units;
388 }
389
390 /**
391 * convert the time in the current unit, to millis
392 * @param numberOfUnits long expressed in the current objects units
393 * @return long representing the value in millis
394 */
395 public long toMillis( long numberOfUnits ) {
396 return numberOfUnits * getMultiplier( );
397 }
398 }
399 }
400
401
402