1 28 29 package com.idaremedia.antx.flowcontrol; 30 31 import org.apache.tools.ant.BuildException; 32 import org.apache.tools.ant.Project; 33 import org.apache.tools.ant.types.Reference; 34 35 import com.idaremedia.antx.AntX; 36 import com.idaremedia.antx.apis.Requester; 37 import com.idaremedia.antx.condition.solo.RulesTk; 38 import com.idaremedia.antx.helpers.GenericParameters; 39 import com.idaremedia.antx.helpers.Tk; 40 import com.idaremedia.antx.ownhelpers.ScopedProperties; 41 import com.idaremedia.antx.parameters.FeedbackLevel; 42 import com.idaremedia.antx.starters.TaskSet; 43 44 73 74 public class RepeatedTaskSet extends TaskSet implements HardLimitEnabled 75 { 76 79 public RepeatedTaskSet() 80 { 81 super(AntX.flow+"RepeatedTaskSet"); 82 } 83 84 85 86 90 protected RepeatedTaskSet(String iam) 91 { 92 super(iam); 93 } 94 95 96 97 104 public void setCriteria(Reference testRef) 105 { 106 require_(testRef!=null,"setTest- nonzro test ref"); 107 m_conditionId = testRef; 108 } 109 110 111 112 116 public final void setTest(Reference testRef) 117 { 118 setCriteria(testRef); 119 } 120 121 122 123 128 public final String getCriteria() 129 { 130 return m_conditionId.getRefId(); 131 } 132 133 134 135 140 public void setMaxLoops(int maxLoops) 141 { 142 m_maxLoops = maxLoops>=0 ? maxLoops : Integer.MAX_VALUE; 143 } 144 145 146 147 150 public final boolean hasMax() 151 { 152 return m_maxLoops!=Integer.MAX_VALUE; 153 } 154 155 156 157 161 public final int getMaxLoops() 162 { 163 return hasMax() ? m_maxLoops : -1; 164 } 165 166 167 168 173 public void setHaltIfMax(boolean halt) 174 { 175 m_haltIfMax = halt ? Boolean.TRUE : Boolean.FALSE; 176 } 177 178 179 180 186 public final Boolean getHaltIfMax() 187 { 188 return m_haltIfMax; 189 } 190 191 192 193 200 public void setFeedback(String level) 201 { 202 require_(level!=null,"setFeedback- nonzro level"); 203 FeedbackLevel fbl = FeedbackLevel.from(level); 204 if (fbl==null) { 205 String e = getAntXMsg("task.illegal.param.value", 206 getTaskName(), level,"feedbacklevel"); 207 log(e, Project.MSG_ERR); 208 throw new BuildException(e, getLocation()); 209 } 210 m_fbLevel = fbl; 211 } 212 213 214 215 219 public final FeedbackLevel getFeedbackLevel() 220 { 221 return m_fbLevel; 222 } 223 224 225 226 227 234 public void setI(String cursorName) 235 { 236 require_(cursorName!=null,"setI- nonzro name"); 237 m_iName = cursorName; 238 } 239 240 241 242 246 public final String getCursorName() 247 { 248 return m_iName; 249 } 250 251 252 253 259 public void setContext(Reference refid) 260 { 261 m_evalContext = refid; 262 } 263 264 265 266 270 public final Reference getContext() 271 { 272 return m_evalContext; 273 } 274 275 276 277 283 protected void verifyCanExecute_(String calr) 284 { 285 super.verifyCanExecute_(calr); 286 287 if (m_conditionId==null) { 288 String error = getAntXMsg("task.needs.this.attr", 289 getTaskName(),"criteria"); 290 log(error, Project.MSG_ERR); 291 throw new BuildException(error,getLocation()); 292 } 293 String testId = m_conditionId.getRefId(); 294 RulesTk.verifyTest(testId,m_rqlink); 295 296 if (m_maxLoops==0 && m_haltIfMax==Boolean.TRUE) { 297 if (getFeedbackLevel()!=FeedbackLevel.NONE) { String e = getAntXMsg("flow.loop.alwaysfail"); 299 log(e, Project.MSG_WARN); 300 } 301 } 302 } 303 304 305 306 313 protected void performNestedTasks() throws BuildException 314 { 315 String testId = m_conditionId.getRefId(); 316 int i=0; 317 final int n=m_maxLoops; 318 319 while (i<n) { 320 boolean continu; 321 ScopedProperties overlay = installOverlay(i,getContext()); 322 try { 323 continu = RulesTk.evalTest(testId,m_rqlink); 324 } finally { 325 uninstallOverlay(overlay); 326 overlay = null; 327 } 328 if (!continu) { 329 break; 330 } 331 performIterationOfTheTasksList(); 332 i++; 333 } 334 335 if (i==n) { 336 String msg = getMsg(); 337 if (Tk.isWhitespace(msg)) { 338 msg = getAntXMsg("flow.loop.overflow",String.valueOf(i)); 339 } 340 if (m_haltIfMax==Boolean.TRUE) { 341 log(msg, Project.MSG_ERR); 342 throw new BuildException(msg, getLocation()); 343 } 344 if (!FeedbackLevel.isQuietish(m_fbLevel,false)) { 345 log(msg, Project.MSG_WARN); 346 } 347 } 348 } 349 350 351 352 356 private ScopedProperties installOverlay(int i, Reference context) 357 { 358 Project P= getProject(); 359 ScopedProperties overlay=null; 360 361 if (context!=null) { 363 Object o = P.getReference(context.getRefId()); 364 if (o instanceof GenericParameters) { 365 overlay = new ScopedProperties(P,true); 366 overlay.put((GenericParameters)o); 367 } 368 } 369 if (m_iName!=null) { 370 if (overlay==null) { 371 overlay = new ScopedProperties(P,true); 372 } 373 overlay.put(m_iName, String.valueOf(i)); 374 } 375 if (overlay!=null) { 376 overlay.install(); 377 } 378 return overlay; 379 } 380 381 382 383 384 387 private void uninstallOverlay(ScopedProperties overlay) 388 { 389 if (overlay!=null) { 390 overlay.uninstall(null); 391 overlay=null; 392 } 393 } 394 395 396 397 401 public void unconfigure() 402 { 403 super.unconfigure(); 404 m_conditionId = null; 405 m_maxLoops = Integer.MAX_VALUE; 406 m_haltIfMax = null; 407 m_fbLevel = null; 408 m_iName = null; 409 m_evalContext = null; 410 } 411 412 413 private Requester m_rqlink = new Requester.ForComponent(this); 414 private Reference m_conditionId; 415 private int m_maxLoops = Integer.MAX_VALUE; 416 private Boolean m_haltIfMax; private FeedbackLevel m_fbLevel; 418 private String m_iName; private Reference m_evalContext; 420 } 421 422 423 | Popular Tags |