1 28 29 package com.idaremedia.antx.flowcontrol.wrap; 30 31 import java.util.Hashtable ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Map ; 35 36 import org.apache.tools.ant.Project; 37 import org.apache.tools.ant.PropertyHelper; 38 import org.apache.tools.ant.UnknownElement; 39 40 import com.idaremedia.antx.AntX; 41 import com.idaremedia.antx.AntXFixture; 42 import com.idaremedia.antx.ExportedProperties; 43 import com.idaremedia.antx.FixtureExaminer; 44 import com.idaremedia.antx.Iteration; 45 import com.idaremedia.antx.apis.Requester; 46 import com.idaremedia.antx.apis.Responses; 47 import com.idaremedia.antx.ownhelpers.ProjectDependentSkeleton; 48 import com.idaremedia.antx.ownhelpers.ProjectPropertiesNet; 49 50 62 63 public class LocalExecutionBubble extends ProjectDependentSkeleton 64 implements ExecutionBubble 65 { 66 private static final String IAM_= AntX.flow+"LocalBubble"; 67 68 69 73 public LocalExecutionBubble() 74 { 75 super(); 76 } 77 78 79 84 public LocalExecutionBubble(String label) 85 { 86 super(); 87 m_label = label; 88 } 89 90 91 92 99 public final void setCarefulObjectChecks(boolean careful) 100 { 101 m_isCareful = careful; 102 } 103 104 105 106 111 public final boolean willBeCareful() 112 { 113 return m_isCareful; 114 } 115 116 117 118 124 public final void setWarningsUpdateProperty(String property) 125 { 126 m_updateProperty = property; 127 } 128 129 130 131 138 public void addFixtureReset(String component, String aspect) 139 { 140 if (m_killMethods==null) { 141 m_killMethods = AntXFixture.newMap(); 142 } 143 m_killMethods.put(component,aspect); 144 } 145 146 147 148 158 public void setNestedBubble(ExecutionBubble innerBubble) 159 { 160 m_innerLayer = innerBubble; 161 } 162 163 164 165 174 public final void setFilterControls(Locals controls) 175 { 176 AntX.verify_(m_propertiesNet==null,IAM_,"setCtrls- not active"); 177 m_controls = controls; 178 } 179 180 181 187 public final boolean isFiltered() 188 { 189 return m_controls!=null; 190 } 191 192 193 194 201 public void setFixtureExcludes(boolean letEmPassThru) 202 { 203 m_defaultExcludes = letEmPassThru; 204 } 205 206 207 208 214 public boolean enter(Requester task) 215 { 216 Project project = task.getProject(); 217 AntX.verify_(project!=null,IAM_,"enter- inited task"); 218 219 setProject(project); 220 221 captureEnvironment(project, task); 222 applyLocals(); 223 224 boolean ok=true; 225 if (m_innerLayer!=null) { 226 ok = m_innerLayer.enter(task); 227 if (!ok) { 228 restoreEnvironment(task); 229 } 230 } 231 return ok; 232 } 233 234 235 236 245 public boolean leave(Requester task) 246 { 247 Project project= task.getProject(); 248 AntX.verify_(getProject()==project,IAM_, 249 "leave- project same as enter()"); 250 251 boolean ok= m_innerLayer==null; 252 try { 253 if (m_innerLayer!=null) { 254 ok = m_innerLayer.leave(task); 255 } 256 } finally { 257 restoreEnvironment(task); 258 resetIterationVarStacks(task); 259 } 260 return ok; 261 } 262 263 264 265 273 private void captureEnvironment(Project project, Requester task) 274 { 275 captureRefList(project); 276 captureVarList(project); 277 m_propertiesNet = new ProjectPropertiesNet(m_label,project); 278 } 279 280 281 282 288 private void restoreEnvironment(Requester task) 289 { 290 m_propertiesNet.uninstall(new Responses.LogUsing(task)); 291 passthruPropList(task); 292 restoreVarList(task); 293 restoreRefList(task); 294 } 295 296 297 298 301 private void applyLocals() 302 { 303 if (m_controls!=null) { 304 applyLocalVars(); 305 applyLocalProperties(); 306 } 307 } 308 309 310 311 312 318 private void applyLocalProperties() 319 { 320 List from = m_controls.getProperties(); 321 if (from!=null) { 322 synchronized(from) { 323 Hashtable ht = new Hashtable (from.size(),0.9f); 324 for (int i=0,N=from.size();i<N;i++) { 325 ConditionalLocal local = (ConditionalLocal)from.get(i); 326 if (local.isEnabled()) { 327 String value = local.getValue(); 329 if (value!=null) { 330 ht.put(local.getName(),value); 331 } 332 else { 333 Boolean f = m_controls.getInheritFlag(local); 334 if (f==null || f==Boolean.FALSE) { 335 ht.put(local.getName(),""); 336 } else { value = getProject().getProperty(local.getName()); 338 if (value==null) { 339 value = ""; 340 } 341 ht.put(local.getName(),value); 342 } 343 } 344 } } m_propertiesNet.seedProperties(ht,false,false); 347 } } } 350 351 352 353 360 private void passthruPropList(Requester task) 361 { 362 if (m_controls!=null) { 363 boolean allowThru = m_controls.isBlocking(); 364 Map edits = m_propertiesNet.getFinalProperties(true,allowThru); 365 if (!edits.isEmpty()) { 366 PropertyHelper ph = PropertyHelper.getPropertyHelper(getProject()); 367 Iterator itr= edits.entrySet().iterator(); 368 while (itr.hasNext()) { 369 Map.Entry e = (Map.Entry )itr.next(); 370 ph.setNewProperty(null,(String )e.getKey(), 371 (String )e.getValue()); } 373 } 374 } 375 } 376 377 378 379 384 private void captureRefList(Project P) 385 { 386 Iterator itr; 387 Map current = P.getReferences(); 388 synchronized(current) { 389 if (willBeCareful()) { 390 Map mp= AntXFixture.newMap(); 391 itr= current.entrySet().iterator(); 392 while(itr.hasNext()) { 393 Map.Entry mE= (Map.Entry )itr.next(); 394 mp.put(mE.getKey(), new RefInfo(mE)); 395 } 396 if (m_defaultExcludes) { 397 List special = Iteration.defaultFixtureExcludes() 398 .copyOfReferencesIfAny(); 399 if (special!=null) { 400 itr= special.iterator(); 401 while (itr.hasNext()) { 402 String refid = itr.next().toString(); 403 if (!mp.containsKey(refid)) { 404 mp.put(refid, new RefInfo(refid)); 405 } 406 } 407 } 408 } 409 m_refInfo= mp; 410 } else { 411 List l= AntXFixture.newList(); 412 itr= current.keySet().iterator(); 413 while (itr.hasNext()) { 414 l.add(itr.next()); 415 } 416 if (m_defaultExcludes) { 417 Iteration.defaultFixtureExcludes().addReferencesTo(l); 418 } 419 m_refInfo= l; 420 } 421 } 422 } 423 424 425 426 437 private void restoreRefList(Requester task) 438 { 439 Project P= task.getProject(); 440 Map current = P.getReferences(); 441 synchronized(current) { 442 if (willBeCareful()) { 443 Map mp = (Map )m_refInfo; 444 current.keySet().retainAll(mp.keySet()); 445 RefInfo ri= new RefInfo(); 446 int whoopsies=0; 447 for (Iterator itr=mp.values().iterator();itr.hasNext();) { 448 RefInfo ri0 = (RefInfo)itr.next(); 449 if (!current.containsKey(ri0.key)) { 450 String warning = AntX.uistrs().get 451 ("fixture.buble.oldref.misin",ri0.key); 452 task.log(warning,Project.MSG_WARN); 453 whoopsies++; 454 } else { 455 ri.set(ri0.key,P); 456 String warning = ri0.compare(ri); 457 if (warning!=null) { 458 task.log(warning,Project.MSG_WARN); 459 whoopsies++; 460 } 461 } 462 } 463 mp.clear(); 465 if (m_updateProperty!=null && whoopsies>0) { 466 String updateProperty = m_updateProperty+"-refs"; 467 FixtureExaminer.checkIfProperty(P,task,updateProperty,true); 468 P.setNewProperty(updateProperty,String.valueOf(whoopsies)); 469 } 470 } 471 else { 472 List l= (List )m_refInfo; 473 current.keySet().retainAll(l); 474 l.clear(); } 476 } 477 m_refInfo=null; 478 } 479 480 481 482 483 488 private void captureVarList(Project P) 489 { 490 if (m_controls!=null && m_controls.hasVariables()) { 491 if (!m_controls.isBlocking()) { 492 m_varInfo = Iteration.exportableProperties().copyOfPropertyNames(); 493 } 494 } 495 } 496 497 498 499 507 private void restoreVarList(Requester task) 508 { 509 if (m_controls!=null && m_controls.hasVariables()) { 510 List vars = m_controls.getVariableNames(); 511 if (m_controls.isBlocking()) { 512 ExportedProperties.delete(vars); 513 } else { 514 List l = (List )m_varInfo; l.addAll(vars); ExportedProperties.retain(l); 517 l.clear(); } 519 } 520 } 521 522 523 524 530 private void applyLocalVars() 531 { 532 List from = m_controls.getVariables(); 533 if (from!=null) { 534 synchronized(from) { 535 for (int i=0,N=from.size();i<N;i++) { 536 ConditionalLocal local = (ConditionalLocal)from.get(i); 537 if (local.isEnabled()) { 538 String value = local.getValue(); 539 if (value!=null) { 540 ExportedProperties.set(local.getName(),value); 541 } 542 else if (m_controls.getInheritFlag(local)==Boolean.FALSE) { 543 ExportedProperties.unset(local.getName()); 544 } 545 } } } } } 550 551 552 553 557 private void resetIterationVarStacks(Requester task) 558 { 559 if (m_killMethods!=null) { 560 Project P= task.getProject(); 561 Iterator itr= m_killMethods.entrySet().iterator(); 562 Responses.LogAndRemember logr = new Responses.LogAndRemember(task); 563 564 while (itr.hasNext()) { 565 Map.Entry mE= (Map.Entry )itr.next(); 566 AntXFixture.reset((String )mE.getKey(),(String )mE.getValue(),logr); 567 if (logr.hadProblem) { 568 if (m_updateProperty!=null) { 569 String updateProperty = m_updateProperty+"-"+mE.getKey(); 570 FixtureExaminer.checkIfProperty(P,task,updateProperty,true); 571 P.setNewProperty(updateProperty,logr.what); 572 } 573 logr.reset(); 574 } 575 } 576 } 577 } 578 579 580 581 private Locals m_controls; 582 private boolean m_isCareful; private boolean m_defaultExcludes = Iteration.defaultdefaults().isFixtureExcludesEnabled(); 584 private ProjectPropertiesNet m_propertiesNet; 585 private Object m_refInfo; 586 private Map m_killMethods; 587 private String m_updateProperty; 588 private ExecutionBubble m_innerLayer; 589 private Object m_varInfo; private String m_label = "isolate"; 591 592 593 594 603 private static class RefInfo 604 { 605 RefInfo() { 606 } 607 RefInfo(Map.Entry e) { 608 set(e.getKey().toString(),e.getValue()); 609 } 610 RefInfo(String refid) { 611 set(refid,null); 612 } 613 void set(String key, Object value) { 614 this.key = key; 615 identityHash = System.identityHashCode(value); 616 contentHash = identityHash; 617 this.unknown = (value instanceof UnknownElement); 618 if (value!=null && !this.unknown) { 619 contentHash = value.hashCode(); 620 } 621 } 622 void set(String key, Project P) { 623 set(key, FixtureExaminer.trueReference(P,key)); 624 } 625 String compare(RefInfo tmp) { 626 if (!unknown) { 627 if (tmp.identityHash!=identityHash) { 628 return Iteration.uistrs().get("fixture.buble.oldref.switched",key); 629 } 630 if (tmp.contentHash!=contentHash) { 631 return Iteration.uistrs().get("fixture.buble.oldref.chged",key); 632 } 633 } 634 return null; 635 } 636 String key; 637 int identityHash; 638 int contentHash; 639 boolean unknown; } 641 } 642 643 644 | Popular Tags |