KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > kilim > model > ComponentFactory


1 /**
2  * Copyright (C) 2002 Kelua SA
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package org.objectweb.kilim.model;
20
21 import java.lang.reflect.Array JavaDoc;
22
23 import java.util.LinkedHashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Stack JavaDoc;
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 /**
66  * @author horn
67  */

68 public class ComponentFactory extends DefaultNamingContext implements Factory {
69     private String JavaDoc localName;
70     private ComponentFactory containingFactory;
71     private List JavaDoc subFactories;
72     
73     private TemplateDescription currentTemplate;
74     private TemplateDescription containingTemplate;
75     
76     private RtComponent currentComponent;
77     private RtComponent containingComponent;
78
79     private LinkedHashMap JavaDoc references;
80     
81     /* ************************************************************************************/
82     //inner class for a runtime descriptor of a getter
83

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 JavaDoc getFieldName() {
91             return ((TpAccessor) getElementDescription()).getFieldName();
92         }
93         
94         boolean isStatic() {
95             return ((TpAccessor) getElementDescription()).isStatic();
96         }
97                 
98         public Object JavaDoc specificGetValue() throws KilimException {
99             if (mappingContext != null) {
100                 mappingContext.getContextStack().push(this);
101             }
102             mapper.enterContext(mappingContext);
103             
104             Object JavaDoc eventSrcValue = getEventSourceValue();
105             
106             //specific part of the getValue
107
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 JavaDoc suppObject = null;
113             if (support instanceof RTEventSource) {
114                 suppObject = eventSrcValue;
115             } else {
116                 suppObject = support.getValue();
117             }
118             
119             String JavaDoc fieldName = getFieldName();
120             
121             //call to the mapper that performs the real task
122
if (mappingContext != null) {
123                 mappingContext.getCallStack().push(this);
124             }
125
126             Object JavaDoc 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 JavaDoc 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     //inner class for a runtime descriptor of a setter
163

164     private static class RTSetter extends RtComponentElement implements RuntimeAction {
165         private String JavaDoc name;
166         private Object JavaDoc 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         /**
181         * @see org.objectweb.kilim.model.ComponentElement#getLocalName()
182         */

183         public String JavaDoc getLocalName() {
184             return name;
185         }
186         
187         public void setEventSourceValue(Object JavaDoc aValue) {
188             source = aValue;
189         }
190
191         public Object JavaDoc getEventSourceValue() throws KilimException {
192             return source;
193         }
194         
195         String JavaDoc 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 JavaDoc suppObject = null;
214             
215             if (support instanceof RTEventSource) {
216                 suppObject = ((RuntimeSource) source).getEventSourceValue();
217             } else {
218                 suppObject = support.getValue();
219             }
220             
221             String JavaDoc fieldName = getFieldName();
222
223             Object JavaDoc 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 JavaDoc 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         //inner class for a runtime descriptor of an array
259

260     private static class RTArray extends RuntimeSourceImpl1 {
261         private RuntimeSource[] arrayElements;
262         private String JavaDoc typeName;
263         
264         RTArray(ArraySource aArray, Component aComponent, RuntimeSource[] elements, String JavaDoc aTypeName) {
265             super(aArray, aComponent);
266
267             typeName = aTypeName;
268             arrayElements = elements;
269         }
270                     
271         /**
272          * @see org.objectweb.kilim.model.RuntimeSource#getValue()
273          */

274         public Object JavaDoc getValue() throws KilimException {
275             Object JavaDoc eventSrcValue = getEventSourceValue();
276             int size = arrayElements.length;
277             Class JavaDoc elementClass = DefaultRuntimeClassLoader.instance.getClass(typeName);
278             Object JavaDoc[] resultValue = (Object JavaDoc[]) 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                     //recursive transmission of the event source ...
286
rtS.setEventSourceValue(eventSrcValue);
287                     resultValue[i] = rtS.getValue();
288                 }
289             }
290             return resultValue;
291         }
292         
293         /**
294          * @see org.objectweb.kilim.model.RuntimeSource#checkValue()
295          */

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 JavaDoc 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     //inner class for a runtime descriptor of a class source
324

325     private static class RTClassSource extends RtComponentProvider {
326         private Class JavaDoc support;
327         private boolean gotAValue;
328         
329         RTClassSource(ClassSource aSource, Component aComponent) {
330             super(aSource, aComponent, null);
331         }
332             
333         String JavaDoc getClassName() {
334             return ((ClassSource) getElementDescription()).getClassName();
335         }
336                 
337         public Object JavaDoc specificGetValue() throws KilimException {
338             if (mappingContext != null) {
339                 mappingContext.getCallStack().push(this);
340             }
341             mapper.enterContext(mappingContext);
342             
343             Object JavaDoc 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 JavaDoc aValue) throws KilimException { }
358         
359         /**
360          * @see org.objectweb.kilim.model.ComponentSource#checkValue()
361          */

362         public boolean hasValue() {
363             return gotAValue;
364         }
365         
366         public boolean checkValue(Stack JavaDoc exclude) throws KilimException {
367             return true;
368         }
369     }
370     
371     /* ************************************************************************************/
372     //inner class for a runtime descriptor of an event source.
373

374     private static class RTEventSource extends RtComponentElement implements RuntimeSource {
375         private Object JavaDoc target;
376         private boolean gotAValue;
377     
378         RTEventSource() {
379             super(null, null);
380         }
381         
382         /**
383         * @see org.objectweb.kilim.model.ComponentElement#getLocalName()
384         */

385         public String JavaDoc getLocalName() {
386             return null;
387         }
388     
389         public void setEventSourceValue(Object JavaDoc aTarget) {
390             target = aTarget;
391             gotAValue = true;
392         }
393         
394         public boolean isEventSource() {
395             return true;
396         }
397         
398         public Object JavaDoc getEventSourceValue() throws KilimException {
399             return target;
400         }
401             
402         public Object JavaDoc 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 JavaDoc exclude) throws KilimException {
415             return true;
416         }
417     }
418     
419     /* ************************************************************************************/
420     //inner class for a runtime descriptor of a null element
421

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 JavaDoc 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 JavaDoc aStack) {
450             return true;
451         }
452         
453         public boolean checkValue(Stack JavaDoc exclude) throws KilimException {
454             return true;
455         }
456     }
457     
458     /* ************************************************************************************/
459     //inner class for a runtime descriptor of a method
460

