1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.util.HashMap ; 22 import java.util.Map ; 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 54 public class WaitFor extends ConditionBase { 55 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 timeoutProperty; 61 62 65 public WaitFor() { 66 super("waitfor"); 67 } 68 69 73 public void setMaxWait(long time) { 74 maxWaitMillis = time; 75 } 76 77 81 public void setMaxWaitUnit(Unit unit) { 82 maxWaitMultiplier = unit.getMultiplier(); 83 } 84 85 89 public void setCheckEvery(long time) { 90 checkEveryMillis = time; 91 } 92 93 97 public void setCheckEveryUnit(Unit unit) { 98 checkEveryMultiplier = unit.getMultiplier(); 99 } 100 101 105 public void setTimeoutProperty(String p) { 106 timeoutProperty = p; 107 } 108 109 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 e) { 142 } 144 } 145 processTimeout(); 146 } finally { 147 maxWaitMillis = savedMaxWaitMillis; 148 checkEveryMillis = savedCheckEveryMillis; 149 } 150 } 151 152 157 protected void processSuccess() { 158 log(getTaskName() + ": condition was met", Project.MSG_VERBOSE); 159 } 160 161 168 protected void processTimeout() { 169 log(getTaskName() + ": timeout", Project.MSG_VERBOSE); 170 if (timeoutProperty != null) { 171 getProject().setNewProperty(timeoutProperty, "true"); 172 } 173 } 174 175 180 public static class Unit extends EnumeratedAttribute { 181 182 183 public static final String MILLISECOND = "millisecond"; 184 185 public static final String SECOND = "second"; 186 187 public static final String MINUTE = "minute"; 188 189 public static final String HOUR = "hour"; 190 191 public static final String DAY = "day"; 192 193 public static final String WEEK = "week"; 194 195 private static final String [] UNITS = { 196 MILLISECOND, SECOND, MINUTE, HOUR, DAY, WEEK 197 }; 198 199 private Map timeTable = new HashMap (); 200 201 202 public Unit() { 203 timeTable.put(MILLISECOND, new Long (1L)); 204 timeTable.put(SECOND, new Long (1000L)); 205 timeTable.put(MINUTE, new Long (1000L * 60L)); 206 timeTable.put(HOUR, new Long (1000L * 60L * 60L)); 207 timeTable.put(DAY, new Long (1000L * 60L * 60L * 24L)); 208 timeTable.put(WEEK, new Long (1000L * 60L * 60L * 24L * 7L)); 209 } 210 211 215 public long getMultiplier() { 216 String key = getValue().toLowerCase(); 217 Long l = (Long ) timeTable.get(key); 218 return l.longValue(); 219 } 220 221 224 225 public String [] getValues() { 226 return UNITS; 227 } 228 } 229 } 230 | Popular Tags |