KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Iterator JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Stack JavaDoc;
25 import java.util.LinkedHashMap JavaDoc;
26
27 import org.objectweb.kilim.InternalException;
28 import org.objectweb.kilim.KilimException;
29 import org.objectweb.kilim.description.KILIM;
30 import org.objectweb.kilim.description.NamedElement;
31 import org.objectweb.kilim.description.Port;
32 import org.objectweb.kilim.description.Trigger;
33     
34 /**
35  * @author horn
36  * RtCollectionPort is the class for collection ports.
37  */

38     
39 public class RtCollectionPort extends RtComponentInterface {
40     private static final Integer JavaDoc FALSE = new Integer JavaDoc(0);
41     private static final Integer JavaDoc BIND_TRUE = new Integer JavaDoc(1);
42     private static final Integer JavaDoc UNBIND_TRUE = new Integer JavaDoc(2);
43         
44     private LinkedHashMap JavaDoc providers;
45     
46     /**
47      * The public constructor of rtCollectionPort.
48      * @param aPort : the corresponding template element.
49      * @param aContainer : the container in which the port is declared and defined (is either a RtComponentSlot or a rtComponent.
50      */

51     public RtCollectionPort(Port aPort, ContainerElement aContainer) {
52         super(aPort, aContainer);
53     }
54
55     /**
56      * @see org.objectweb.kilim.model.ComponentInterface#isSingleValueInterface()
57      */

58     public boolean isSingleValuePort() {
59         return false;
60     }
61     
62     /**
63      * @see org.objectweb.kilim.model.ComponentInterface#isCollectionInterface()
64      */

65     public boolean isCollectionPort() {
66         return true;
67     }
68     
69     /**
70      * @see org.objectweb.kilim.model.ComponentInterface#isProvider()
71      */

72     public boolean isProvider() {
73         return false;
74     }
75     
76     /**
77      * @see org.objectweb.kilim.model.ComponentInterface#isProperty()
78      */

79     public boolean isProperty() {
80         return false;
81     }
82     
83     /**
84      * @see org.objectweb.kilim.model.ComponentSource#hasValue()
85      */

86     public boolean hasValue() {
87         return false;
88     }
89     
90     /**
91      * @see org.objectweb.kilim.model.RuntimeSource#checkValue(Stack)
92      */

93     public boolean checkValue(Stack JavaDoc exclude) throws KilimException {
94         return false;
95     }
96
97     /**
98      * @see org.objectweb.kilim.model.ComponentElement#getLocalName()
99      */

100     public String JavaDoc getLocalName() {
101         NamedElement elem = (NamedElement) getElementDescription();
102         return elem.getLocalName();
103     }
104     
105     /**
106      * Triggers associated to collection ports must be performed for each bound source when the getValue() or unbound method is invoked on the source.
107      * This method stores a flag indicating that triggers have been executed on a given source.
108      * @param aProvider : the value source.
109      * @param aEKind : the kind of event. The 2 possible values are Trigger.BIND (when getValue is effectively performed) or Trigger.UNBIND
110      * (when unbound is executed).
111      * @param isU : is true for update.
112      * @throws KilimException : generated if aProvider is null, if no sources are bound to the port, if the source is unknown.
113      */

114     public void setTriggersDone(RuntimeSource aProvider, int aEKind, boolean isU) throws KilimException {
115         if (aProvider == null) {
116             throw new KilimException("attempt to set the use state of a null provider to an interface " + getQualifiedName());
117         }
118                     
119         if (providers == null) {
120             throw new KilimException("attempt to set the use state of a provider in an empty nary port " + getQualifiedName());
121         }
122         
123         boolean cont = providers.containsKey(aProvider);
124
125         if (!cont) {
126             throw new InternalException("attempt to get the inner state of an unknown provider in an nary port " + getQualifiedName() + " # " + aProvider);
127         } else {
128             switch(aEKind) {
129                 case Trigger.BIND :
130                     providers.put(aProvider, isU ? BIND_TRUE : FALSE);
131                     break;
132                 case Trigger.UNBIND :
133                     providers.put(aProvider, isU ? UNBIND_TRUE : FALSE);
134                     break;
135                 default :
136                     throw new InternalException("attempt to get the inner state of an unknown provider in an nary port " + getQualifiedName());
137             }
138         }
139     }
140     
141     /**
142      * returns whether the triggers associated to a collection port have been executed for a given source.
143      * @param aProvider : the value source.
144      * @param aEKind : the kind of event. The 2 possible values are Trigger.BIND (when getValue is effectively performed) or Trigger.UNBIND
145      * (when unbound is executed).
146      * @return boolean
147      * @throws KilimException : generated if aProvider is null, if the collection port is empty or if the port is not bound to the source.
148      */

149     public boolean triggersDone(RuntimeSource aProvider, int aEKind) throws KilimException {
150         if (aProvider == null) {
151             throw new InternalException("attempt to get the inner state of a null provider to an interface " + getQualifiedName());
152         }
153                     
154         if (providers == null) {
155             throw new InternalException("attempt to get the inner state of a provider from an empty nary port " + getQualifiedName());
156         }
157         
158         Integer JavaDoc result = (Integer JavaDoc) providers.get(aProvider);
159         
160         if (result == null) {
161             throw new InternalException("attempt to get the inner state of an unknown provider in an nary port " + getQualifiedName() + " # " + aProvider);
162         } else {
163             switch(aEKind) {
164                 case Trigger.BIND :
165                     return result == BIND_TRUE;
166                 case Trigger.UNBIND :
167                     return result == UNBIND_TRUE;
168                 default :
169                     throw new InternalException("attempt to get the inner state of an unknown provider in an nary port " + getQualifiedName());
170             }
171         }
172     }
173     
174     /**
175      * binds a new value source to a collection port.
176      * @param aProvider : the value source.
177      * @param jReplace : allows to rebind an already bound source, when true (this parameter is totally stupid !!!! FHO)
178      * @throws KilimException : generated when aProvider is null or when aProvider is already bound and jReplace is false.
179      */

180     public void bindProvider(RuntimeSource aProvider, boolean jReplace) throws KilimException {
181         if (aProvider == null) {
182             throw new KilimException("attempt to bind a null provider to an interface " + getQualifiedName());
183         }
184                     
185         if (providers == null) {
186             providers = new LinkedHashMap JavaDoc();
187         }
188         
189         boolean cont = providers.containsKey(aProvider);
190         if (!cont) {
191             providers.put (aProvider, FALSE);
192         } else {
193             if (jReplace) {
194                 providers.put(aProvider, FALSE);
195             } else {
196                 throw new KilimException("attempt to rebind a provider to a nary interface " + getQualifiedName());
197             }
198         }
199         
200         aProvider.addInterfaceListener(this);
201     }
202     
203     /**
204      * @see org.objectweb.kilim.model.ComponentInterface#unbindProvider(RuntimeSource)
205      */

206     public void unbindProvider(RuntimeSource aProvider) throws KilimException {
207         if (aProvider == null) {
208             throw new KilimException("attempt to unbind a null provider in the nary interface " + getQualifiedName());
209         }
210         if (providers == null) {
211             throw new KilimException("attempt to unbind a provider from the empty interface " + getQualifiedName());
212         }
213         
214         Object JavaDoc result = providers.remove(aProvider);
215         if (result == null) {
216             throw new KilimException("attempt to unbind a provider not presently bound to the interface " + getQualifiedName());
217         }
218     
219         aProvider.removeInterfaceListener(this);
220     }
221     
222     /**
223      * returns as an iterator the bound value sources.
224      * @return Iterator
225      */

226     public Iterator JavaDoc getBoundProviders() {
227         if (providers == null) {
228             return KILIM.EMPTY_ITERATOR;
229         }
230         return providers.keySet().iterator();
231     }
232
233     /**
234      * @see org.objectweb.kilim.model.ComponentInterface#bindValue(Object, RuntimeContext)
235      */

236     public void bindValue(Object JavaDoc aValue) throws KilimException {
237         throw new KilimException("attempt to get a value from a multiple value port " + getQualifiedName());
238     }
239         
240     /**
241      * @see org.objectweb.kilim.model.RuntimeSource#unbindValue(RuntimeContext)
242      */

243     public void unbindValue() throws KilimException {
244         throw new KilimException("attempt to unbind a value from a multiple value port " + getQualifiedName());
245     }
246         
247     /**
248      * @see org.objectweb.kilim.model.RuntimeSource#getValue(RuntimeContext)
249      */

250     public Object JavaDoc getValue() throws KilimException {
251         throw new KilimException("attempt to get a value from a multiple value port " + getQualifiedName());
252     }
253     
254     /**
255      * @see org.objectweb.kilim.model.RuntimeSource#getValue(RuntimeContext)
256      */

257     
258     protected Object JavaDoc specificGetValue() throws KilimException { return null; }
259     
260     /**
261      * @see org.objectweb.kilim.model.RtComponentInterface#specificBindValue(Object)
262      */

263     protected void specificBindValue(Object JavaDoc aValue) throws KilimException { }
264     
265     /**
266      * @see org.objectweb.kilim.model.RtComponentInterface#specificUnbindValue()
267      */

268     protected void specificUnbindValue() throws KilimException { }
269     
270     /**
271      * @see org.objectweb.kilim.model.RtComponentInterface#specificUpdate()
272      */

273     public void update() throws KilimException {
274         Iterator JavaDoc iter = getBoundProviders();
275         while (iter.hasNext()) {
276             ComponentInterface provider = (ComponentInterface) iter.next();
277             if (provider instanceof RtCollectionPort) {
278                 throw new KilimException("attempt to use imbricated nary ports : " + provider.getQualifiedName() + " in " + getQualifiedName());
279             } else {
280                 if (!triggersDone((RuntimeSource) provider, Trigger.BIND)) {
281                     setTriggersDone((RuntimeSource) provider, Trigger.BIND, true);
282                     Object JavaDoc resultValue = provider.getValue();
283                     Iterator JavaDoc iter2 = getTriggers(Trigger.BIND);
284                     while (iter2.hasNext()) {
285                         RuntimeTrigger rtElem = (RuntimeTrigger) iter2.next();
286                         if (rtElem.getEventKind() == Trigger.BIND) {
287                             Iterator JavaDoc iter3 = rtElem.getTransformers();
288                             while (iter3.hasNext()) {
289                                 RuntimeAction rtElem3 = (RuntimeAction) iter3.next();
290                                 rtElem3.setEventSourceValue(resultValue);
291                                 rtElem3.execute();
292                                 rtElem3.setEventSourceValue(null);
293                             }
294                         }
295                     }
296                 }
297             }
298         }
299     }
300 }
Popular Tags