KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.FileWriter JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.objectweb.kilim.KilimException;
28 import org.objectweb.kilim.description.KILIM;
29 import org.objectweb.kilim.description.TemplateElementImpl;
30 import org.objectweb.kilim.description.Trigger;
31 import org.objectweb.kilim.model.instanciation.InstanciationMger;
32
33 /**
34  * @author horn
35  */

36 public abstract class RtComponentInterface extends RtComponentSource implements ComponentInterface, RuntimeSource {
37     private List JavaDoc bTriggers;
38     private List JavaDoc uTriggers;
39     private boolean bTriggersDone;
40     private boolean uTriggersDone;
41     private boolean isModified = true;
42     
43     /**
44      * The public constructor of RtComponentInterface.
45      * @param aElement : the template description of the interface.
46      * @param aComponent :the container element in which the interface is defined.
47      */

48     protected RtComponentInterface(TemplateElementImpl aElement, ContainerElement aComponent) {
49         super(aElement, aComponent);
50     }
51     
52     /**
53      * returns whether the value has been changed since the last execution of bind triggers associated to the interface.
54      * @return boolean
55      * @throws KilimException :
56      */

57     public boolean isModified() throws KilimException {
58         return isModified;
59     }
60     
61     /**
62      * returns whether the triggers associated to a given kind of event have been executed or not.
63      * @param aKind : the kind of event. It is either Trigger.BIND or Trigger.UNBIND.
64      * @return boolean
65      * @throws KilimException : generated if aKind has an illegal value.
66      */

67     public boolean triggersDone(int aKind) throws KilimException {
68         switch(aKind) {
69             case Trigger.BIND :
70                 return bTriggersDone;
71             case Trigger.UNBIND :
72                 return uTriggersDone;
73             default :
74                 throw new KilimException("attempt to get the trigger status in an interface " + getQualifiedName() + " for an unknow event kind : " + aKind);
75         }
76     }
77     
78     /**
79      * sets a flag indicating whether the triggers associated to a given kind of event have been performed or not.
80      * @param aKind : the kind of event. It is either Trigger.BIND or Trigger.UNBIND.
81      * @param done : true if the triggersz have been performed.
82      * @throws KilimException : generated if aKind has an illegal value.
83      */

84     public void setTriggersDone(int aKind, boolean done) throws KilimException {
85         switch(aKind) {
86             case Trigger.BIND :
87                 bTriggersDone = done;
88                 break;
89             case Trigger.UNBIND :
90                 uTriggersDone = done;
91                 break;
92             default :
93                 throw new KilimException("attempt to get the trigger status in an interface " + getQualifiedName() + " for an unknow event kind : " + aKind);
94         }
95     }
96
97     /**
98      * binds an interface to a runtime source.
99      * @param aProvider : the value source to be bound.
100      * @param jReplace : true if replacement is allowed.
101      * @throws KilimException : generated if aProvider is null, if the interface is already bound and jReplace is false.
102      */

103     public abstract void bindProvider(RuntimeSource aProvider, boolean jReplace) throws KilimException;
104     
105     /**
106      * unbinds an interface from a runtime source.
107      * @param aProvider : the source value to be unbound.
108      * @throws KilimException : generated if aProvider is null or if the interface is not bound to aProvider.
109      */

110     public abstract void unbindProvider(RuntimeSource aProvider) throws KilimException;
111     
112     /**
113      * adds a new trigger to the interface.
114      * @param aTrigger : the trigger to be added.
115      */

116     public void addTrigger(RuntimeTrigger aTrigger) {
117         if (aTrigger == null) {
118             return;
119         }
120         
121         if (aTrigger.getEventKind() == Trigger.BIND) {
122             if (bTriggers == null) {
123                 bTriggers = new ArrayList JavaDoc();
124             }
125             bTriggers.add(aTrigger);
126         } else {
127             if (uTriggers == null) {
128                 uTriggers = new ArrayList JavaDoc();
129             }
130             uTriggers.add(aTrigger);
131         }
132     }
133     
134     /**
135      * removes a trigger from an interface.
136      * @param aTrigger : the trigger to be added.
137      */

138     public void removeTrigger(RuntimeTrigger aTrigger) {
139         if (aTrigger == null) {
140             return;
141         }
142         
143         if (aTrigger.getEventKind() == Trigger.BIND) {
144             if (bTriggers == null) {
145                 return;
146             }
147             bTriggers.remove(aTrigger);
148             if (bTriggers.size() == 0) {
149                 bTriggers = null;
150             }
151         } else {
152             if (uTriggers == null) {
153                 return;
154             }
155             uTriggers.remove(aTrigger);
156             if (uTriggers.size() == 0) {
157                 uTriggers = null;
158             }
159         }
160     }
161
162     
163     /**
164      * returns the ordered list of the triggers associated to an interface.
165      * @param aEventKind : the kind of trigger event. It should be one Trigger.BIND or Trigger.UNBIND.
166      * @return LinkedList
167      */

168     public List JavaDoc getTriggerList(int aEventKind) {
169         List JavaDoc list = (aEventKind == Trigger.BIND) ? bTriggers : uTriggers;
170         if (list == null) {
171             return KILIM.EMPTY_LIST;
172         }
173         return list;
174     }
175     
176     /**
177      * returns the as an iterator the triggers associated to an interface.
178      * @param aEventKind : the kind of trigger event. It should be one Trigger.BIND or Trigger.UNBIND.
179      * @return Iterator
180      */

181     public Iterator JavaDoc getTriggers(int aEventKind) {
182         return getTriggerList(aEventKind).iterator();
183     }
184         
185     /**
186      * @see org.objectweb.kilim.model.RuntimeSource#isEventSource()
187      */

188     public boolean isEventSource() {
189         return false;
190     }
191     
192     /**
193      * @see org.objectweb.kilim.model.RuntimeSource#setEventSource(Object)
194      */

195     public void setEventSourceValue(Object JavaDoc aValue) throws KilimException {
196         throw new KilimException("attempt to set an event source to an interface " + getQualifiedName());
197     }
198         
199     /**
200      * @see org.objectweb.kilim.model.RuntimeSource#getEventSource()
201      */

202     public Object JavaDoc getEventSourceValue() throws KilimException {
203         throw new KilimException("attempt to get an event source from an interface " + getQualifiedName());
204     }
205
206     /**
207      * @see org.objectweb.kilim.model.ComponentSource#getValue()
208      */

209     
210     PrintWriter JavaDoc pW=null;
211     FileWriter JavaDoc fW = null;
212
213     public Object JavaDoc getValue() throws KilimException {
214         initializeInstanciation();
215         //the local variable hasAValue indicates whether the source had a value prior to this call and thus to deterrmine whether
216
//triggers should be executed or not.
217
boolean hasAValue = hasValue();
218         Object JavaDoc resultValue = specificGetValue();
219         
220         //if (fW == null || pW == null) {
221
// try {
222
// fW = new FileWriter("c:\\temp\\webK.log");
223
// pW = new PrintWriter(fW);
224
// } catch (Exception exc) {
225
// throw new KilimException("Problem when creating log file : c:\\temp\\webK.log");
226
// }
227
//}
228

229         //if ("addViewer".equals(aMethodName) && aSupport.getClass().getName().endsWith("Row")) {
230
// pW.println("addViewer sur " + aSupport);
231
// boolean pr = false;
232
// for (int i=0; i< paramObjects.length;i++) {
233
// pW.println("argument _" + i + " : " +paramObjects[i] + " (" + typeNames[i] + ")");
234
// if (paramObjects[i] != null && paramObjects[i].getClass().getName().endsWith("Cell")) pr= true;
235
// }
236
//if (resultValue == null) {
237
// System.out.println("=================================================================");
238
// System.out.println("null interface " + getQualifiedName());
239
// //new Exception().printStackTrace();
240
// System.out.println("=================================================================");
241
//}
242

243         if (hasAValue && !isModified) {
244             return resultValue;
245         }
246         //triggers should not be executed here because it has already been done (in an evaluation cycle).
247
if (triggersDone(Trigger.BIND)) {
248             return resultValue;
249         }
250         
251         //triggers are executed here.
252
isModified = false;
253         setTriggersDone(Trigger.BIND, true);
254         fireTriggers(Trigger.BIND, resultValue);
255         return resultValue;
256     }
257         
258     /**
259      * @see org.objectweb.kilim.model.ComponentInterface#unbindValue()
260      */

261     public void unbindValue() throws KilimException {
262         initializeInstanciation();
263         Object JavaDoc aValue = getValue();
264         specificUnbindValue();
265         //triggers are executed here.
266
isModified = true;
267         if (triggersDone(Trigger.UNBIND)) {
268             return;
269         }
270         setTriggersDone(Trigger.UNBIND, true);
271         fireTriggers(Trigger.UNBIND, aValue);
272     }
273     
274     /**
275      * @see org.objectweb.kilim.model.ComponentInterface#bindValue(Object)
276      */

277     public void bindValue(Object JavaDoc aValue) throws KilimException {
278         if (hasValue()) {
279             unbindValue();
280         } else {
281             initializeInstanciation();
282         }
283         specificBindValue(aValue);
284         //triggers are executed here.
285
isModified = true;
286         setTriggersDone(Trigger.BIND, true);
287         fireTriggers(Trigger.BIND, aValue);
288     }
289     
290     /**
291      * @see org.objectweb.kilim.model.ComponentInterface#bindValue(Object)
292      */

293     public void update() throws KilimException {
294         initializeInstanciation();
295         //the local variable hasAValue indicates whether the source had a value prior to this call and thus to deterrmine whether
296
//triggers should be executed or not.
297
boolean hasAValue = hasValue();
298         Object JavaDoc resultValue = specificGetValue();
299         //triggers are executed here.
300
isModified = false;
301         setTriggersDone(Trigger.BIND, true);
302         fireTriggers(Trigger.BIND, resultValue);
303     }
304
305     protected void fireTriggers(int eventKind, Object JavaDoc resultValue) throws KilimException {
306         //this method executes all triggers associated to the interface.
307
//It therefore gets all the triggers corresponding to the eventKind.
308
List JavaDoc lTriggers = getTriggerList(eventKind);
309         if (lTriggers != null) {
310             int lSize = lTriggers.size();
311             for (int i = 0; i < lSize; i++) {
312                 RuntimeTrigger trig = (RuntimeTrigger) lTriggers.get(i);
313                 if (trig.getEventKind() == eventKind) {
314                     Iterator JavaDoc iter1 = trig.getTransformers();
315                     //all transformers defined in the trigger are executed.
316
while (iter1.hasNext()) {
317                         RuntimeAction rtElem = (RuntimeAction) iter1.next();
318                         rtElem.setEventSourceValue(resultValue);
319                         rtElem.execute();
320                         rtElem.setEventSourceValue(null);
321                     }
322                 }
323             }
324         }
325     }
326     
327     protected void initializeInstanciation() throws KilimException {
328         Component contain = getContainingComponent();
329         if (contain == null) {
330             throw new KilimException("attempt to use a source without containing component " + getLocalName());
331         }
332
333         //instanciation managers are used when an interface becomes bound in a component that is not yet instanciated.
334
if (!contain.isInitialized()) {
335             //InstanciationMger mger = (InstanciationMger) contain.getController("instanciation manager");
336
InstanciationMger mger = contain.getInstanciationMger();
337             if (mger == null) {
338                 mger = contain.getInstanciationMgerFromConfiguration();
339             }
340             
341             if (mger == null) {
342                 return;
343             }
344             
345             mger.initializeInstanciation(contain, this);
346
347             Iterator JavaDoc iter = contain.getSlots();
348             while (iter.hasNext()) {
349                 ComponentSlot slot = (ComponentSlot) iter.next();
350                 Iterator JavaDoc iter0 = slot.getInterfaces();
351                 while (iter0.hasNext()) {
352                     RtComponentInterface interf = (RtComponentInterface) iter0.next();
353                     if (interf instanceof RtCollectionPort) {
354                         mger.initializePlug(contain, slot);
355                     }
356                 }
357             }
358         }
359     }
360     
361     /**
362      * Method specificGetValue.
363      * @return Object
364      * @throws KilimException :
365      */

366     protected abstract Object JavaDoc specificGetValue() throws KilimException;
367     
368     /**
369      * Method specificBindValue.
370      * @param aValue :
371      * @throws KilimException :
372      */

373     protected abstract void specificBindValue(Object JavaDoc aValue) throws KilimException;
374     
375     /**
376      * Method specificUnbindValue.
377      * @throws KilimException :
378      */

379     protected abstract void specificUnbindValue() throws KilimException;
380 }
Popular Tags