1 18 19 package org.objectweb.kilim.model; 20 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Stack ; 25 import java.util.HashMap ; 26 27 import org.objectweb.kilim.KilimConfiguration; 28 import org.objectweb.kilim.KilimException; 29 import org.objectweb.kilim.description.Instance; 30 import org.objectweb.kilim.description.KILIM; 31 import org.objectweb.kilim.description.NamedElement; 32 import org.objectweb.kilim.description.TemplateDescription; 33 import org.objectweb.kilim.description.Trigger; 34 import org.objectweb.kilim.model.instanciation.InstanciationMger; 35 36 39 public class RtComponent extends RtComponentElement implements Component, RuntimeSource { 40 41 private boolean isInitialized; 42 private boolean isForkedComponent; 43 private ComponentFactory factory; 44 private TemplateDescription template; 45 private TemplateDescription containingTemplate; 46 private InstanciationMger instanciationMger; 47 48 private List subComponents; 49 private List interfaces; 50 private List slots; 51 private List plugTos; 52 private List nAryListeners; 53 54 private HashMap controllers; 56 57 64 public RtComponent(Instance aInstance, Component aComponent, ComponentFactory aFactory) throws KilimException { 65 super(aInstance, aComponent); 66 if (aInstance == null) { 67 throw new KilimException("attempt to construct a component without instance description "); 68 } 69 if (aComponent != null) { 70 aComponent.addSubComponent(this); 71 } 72 factory = aFactory; 73 nbComponent++; 74 } 75 76 79 public boolean isComponent() { 80 return true; 81 } 82 83 86 public boolean isSlot() { 87 return false; 88 } 89 90 93 public String getLocalName() { 94 NamedElement elem = (NamedElement) getElementDescription(); 95 return elem.getLocalName(); 96 } 97 98 101 public boolean hasValue() throws KilimException { 102 return true; 103 } 104 105 108 public boolean checkValue(Stack exclude) throws KilimException { 109 return true; 110 } 111 112 117 public void addInterface(ComponentInterface aElement) throws KilimException { 118 if (aElement == null) { 119 throw new KilimException("attempt to add a null interface to component " + getQualifiedName()); 120 } 121 122 if (!(aElement instanceof RtComponentInterface)) { 123 throw new KilimException("attempt to add a non Kilim interface to component " + getQualifiedName()); 124 } 125 126 String lName = aElement.getLocalName(); 127 128 if (interfaces == null) { 129 interfaces = new ArrayList (); 130 } 131 132 if (containsElement(interfaces, lName)) { 133 String interfSort = null; 134 if (aElement.isProperty()) { 135 interfSort = "property"; 136 } else if (aElement.isCollectionPort()) { 137 interfSort = "collection interface "; 138 } else if (aElement.isSingleValuePort()) { 139 interfSort = "interface"; 140 } else { 141 interfSort = "provider [" + aElement.getClass() + "]"; 142 } 143 throw new KilimException("name clash when adding the " + interfSort + " " + lName + " in component " + getQualifiedName()); 144 } 145 146 interfaces.add(aElement); 147 } 148 149 154 public void removeInterface(ComponentInterface aElement) throws KilimException { 155 if (aElement == null) { 156 throw new KilimException("attempt to remove a null interface from component " + getQualifiedName()); 157 } 158 159 if (interfaces == null) { 160 throw new KilimException("no interface in current component"); 161 } 162 163 Object previous = removeElement(interfaces, aElement.getLocalName()); 164 if (previous == null) { 165 throw new KilimException("interface " + aElement.getLocalName() + " unknown in current component " + getQualifiedName()); 166 } 167 } 168 169 174 public ComponentInterface getInterface(String aName) { 175 if (interfaces == null) { 176 return null; 177 } 178 return (ComponentInterface) getElement(interfaces, aName); 179 } 180 181 185 public Iterator getInterfaces() { 186 if (interfaces == null) { 187 return KILIM.EMPTY_ITERATOR; 188 } 189 return interfaces.iterator(); 190 } 191 192 195 public Factory getFactory() { 196 return factory; 197 } 198 199 202 public TemplateDescription getTemplate() { 203 Instance instance = ((Instance) getElementDescription()); 204 if (instance == null) { 205 return null; 206 } else { 207 return instance.getTemplate(); 208 } 209 } 210 211 214 public void setTemplate(TemplateDescription aTemplate) { 215 template = aTemplate; 216 } 217 218 221 public String toString() { 222 return getQualifiedName(); 223 } 224 225 230 public void addSubComponent(Component aElement) throws KilimException { 231 if (aElement == null) { 232 throw new KilimException("attempt to add a null subcomponent to component " + getQualifiedName()); 233 } 234 235 String lName = aElement.getLocalName(); 236 237 if (subComponents == null) { 238 subComponents = new ArrayList (); 239 } 240 241 if (containsElement(subComponents, lName)) { 242 throw new KilimException("name clash when adding the component " + lName + "in component " + getQualifiedName()); 243 } 244 245 subComponents.add(aElement); 246 } 247 248 253 public void removeSubComponent(String aName) throws KilimException { 254 if (aName == null) { 255 throw new KilimException("attempt to remove a child component through a null name in component " + getQualifiedName()); 256 } 257 258 if (subComponents == null) { 259 throw new KilimException("attempt to remove a component from component " + getQualifiedName() + " without subcomponent "); 260 } 261 262 Object previous = removeElement(subComponents, aName); 263 if (previous == null) { 264 throw new KilimException("attempt to remove unknown subcomponent " + aName + " from component " + getQualifiedName()); 265 } 266 } 267 268 274 public void removeSubComponent(Component aElement) throws KilimException { 275 if (aElement == null) { 276 throw new KilimException("attempt to remove a null component in component " + getQualifiedName()); 277 } 278 279 if (subComponents == null) { 280 throw new KilimException("attempt to remove a component from component " + getQualifiedName() + " without subcomponent "); 281 } 282 283 Object previous = removeElement(subComponents, aElement.getLocalName()); 284 if (previous == null) { 285 throw new KilimException("attempt to remove unknown subcomponent " + aElement.getLocalName() + " from component " + getQualifiedName()); 286 } 287 } 288 289 294 public Component getSubComponent(String aName) { 295 if (subComponents == null) { 296 return null; 297 } 298 return (Component) getElement(subComponents, aName); 299 } 300 301 305 public Iterator getSubComponents() { 306 if (subComponents == null) { 307 return KILIM.EMPTY_ITERATOR; 308 } 309 return subComponents.iterator(); 310 } 311 312 317 public void addSlot(ComponentSlot aSlot) throws KilimException { 318 if (aSlot == null) { 319 throw new KilimException("attempt to add a null slot in component " + getQualifiedName()); 320 } 321 if (slots == null) { 322 slots = new ArrayList (); 323 } 324 slots.add(aSlot); 325 } 326 327 333 public void removeSlot(String aName) throws KilimException { 334 if (aName == null) { 335 throw new KilimException("attempt to remove a slot through a null name in component " + getQualifiedName()); 336 } 337 if (slots == null) { 338 throw new KilimException("attempt to remove a slot from component " + getQualifiedName() + " without slot"); 339 } 340 341 Object result = removeElement(slots, aName); 342 if (result == null) { 343 throw new KilimException("attempt to remove an unknown slot " + aName + " from component " + getQualifiedName()); 344 } 345 } 346 347 354 public ComponentSlot getSlot(String aName) throws KilimException { 355 if (aName == null) { 356 throw new KilimException("attempt to get a null slot in component " + getQualifiedName()); 357 } 358 if (slots == null) { 359 throw new KilimException("attempt to get slot " + aName + " from a component " + getQualifiedName() + " without slot"); 360 } 361 362 RtComponentSlot slot1 = (RtComponentSlot) getElement(slots, aName); 363 if (slot1 == null) { 364 throw new KilimException("attempt to get an unknown slot " + aName + " in a component " + getQualifiedName()); 365 } 366 return slot1; 367 } 368 369 373 public Iterator getSlots() { 374 if (slots == null) { 375 return KILIM.EMPTY_ITERATOR; 376 } 377 return slots.iterator(); 378 } 379 380 386 public void addPlugTo(ComponentSlot aSlot) throws KilimException { 387 if (aSlot == null) { 388 throw new KilimException("attempt to add a null slot in the list of plugged slotTo of component " + getQualifiedName()); 389 } 390 if (plugTos == null) { 391 plugTos = new ArrayList (); 392 } 393 394 if (plugTos.contains(aSlot)) { 395 throw new KilimException("attempt to add an already plugged component " + aSlot.getQualifiedName() + " in the list of plugged components of component " + getQualifiedName()); 396 } 397 plugTos.add(aSlot); 398 } 399 400 406 public void removePlugTo(ComponentSlot aSlot) throws KilimException { 407 if (aSlot == null) { 408 throw new KilimException("attempt to remove a null slotTo from component " + getQualifiedName()); 409 } 410 if (plugTos == null) { 411 throw new KilimException("attempt to remove a slotTo " + aSlot.getQualifiedName() + " from an unplugged component " + getQualifiedName()); 412 } 413 414 boolean result = plugTos.remove(aSlot); 415 if (result == false) { 416 throw new KilimException("attempt to remove an unknown slotTo " + aSlot.getQualifiedName() + " from an component " + getQualifiedName()); 417 } 418 } 419 420 423 public Iterator getPlugTos() { 424 if (plugTos == null) { 425 return KILIM.EMPTY_ITERATOR; 426 } 427 return plugTos.listIterator(); 428 } 429 430 436 public void plug(String aName, Component aComponent) throws KilimException { 437 RtComponentSlot slt = (RtComponentSlot) getSlot(aName); 438 slt.plug(aComponent); 439 } 440 441 447 public void unplug(String aName, Component aComponent) throws KilimException { 448 RtComponentSlot slt = (RtComponentSlot) getSlot(aName); 449 slt.unplug(aComponent); 450 ((RtComponent) aComponent).removePlugTo(slt); 451 } 452 453 456 public Object getValue() { 457 return this; 458 } 459 460 463 public void setEventSourceValue(Object aValue) throws KilimException { 464 throw new KilimException("attempt to set an event source to a component " + getQualifiedName()); 465 } 466 467 470 public Object getEventSourceValue() throws KilimException { 471 throw new KilimException("attempt to get the event source of a component " + getQualifiedName()); 472 } 473 474 477 public boolean isEventSource() { 478 return false; 479 } 480 481 484 public void addInterfaceListener(RtCollectionPort aInterface) throws KilimException { 485 if (nAryListeners == null) { 486 nAryListeners = new ArrayList (); 487 } 488 489 nAryListeners.add(aInterface); 490 } 491 492 495 public void removeInterfaceListener(RtCollectionPort aInterface) throws KilimException { 496 nAryListeners.remove(aInterface); 497 } 498 499 503 public Iterator getInterfaceListeners() { 504 if (nAryListeners == null) { 505 return KILIM.EMPTY_ITERATOR; 506 } 507 return nAryListeners.listIterator(); 508 } 509 510 516 public Component fork() throws KilimException { 517 if (factory == null) { 518 throw new KilimException("attempt to fork a component " + getQualifiedName() + " with a null factory "); 519 } 520 521 RtComponent pCompo = (RtComponent) getContainingComponent(); 522 if (pCompo != null) { 523 Component compo = ((ComponentFactory) pCompo.getFactory()).fork(this); 524 return compo; 525 } else { 526 TemplateDescription tmp = getTemplate(); 527 Component compo = ComponentFactory.newComponent(tmp); 528 if (compo instanceof RtComponent) { 529 ((RtComponent) compo).isForkedComponent(true); 530 } 531 return compo; 532 } 533 } 534 535 538 public boolean isInitialized() { 539 return isInitialized; 540 } 541 542 545 public void setInitialized() { 546 isInitialized = true; 547 } 548 549 private boolean isForkedComponent() { 550 return isForkedComponent; 551 } 552 553 private void isForkedComponent(boolean iFE) { 554 isForkedComponent = iFE; 555 } 556 557 560 public InstanciationMger getInstanciationMgerFromConfiguration() throws KilimException { 561 instanciationMger = null; 563 564 instanciationMger = KilimConfiguration.getInstanciationStrategy().getPerInstanceMger(getQualifiedName()); 566 567 if (instanciationMger == null) { 569 TemplateDescription template1 = getTemplate(); 570 TemplateDescription template2 = null; 571 do { 572 template2 = template1.getSuperTemplate(); 573 if (template2 != null) { 574 template1 = template2; 575 } 576 } while (template2 != null); 577 578 instanciationMger = KilimConfiguration.getInstanciationStrategy().getPerTemplateMger(template1.getName()); 579 } 580 581 if (instanciationMger == null) { 583 instanciationMger = KilimConfiguration.getInstanciationStrategy().getDefaultMger(); 584 } 585 586 return instanciationMger; 587 } 588 589 592 public InstanciationMger getInstanciationMger() { 593 return instanciationMger; 594 } 595 596 599 public void setInstanciationMger(InstanciationMger aMger) throws KilimException { 600 if (aMger == null) { 601 throw new KilimException("attempt to install a null instanciation manager to component " + getQualifiedName()); 602 } 603 instanciationMger = aMger; 604 } 605 606 609 public void release() throws KilimException { 610 Component compo = getContainingComponent(); 612 boolean isSubCompo = false; 613 if (compo != null) { 614 compo.removeSubComponent(this); 615 isSubCompo = true; 616 } 617 618 ComponentFactory fct = (ComponentFactory) getFactory(); 619 ComponentFactory nfct = (ComponentFactory) fct.getContainingFactory(); 620 if (nfct != null) { 621 nfct.removeSubFactory(fct); 622 nfct.removeChildNamingContext(fct.getLocalName()); 623 fct.setParentNamingContext(null); 624 625 if (isSubCompo) { 626 nfct.removeBoundName(getLocalName()); 627 } 628 } 629 630 ArrayList list = null; 632 for (Iterator iter = getPlugTos(); iter.hasNext();) { 633 if (list == null) { 634 list = new ArrayList (); 635 } 636 list.add(iter.next()); 637 } 638 639 if (list != null) { 640 for (Iterator iter = list.iterator(); iter.hasNext();) { 641 RtComponentSlot slt = (RtComponentSlot) iter.next(); 642 slt.unplug(this); 643 } 644 } 645 } 646 647 655 public void addController(String aName, Object aController) throws KilimException { 656 if (aName == null) { 657 throw new KilimException("attempt to add a controller with a null name in component " + getQualifiedName()); 658 } 659 if (aController == null) { 660 throw new KilimException("attempt to add a null controller in component " + getQualifiedName()); 661 } 662 if (controllers == null) { 663 controllers = new HashMap (); 664 } 665 666 controllers.put(aName, aController); 667 } 668 669 676 public void removeController(String aName) throws KilimException { 677 if (aName == null) { 678 throw new KilimException("attempt to remove a null controller in component " + getQualifiedName()); 679 } 680 if (controllers == null) { 681 throw new KilimException("attempt to remove a controller from component " + getQualifiedName() + " without controller"); 682 } 683 684 Object result = controllers.remove(aName); 685 if (result == null) { 686 throw new KilimException("attempt to remove an unknown controller " + aName + " from component " + getQualifiedName()); 687 } 688 } 689 690 698 public Object getController(String aName) throws KilimException { 699 if (aName == null) { 700 throw new KilimException("attempt to get a controller through a null name in component " + getQualifiedName()); 701 } 702 703 if (controllers == null) { 704 throw new KilimException("attempt to get a controller " + aName + " from a component " + getQualifiedName() + " without controller"); 705 } 706 707 Object ctrl = controllers.get(aName); 708 if (ctrl == null) { 709 throw new KilimException("attempt to get an unknown controller " + aName + " in a component " + getQualifiedName()); 710 } 711 return ctrl; 712 } 713 714 720 public Iterator getControllers() { 721 if (controllers == null) { 722 return KILIM.EMPTY_ITERATOR; 723 } 724 return controllers.values().iterator(); 725 } 726 } | Popular Tags |