| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 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 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class Limit extends Task implements TaskContainer { |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
private Vector tasks = new Vector(); |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
private long maxwait = 180; |
| 53 |
protected TimeUnit unit = TimeUnit.SECOND_UNIT; |
| 54 |
|
| 55 |
|
| 56 |
private String timeoutProperty = null; |
| 57 |
private String timeoutValue = "true"; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
private Task currentTask = null; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
private Thread taskRunner = null; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
private boolean failOnError = false; |
| 70 |
|
| 71 |
|
| 72 |
private Exception exception = null; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public void addTask( Task task ) throws BuildException { |
| 84 |
tasks.addElement( task ); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public void setMaxwait( int wait ) { |
| 97 |
maxwait = wait; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 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 |
|
| 138 |
|
| 139 |
|
| 140 |
public void setMilliseconds( int value ) { |
| 141 |
setMaxwait( value ); |
| 142 |
setMaxWaitUnit( TimeUnit.MILLISECOND_UNIT ); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public void setSeconds( int value ) { |
| 150 |
setMaxwait( value ); |
| 151 |
setMaxWaitUnit( TimeUnit.SECOND_UNIT ); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public void setMinutes( int value ) { |
| 159 |
setMaxwait( value ); |
| 160 |
setMaxWaitUnit( TimeUnit.MINUTE_UNIT ); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public void setHours( int value ) { |
| 168 |
setMaxwait( value ); |
| 169 |
setMaxWaitUnit( TimeUnit.HOUR_UNIT ); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public void setDays( int value ) { |
| 177 |
setMaxwait( value ); |
| 178 |
setMaxWaitUnit( TimeUnit.DAY_UNIT ); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public void setWeeks( int value ) { |
| 186 |
setMaxwait( value ); |
| 187 |
setMaxWaitUnit( TimeUnit.WEEK_UNIT ); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public void setMaxWaitUnit( TimeUnit unit ) { |
| 194 |
this.unit = unit; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public void setFailonerror( boolean fail ) { |
| 206 |
failOnError = fail; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public void setProperty( String p ) { |
| 216 |
timeoutProperty = p; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public void setValue( String v ) { |
| 226 |
timeoutValue = v; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public void execute() throws BuildException { |
| 237 |
try { |
| 238 |
|
| 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 |
|
| 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 |
|
| 283 |
if ( timeoutProperty != null ) { |
| 284 |
getProject().setNewProperty( timeoutProperty, timeoutValue ); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 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 |
|
| 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 |
|
| 320 |
|
| 321 |
|
| 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 |
|
| 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 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
private TimeUnit( String value ) { |
| 368 |
this( ); |
| 369 |
setValueProgrammatically( value ); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 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 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
public long toMillis( long numberOfUnits ) { |
| 396 |
return numberOfUnits * getMultiplier( ); |
| 397 |
} |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|