| 1 18 package org.objectweb.kilim.description; 19 import java.util.Collection; 20 import java.util.HashSet; 21 import java.util.LinkedHashMap; 22 import java.util.Set; 23 import java.util.Iterator; 24 import java.util.List; 25 import java.util.ArrayList; 26 27 import org.objectweb.kilim.InternalException; 28 import org.objectweb.kilim.KilimException; 29 import org.objectweb.kilim.repository.ResourceLoader; 30 31 35 36 public class TemplateDescription { 37 38 private static final Object LOCK = new Object(); 39 40 private static final int PROVIDER_REFERENCE = 1; 41 private static final int TRANSFORMER_REFERENCE = 2; 42 43 private static final int PROVIDER_KEY = 1; 44 private static final int TRANSFORMER_KEY = 2; 45 private static final int PROPERTY_KEY = 3; 46 private static final int INSTANCE_KEY = 4; 47 private static final int PORT_KEY = 5; 48 private static final int TRIGGER_KEY = 6; 49 private static final int BINDING_KEY = 7; 50 private static final int SLOT_KEY = 8; 51 private static final int PLUG_KEY = 9; 52 private static final int ANY_PROVIDER_KEY = 10; 53 54 private static final int LEGAL_OVERRIDE = 1; 55 private static final int ILLEGAL_OVERRIDE = 2; 56 private static final int NAME_CLASH = 3; 57 private static final int NO_OVERRIDE = 4; 58 59 private String name; 60 private TemplateDescription superTemplate; 61 62 private LinkedHashMap transformers; 63 private LinkedHashMap instances; 64 private LinkedHashMap slots; 65 private LinkedHashMap references; 66 private LinkedHashMap allProviders; 67 68 private Set subTemplates; 69 private List triggers; 70 private List plugs; 71 private List bindings; 72 73 private ResourceLoader resourceLoader; 74 75 private class ReferenceData { 76 int referenceKind; 77 Object reference; 78 79 ReferenceData(int aKind, Object aReference) { 80 referenceKind = aKind; 81 reference = aReference; 82 } 83 } 84 85 private class ProviderData { 86 int referenceKind; 87 NamedElement provider; 88 89 ProviderData(int aKind, NamedElement aProvider) { 90 referenceKind = aKind; 91 provider = aProvider; 92 } 93 } 94 95 101 public TemplateDescription(String aName, TemplateDescription aSuperTemplate) throws KilimException { 102 if (aName == null) { 103 if (superTemplate != null) { 104 throw new KilimException("attempt to create a sub-template with a null name in template " + aSuperTemplate); 105 } else { 106 throw new KilimException("attempt to create a sub-template with a null name "); 107 } 108 } 109 name = aName; 110 111 superTemplate = aSuperTemplate; 112 if (superTemplate != null) { 113 superTemplate.addSubTemplate(this); 114 } 115 } 116 117 122 public TemplateDescription(String aName) throws KilimException { 123 this(aName, null); 124 } 125 126 129 public String getName() { 130 return name; 131 } 132 133 136 public void setName(String aName) throws KilimException { 137 if (aName == null) { 138 throw new KilimException("illegal null name in setName of TemplateImpl"); 139 } 140 name = aName; 141 } 142 143 146 public TemplateDescription getSuperTemplate() { 147 return superTemplate; 148 } 149 150 153 public void setSuperTemplate(TemplateDescription aTemplate) throws KilimException { 154 if (superTemplate != null) { 155 superTemplate.removeSubTemplate(this); 156 } 157 superTemplate = aTemplate; 158 if (superTemplate != null) { 159 superTemplate.addSubTemplate(this); 160 } 161 } 162 163 166 public void addSubTemplate(TemplateDescription aTemplate) throws KilimException { 167 if (aTemplate == null) { 168 throw new KilimException("null template in addSubTemplate"); 169 } 170 if (subTemplates == null) { 171 subTemplates = new HashSet(); 172 } 173 subTemplates.add(aTemplate); 174 175 Iterator iter = getPorts(false); 176 while (iter.hasNext()) { 177 NamedElement element = (NamedElement) iter.next(); 178 addSubTemplate1(element, PORT_KEY, aTemplate); 179 } 180 iter = getProperties(false); 181 while (iter.hasNext()) { 182 NamedElement element = (NamedElement) iter.next(); 183 addSubTemplate1(element, PROPERTY_KEY, aTemplate); 184 } 185 iter = getProviders(false); 186 while (iter.hasNext()) { 187 NamedElement element = (NamedElement) iter.next(); 188 addSubTemplate1(element, PROVIDER_KEY, aTemplate); 189 } 190 iter = getTransformers(false); 191 while (iter.hasNext()) { 192 NamedElement element = (NamedElement) iter.next(); 193 addSubTemplate1(element, TRANSFORMER_KEY, aTemplate); 194 } 195 } 196 197 private void addSubTemplate1(NamedElement element, int tabKey, TemplateDescription aTemplate) throws KilimException { 198 String eName = element.getLocalName(); 199 int status = element.getStatus(); 200 boolean canOverride = (status == KILIM.PUBLIC || status == KILIM.PROTECTED); 201 int result = aTemplate.checkDownOverride(eName, tabKey, canOverride); 202 if (result == NAME_CLASH) { 203 throw new KilimException("setting template " + name + " as super template of " + aTemplate + " leads to a name clash for" + eName); 204 } 205 if (result == ILLEGAL_OVERRIDE) { 206 throw new KilimException("setting template " + name + " as super template of " + aTemplate + " leads to an illegal override" + eName); 207 } 208 } 209 210 213 public void removeSubTemplate(TemplateDescription aTemplate) throws KilimException { 214 if (aTemplate == null) { 215 throw new KilimException("illegal null template in removeSubTemplate"); 216 } 217 subTemplates.remove(aTemplate); 218 } 219 220 223 public Iterator getSubTemplates() { 224 if (subTemplates == null) { 225 return KILIM.EMPTY_ITERATOR; 226 } 227 return subTemplates.iterator(); 228 } 229 230 237 public Iterator getTemplateDefHierarchy(TemplateElementImpl aElement) throws KilimException { 238 String lName = null; 239 if (aElement instanceof Port) { 240 lName = ((Port) aElement).getLocalName(); 241 return getMapDefHierarchy(lName, PORT_KEY); 242 } 243 if (aElement instanceof Property) { 244 lName = ((Property) aElement).getLocalName(); 245 return getMapDefHierarchy(lName, PROPERTY_KEY); 246 } 247 if (aElement instanceof Provider) { 248 lName = ((Provider) aElement).getLocalName(); 249 return getMapDefHierarchy(lName, PROVIDER_KEY); 250 } 251 if (aElement instanceof Transformer) { 252 lName = ((Transformer) aElement).getLocalName(); 253 return getMapDefHierarchy(lName, TRANSFORMER_KEY); 254 } 255 if (aElement instanceof Slot) { 256 lName = ((Slot) aElement).getLocalName(); 257 return getMapDefHierarchy(lName, SLOT_KEY); 258 } 259 if (aElement instanceof Instance) { 260 lName = ((Instance) aElement).getLocalName(); 261 return getMapDefHierarchy(lName, INSTANCE_KEY); 262 } 263 if (aElement instanceof Binding) { 264 lName = ((Binding) aElement).getPortName(); 265 return getListDefHierarchy(lName, BINDING_KEY); 266 } 267 if (aElement instanceof Trigger) { 268 lName = ((Trigger) aElement).getSourceName(); 269 return getListDefHierarchy(lName, TRIGGER_KEY); 270 } 271 if (aElement instanceof Plug) { 272 lName = ((Plug) aElement).getSlotName(); 273 return getListDefHierarchy(lName, PLUG_KEY); 274 } 275 return KILIM.EMPTY_ITERATOR; 276 } 277 278 285 private Iterator getMapDefHierarchy(String aName, int tabKey) throws KilimException { 286 if (aName == null) { 287 throw new KilimException("attempt to get the containing templates of an element through a null name"); 288 } 289 290 TemplateDescription tmp0 = this; 291 List list = new ArrayList(); 292 do { 293 NamedElement elem = tmp0.getLocalMapElement(aName, tabKey); 294 if (elem != null) { 295 list.add(tmp0); 296 } 297 } while ((tmp0 = tmp0.getSuperTemplate()) != null); 298 if (list.size() == 0) { 299 return KILIM.EMPTY_ITERATOR; 300 } 301 return list.iterator(); 302 } 303 304 305 private Iterator getListDefHierarchy(String aName, int tabKey) throws KilimException { 306 if (aName == null) { 307 throw new KilimException("attempt to get the containing templates of an element through a null name"); 308 } 309 310 TemplateDescription tmp0 = this; 311 List list = new ArrayList(); 312 do { 313 Iterator iter = tmp0.getLocalListElements(aName, tabKey); 314 if (iter != KILIM.EMPTY_ITERATOR) { 315 list.add(tmp0); 316 } 317 } while ((tmp0 = tmp0.getSuperTemplate()) != null); 318 if (list.size() == 0) { 319 return KILIM.EMPTY_ITERATOR; 320 } 321 return list.iterator(); 322 } 323 324 327 public BasicNamedElement getAnyProvider(String aName, boolean onlyLocal) throws KilimException { 328 LinkedHashMap map = getMap(ANY_PROVIDER_KEY, false); 329 BasicNamedElement provider = null; 330 if (map != null) { 331 ProviderData data = (ProviderData) map.get(aName); 332 if (data != null) { 333 provider = (BasicNamedElement) data.provider; 334 } 335 } 336 if (provider != null || onlyLocal || superTemplate == null) { 337 return provider; 338 } 339 return (BasicNamedElement) getSuperTemplate().getRMapElement(aName, ANY_PROVIDER_KEY); 340 } 341 342 345 public Iterator getProviders(boolean onlyLocal) throws KilimException { 346 List list = new ArrayList(); 347 Iterator iter = getAllInterfaces(onlyLocal); 348 while (iter.hasNext()) { 349 Object elem = iter.next(); 350 if (elem instanceof Provider) { 351 list.add(elem); 352 } 353 } 354 355 if (list.size() == 0) { 356 return KILIM.EMPTY_ITERATOR; 357 } 358 return list.iterator(); 359 } 360 361 364 public Iterator getPorts(boolean onlyLocal) throws KilimException { 365 List list = new ArrayList(); 366 Iterator iter = getAllInterfaces(onlyLocal); 367 while (iter.hasNext()) { 368 Object elem = iter.next(); 369 if (elem instanceof Port) { 370 list.add(elem); 371 } 372 } 373 374 if (list.size() == 0) { 375 return KILIM.EMPTY_ITERATOR; 376 } 377 return list.iterator(); 378 } 379 380 383 public Iterator getProperties(boolean onlyLocal) throws KilimException { 384 List list = new ArrayList(); 385 Iterator iter = getAllInterfaces(onlyLocal); 386 while (iter.hasNext()) { 387 Object elem = iter.next(); 388 if (elem instanceof Property) { 389 list.add(elem); 390 } 391 } 392 393 if (list.size() == 0) { 394 return KILIM.EMPTY_ITERATOR; 395 } 396 return list.iterator(); 397 } 398 399 402 public Slot getSlot(String aName, boolean onlyLocal) throws KilimException { 403 LinkedHashMap map = getMap(SLOT_KEY, false); 404 Slot slot = null; 405 if (map != null) { 406 slot = (Slot) map.get(aName); 407 } 408 409 if (slot != null || onlyLocal || superTemplate == null) { 410 return slot; 411 } 412 413 return (Slot) getSuperTemplate().getRMapElement(aName, SLOT_KEY); 414 } 415 416 419 public Iterator getSlots(boolean onlyLocal) throws KilimException { 420 if (onlyLocal) { 421 if (slots == null) { 422 return KILIM.EMPTY_ITERATOR; 423 } 424 return slots.values().iterator(); 425 } 426 427 LinkedHashMap map = new LinkedHashMap(); 428 addMapVisible(map, SLOT_KEY, false); 429 return map.values().iterator(); 430 } 431 432 435 public BasicNamedElement getTransformer(String aName, boolean onlyLocal) throws KilimException { 436 LinkedHashMap map = getMap(TRANSFORMER_KEY, false); 437 BasicNamedElement transformer = null; 438 if (map != null) { 439 transformer = (BasicNamedElement) map.get(aName); 440 } 441 442 if (transformer != null || onlyLocal || superTemplate == null) { 443 return transformer; 444 } 445 return (BasicNamedElement) ((TemplateDescription) superTemplate).getRMapElement(aName, TRANSFORMER_KEY); 446 } 447 448 451 public Iterator getTransformers(boolean onlyLocal) throws KilimException { 452 if (onlyLocal) { 453 if (transformers == null) { 454 return KILIM.EMPTY_ITERATOR; 455 } 456 return transformers.values().iterator(); 457 } 458 459 LinkedHashMap map = new LinkedHashMap(); 460 addMapVisible(map, TRANSFORMER_KEY, false); 461 return map.values().iterator(); 462 } 463 464 467 public Instance getInstance(String aName, boolean onlyLocal) throws KilimException { 468 LinkedHashMap map = getMap(INSTANCE_KEY, false); 469 Instance instance = null; 470 if (map != null) { 471 instance = (Instance) map.get(aName); 472 } 473 474 if (instance != null || onlyLocal || superTemplate == null) { 475 return instance; 476 } 477 return (Instance) ((TemplateDescription) superTemplate).getRMapElement(aName, INSTANCE_KEY); 478 } 479 480 483 public Iterator getInstances(boolean onlyLocal) throws KilimException { 484 if (onlyLocal) { 485 if (instances == null) { 486 return KILIM.EMPTY_ITERATOR; 487 } 488 return instances.values().iterator(); 489 } 490 491 LinkedHashMap map = new LinkedHashMap(); 492 addMapVisible(map, INSTANCE_KEY, false); 493 494 Collection vls = map.values(); 495 return vls.iterator(); 496 } 497 498 501 public void addProviderReference(Reference reference) throws KilimException { 502 if (reference == null) { 503 throw new KilimException("illegal null reference in addProviderReference of TemplateImpl " + name); 504 } 505 if (!reference.providesValue()) { 506 throw new KilimException("illegal attempt to use a non provider reference in addProvider"); 507 } 508 509 String rName = reference.getTargetName(); 510 if (rName == null) { 511 throw new KilimException("illegal null name in addProvider of TemplateImpl " + name); 512 } 513 ReferenceData data = (ReferenceData) getReferenceMap().get(rName); 514 515 if (data == null) { 516 getReferenceMap().put(rName, new ReferenceData(PROVIDER_REFERENCE, reference)); 517 return; 518 } 519 } 520 521 524 public void removeProviderReference(String aName) throws KilimException { 525 if (aName == null) { 526 throw new KilimException("illegal null name in removeProviderReference of TemplateImpl " + name); 527 } 528 529 ReferenceData data = (ReferenceData) getReferenceMap().get(aName); 530 531 if (data == null) { 532 throw new KilimException("name " + aName + " is unknown in removeProviderReference of TemplateImpl" + name); 533 } 534 535 if (data.referenceKind == PROVIDER_REFERENCE) { 536 getReferenceMap().remove(aName); 537 } else { 538 throw new KilimException("use of a transformer name in removeProviderReference of TemplateImpl " + name); 539 } 540 } 541 542 545 public Reference getProviderReference(String aName, boolean onlyLocal) throws KilimException { 546 if (aName == null) { 547 throw new KilimException("illegal null name in getProviderReference of TemplateImpl" + name); 548 } 549 550 ReferenceData data = (ReferenceData) getReferenceMap().get(aName); 551 552 if (data == null) { 553 return null; 555 } 556 557 if (data.referenceKind == PROVIDER_REFERENCE) { 558 return (Reference) data.reference; 559 } else { 560 throw new KilimException("use of a transformer name in removeProviderReference of TemplateImpl " + name); 561 } 562 } 563 564 567 public Reference getTransformerReference(String aName, boolean onlyLocal) throws KilimException { 568 if (aName == null) { 569 throw new KilimException("illegal null name in getTransformerReference of TemplateImpl " + name); 570 } 571 572 ReferenceData data = (ReferenceData) getReferenceMap().get(aName); 573 574 if (data == null) { 575 return null; 577 } 578 579 if (data.referenceKind == TRANSFORMER_REFERENCE) { 580 return (Reference) data.reference; 581 } else { 582 throw new KilimException("use of a transformer name in removeProviderReference of TemplateImpl " + name); 583 } 584 } 585 586 589 public Iterator getProviderReferences(boolean onlyLocal) { 590 List list = new ArrayList(); 591 getReferences(list, onlyLocal, PROVIDER_REFERENCE); 592 return list.listIterator(); 593 } 594 595 598 public void addTransformerReference(Reference reference) throws KilimException { 599 if (reference == null) { 600 throw new KilimException("illegal null reference in addProviderReference of TemplateImpl " + name); 601 } 602 if (!reference.performsAction()) { 603 throw new KilimException("attempt to use a non transformer reference in addTransformer"); 604 } 605 606 String rName = reference.getTargetName(); 607 if (rName == null) { 608 throw new KilimException("illegal null name in addTransformer of TemplateImpl"); 609 } 610 ReferenceData data = (ReferenceData) getReferenceMap().get(rName); 611 612 if (data == null) { 613 getReferenceMap().put(rName, new ReferenceData(TRANSFORMER_REFERENCE, reference)); 614 return; 615 } 616 } 617 618 621 public void removeTransformerReference(String aName) throws KilimException { 622 if (aName == null) { 623 throw new KilimException("illegal null name in removeTransformerReference of TemplateImpl " + name); 624 } 625 626 ReferenceData data = (ReferenceData) getReferenceMap().get(aName); 627 628 if (data == null) { 629 throw new KilimException("transformer name " + aName + " is unknown in removeTransformerReference of TemplateImpl " + name); 630 } 631 632 if (data.referenceKind == TRANSFORMER_REFERENCE) { 633 getReferenceMap().remove(aName); 634 } else { 635 throw new KilimException("use of a provider name in removeTransformerReference of TemplateImpl " + name); 636 } 637 } 638 639 642 public Iterator getTransformerReferences(boolean onlyLocal) { 643 List list = new ArrayList(); 644 getReferences(list, onlyLocal, TRANSFORMER_REFERENCE); 645 return list.listIterator(); 646 } 647 648 651 public Reference getReference(String aName, boolean onlyLocal) throws KilimException { 652 if (aName == null) { 653 throw new KilimException("illegal null name in getProviderReference of TemplateImpl" + name); 654 } 655 656 ReferenceData data = (ReferenceData) getReferenceMap().get(aName); 657 658 if (data == null) { 659 return null; 660 } 661 662 return (Reference) data.reference; 663 } 664 665 668 public void addLocalPort(Port port) throws KilimException { 669 addLocalMapElement(port, PORT_KEY); 670 } 671 672 675 public void removeLocalPort(String aName) throws KilimException { 676 removeLocalMapElement(aName, PORT_KEY); 677 } 678 679 682 public void addLocalProperty(Property property) throws KilimException { 683 addLocalMapElement(property, PROPERTY_KEY); 684 } 685 686 689 public void removeLocalProperty(String aName) throws KilimException { 690 removeLocalMapElement(aName, PROPERTY_KEY); 691 } 692 693 696 public void addLocalProvider(BasicNamedElement aProvider) throws KilimException { 697 addLocalMapElement(aProvider, PROVIDER_KEY); 698 } 699 700 703 public void removeLocalProvider(String aName) throws KilimException { 704 removeLocalMapElement(aName, PROVIDER_KEY); 705 } 706 707 710 public void addLocalTransformer(BasicNamedElement transformer) throws KilimException { 711 addLocalMapElement(transformer, TRANSFORMER_KEY); 712 } 713 714 717 public void removeLocalTransformer(String aName) throws KilimException { 718 removeLocalMapElement(aName, TRANSFORMER_KEY); 719 } 720 721 724 public void addLocalInstance(Instance aInstance) throws KilimException { 725 addLocalMapElement(aInstance, INSTANCE_KEY); 726 } 727 728 731 public void removeLocalInstance(String aName) throws KilimException { 732 removeLocalMapElement(aName, INSTANCE_KEY); 733 } 734 735 740 public void addLocalSlot(Slot aSlot) throws KilimException { 741 addLocalMapElement(aSlot, SLOT_KEY); 742 } 743 744 749 public void removeLocalSlot(String aName) throws KilimException { 750 removeLocalMapElement(aName, SLOT_KEY); 751 } 752 753 756 public void addLocalTrigger(Trigger aTrigger) throws KilimException { 757 addLocalListElement(aTrigger, TRIGGER_KEY); 758 } 759 760 763 public void removeLocalTrigger(Trigger aTrigger) throws KilimException { 764 removeLocalListElement(aTrigger, TRIGGER_KEY); 765 } 766 767 770 public void addLocalBinding(Binding aBinding) throws KilimException { 771 addLocalListElement(aBinding, BINDING_KEY); 772 } 773 774 777 public void removeLocalBinding(Binding aBinding) throws KilimException { 778 removeLocalListElement(aBinding, BINDING_KEY); 779 } 780 781 784 public void addLocalPlug(Plug aPlug) throws KilimException { 785 addLocalListElement(aPlug, PLUG_KEY); 786 } 787 788 791 public void removeLocalPlug(Plug aPlug) throws KilimException { 792 removeLocalListElement(aPlug, PLUG_KEY); 793 } 794 795 798 public Iterator getTriggers(String aName, boolean onlyLocal) { 799 if (onlyLocal) { 800 return getLocalListElements(aName, TRIGGER_KEY); 801 } 802 803 List list = new ArrayList(); 804 addVisibleListElements(list, aName, TRIGGER_KEY); 805 return list.listIterator(); 806 } 807 808 811 public Iterator getBindings(String aName, boolean onlyLocal) { 812 if (onlyLocal) { 813 return getLocalListElements(aName, BINDING_KEY); 814 } 815 816 List list = new ArrayList(); 817 addVisibleListElements(list, aName, BINDING_KEY); 818 return list.listIterator(); 819 } 820 821 824 public Iterator getPlugs(String aName, boolean onlyLocal) { 825 if (onlyLocal) { 826 return getLocalListElements(aName, PLU
|