| 1 18 19 package org.objectweb.kilim.model; 20 21 import java.lang.reflect.Array ; 22 23 import java.util.LinkedHashMap ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.ArrayList ; 27 import java.util.Stack ; 28 29 import org.objectweb.kilim.InternalException; 30 import org.objectweb.kilim.KilimConfiguration; 31 import org.objectweb.kilim.KilimException; 32 import org.objectweb.kilim.description.ArraySource; 33 import org.objectweb.kilim.description.BasicElement; 34 import org.objectweb.kilim.description.BasicNamedElement; 35 import org.objectweb.kilim.description.Binding; 36 import org.objectweb.kilim.description.ClassSource; 37 import org.objectweb.kilim.description.EventSource; 38 import org.objectweb.kilim.description.Instance; 39 import org.objectweb.kilim.description.KILIM; 40 import org.objectweb.kilim.description.NullElement; 41 import org.objectweb.kilim.description.Plug; 42 import org.objectweb.kilim.description.Port; 43 import org.objectweb.kilim.description.Property; 44 import org.objectweb.kilim.description.Provider; 45 import org.objectweb.kilim.description.Reference; 46 import org.objectweb.kilim.description.Slot; 47 import org.objectweb.kilim.description.TemplateDescription; 48 import org.objectweb.kilim.description.TemplateElementImpl; 49 import org.objectweb.kilim.description.TpAccessor; 50 import org.objectweb.kilim.description.TpConstructor; 51 import org.objectweb.kilim.description.TpGetter; 52 import org.objectweb.kilim.description.TpMethod; 53 import org.objectweb.kilim.description.TpSetter; 54 import org.objectweb.kilim.description.Transformer; 55 import org.objectweb.kilim.description.Trigger; 56 57 import org.objectweb.kilim.model.instanciation.InstanciationStrategy; 58 59 import org.objectweb.kilim.model.mapping.Mapper; 60 import org.objectweb.kilim.model.mapping.MappingContext; 61 62 import org.objectweb.kilim.model.services.DefaultNamingContext; 63 import org.objectweb.kilim.model.services.DefaultRuntimeClassLoader; 64 65 68 public class ComponentFactory extends DefaultNamingContext implements Factory { 69 private String localName; 70 private ComponentFactory containingFactory; 71 private List subFactories; 72 73 private TemplateDescription currentTemplate; 74 private TemplateDescription containingTemplate; 75 76 private RtComponent currentComponent; 77 private RtComponent containingComponent; 78 79 private LinkedHashMap references; 80 81 82 84 private static class RTGetter extends RtComponentProvider { 85 86 RTGetter(TpGetter aGetter, Component aComponent, RuntimeSource aSupport) { 87 super(aGetter, aComponent, aSupport); 88 } 89 90 String getFieldName() { 91 return ((TpAccessor) getElementDescription()).getFieldName(); 92 } 93 94 boolean isStatic() { 95 return ((TpAccessor) getElementDescription()).isStatic(); 96 } 97 98 public Object specificGetValue() throws KilimException { 99 if (mappingContext != null) { 100 mappingContext.getContextStack().push(this); 101 } 102 mapper.enterContext(mappingContext); 103 104 Object eventSrcValue = getEventSourceValue(); 105 106 RuntimeSource support = (RuntimeSource) getSupport(); 108 if (support == null) { 109 throw new KilimException("attempt to get the value of a non initialized getter"); 110 } 111 112 Object suppObject = null; 113 if (support instanceof RTEventSource) { 114 suppObject = eventSrcValue; 115 } else { 116 suppObject = support.getValue(); 117 } 118 119 String fieldName = getFieldName(); 120 121 if (mappingContext != null) { 123 mappingContext.getCallStack().push(this); 124 } 125 126 Object resultValue = mapper.getGetterValue(suppObject, isStatic(), fieldName, mappingContext); 127 128 if (mappingContext != null) { 129 mappingContext.getCallStack().pop(); 130 } 131 mapper.leaveContext(mappingContext); 132 133 return resultValue; 134 } 135 136 public boolean hasValue() throws KilimException { 137 return getSupport().hasValue(); 138 } 139 140 public boolean checkValue(Stack exclude) throws KilimException { 141 RuntimeSource lSupport = (RuntimeSource) getSupport().getTarget(); 142 143 if (exclude == null || lSupport.hasValue()) { 144 return true; 145 } 146 147 int eSize = exclude.size(); 148 for (int i = 0; i < eSize; i++) { 149 if (lSupport == exclude.get(i)) { 150 return false; 151 } 152 } 153 154 exclude.push(this); 155 boolean isOK = lSupport.checkValue(exclude); 156 exclude.pop(); 157 return isOK; 158 } 159 } 160 161 162 164 private static class RTSetter extends RtComponentElement implements RuntimeAction { 165 private String name; 166 private Object source; 167 private RuntimeSource value; 168 private RuntimeSource support; 169 private Mapper mapper; 170 private MappingContext mappingContext; 171 172 RTSetter(TpSetter aGetter, Component aComponent, RuntimeSource aSupport, RuntimeSource aValue) { 173 super(aGetter, aComponent); 174 mapper = KilimConfiguration.getMappingStrategy().getDefaultMapper(); 175 mappingContext = KilimConfiguration.getMappingContext(); 176 support = aSupport; 177 value = aValue; 178 } 179 180 183 public String getLocalName() { 184 return name; 185 } 186 187 public void setEventSourceValue(Object aValue) { 188 source = aValue; 189 } 190 191 public Object getEventSourceValue() throws KilimException { 192 return source; 193 } 194 195 String getFieldName() { 196 return ((TpAccessor) getElementDescription()).getFieldName(); 197 } 198 199 boolean isStatic() { 200 return ((TpAccessor) getElementDescription()).isStatic(); 201 } 202 203 public void execute() throws KilimException { 204 if (support == null) { 205 throw new KilimException("attempt to get the value of a non initialized setter"); 206 } 207 208 if (mappingContext != null) { 209 mappingContext.getCallStack().push(this); 210 } 211 mapper.enterContext(mappingContext); 212 213 Object suppObject = null; 214 215 if (support instanceof RTEventSource) { 216 suppObject = ((RuntimeSource) source).getEventSourceValue(); 217 } else { 218 suppObject = support.getValue(); 219 } 220 221 String fieldName = getFieldName(); 222 223 Object toBeSet = null; 224 if (value instanceof RTEventSource) { 225 toBeSet = source; 226 } else { 227 toBeSet = value.getValue(); 228 } 229 230 mapper.executeSetter(suppObject, isStatic(), fieldName, toBeSet, mappingContext); 231 if (mappingContext != null) { 232 mappingContext.getCallStack().pop(); 233 } 234 mapper.leaveContext(mappingContext); 235 } 236 237 public boolean checkAction(Stack exclude) throws KilimException { 238 RuntimeSource lSupport = (RuntimeSource) support.getTarget(); 239 if (exclude == null || lSupport.hasValue()) { 240 return true; 241 } 242 243 int eSize = exclude.size(); 244 for (int i = 0; i < eSize; i++) { 245 if (lSupport == exclude.get(i)) { 246 return false; 247 } 248 } 249 250 exclude.push(this); 251 boolean isOK = lSupport.checkValue(exclude); 252 exclude.pop(); 253 return isOK; 254 } 255 } 256 257 258 260 private static class RTArray extends RuntimeSourceImpl1 { 261 private RuntimeSource[] arrayElements; 262 private String typeName; 263 264 RTArray(ArraySource aArray, Component aComponent, RuntimeSource[] elements, String aTypeName) { 265 super(aArray, aComponent); 266 267 typeName = aTypeName; 268 arrayElements = elements; 269 } 270 271 274 public Object getValue() throws KilimException { 275 Object eventSrcValue = getEventSourceValue(); 276 int size = arrayElements.length; 277 Class elementClass = DefaultRuntimeClassLoader.instance.getClass(typeName); 278 Object [] resultValue = (Object []) Array.newInstance(elementClass, size); 279 280 for (int i = 0; i < size; i++) { 281 if (arrayElements[i] instanceof RTEventSource) { 282 resultValue[i] = eventSrcValue; 283 } else { 284 RuntimeSource rtS = (RuntimeSource) arrayElements[i]; 285 rtS.setEventSourceValue(eventSrcValue); 287 resultValue[i] = rtS.getValue(); 288 } 289 } 290 return resultValue; 291 } 292 293 296 public boolean hasValue() throws KilimException { 297 int size = arrayElements.length; 298 for (int i = 0; i < size; i++) { 299 if (!arrayElements[i].hasValue()) { 300 return false; 301 } 302 } 303 return true; 304 } 305 306 public boolean checkValue(Stack exclude) throws KilimException { 307 int size = arrayElements.length; 308 for (int i = 0; i < size; i++) { 309 if (!arrayElements[i].hasValue()) { 310 exclude.push(arrayElements[i]); 311 boolean isOK = arrayElements[i].checkValue(exclude); 312 exclude.pop(); 313 if (!isOK) { 314 return false; 315 } 316 } 317 } 318 return true; 319 } 320 } 321 322 323 325 private static class RTClassSource extends RtComponentProvider { 326 private Class support; 327 private boolean gotAValue; 328 329 RTClassSource(ClassSource aSource, Component aComponent) { 330 super(aSource, aComponent, null); 331 } 332 333 String getClassName() { 334 return ((ClassSource) getElementDescription()).getClassName(); 335 } 336 337 public Object specificGetValue() throws KilimException { 338 if (mappingContext != null) { 339 mappingContext.getCallStack().push(this); 340 } 341 mapper.enterContext(mappingContext); 342 343 Object resultValue = mapper.getClassValue(getClassName(), mappingContext); 344 gotAValue = true; 345 346 if (mappingContext != null) { 347 mappingContext.getCallStack().pop(); 348 } 349 mapper.leaveContext(mappingContext); 350 return resultValue; 351 } 352 353 public void addInterfaceListener(RtCollectionPort aInterface) throws KilimException { } 354 355 public void removeInterfaceListener(RtCollectionPort aInterface) throws KilimException { } 356 357 public void setEventSourceValue(Object aValue) throws KilimException { } 358 359 362 public boolean hasValue() { 363 return gotAValue; 364 } 365 366 public boolean checkValue(Stack exclude) throws KilimException { 367 return true; 368 } 369 } 370 371 372 374 private static class RTEventSource extends RtComponentElement implements RuntimeSource { 375 private Object target; 376 private boolean gotAValue; 377 378 RTEventSource() { 379 super(null, null); 380 } 381 382 385 public String getLocalName() { 386 return null; 387 } 388 389 public void setEventSourceValue(Object aTarget) { 390 target = aTarget; 391 gotAValue = true; 392 } 393 394 public boolean isEventSource() { 395 return true; 396 } 397 398 public Object getEventSourceValue() throws KilimException { 399 return target; 400 } 401 402 public Object getValue() throws KilimException { 403 return getEventSourceValue(); 404 } 405 406 public void addInterfaceListener(RtCollectionPort aInterface) throws KilimException { } 407 408 public void removeInterfaceListener(RtCollectionPort aInterface) throws KilimException { } 409 410 public boolean hasValue() { 411 return gotAValue; 412 } 413 414 public boolean checkValue(Stack exclude) throws KilimException { 415 return true; 416 } 417 } 418 419 420 422 private static class RTNullElement extends RtComponentProvider implements RuntimeAction { 423 424 RTNullElement(NullElement aNullElement, Component aComponent) { 425 super(aNullElement, aComponent, null); 426 } 427 428 public Object specificGetValue() throws KilimException { 429 if (mappingContext != null) { 430 mappingContext.getCallStack().push(this); 431 } 432 mapper.enterContext(mappingContext); 433 mapper.getNullElementValue(mappingContext); 434 if (mappingContext != null) { 435 mappingContext.getCallStack().pop(); 436 } 437 mapper.leaveContext(mappingContext); 438 return null; 439 } 440 441 public void execute() throws KilimException { 442 mapper.executeNullElement(mappingContext); 443 } 444 445 public boolean hasValue() { 446 return true; 447 } 448 449 public boolean checkAction(Stack aStack) { 450 return true; 451 } 452 453 public boolean checkValue(Stack exclude) throws KilimException { 454 return true; 455 } 456 } 457 458 459 461 private static class RTMethod extends RtComponentProvider implements RuntimeAction { 462 private RuntimeSource[] parameters; 463 private String [] typeNames; 464 465 RTMethod(TpMethod aMethod, Component aComponent, RuntimeSource aSupport, RuntimeSource[] params, String [] tpNames) { 466 super(aMethod, aComponent, aSupport); 467 parameters = params; 468 typeNames = tpNames; 469 } 470 471 boolean isStatic() { 472 return ((TpMethod) getElementDescription()).isStatic(); 473 } 474 475 String getMethodName() { 476 return ((TpMethod) getElementDescription()).getMethodName(); 477 } 478 479 private Object perform(boolean provides) throws KilimException { 480 Object eventSrcValue = getEventSourceValue(); 481 RuntimeSource support = (RuntimeSource) getSupport(); 483 484 Object resultValue = null; 485 if (mappingContext != null) { 486 mappingContext.getCallStack().push(this); 487 } 488 mapper.enterContext(mappingContext); 489 490 callStack.push(this); 491 492 Object suppObject = null; 493 if (support instanceof RTEventSource) { 494 suppObject = eventSrcValue; 495 } else { 496 support.setEventSourceValue(eventSrcValue); 498 suppObject = support.getValue(); 499 } 500 501 506 RtSingleValuePort svp = getCurrentSVP(); 507 if (provides && svp != null && svp.hasValue()) { 510 callStack.pop(); 511 List iter = svp.getTriggerList(Trigger.BIND); 512 if (iter != KILIM.EMPTY_LIST) { 513 System.err.println("warning : execution of transformers associated to the port " + svp.getQualifiedName() + " may not follow the tree order : " + svpStack.toString()); 514 } 515 return svp.getBufferedValue(); 516 } 517 518 int paramNumber = parameters.length; 519 Object [] paramObjects = new Object [paramNumber]; 520 521 for (int i = 0; i < paramNumber; i++) { 522 if (provides && svp != null && svp.hasValue()) { 525 callStack.pop(); 526 List iter = svp.getTriggerList(Trigger.BIND); 527 if (iter != KILIM.EMPTY_LIST) { 528 System.err.println("warning : execution of transformers associated to the port " + svp.getQualifiedName() + " may not follow the tree order : " + svpStack.toString()); 529 } 530 return svp.getBufferedValue(); 531 } 532 paramObjects[i] = null; 533 if (parameters[i] != null) { 534 if (parameters[i] instanceof RTEventSource) { 535 paramObjects[i] = eventSrcValue; 536 } else { 537 parameters[i].setEventSourceValue(eventSrcValue); 539 paramObjects[i] = parameters[i].getValue(); 540 } 541 } 542 } 543 544 if (provides) { 546 if (svp != null && svp.hasValue()) { 547 callStack.pop(); 548 List iter = svp.getTriggerList(Trigger.BIND); 549 if (iter != KILIM.EMPTY_LIST) { 550 System.err.println("warning : execution of transformers associated to the port " + svp.getQualifiedName() + " may not follow the tree order : " + svpStack.toString()); 551 } 552 return svp.getBufferedValue(); 553 } else { 554 resultValue = mapper.getMethodValue(suppObject, isStatic(), getMethodName(), paramObjects, typeNames, mappingContext); 555 } 556 } else { 557 mapper.executeMethod(suppObject, isStatic(), getMethodName(), paramObjects, typeNames, mappingContext); 558 } 559 callStack.pop(); 560 if (mappingContext != null) { 561 mappingContext.getCallStack().pop(); 562 } 563 mapper.leaveContext(mappingContext); 564 565 return resultValue; 566 } 567 568 571 public Object specificGetValue() throws KilimException { 572 Object resultValue = perform(true); 573 return resultValue; 574 } 575 576 579 public void execute() throws KilimException { 580 perform(false); 581 } 582 583 586 public boolean hasValue() throws KilimException { 587 if (!getSupport().hasValue()) { 588 return false; 589 } 590 591 int paramNumber = parameters.length; 592 for (int i = 0; i < paramNumber; i++) { 593 if (!parameters[i].hasValue()) { 594 return false; 595 } 596 } 597 return true; 598 } 599 600 603 public boolean checkValue(Stack exclude) throws KilimException { 604 if (exclude == null) { 605 return true; 606 } 607 608 int eSize = exclude.size(); 609 610 RuntimeSource lSupport = (RuntimeSource) getSupport().getTarget(); 611 if (!lSupport.hasValue()) { 612 for (int i = 0; i < eSize; i++) { 613 if (lSupport == exclude.get(i)) { 614 return false; 615 } 616 } 617 618 exclude.push(lSupport); 619 boolean isOK = lSupport.checkValue(exclude); 620 exclude.pop(); 621 if (!isOK) { 622 return false; 623 } 624 } 625 626 int paramNumber = parameters.length; 627 628 for (int i = 0; i < paramNumber; i++) { 629 RuntimeSource parm = (RuntimeSource) parameters[i].getTarget(); 630 if (parm.hasValue()) { 631 continue; 632 } 633 634 for (int j = 0; j < eSize; j++) { 635 if (parm == exclude.get(j)) { 636 return false; 637 } 638 } 639 640 exclude.push(this); 641 boolean isOK = parm.checkValue(exclude); 642 exclude.pop(); 643 644 if (!isOK) { 645 return false; 646 } 647 } 648 return true; 649 } 650 651 654 public boolean checkAction(Stack aStack) throws KilimException { 655 RuntimeSource lSupport = (RuntimeSource) getSupport().getTarget(); 656 if (aStack == null) { 657 return true; 658 } 659 660 if (!lSupport.checkValue(aStack)) { 661 return false; 662 } 663 664 int stackSize = aStack.size(); 665 int paramNumber = parameters.length; 666 for (int i = 0; i < paramNumber; i++) { 667 RuntimeSource parm = (RuntimeSource) parameters[i].getTarget(); 668 if (!parm.checkValue(aStack)) { 669 return false; 670 } 671 } 672 return true; 673 } 674 } 675 676 677 679 private static class RTConstructor extends RtComponentProvider implements RuntimeAction { 680 private RuntimeSource[] parameters; 681 private String [] typeNames; 682 683 RTConstructor(TpConstructor aCtor, Component aComponent, RuntimeSource aSupport, RuntimeSource[] params, String [] tNames) { 684 super(aCtor, aComponent, aSupport); 685 parameters = params; 686 typeNames = tNames; 687 } 688 689 public Object specificGetValue() throws KilimException { 690 Object resultValue = perform(true); 691 return resultValue; 692 } 693 694 private Object perform(boolean provides) throws KilimException { 695 if (mappingContext != null) { 696 mappingContext.getCallStack().push(this); 697 } 698 mapper.enterContext(mappingContext); 699 700 callStack.push(this); 701 702 Object eventSrcValue = getEventSourceValue(); 703 RuntimeSource support = (RuntimeSource) getSupport(); 7
|