Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 214   Methods: 9
NCLOC: 90   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
WaitFor.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 
 55   
 package org.apache.tools.ant.taskdefs;
 56   
 
 57   
 import java.util.Hashtable;
 58   
 import org.apache.tools.ant.BuildException;
 59   
 import org.apache.tools.ant.taskdefs.condition.Condition;
 60   
 import org.apache.tools.ant.taskdefs.condition.ConditionBase;
 61   
 import org.apache.tools.ant.types.EnumeratedAttribute;
 62   
 
 63   
 /**
 64   
  * Wait for an external event to occur.
 65   
  *
 66   
  * Wait for an external process to start or to complete some
 67   
  * task. This is useful with the <code>parallel</code> task to
 68   
  * syncronize the execution of tests with server startup.
 69   
  *
 70   
  * The following attributes can be specified on a waitfor task:
 71   
  * <ul>
 72   
  * <li>maxwait - maximum length of time to wait before giving up</li>
 73   
  * <li>maxwaitunit - The unit to be used to interpret maxwait attribute</li>
 74   
  * <li>checkevery - amount of time to sleep between each check</li>
 75   
  * <li>checkeveryunit - The unit to be used to interpret checkevery attribute</li>
 76   
  * <li>timeoutproperty - name of a property to set if maxwait has been exceeded.</li>
 77   
  * </ul>
 78   
  *
 79   
  * The maxwaitunit and checkeveryunit are allowed to have the following values:
 80   
  * millesond, second, minute, hour, day and week. The default is millisecond.
 81   
  *
 82   
  * @author <a href="mailto:denis@network365.com">Denis Hennessy</a>
 83   
  * @author Magesh Umasankar
 84   
  *
 85   
  * @since Ant 1.5
 86   
  *
 87   
  * @ant.task category="control"
 88   
  */
 89   
 
 90   
 public class WaitFor extends ConditionBase {
 91   
     private long maxWaitMillis = 1000l * 60l * 3l; // default max wait time
 92   
     private long maxWaitMultiplier = 1l;
 93   
     private long checkEveryMillis = 500l;
 94   
     private long checkEveryMultiplier = 1l;
 95   
     private String timeoutProperty;
 96   
 
 97   
     /**
 98   
      * Set the maximum length of time to wait
 99   
      */
 100  0
     public void setMaxWait(long time) {
 101  0
         maxWaitMillis = time;
 102   
     }
 103   
 
 104   
     /**
 105   
      * Set the max wait time unit
 106   
      */
 107  0
     public void setMaxWaitUnit(Unit unit) {
 108  0
         maxWaitMultiplier = unit.getMultiplier();
 109   
     }
 110   
 
 111   
     /**
 112   
      * Set the time between each check
 113   
      */
 114  0
     public void setCheckEvery(long time) {
 115  0
         checkEveryMillis = time;
 116   
     }
 117   
 
 118   
     /**
 119   
      * Set the check every time unit
 120   
      */
 121  0
     public void setCheckEveryUnit(Unit unit) {
 122  0
         checkEveryMultiplier = unit.getMultiplier();
 123   
     }
 124   
 
 125   
     /**
 126   
      * Name the property to set after a timeout.
 127   
      */
 128  0
     public void setTimeoutProperty(String p) {
 129  0
         timeoutProperty = p;
 130   
     }
 131   
 
 132   
     /**
 133   
      * Check repeatedly for the specified conditions until they become
 134   
      * true or the timeout expires.
 135   
      */
 136  0
     public void execute() throws BuildException {
 137  0
         if (countConditions() > 1) {
 138  0
             throw new BuildException("You must not nest more than one "
 139   
                                      + "condition into <waitfor>");
 140   
         }
 141  0
         if (countConditions() < 1) {
 142  0
             throw new BuildException("You must nest a condition into "
 143   
                                      + "<waitfor>");
 144   
         }
 145  0
         Condition c = (Condition) getConditions().nextElement();
 146   
 
 147  0
         long savedMaxWaitMillis = maxWaitMillis;
 148  0
         long savedCheckEveryMillis = checkEveryMillis;
 149  0
         try {
 150  0
             maxWaitMillis *= maxWaitMultiplier;
 151  0
             checkEveryMillis *= checkEveryMultiplier;
 152  0
             long start = System.currentTimeMillis();
 153  0
             long end = start + maxWaitMillis;
 154   
 
 155  0
             while (System.currentTimeMillis() < end) {
 156  0
                 if (c.eval()) {
 157  0
                     return;
 158   
                 }
 159  0
                 try {
 160  0
                     Thread.sleep(checkEveryMillis);
 161   
                 } catch (InterruptedException e) {
 162   
                 }
 163   
             }
 164   
 
 165  0
             if (timeoutProperty != null) {
 166  0
                 getProject().setNewProperty(timeoutProperty, "true");
 167   
             }
 168   
         } finally {
 169  0
             maxWaitMillis = savedMaxWaitMillis;
 170  0
             checkEveryMillis = savedCheckEveryMillis;
 171   
         }
 172   
     }
 173   
 
 174   
     /**
 175   
      * The enumeration of units: 
 176   
      * millisecond, second, minute, hour, day, week
 177   
      * @todo we use timestamps in many places, why not factor this out
 178   
      */
 179   
     public static class Unit extends EnumeratedAttribute {
 180   
 
 181   
         private static final String MILLISECOND = "millisecond";
 182   
         private static final String SECOND = "second";
 183   
         private static final String MINUTE = "minute";
 184   
         private static final String HOUR = "hour";
 185   
         private static final String DAY = "day";
 186   
         private static final String WEEK = "week";
 187   
 
 188   
         private static final String[] units = {
 189   
             MILLISECOND, SECOND, MINUTE, HOUR, DAY, WEEK
 190   
         };
 191   
 
 192   
         private Hashtable timeTable = new Hashtable();
 193   
 
 194  0
         public Unit() {
 195  0
             timeTable.put(MILLISECOND, new Long(1l));
 196  0
             timeTable.put(SECOND,      new Long(1000l));
 197  0
             timeTable.put(MINUTE,      new Long(1000l * 60l));
 198  0
             timeTable.put(HOUR,        new Long(1000l * 60l * 60l));
 199  0
             timeTable.put(DAY,         new Long(1000l * 60l * 60l * 24l));
 200  0
             timeTable.put(WEEK,        new Long(1000l * 60l * 60l * 24l * 7l));
 201   
         }
 202   
 
 203  0
         public long getMultiplier() {
 204  0
             String key = getValue().toLowerCase();
 205  0
             Long l = (Long) timeTable.get(key);
 206  0
             return l.longValue();
 207   
         }
 208   
 
 209  0
         public String[] getValues() {
 210  0
             return units;
 211   
         }
 212   
     }
 213   
 }
 214