KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > kilim > model > instanciation > BDUInstanciationMger


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.instanciation;
20
21 import java.util.Iterator JavaDoc;
22
23 import org.objectweb.kilim.KilimException;
24 import org.objectweb.kilim.description.Trigger;
25 import org.objectweb.kilim.model.Component;
26 import org.objectweb.kilim.model.ComponentInterface;
27 import org.objectweb.kilim.model.ComponentSlot;
28 import org.objectweb.kilim.model.RtCollectionPort;
29 import org.objectweb.kilim.model.RtComponentInterface;
30 import org.objectweb.kilim.model.RtComponentSlot;
31 import org.objectweb.kilim.model.RuntimeAction;
32 import org.objectweb.kilim.model.RuntimeSource;
33 import org.objectweb.kilim.model.RuntimeTrigger;
34 import org.objectweb.kilim.model.mapping.MappingContext;
35
36 /**
37  * @author horn
38  */

39     
40 public class BDUInstanciationMger extends InstanciationMger {
41     
42     /**
43      * @see org.objectweb.kilim.model.InstanciationMger#finalizeInstanciation(TemplateDescription)
44      */

45     public void finalizeInstanciation(Component aComponent, ComponentInterface aInterface, MappingContext aContext) throws KilimException { }
46     
47     /**
48      * @see org.objectweb.kilim.model.InstanciationMger#initializeInstanciation(Component)
49      */

50     public void initializeInstanciation(Component aComponent, ComponentInterface aInterface, MappingContext aContext) throws KilimException {
51         doWhenInstanciate(aComponent, aInterface, aContext);
52     }
53     
54     /**
55      * @see org.objectweb.kilim.model.services.InstanciationMger#initializePlug(Component, ComponentSlot, MappingContext)
56      */

57     public void initializePlug(Component aComponent, ComponentSlot aSlot, MappingContext aContext) throws KilimException {
58         doWhenPlug(aComponent, aSlot, aContext);
59     }
60     
61     /**
62      * @see org.objectweb.kilim.model.services.InstanciationMger#finalizePlug(Component, ComponentSlot, MappingContext)
63      */

64     public void finalizePlug(Component aComponent, ComponentSlot aSlot, MappingContext aContext) throws KilimException { }
65         
66     /**
67      * Method doWhenInstanciate performs the component instanciation according to the following rules : it first looks up for all the nary ports
68      * directly declared at the component level and invokes the getValue() method on every provider they contain. It then looks up for
69      * all the nary ports declared in slots and invokes the getValue() method on every provider they contain. No getValue() on single
70      * value port is performed without need.
71      * @param aComponent is the component containing the current interfarface.
72      * @param aInterface is the current interface.
73      * @param aContext :
74      * @throws KilimException is generated when aComponent is null.
75      */

76
77     public static void doWhenInstanciate(Component aComponent, ComponentInterface aInterface, MappingContext aContext) throws KilimException {
78         if (aComponent == null) {
79             throw new KilimException("attempt to invoke an instanciation manager on a null component");
80         }
81         
82         if (aComponent.isInitialized()) {
83             return;
84         }
85         aComponent.setInitialized();
86         
87         Component contain = aComponent.getContainingComponent();
88         while (contain != null && !contain.isInitialized()) {
89             doWhenInstanciate(contain, null, aContext);
90         }
91
92         Iterator JavaDoc iter0 = aComponent.getInterfaces();
93         while (iter0.hasNext()) {
94             RtComponentInterface interf = (RtComponentInterface) iter0.next();
95             if (interf instanceof RtCollectionPort) {
96                 doWhenInstanciate1((RtCollectionPort) interf, aContext);
97             }
98         }
99         
100         Iterator JavaDoc iter1 = aComponent.getSlots();
101         while (iter1.hasNext()) {
102             RtComponentSlot slot = (RtComponentSlot) iter1.next();
103             doWhenPlug(aComponent, slot, aContext);
104         }
105     }
106     
107     private static void doWhenInstanciate1(RtCollectionPort aInterface, MappingContext aContext) throws KilimException {
108         Iterator JavaDoc iter = aInterface.getBoundProviders();
109         while (iter.hasNext()) {
110             ComponentInterface provider = (ComponentInterface) iter.next();
111             if (provider instanceof RtCollectionPort) {
112                 //doWhenInstanciate1((RtCollectionPort) provider, aContext);
113
throw new KilimException("attempt to use a nary port " + provider.getQualifiedName() + " inside another nary port " + aInterface.getQualifiedName());
114             } else {
115                 RuntimeSource rtSource = (RuntimeSource) ((RuntimeSource) provider).getTarget();
116                 if (!aInterface.triggersDone(rtSource, Trigger.BIND)) {
117                     aInterface.setTriggersDone(rtSource, Trigger.BIND, true);
118                     Iterator JavaDoc iter2 = aInterface.getTriggers(Trigger.BIND);
119                     Object JavaDoc resultValue = provider.getValue();
120                     while (iter2.hasNext()) {
121                         RuntimeTrigger rtElem = (RuntimeTrigger) iter2.next();
122                         if (rtElem.getEventKind() == Trigger.BIND) {
123                             Iterator JavaDoc iter3 = rtElem.getTransformers();
124                             while (iter3.hasNext()) {
125                                 RuntimeAction rtElem3 = (RuntimeAction) iter3.next();
126                                 rtElem3.setEventSourceValue(resultValue);
127                                 
128                                 rtElem3.execute();
129                             }
130                         }
131                     }
132                 }
133             }
134         }
135     }
136     
137     /**
138      * Method doWhenPlug.
139      * @param aComponent :
140      * @param aSlot :
141      * @param aContext :
142      * @throws KilimException :
143      */

144     public static void doWhenPlug(Component aComponent, ComponentSlot aSlot, MappingContext aContext) throws KilimException {
145         if (aSlot.isInitialized()) {
146             return;
147         }
148         aSlot.setInitialized();
149
150         Iterator JavaDoc iter0 = aSlot.getInterfaces();
151         while (iter0.hasNext()) {
152             RtComponentInterface interf = (RtComponentInterface) iter0.next();
153             if (interf instanceof RtCollectionPort) {
154                 doWhenInstanciate1((RtCollectionPort) interf, aContext);
155             }
156         }
157     }
158 }
Popular Tags