461     private static class RTMethod extends RtComponentProvider implements RuntimeAction {
462         private RuntimeSource[] parameters;
463         private String JavaDoc[] typeNames;
464
465         RTMethod(TpMethod aMethod, Component aComponent, RuntimeSource aSupport, RuntimeSource[] params, String JavaDoc[] 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 JavaDoc getMethodName() {
476             return ((TpMethod) getElementDescription()).getMethodName();
477         }
478         
479         private Object JavaDoc perform(boolean provides) throws KilimException {
480             Object JavaDoc eventSrcValue = getEventSourceValue();
481             //specific part of getValue
482
RuntimeSource support = (RuntimeSource) getSupport();
483
484             Object JavaDoc resultValue = null;
485             if (mappingContext != null) {
486                 mappingContext.getCallStack().push(this);
487             }
488             mapper.enterContext(mappingContext);
489             
490             callStack.push(this);
491             
492             Object JavaDoc suppObject = null;
493             if (support instanceof RTEventSource) {
494                 suppObject = eventSrcValue;
495             } else {
496                 //recursive transmission of the event source ...
497
support.setEventSourceValue(eventSrcValue);
498                 suppObject = support.getValue();
499             }
500             
501             //this test does not work with a null mapper..
502
//if (suppObject == null) {
503
// throw new KilimException("attempt to invoke a method " + getMethodName() + " with a null support " + ((RTMethod) support.getTarget()).getMethodName());
504
//}
505

506             RtSingleValuePort svp = getCurrentSVP();
507             //If the method is performed for obtaining the value of a single value port, this value may have been obtained
508
//as a result of an evaluation cycle...
509
if (provides && svp != null && svp.hasValue()) {
510                 callStack.pop();
511                 List JavaDoc 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 JavaDoc[] paramObjects = new Object JavaDoc[paramNumber];
520
521             for (int i = 0; i < paramNumber; i++) {
522                 //If the method is performed for obtaining the value of a single value port, this value may have
523
//been obtained as a result of an evaluation cycle...
524
if (provides && svp != null && svp.hasValue()) {
525                     callStack.pop();
526                     List JavaDoc 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                         //recursive transmission of the event source ...
538
parameters[i].setEventSourceValue(eventSrcValue);
539                         paramObjects[i] = parameters[i].getValue();
540                     }
541                 }
542             }
543
544             //call to the mapper that performs the real task ....
545
if (provides) {
546                 if (svp != null && svp.hasValue()) {
547                     callStack.pop();
548                     List JavaDoc 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         /**
569          * @see org.objectweb.kilim.model.RtComponentInterface#specificGetValue()
570          */

571         public Object JavaDoc specificGetValue() throws KilimException {
572             Object JavaDoc resultValue = perform(true);
573             return resultValue;
574         }
575     
576         /**
577          * @see org.objectweb.kilim.model.RuntimeAction#execute()
578          */

579         public void execute() throws KilimException {
580             perform(false);
581         }
582         
583         /**
584          * @see org.objectweb.kilim.model.ComponentSource#checkValue()
585          */

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         /**
601          * @see org.objectweb.kilim.model.ComponentSource#checkValue()
602          */

603         public boolean checkValue(Stack JavaDoc 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         /**
652          * @see org.objectweb.kilim.model.RuntimeAction#checkAction()
653          */

654         public boolean checkAction(Stack JavaDoc 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     //inner class for a runtime descriptor of a constructor
678

679     private static class RTConstructor extends RtComponentProvider implements RuntimeAction {
680         private RuntimeSource[] parameters;
681         private String JavaDoc[] typeNames;
682
683         RTConstructor(TpConstructor aCtor, Component aComponent, RuntimeSource aSupport, RuntimeSource[] params, String JavaDoc[] tNames) {
684             super(aCtor, aComponent, aSupport);
685             parameters = params;
686             typeNames = tNames;
687         }
688                 
689         public Object JavaDoc specificGetValue() throws KilimException {
690             Object JavaDoc resultValue = perform(true);
691             return resultValue;
692         }
693             
694         private Object JavaDoc 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 JavaDoc eventSrcValue = getEventSourceValue();
703             RuntimeSource support = (RuntimeSource) getSupport();
704             if (support == null) {
705                 throw new KilimException("attempt to get the value of a non initialized constructor");
706             }
707
708             Class JavaDoc clazz = null;
709             if (support.isEventSource()) {
710                 clazz = (Class JavaDoc) eventSrcValue;
711             } else {
712                 clazz = (Class JavaDoc) support.getValue();
713             }
714
715             //If the method is performed for obtaining the value of a single value port, this value may have been obtained
716
//as a result of an evaluation cycle...
717
RtSingleValuePort svp = getCurrentSVP();
718
719             if (provides && svp != null && svp.hasValue()) {
720                 callStack.pop();
721                 List JavaDoc iter = svp.getTriggerList(Trigger.BIND);
722                 if (iter != KILIM.EMPTY_LIST) {
723                     System.err.println("warning : execution of transformers associated to the port " + svp.getQualifiedName() + " may not follow the tree order : " + svpStack.toString());
724                 }
725                 return svp.getBufferedValue();
726             }
727                 
728             int paramNumber = parameters.length;
729             Object JavaDoc[] paramObjects = new Object JavaDoc[paramNumber];
730
731             for (int i = 0; i < paramNumber; i++) {
732                 //If the method is performed for obtaining the value of a single value port, this value may have
733
//been obtained as a result of an evaluation cycle...
734

735                 if (provides && svp != null && svp.hasValue()) {
736                     callStack.pop();
737                     List JavaDoc iter = svp.getTriggerList(Trigger.BIND);
738                     if (iter != KILIM.EMPTY_LIST) {
739                         System.err.println("warning : execution of transformers associated to the port " + svp.getQualifiedName() + " may not follow the tree order : " + svpStack.toString());
740                     }
741                     return svp.getBufferedValue();
742                 }
743
744                 paramObjects[i] = null;
745                 if (parameters[i] != null) {
746                     if (parameters[i] instanceof RTEventSource) {
747                         paramObjects[i] = eventSrcValue;
748                     } else {
749                         //recursive transmission of the event source ...
750
parameters[i].setEventSourceValue(eventSrcValue);
751                         paramObjects[i] = parameters[i].getValue();
752                     }
753                 }
754             }
755             Object JavaDoc resultValue = null;
756             
757             if (provides) {
758                 if (svp != null && svp.hasValue()) {
759                     callStack.pop();
760                     List JavaDoc iter = svp.getTriggerList(Trigger.BIND);
761                     if (iter != KILIM.EMPTY_LIST) {
762                         System.err.println("warning : execution of transformers associated to the port " + svp.getQualifiedName() + " may not follow the tree order : " + svpStack.toString());
763                     }
764                     return svp.getBufferedValue();
765                 } else {
766                     resultValue = mapper.getConstructorValue(clazz, paramObjects, typeNames, mappingContext);
767                 }
768             } else {
769                 mapper.getConstructorValue(clazz, paramObjects, typeNames, mappingContext);
770             }
771             
772             callStack.pop();
773             if (mappingContext != null) {
774                 mappingContext.getCallStack().pop();
775             }
776             mapper.leaveContext(mappingContext);
777
778             return resultValue;
779         }
780     
781         /**
782          * @see org.objectweb.kilim.model.RuntimeAction#execute()
783          */

784         public void execute() throws KilimException {
785             perform(false);
786         }
787         
788         /**
789          * @see org.objectweb.kilim.model.ComponentSource#checkValue()
790          */

791         public boolean hasValue() throws KilimException {
792             if (!getSupport().hasValue()) {
793                 return false;
794             }
795             
796             int paramNumber = parameters.length;
797             for (int i = 0; i < paramNumber; i++) {
798                 if (!parameters[i].hasValue()) {
799                     return false;
800                 }
801             }
802             return true;
803         }
804         
805                 /**
806          * @see org.objectweb.kilim.model.ComponentSource#checkValue()
807          */

808         public boolean checkValue(Stack JavaDoc exclude) throws KilimException {
809             if (exclude == null) {
810                 return true;
811             }
812             
813             int eSize = exclude.size();
814             
815             RuntimeSource lSupport = (RuntimeSource) getSupport().getTarget();
816             if (!lSupport.hasValue()) {
817                 for (int i = 0; i < eSize; i++) {
818                     if (lSupport == exclude.get(i)) {
819                         return false;
820                     }
821                 }
822                 
823                 exclude.push(lSupport);
824                 boolean isOK = lSupport.checkValue(exclude);
825                 exclude.pop();
826                 if (!isOK) {
827                     return false;
828                 }
829             }
830             
831             int paramNumber = parameters.length;
832             
833             for (int i = 0; i < paramNumber; i++) {
834                 RuntimeSource parm = (RuntimeSource) parameters[i].getTarget();
835                 if (parm.hasValue()) {
836                     continue;
837                 }
838                 
839                 for (int j = 0; j < eSize; j++) {
840                     if (parm == exclude.get(j)) {
841                         return false;
842                     }
843                 }
844                 
845                 exclude.push(this);
846                 boolean isOK = parm.checkValue(exclude);
847                 exclude.pop();
848                 
849                 if (!isOK) {
850                     return false;
851                 }
852             }
853             return true;
854         }
855
856         /**
857          * @see org.objectweb.kilim.model.RuntimeAction#checkAction()
858          */

859         public boolean checkAction(Stack JavaDoc aStack) throws KilimException {
860             RuntimeSource lSupport = (RuntimeSource) getSupport().getTarget();
861             
862             if (aStack == null) {
863                 return true;
864             }
865             
866             if (!lSupport.checkValue(aStack)) {
867                 return false;
868             }
869             
870             int stackSize = aStack.size();
871             int paramNumber = parameters.length;
872             for (int i = 0; i < paramNumber; i++) {
873                 RuntimeSource parm = (RuntimeSource) parameters[i].getTarget();
874                 if (!parm.checkValue(aStack)) {
875                     return false;
876                 }
877             }
878             return true;
879         }
880     }
881     
882     /* ************************************************************************************/
883     
884     private static class RTReference extends RuntimeSourceImpl1 implements RuntimeAction {
885         private RuntimeElement target;
886     
887         RTReference(TemplateElementImpl aElement, Component aComponent) {
888             super(aElement, aComponent);
889         }
890
891
892         String JavaDoc getTargetName() {
893             return ((Reference) getElementDescription()).getTargetName();
894         }
895     
896         public RuntimeElement getTarget() {
897             if (target == null) {
898                 return null;
899             }
900             if (target instanceof RTReference) {
901                 return ((RTReference) target).getTarget();
902             }
903             return (RuntimeElement) target;
904         }
905         
906         void setTarget(RuntimeElement aTarget) {
907             target = aTarget;
908         }
909                     
910         public void execute() throws KilimException {
911             if (target == null) {
912                 throw new KilimException("attempt to execute an action from an unbound reference " + this + " in component ");
913             }
914         
915             RuntimeElement tgt = target;
916             if (tgt instanceof RTReference) {
917                 tgt = ((RTReference) tgt).getTarget();
918             }
919             ((RuntimeAction) tgt).execute();
920
921         }
922         
923         public Object JavaDoc getValue() throws KilimException {
924             if (target == null) {
925                 throw new KilimException("attempt to get a value from an unbound reference " + this + " in component ");
926             }
927         
928             RuntimeElement tgt = target;
929             if (tgt instanceof RTReference) {
930                 tgt = ((RTReference) tgt).getTarget();
931             }
932             return ((RuntimeSource) tgt).getValue();
933         }
934         
935         /**
936          * @see org.objectweb.kilim.model.RuntimeSource#checkValue()
937          */

938         public boolean hasValue() throws KilimException {
939             if (target == null) {
940                 throw new KilimException("attempt to check the value from an unbound reference " + this + " in component ");
941             }
942             
943             if (target instanceof RuntimeSource) {
944                     return ((RuntimeSource) target).hasValue();
945             }
946             throw new KilimException("attempt to check the value from non source element " + this + " in component ");
947         }
948         
949         /**
950          * @see org.objectweb.kilim.model.RuntimeAction#checkAction()
951          */

952         public boolean checkAction(Stack JavaDoc aStack) throws KilimException {
953             if (target == null) {
954                 throw new KilimException("attempt to check the value from an unbound reference " + this + " in component ");
955             }
956             
957             if (target instanceof RuntimeAction) {
958                     return ((RuntimeAction) target).checkAction(aStack);
959             }
960             throw new KilimException("attempt to check the action from non action element " + this + " in component ");
961         }
962         
963         /**
964          * @see org.objectweb.kilim.model.RuntimeAction#checkAction()
965          */

966         public boolean checkValue(Stack JavaDoc aStack) throws KilimException {
967             if (target == null) {
968                 throw new KilimException("attempt to check the value from an unbound reference " + this + " in component ");
969             }
970             
971             if (target instanceof RuntimeSource) {
972                     return ((RuntimeSource) target).checkValue(aStack);
973             }
974             throw new KilimException("attempt to check the action from non action element " + this + " in component ");
975         }
976     }
977     
978     /**
979      * A factory method for creating a new component.
980      * @param template : the template to be used
981      * @return Component
982      * @throws KilimException : generated if template is null.
983      */

984     public static Component newComponent(TemplateDescription template) throws KilimException {
985         ComponentFactory factory = new ComponentFactory("", template, null, null);
986         factory.initializeInstanciation();
987         factory.initNewComponent();
988         factory.resolveReferences();
989         factory.installBindings();
990         factory.installTriggers();
991         factory.installPlugs();
992         factory.finalizeInstanciation();
993         return factory.getComponent();
994     }
995         
996     /**
997      * A factory method for creating a new component.
998      * @param template : the template to be used.
999      * @param aStrgy : the implementation strategy to be used.
1000     * @return Component
1001     * @throws KilimException : generated if template is null.
1002     */

1003    public static Component newComponent(TemplateDescription template, InstanciationStrategy aStrgy) throws KilimException {
1004        if (aStrgy == null) {
1005            throw new KilimException("attempt to assign a null instanciation strategy for template " + template.getName());
1006        }
1007        KilimConfiguration.setInstanciationStrategy(aStrgy);
1008        return newComponent(template);
1009    }
1010    
1011    private ComponentFactory(String JavaDoc aName, TemplateDescription aTemplate, ComponentFactory aFactory, RtComponent aComponent) throws KilimException {
1012        super(aName, aFactory, null);
1013        if (aTemplate == null) {
1014            throw new KilimException("attempt to construct a component without template ");
1015        }
1016        localName = aName;
1017        containingFactory = aFactory;
1018        if (containingFactory != null) {
1019            containingFactory.addChildNamingContext(aName, this);
1020        }
1021        
1022        containingComponent = aComponent;
1023        currentTemplate = aTemplate;
1024        //instanciationStrategy = KilimConfiguration.getInstanciationStgy();
1025
setExternalReferences(KilimConfiguration.getExternalReferences());
1026    }
1027        
1028    /**
1029     * returns the local name of the factory (which is similar to the component name !!!!)
1030     * @return String
1031     */

1032    public String JavaDoc getLocalName() {
1033        if (localName == null) {
1034            return "";
1035        }
1036        return localName;
1037    }
1038
1039    /**
1040     * returns the component built by the factory.
1041     * This method returns a reference of the Component meta-object bound to the "this" interface.
1042     * @return Component
1043     */

1044    public Component getComponent() {
1045        return currentComponent;
1046    }
1047    
1048    /**
1049     * In the present implementation factories are used as naming contexts. As a consequence they are organized according to a tree-like structure.
1050     * This method therefore returns the containing factory.
1051     * @return ComponentFactory
1052     */

1053    public ComponentFactory getContainingFactory() {
1054        return containingFactory;
1055    }
1056
1057        
1058    /**
1059     * In the present implementation factories are used as naming contexts. As a consequence they are organized according to a tree-like structure.
1060     * This method adds a SubFactory.
1061     * @param aElement : the Child factory to be added.
1062     * @throws KilimException :
1063     */

1064    public void addSubFactory(ComponentFactory aElement) throws KilimException {
1065        if (aElement == null) {
1066            throw new KilimException("attempt to add a null subfactory to factory " + getQualifiedName());
1067        }
1068                
1069        String JavaDoc lName = aElement.getLocalName();
1070        
1071        if (subFactories == null) {
1072            subFactories = new ArrayList JavaDoc();
1073        }
1074        
1075        if (containsElement(subFactories, lName)) {
1076            throw new KilimException("name clash when adding the component " + lName + "in factory " + getQualifiedName());
1077        }
1078        
1079        subFactories.add(aElement);
1080    }
1081    
1082    /**
1083     * In the present implementation factories are used as naming contexts. As a consequence they are organized according to a tree-like structure.
1084     * This method removes a Sub Factory.
1085     * @param aElement :
1086     * @throws KilimException :
1087     */

1088    public void removeSubFactory(ComponentFactory aElement) throws KilimException {
1089        if (aElement == null) {
1090            throw new KilimException("attempt to remove a null factory in factory " + getQualifiedName());
1091        }
1092        
1093        if (subFactories == null) {
1094            throw new KilimException("no subfactory in current factory");
1095        }
1096        
1097        Object JavaDoc previous = removeElement(subFactories, aElement.getLocalName());
1098        if (previous == null) {
1099            throw new KilimException("subfactory " + aElement.getLocalName() + " unknown in current factory " + getQualifiedName());
1100        }
1101    }
1102    
1103    /**
1104     *In the present implementation factories are used as naming contexts. As a consequence they are organized according to a tree-like structure.
1105     * This method returns a SubFactory.
1106     * @param aName :
1107     * @return ComponentFactoryElement
1108     */

1109    public ComponentFactory getSubFactory(String JavaDoc aName) {
1110        if (subFactories == null) {
1111            return null;
1112        }
1113        return (ComponentFactory) getElement(subFactories, aName);
1114    }
1115    
1116    /**
1117     * Method getComponentFactorys.
1118     * @return Iterator
1119     */

1120    public Iterator JavaDoc getSubFactories() {
1121        if (subFactories == null) {
1122            return KILIM.EMPTY_ITERATOR;
1123        }
1124        return subFactories.iterator();
1125    }
1126            
1127    /**
1128     * Method newComponent.
1129     * @param instance :
1130     * @return Component :
1131     * @throws KilimException :
1132     */

1133    private Component newComponent(Instance instance) throws KilimException {
1134        TemplateDescription template = instance.getTemplate();
1135        ComponentFactory factory = new ComponentFactory(instance.getLocalName(), template, this, (RtComponent) getComponent());
1136        addSubFactory(factory);
1137        factory.initNewComponent();
1138        return factory.getComponent();
1139    }
1140    
1141    private void initNewComponent() throws KilimException {
1142        //creation of the new component meta object
1143
Instance instance = new Instance(localName, KILIM.PUBLIC, currentTemplate, null);
1144        currentComponent = new RtComponent(instance, containingComponent, this);
1145        //creation of the "THIS" interface referencing the meta object itself
1146
//RtSingleValuePort thisInterface = new RtSingleValuePort(Port.THIS_PORT, currentComponent, fMapper, fMappingContext);
1147
RtSingleValuePort thisInterface = new RtSingleValuePort(Port.THIS_PORT, currentComponent);
1148        currentComponent.addInterface(thisInterface);
1149        addBoundName("THIS", thisInterface);
1150        thisInterface.bindProvider(currentComponent, false);
1151        //setInstanciationMger(currentComponent);
1152

1153        //instanciation of all local provider references
1154
TemplateDescription template1 = currentTemplate;
1155        
1156        Iterator JavaDoc iter = null;
1157        do {
1158            iter = template1.getProviderReferences(true);
1159            while (iter.hasNext()) {
1160                Reference elem = (Reference) iter.next();
1161                RTReference ref = newInnerReference(elem);
1162                addReference(elem.getTargetName(), ref);
1163            }
1164            template1 = template1.getSuperTemplate();
1165        } while (template1 != null);
1166        
1167        //instanciation of all local transformer references
1168
template1 = currentTemplate;
1169        do {
1170            iter = template1.getTransformerReferences(true);
1171            while (iter.hasNext()) {
1172                Reference elem = (Reference) iter.next();
1173                RTReference ref = newInnerReference(elem);
1174                addReference(elem.getTargetName(), ref);
1175            }
1176            template1 = template1.getSuperTemplate();
1177        } while (template1 != null);
1178
1179        ComponentElement result = null;
1180
1181        //instanciation of all local ports not declared as slot members
1182
iter = currentTemplate.getAllInterfaces(false);
1183        while (iter.hasNext()) {
1184            BasicNamedElement elem = (BasicNamedElement) iter.next();
1185            result = newComponentElement1(elem);
1186            addBoundName(elem.getLocalName(), result);
1187        }
1188                
1189        //instanciation of all local transformers
1190
iter = currentTemplate.getTransformers(false);
1191        while (iter.hasNext()) {
1192            BasicNamedElement elem = (BasicNamedElement) iter.next();
1193            result = newComponentElement1(elem);
1194            addBoundName(elem.getLocalName(), result);
1195        }
1196
1197        //instanciation of all local ports declared as slot members
1198
iter = currentTemplate.getSlots(false);
1199        while (iter.hasNext()) {
1200            Slot elem = (Slot) iter.next();
1201            String JavaDoc sName = elem.getLocalName();
1202            //SlotFactory sFactory = new SlotFactory(sName, this, fMapper, fMappingContext);
1203
SlotFactory sFactory = new SlotFactory(sName, this);
1204            RtComponentSlot rSlot = sFactory.newSlot(elem);
1205            ((RtComponent) getComponent()).addSlot(rSlot);
1206        }
1207                
1208        //instanciation of all local sinstances
1209
iter = currentTemplate.getInstances(false);
1210        while (iter.hasNext()) {
1211            Instance elem = (Instance) iter.next();
1212            Component compo1 = newComponent(elem);
1213            addBoundName(elem.getLocalName(), compo1);
1214        }
1215    }
1216    
1217    private ComponentElement newComponentElement1(BasicElement elem) throws KilimException {
1218        ComponentElement result = null;
1219        
1220        switch(elem.getKind()) {
1221            case KILIM.PORT :
1222                result = newInterface((Port) elem);
1223                currentComponent.addInterface((RtComponentInterface) result);
1224                return result;
1225            case KILIM.ARRAY :
1226                return newInnerArraySource((ArraySource) elem);
1227            case KILIM.CLASS :
1228                return newInnerClassSource((ClassSource) elem);
1229            case KILIM.GETTER :
1230                return newInnerGetter((TpGetter) elem);
1231            case KILIM.SETTER :
1232                return newInnerSetter((TpSetter) elem);
1233            case KILIM.METHOD :
1234                return newInnerMethod((TpMethod) elem);
1235            case KILIM.CONSTRUCTOR :
1236                return newInnerConstructor((TpConstructor) elem);
1237            case KILIM.REFERENCE :
1238                return getReference(((Reference) elem).getTargetName());
1239            case KILIM.PROVIDER :
1240                result = newComponentElement1(((Provider) elem).getSource());
1241                currentComponent.addInterface((RtComponentInterface) result);
1242                return result;
1243            case KILIM.TRANSFORMER :
1244                result = newComponentElement1(((Transformer) elem).getAction());
1245                return result;
1246            case KILIM.PROPERTY :
1247                result = newProperty((Property) elem);
1248                currentComponent.addInterface((RtComponentInterface) result);
1249                return result;
1250            case KILIM.EVENT_SOURCE :
1251                return newInnerEventSource((EventSource) elem);
1252            case KILIM.NULL_ELEMENT :
1253                    //return new RTNullElement((NullElement) elem, getComponent(), fMapper, fMappingContext);
1254
return new RTNullElement((NullElement) elem, getComponent());
1255            default :
1256                throw new KilimException("attempt to create an unknown kind (" + elem.getClass() + ") of inner element in component " + getQualifiedName());
1257        }
1258    }
1259
1260    private RtComponentProperty newProperty(Property aProperty) throws KilimException {
1261        return new RtComponentProperty(aProperty, getComponent());
1262    }
1263    
1264    private RtComponentInterface newInterface(Port port) throws KilimException {
1265        RtComponentInterface interf = null;
1266        if (port.isUnary()) {
1267            interf = new RtSingleValuePort(port, getComponent());
1268        } else {
1269            interf = new RtCollectionPort(port, getComponent());
1270        }
1271        return interf;
1272    }
1273
1274    private RTArray newInnerArraySource(ArraySource aArray) throws KilimException {
1275        int size = aArray.getCurrentSize();
1276        RuntimeSource[] values = new RuntimeSource[size];
1277        for (int i = 0; i < size; i++) {
1278             values[i] = (RuntimeSource) newComponentElement1(aArray.getElement(i));
1279        }
1280        
1281        return new RTArray(aArray, getComponent(), values, aArray.getTypeName());
1282    }
1283    
1284    private RTClassSource newInnerClassSource(ClassSource aSource) throws KilimException {
1285        String JavaDoc className = aSource.getClassName();
1286        return new RTClassSource(aSource, getComponent());
1287    }
1288
1289    private RTGetter newInnerGetter(TpGetter aGetter) throws KilimException {
1290        RuntimeSource support = (RuntimeSource) newComponentElement1(aGetter.getSupport());
1291        return new RTGetter(aGetter, getComponent(), support);
1292    }
1293
1294    private RTSetter newInnerSetter(TpSetter aSetter) throws KilimException {
1295        RuntimeSource aSupport = (RuntimeSource) newComponentElement1(aSetter.getSupport());
1296        RuntimeSource aValue = (RuntimeSource) newComponentElement1(aSetter.getValue());
1297        RTSetter result = new RTSetter(aSetter, getComponent(), aSupport, aValue);
1298        return result;
1299    }
1300    
1301    private RTMethod newInnerMethod(TpMethod aMethod) throws KilimException {
1302        RuntimeSource aSupport = (RuntimeSource) newComponentElement1(aMethod.getSupport());
1303        int parmNumber = aMethod.getParameterNumber();
1304        RuntimeSource[] params = new RuntimeSource[parmNumber];
1305        String JavaDoc[] typeNames = new String JavaDoc[parmNumber];
1306        
1307        for (int i = 0; i < parmNumber ; i++) {
1308            params[i] = (RuntimeSource) newComponentElement1(aMethod.getParameter(i).getTarget());
1309            typeNames[i] = aMethod.getParameter(i).getTypeName();
1310        }
1311        return new RTMethod(aMethod, getComponent(), aSupport, params, typeNames);
1312    }
1313    
1314    private RTConstructor newInnerConstructor(TpConstructor aCtor) throws KilimException {
1315        RuntimeSource aSupport = (RuntimeSource) newComponentElement1(aCtor.getSupport());
1316        int parmNumber = aCtor.getParameterNumber();
1317        RuntimeSource[] params = new RuntimeSource[parmNumber];
1318        String JavaDoc[] typeNames = new String JavaDoc[parmNumber];
1319        
1320        for (int i = 0; i < parmNumber ; i++) {
1321            params[i] = (RuntimeSource) newComponentElement1(aCtor.getParameter(i).getTarget());
1322            typeNames[i] = aCtor.getParameter(i).getTypeName();
1323        }
1324        return new RTConstructor(aCtor, getComponent(), aSupport, params, typeNames);
1325    }
1326    
1327    private RTReference newInnerReference(Reference aReference) throws KilimException {
1328        if (references == null) {
1329            references = new LinkedHashMap JavaDoc();
1330        }
1331        RTReference ref = new RTReference(aReference, getComponent());
1332        return ref;
1333    }
1334    
1335    private RTEventSource newInnerEventSource(EventSource aSource) throws KilimException {
1336        return new RTEventSource();
1337    }
1338    
1339    /**
1340     * returns a new component which is a copy of aComponent (in the state defined by the template). All operations performed at runtime is ignored.
1341     * @param aComponent : the component to be forked
1342     * @return Component
1343     * @throws KilimException :
1344     */

1345    public Component fork(Component aComponent) throws KilimException {
1346        if (aComponent == null) {
1347            throw new KilimException("attempt to fork a null component in component factory " + getQualifiedName());
1348        }
1349        
1350        Instance instance = (Instance) aComponent.getElementDescription().clone();
1351        instance.setLocalName("%fork" + KILIM.getUniqueIndex());
1352        
1353        Component nCompo = newComponent(instance);
1354        addBoundName(nCompo.getLocalName(), nCompo);
1355        ComponentFactory factory = (ComponentFactory) nCompo.getFactory();
1356        factory.resolveReferences();
1357        factory.installBindings();
1358        factory.installTriggers();
1359        factory.installPlugs();
1360        Iterator JavaDoc iter = nCompo.getInterfaces();
1361        while (iter.hasNext()) {
1362            RtComponentInterface interf = (RtComponentInterface) iter.next();
1363            Iterator JavaDoc iter1 = interf.getInterfaceListeners();
1364            while (iter1.hasNext()) {
1365                RtCollectionPort interf1 = (RtCollectionPort) iter1.next();
1366                interf1.bindProvider(interf, false);
1367            }
1368        }
1369        
1370        Iterator JavaDoc iter1 = aComponent.getPlugTos();
1371        while (iter1.hasNext()) {
1372            ComponentSlot rtSlot = (ComponentSlot) iter1.next();
1373            rtSlot.plug(nCompo);
1374        }
1375        factory.finalizeInstanciation();
1376        return nCompo;
1377    }
1378        
1379    /**
1380     * Method addReference.
1381     * @param aName :
1382     * @throws KilimException :
1383     */

1384    private void addReference(String JavaDoc aName, RTReference aReference) throws KilimException {
1385        if (aReference == null) {
1386            throw new KilimException("attempt to add a null target in component " + getQualifiedName());
1387        }
1388        
1389        if (aName == null) {
1390            throw new KilimException("attempt to add a reference with a null target name in component " + getQualifiedName());
1391        }
1392            
1393        if (references == null) {
1394            references = new LinkedHashMap JavaDoc();
1395        }
1396
1397        if (!references.containsKey(aName)) {
1398            references.put(aName, aReference);
1399        }
1400    }
1401    
1402    /**
1403     * Method removeSubContext.
1404     * @param aName :
1405     * @throws KilimException :
1406     */

1407    private void removeReference(String JavaDoc aName) throws KilimException {
1408        if (aName == null) {
1409            throw new KilimException("attempt to remove a reference through a null name in component " + getQualifiedName());
1410        }
1411        
1412        if (references == null) {
1413            throw new KilimException("no unbound names in current context");
1414        }
1415        
1416        Object JavaDoc previous = references.remove(aName);
1417        if (previous == null) {
1418            throw new KilimException("name " + aName + " unknown in current context ");
1419        }
1420    }
1421    
1422    /**
1423     * Method containsReference.
1424     * @param aName :
1425     * @return boolean
1426     * @throws KilimException :
1427     */

1428    private RTReference getReference(String JavaDoc aName) throws KilimException {
1429        if (aName == null) {
1430            throw new KilimException("null name in containsReference of Component ");
1431        }
1432        if (references == null) {
1433        // throw new KilimException("attempt to get name " + aName + " in a component without references " + getLocalName());
1434
return null;
1435        }
1436        return (RTReference) references.get(aName);
1437    }
1438        
1439    /**
1440     * Method resolveReference.
1441     * This method recursively executes the resolution process in all factories.
1442     * @throws KilimException :
1443     */

1444    private void resolveReferences() throws KilimException {
1445        if (subFactories != null) {
1446            Iterator JavaDoc iter = subFactories.iterator();
1447            while (iter.hasNext()) {
1448                ComponentFactory factory = (ComponentFactory) iter.next();
1449                factory.resolveReferences();
1450            }
1451        }
1452
1453        if (references != null) {
1454            Iterator JavaDoc iter = references.values().iterator();
1455            while (iter.hasNext()) {
1456                RTReference ref = (RTReference) iter.next();
1457                ComponentElement elem = resolveReference(ref.getTargetName(), getComponent());
1458                ref.setTarget((RuntimeElement) elem);
1459            }
1460        }
1461    }
1462                
1463    /**
1464     * Method getInstances.
1465     * @return Iterator
1466     */

1467    private Iterator JavaDoc getReferences() {
1468        if (references == null) {
1469            return KILIM.EMPTY_ITERATOR;
1470        }
1471        return references.values().iterator();
1472    }
1473    
1474    /**
1475     * Method installBinding.
1476     * @param aBinding :
1477     * @throws KilimException :
1478     */

1479    private void installBinding(Binding aBinding) throws KilimException {
1480        String JavaDoc portName = aBinding.getPortName();
1481        RtComponentInterface interf = (RtComponentInterface) resolveReference(portName, getComponent());
1482        
1483        if (interf == null) {
1484            String JavaDoc compoQName = getComponent().getQualifiedName();
1485            throw new KilimException ("attempt to install a binding on an unknown port " + portName + " in component " + compoQName + "(template : " + getFullTemplateSequence() + ")");
1486        }
1487        
1488        Iterator JavaDoc iter = aBinding.getBoundProviders();
1489        while (iter.hasNext()) {
1490            BasicElement elem1 = (BasicElement) iter.next();
1491            ComponentElement compo = null;
1492            if (elem1 instanceof Reference) {
1493                compo = resolveReference(((Reference) elem1).getTargetName(), getComponent());
1494                if (compo == null) {
1495                    String JavaDoc compoQName = getComponent().getQualifiedName();
1496                    throw new KilimException("attempt to bind an unknown reference " + ((Reference) elem1).getTargetName() + " to port " + portName + " in component " + compoQName + "(template : " + getFullTemplateSequence() + ")");
1497                }
1498                
1499                interf.bindProvider((RuntimeSource) compo, false);
1500            } else {
1501                compo = newComponentElement1(elem1);
1502                if (compo == null) {
1503                    String JavaDoc compoQName = getComponent().getQualifiedName();
1504                    throw new KilimException("attempt to bind a null provider to port " + portName + " in component " + compoQName + "(template : " + getFullTemplateSequence() + ")");
1505                } else {
1506                    interf.bindProvider((RuntimeSource) compo, false);
1507                }
1508            }
1509        }
1510    }
1511    
1512    private String JavaDoc getFullTemplateSequence() {
1513        TemplateDescription tmp0 = getComponent().getTemplate();
1514        String JavaDoc tmpName = tmp0.getName();
1515        while ((tmp0 = tmp0.getSuperTemplate()) != null) {
1516            tmpName = tmpName + ", " + tmp0.getName();
1517        }
1518        return tmpName;
1519    }
1520        
1521    /**
1522     * Method installBindings.
1523     * This method recursively installs all bindings.
1524     * @throws KilimException :
1525     */

1526    public void installBindings() throws KilimException {
1527        Component compo = getComponent();
1528        TemplateDescription template = compo.getTemplate();
1529        Iterator JavaDoc iter = template.getBindings(false);
1530        while (iter.hasNext()) {
1531            installBinding((Binding) iter.next());
1532        }
1533            
1534        iter = getSubFactories();
1535        while (iter.hasNext()) {
1536            ((ComponentFactory) iter.next()).installBindings();
1537        }
1538    }
1539    
1540        private void installPlug(Plug aPlug) throws KilimException {
1541        if (aPlug == null) {
1542            throw new KilimException ("attempt to install a plug binding through a null plug in component " + getComponent().getQualifiedName());
1543        }
1544        
1545        String JavaDoc slotName = aPlug.getSlotName();
1546        RtComponentSlot rSlot = (RtComponentSlot) getComponent().getSlot(slotName);
1547        if (rSlot == null) {
1548            throw new KilimException ("attempt to install a plug binding through a null slot in component " + getComponent().getQualifiedName());
1549        }
1550        
1551        String JavaDoc instanceName = aPlug.getInstanceName();
1552        Component compo = (Component) resolveReference(instanceName, getComponent());
1553        rSlot.plug(compo);
1554    }
1555
1556    /**
1557     * Method installPlugs.
1558     * This method recursively installs all bindings.
1559     * @throws KilimException :
1560     */

1561    public void installPlugs() throws KilimException {
1562        Component compo = getComponent();
1563        TemplateDescription template = compo.getTemplate();
1564            
1565        Iterator JavaDoc iter = getSubFactories();
1566        while (iter.hasNext()) {
1567            ((ComponentFactory) iter.next()).installPlugs();
1568        }
1569        
1570        iter = template.getPlugs(false);
1571        while (iter.hasNext()) {
1572            installPlug((Plug) iter.next());
1573        }
1574    }
1575    
1576    /**
1577     * Method installTrigger.
1578     * @param aTrigger
1579     * @throws KilimException
1580     */

1581    private void installTrigger(Trigger aTrigger) throws KilimException {
1582        String JavaDoc sourceName = aTrigger.getSourceName();
1583        ComponentElement elem = (ComponentElement) resolveReference(sourceName, getComponent());
1584        if (elem == null) {
1585            throw new KilimException ("attempt to install a binding on an unknown port " + sourceName + " in component " + getComponent().getQualifiedName());
1586        }
1587        
1588        RuntimeTrigger rContext = new RuntimeTrigger(aTrigger.getEventKind(), ((RtComponentInterface) elem));
1589        Iterator JavaDoc iter = aTrigger.getTransformers();
1590        while (iter.hasNext()) {
1591            BasicElement elem1 = (BasicElement) iter.next();
1592            ComponentElement elem2 = null;
1593            if (elem1 instanceof Reference) {
1594                elem2 = resolveReference(((Reference) elem1).getTargetName(), getComponent());
1595                if (elem2 == null) {
1596                    throw new KilimException("attempt to bind an unknown reference " + ((Reference) elem1).getTargetName() + " to port "
1597                    + sourceName + " in component " + getComponent().getQualifiedName());
1598                }
1599                rContext.addTransformer((RuntimeAction) elem2);
1600            } else {
1601                elem2 = newComponentElement1(elem1);
1602                if (elem2 == null) {
1603                    throw new KilimException("attempt to bind a null provider to port " + sourceName + " in component " + getComponent().getQualifiedName());
1604                } else {
1605                    rContext.addTransformer((RuntimeAction) elem2);
1606                }
1607            }
1608        }
1609        ((RtComponentInterface) elem).addTrigger(rContext);
1610    }
1611    
1612    /**
1613     * Method installTriggers.
1614     * This method recursively installs all triggers
1615     * @throws KilimException :
1616     */

1617    public void installTriggers() throws KilimException {
1618        Component compo = getComponent();
1619        TemplateDescription template = compo.getTemplate();
1620        Iterator JavaDoc iter = template.getTriggers(false);
1621        while (iter.hasNext()) {
1622            Trigger trig = (Trigger) iter.next();
1623            installTrigger(trig);
1624        }
1625        
1626        iter = getSubFactories();
1627        while (iter.hasNext()) {
1628            ((ComponentFactory) iter.next()).installTriggers();
1629        }
1630    }
1631        
1632    /**
1633     * Method finalizeInstanciation.
1634     * This method is executed by the component factory after any other resolution/instanciation actions.
1635     * This method recursively executes in each factory the finalizeInstanciation() method provided by the
1636     * corresponding InstanciationMger.It is used among other things to associate an instanciation manager
1637     * to each component.
1638     * @throws KilimException :
1639     */

1640    public void finalizeInstanciation() throws KilimException {
1641        //recursive invocation of finalizeInstnaciation on subfactories (and thus subcomponents).
1642
Iterator JavaDoc iter = getSubFactories();
1643        while (iter.hasNext()) {
1644            ((ComponentFactory) iter.next()).finalizeInstanciation();
1645        }
1646        //is empty for the moment : no action required ....
1647
}
1648    
1649    /**
1650     * Method initializeInstanciation
1651     * This method is executed once by the component factory before any other actions. It can be used to settle
1652     * resources required by the compoonent factories or any preliminary tasks.
1653     * @throws KilimException :
1654     */

1655    public void initializeInstanciation() throws KilimException {
1656        //is empty for the moment : no action required ....
1657
}
1658    
1659    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1660
protected boolean containsElement(List JavaDoc aList, String JavaDoc aName) throws InternalException {
1661        int lSize = aList.size();
1662        if (lSize == 0) {
1663            return false;
1664        }
1665        
1666        for (int i = 0; i < lSize; i++) {
1667            ComponentFactory el = (ComponentFactory) aList.get(i);
1668            if (aName.equals(el.getLocalName())) {
1669                return true;
1670            }
1671        }
1672        return false;
1673    }
1674    
1675    protected Object JavaDoc removeElement(List JavaDoc aList, String JavaDoc aName) throws InternalException {
1676        int lSize = aList.size();
1677        if (lSize == 0) {
1678            return null;
1679        }
1680        
1681        for (int i = 0; i < lSize; i++) {
1682            ComponentFactory el = (ComponentFactory) aList.get(i);
1683            if (aName.equals(el.getLocalName())) {
1684                return aList.remove(i);
1685            }
1686        }
1687        return null;
1688    }
1689    
1690    protected Object JavaDoc getElement(List JavaDoc aList, String JavaDoc aName) throws InternalException {
1691        int lSize = aList.size();
1692        if (lSize == 0) {
1693            return null;
1694        }
1695        
1696        for (int i = 0; i < lSize; i++) {
1697            ComponentFactory el = (ComponentFactory) aList.get(i);
1698            if (aName.equals(el.getLocalName())) {
1699                return el;
1700            }
1701        }
1702        return null;
1703    }
1704}
1705
1706/**
1707 * This is a base for all classes that implement runtime sources that do not support
1708 * event sources and inputs (i.e. array, class source, null source, reference).
1709 * @author horn
1710*/

1711
1712abstract class RuntimeSourceImpl1 extends RtComponentSource {
1713    
1714    public RuntimeSourceImpl1(TemplateElementImpl aElement, Component aComponent) {
1715        super(aElement, aComponent);
1716    }
1717
1718    /**
1719     * @see org.objectweb.kilim.model.ComponentElement#getLocalName()
1720     */

1721    public String JavaDoc getLocalName() {
1722        return null;
1723    }
1724    
1725    public void addInterfaceListener(RtCollectionPort aInterface) throws KilimException { }
1726    
1727    public void removeInterfaceListener(RtCollectionPort aInterface) throws KilimException { }
1728    
1729    public boolean isEventSource() {
1730        return false;
1731    }
1732    
1733    public Object JavaDoc getEventSourceValue() throws KilimException {
1734        return null;
1735        //throw new KilimException("attempt to get the event source of element not supporting event sources ");
1736
}
1737    
1738    public void setEventSourceValue(Object JavaDoc aSource) throws KilimException {
1739        //throw new KilimException("attempt to set the event source to a element not supporting event sources ");
1740
}
1741}
Popular Tags