1 28 29 package com.idaremedia.antx; 30 31 import java.lang.reflect.Field ; 32 import java.util.Map ; 33 34 import org.apache.tools.ant.BuildException; 35 import org.apache.tools.ant.Project; 36 37 import com.idaremedia.apis.UIStringManager; 38 39 import com.idaremedia.antx.apis.ProblemHandler; 40 import com.idaremedia.antx.apis.Requester; 41 import com.idaremedia.antx.helpers.InstanceFactory; 42 import com.idaremedia.antx.helpers.SIDs; 43 import com.idaremedia.antx.helpers.Tk; 44 45 79 80 public class Iteration 81 { 82 86 private static final Map IDs = AntXFixture.newMap(); 88 89 101 private static Iteration newi() 102 { 103 String classname = Tk.loadSpiImpl(Iteration.class,Requester.ANONYMOUS); 104 if (classname!=null) { 105 try { 106 Class c = Class.forName(classname); 107 return (Iteration)c.newInstance(); 108 } catch(Exception anyX) { 109 String error = AntX.uistrs().get("task.bad.custimpl.class1", 110 classname, Iteration.class.getName()); 111 Requester.ANONYMOUS.log(error, Project.MSG_ERR); 112 throw new BuildException(error,anyX); 113 } 114 } 115 return new Iteration(); 116 } 117 118 119 120 124 private static Iteration INSTANCE = newi(); 126 127 128 131 public static final Iteration get() 132 { 133 return INSTANCE; 134 } 135 136 137 138 146 public static final void set(Iteration iteration) 147 { 148 if (iteration==null) { 149 throw new IllegalArgumentException ("iteration reference"); 150 } 151 INSTANCE = iteration; 152 } 153 154 155 156 162 public static final void reset() 163 { 164 INSTANCE = newi(); 165 } 166 167 171 183 public String toString(String uri) 184 { 185 if (uri==null) { 186 throw new IllegalArgumentException (); 187 } 188 if ("classname".equals(uri)) { 189 return getClass().getName(); 190 } 191 if ("id".equals(uri)) { 192 return getITID(); 193 } 194 if (uri.startsWith("property?")) { 195 String property = uri.substring(9); 196 return FixtureExaminer.getIterationProperty(property,null); 197 } 198 if (uri.startsWith("valueuri?")) { 199 String urischeme = uri.substring(9); 200 if (FixtureExaminer.isBuiltinValueURIScheme(urischeme)) { 201 return "builtin"; 202 } 203 Class c = valueURIHandler(urischeme).getClass(); 204 return (c==ValueURIHandler.None.class ? "none" : c.getName()); 205 } 206 return null; 207 } 208 209 210 214 219 protected Iteration() 220 { 221 String sId = null; 222 synchronized(IDs) { 223 sId = SIDs.next(new SIDs.Finder() { 224 public boolean exists(String id) { 225 if (!IDs.containsKey(id)) { 226 IDs.put(id,Boolean.TRUE); 227 return false; 228 } 229 return true; 230 } 231 }); 232 } 233 m_itid = "Iteration"+sId; 234 m_refTable.put("Iteration.ITID",m_itid); 235 m_valueUriFactory.put("null",ValueURIHandler.None.class); 236 m_testField=sId; } 238 239 240 241 247 public final String getITID() 248 { 249 return m_itid; 250 } 251 252 253 254 261 public Object getConfigLock(String aspect) 262 { 263 return m_configLock; 264 } 265 266 267 268 274 public static final Object lock(String aspect) 275 { 276 return Iteration.get().getConfigLock(aspect); 277 } 278 279 280 281 286 protected UIStringManager getUIStrs() 287 { 288 return AntX.uistrs(); 289 } 290 291 292 293 298 public static final UIStringManager uistrs() 299 { 300 return Iteration.get().getUIStrs(); 301 } 302 303 304 305 310 protected String getConfigId() 311 { 312 return AntX.ANTX_CONFIG_ID; 313 } 314 315 316 317 321 public static final String configId() 322 { 323 return Iteration.get().getConfigId(); 324 } 325 326 327 328 332 protected Defaults getDefaults() 333 { 334 return m_defaultsINSTANCE; 335 } 336 337 338 339 343 public static final Defaults defaultdefaults() 344 { 345 return Iteration.get().getDefaults(); 346 } 347 348 349 350 354 protected ExcludedFixture getFixtureExcludes() 355 { 356 return m_xcludedFixtureINSTANCE; 357 } 358 359 360 361 365 public static final ExcludedFixture defaultFixtureExcludes() 366 { 367 return Iteration.get().getFixtureExcludes(); 368 } 369 370 371 372 376 protected Stringifier getLenientStringifier() 377 { 378 return m_stringifierINSTANCE; 379 } 380 381 382 383 387 public static final Stringifier lenientStringifer() 388 { 389 return Iteration.get().getLenientStringifier(); 390 } 391 392 393 394 398 protected Stringifier getStrictStringifier() 399 { 400 return m_strictStringifierINSTANCE; 401 } 402 403 404 405 409 public static final Stringifier strictStringifier() 410 { 411 return Iteration.get().getStrictStringifier(); 412 } 413 414 415 416 420 protected ExportedProperties getExportableProperties() 421 { 422 return m_exportedPropertiesINSTANCE; 423 } 424 425 426 427 431 public static final ExportedProperties exportableProperties() 432 { 433 return Iteration.get().getExportableProperties(); 434 } 435 436 437 438 445 static FixtureOverlays.Handle fixtureOverlays() 446 { 447 return Iteration.get().m_fixtureOverlays; 448 } 449 450 451 452 459 protected Object getNearestOverlayFor(String FXID) 460 { 461 return FixtureOverlays.getContextInstance().nearest(FXID); 462 } 463 464 465 466 473 public static final Object nearestOverlayFor(String FXID) 474 { 475 return Iteration.get().getNearestOverlayFor(FXID); 476 } 477 478 479 480 486 protected UIStringManager getStringManager() 487 { 488 return UISMContext.getStringManagerNoNull(); 489 } 490 491 492 493 497 public static final UIStringManager stringManager() 498 { 499 return Iteration.get().getStringManager(); 500 } 501 502 503 504 508 protected ProblemHandler getProblemsHandler() 509 { 510 return Problems.getDefaultHandlerNoNull(); 511 } 512 513 514 515 519 public static final ProblemHandler problemsHandler() 520 { 521 return Iteration.get().getProblemsHandler(); 522 } 523 524 528 537 public static ValueURIHandler valueURIHandler(String prefix) 538 { 539 if (prefix==null) { 540 throw new IllegalArgumentException (); 541 } 542 return (ValueURIHandler) 543 Iteration.get().m_valueUriFactory.newInstance(prefix); 544 } 545 546 547 548 557 public static boolean initValueURIHandler(String prefix, Class handlerClass) 558 { 559 if (prefix==null || handlerClass==null) { 560 throw new IllegalArgumentException (); 561 } 562 InstanceFactory factory = Iteration.get().m_valueUriFactory; 563 synchronized(factory) { 564 if (!factory.containsKey(prefix)) { 565 factory.put(prefix,handlerClass); 566 return true; 567 } 568 } 569 return false; 570 } 571 572 573 574 583 public static Class replaceValueURIHandler(String prefix, Class handlerClass) 584 { 585 if (prefix==null || handlerClass==null) { 586 throw new IllegalArgumentException (); 587 } 588 InstanceFactory factory = Iteration.get().m_valueUriFactory; 589 return (Class )factory.put(prefix,handlerClass); 590 } 591 592 593 594 601 public static void removeValueURIHandler(String prefix) 602 { 603 if (prefix==null) { 604 throw new IllegalArgumentException (); 605 } 606 InstanceFactory factory = Iteration.get().m_valueUriFactory; 607 factory.put(prefix,null); 608 } 609 610 614 private static final String PROPERTYARG= "property name"; 615 private static final String CATEGORYARG= "category"; 616 617 623 public static Object getProperty(String name) 624 { 625 if (name==null) { 626 throw new IllegalArgumentException (PROPERTYARG); 627 } 628 return Iteration.get().m_refTable.get(name); 629 } 630 631 632 633 640 public static Object getProperty(String category, String name) 641 { 642 if (category==null) { 643 throw new IllegalArgumentException (CATEGORYARG); 644 } 645 if (name==null) { 646 throw new IllegalArgumentException (PROPERTYARG); 647 } 648 Object item = Iteration.get().m_refTable.get(category); 649 if (item instanceof Map ) { 650 return ((Map )item).get(name); 651 } 652 return null; 653 } 654 655 656 657 664 public static Object setProperty(String name, Object value) 665 { 666 if (name==null) { 667 throw new IllegalArgumentException (PROPERTYARG); 668 } 669 return Iteration.get().m_refTable.put(name,value); 670 } 671 672 673 674 683 public static Object setProperty(String category, String name, Object value) 684 { 685 if (category==null) { 686 throw new IllegalArgumentException (CATEGORYARG); 687 } 688 if (name==null) { 689 throw new IllegalArgumentException (PROPERTYARG); 690 } 691 Map reftable = Iteration.get().m_refTable; 692 Object prev = null; 693 synchronized(reftable) { 694 Object item = reftable.get(category); 695 if (item==null) { 696 item = AntXFixture.newSynchronizedMap(); 697 reftable.put(category,item); 698 prev = ((Map )item).put(name,value); 699 } else if (item instanceof Map ) { 700 prev = ((Map )item).put(name,value); 701 } 702 } 703 return prev; 704 } 705 706 707 708 714 public static Object removeProperty(String name) 715 { 716 if (name==null) { 717 throw new IllegalArgumentException (PROPERTYARG); 718 } 719 return Iteration.get().m_refTable.remove(name); 720 } 721 722 723 724 733 public static Object removeProperty(String category, String name) 734 { 735 if (category==null) { 736 throw new IllegalArgumentException (CATEGORYARG); 737 } 738 if (name==null) { 739 throw new IllegalArgumentException (PROPERTYARG); 740 } 741 Object item = Iteration.get().m_refTable.get(category); 742 if (item instanceof Map ) { 743 return ((Map )item).remove(name); 744 } 745 return null; 746 } 747 748 749 750 754 759 protected Defaults m_defaultsINSTANCE= new Defaults(); 760 761 762 768 protected ExcludedFixture m_xcludedFixtureINSTANCE= new ExcludedFixture(); 769 770 771 775 protected Stringifier m_stringifierINSTANCE= new Stringifier(true); 776 777 778 782 protected Stringifier m_strictStringifierINSTANCE= new Stringifier(false); 783 784 785 789 protected ExportedProperties m_exportedPropertiesINSTANCE= new ExportedProperties(); 790 791 792 799 protected final InstanceFactory m_valueUriFactory = 800 new InstanceFactory(ValueURIHandler.None.class); 801 802 803 804 814 protected final Object getFieldValue(String fieldname) 815 throws NoSuchFieldException , IllegalAccessException 816 { 817 if (fieldname==null) { 818 throw new IllegalArgumentException (); 819 } 820 Field f = Tk.findField(getClass(),fieldname); 821 boolean was = f.isAccessible(); 822 if (!was) { 823 f.setAccessible(true); 824 } 825 try { 826 return f.get(this); 827 } finally { 828 if (!was) { 829 f.setAccessible(false); 830 } 831 } 832 } 833 834 835 836 private Object m_configLock = new int[0]; 837 private final String m_itid; 838 private Map m_refTable = AntXFixture.newSynchronizedMap(); 839 840 private final FixtureOverlays.Handle m_fixtureOverlays 847 = new FixtureOverlays.Handle(); 848 849 String m_testField; 851 } 852 853 854 | Popular Tags |