KickJava   Java API By Example, From Geeks To Geeks.

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


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

36     
37 public class RtComponentSlot extends RtComponentElement implements ComponentSlot {
38     private SlotFactory factory;
39     private boolean isInitialized;
40     private List JavaDoc interfaces;
41     private List JavaDoc plugIns;
42     
43     /**
44      * The public constructor for RtComponentSlots
45      * @param aSlot : the slot template description
46      * @param aComponent : the component containing the slot
47      * @param aFactory : the slot factory.
48      */

49     public RtComponentSlot(Slot aSlot, Component aComponent, SlotFactory aFactory) {
50         super(aSlot, aComponent);
51         factory = aFactory;
52     }
53     
54     /**
55      * @see org.objectweb.kilim.model.ContainerElement#isComponent()
56      */

57     public boolean isComponent() {
58         return false;
59     }
60     
61     /**
62      * @see org.objectweb.kilim.model.ContainerElement#isSlot()
63      */

64     public boolean isSlot() {
65         return true;
66     }
67     
68     /**
69      * @see org.objectweb.kilim.model.ContainerElement#getFactory()
70      */

71     public Factory getFactory() {
72         return factory;
73     }
74     
75     /**
76      * @see org.objectweb.kilim.model.Component#isInitialized()
77      */

78     public boolean isInitialized() {
79         return isInitialized;
80     }
81     
82     /**
83      * @see org.objectweb.kilim.model.Component#setInitialized(boolean)
84      */

85     public void setInitialized() {
86         isInitialized = true;
87     }
88     
89     /**
90      * @see org.objectweb.kilim.model.ComponentElement#getLocalName()
91      */

92     public String JavaDoc getLocalName() {
93         NamedElement elem = (NamedElement) getElementDescription();
94         return elem.getLocalName();
95     }
96
97     /**
98      * adds an Interface.in the slot.
99      * @param aInterface : the interface to be added.
100      * @throws KilimException : generated if aInterface is null or is already present in the slot.
101      */

102     public void addInterface(ComponentInterface aInterface) throws KilimException {
103         if (aInterface == null) {
104             throw new KilimException("attempt to add a null interface in slot " + getQualifiedName());
105         }
106         
107         if (!(aInterface instanceof RtComponentInterface)) {
108             throw new KilimException("attempt to add a non kilim interface in slot " + getQualifiedName());
109         }
110         
111         if (interfaces == null) {
112             interfaces = new ArrayList JavaDoc();
113         }
114         String JavaDoc lName = aInterface.getLocalName();
115         if (containsElement(interfaces, lName)) {
116             throw new KilimException("attempt to add an already added interface " + aInterface.getQualifiedName() + " in slot " + getQualifiedName());
117         }
118         interfaces.add(aInterface);
119     }
120     
121     /**
122      * removes an interface. from the slot.
123      * @param aInterface : the interface to be removed.
124      * @throws KilimException : generated if aInterface is null or is unknown.
125      */

126     public void removeInterface(ComponentInterface aInterface) throws KilimException {
127         if (aInterface == null) {
128             throw new KilimException("attempt to remove a null interface from slot " + getQualifiedName());
129         }
130         if (interfaces == null) {
131             throw new KilimException("attempt to remove an interface" + aInterface.getLocalName() + " from slot " + getQualifiedName());
132         }
133         String JavaDoc lName = aInterface.getLocalName();
134         Object JavaDoc previous = removeElement(interfaces, lName);
135         if (previous == null) {
136             throw new KilimException("attempt to remove an unknown interface" + lName + " from slot " + getQualifiedName());
137         }
138     }
139
140     /**
141      * returns as an interator the interfaces known inthe slot.
142      * @return Iterator
143      */

144     public Iterator JavaDoc getInterfaces() {
145         if (interfaces == null) {
146             return KILIM.EMPTY_ITERATOR;
147         }
148         return interfaces.iterator();
149     }
150     
151     /**
152      * returns the local interface identified by its local name.
153      * @param aName : the local name of the interface.
154      * @return ComponentInterface :
155      * @throws KilimException : generated if aName is null or if the slot does not contain an interface whose name is aName.
156      */

157     public ComponentInterface getInterface(String JavaDoc aName) throws KilimException {
158         if (aName == null) {
159             throw new KilimException("attempt to get an interface through a null name in slot " + getQualifiedName());
160         }
161         if (interfaces == null) {
162             throw new KilimException("attempt to get interface " + aName + " from empty slot " + getQualifiedName());
163         }
164         
165         ComponentInterface result = (ComponentInterface) getElement(interfaces, aName);
166         if (result == null) {
167             throw new KilimException("attempt to get a unknown interface " + aName + " from slot " + getQualifiedName());
168         }
169         return result;
170     }
171     
172     /**
173      * returns as an iterator the components plugged in the slot..
174      * @return Iterator
175      */

176     public Iterator JavaDoc getPlugIns() {
177         if (plugIns == null) {
178             return KILIM.EMPTY_ITERATOR;
179         }
180         return plugIns.listIterator();
181     }
182     
183     /**
184      * plugs a component in the slot.
185      * @param aComponent : the component to be plugged.
186      * @throws KilimException : generated if aComponent is null or is already plugged in the slot.
187      */

188     public void plug(Component aComponent) throws KilimException {
189         if (aComponent == null) {
190             throw new KilimException("attempt to plug a null Component in slot " + getQualifiedName());
191         }
192         
193         if (plugIns == null) {
194             plugIns = new ArrayList JavaDoc();
195         }
196         
197         if (interfaces == null) {
198             return;
199         }
200         
201         if (plugIns.contains(aComponent)) {
202             throw new KilimException("attempt to plug a Component " + aComponent.getQualifiedName() + " already present in slot " + getQualifiedName());
203         }
204         
205         Iterator JavaDoc iter = interfaces.iterator();
206         
207         while (iter.hasNext()) {
208             RtComponentInterface interf = (RtComponentInterface) iter.next();
209             String JavaDoc lName = interf.getLocalName();
210             if (interf == null) {
211                 throw new KilimException ("attempt to use an unknown offered port " + lName + " when plugging a component " + aComponent + " into slot " + getQualifiedName());
212             }
213             
214             RtComponentInterface interf1 = (RtComponentInterface) aComponent.getInterface(lName);
215             
216             // The following code replaces the previous commented lines : only roles of ports defined in slots are considered.
217
//The role of the component ports is considered as meaningless by Kilim2.
218
boolean isOff = true;
219             TemplateElementImpl elDesc = interf.getElementDescription();
220             boolean isOff1 = true;
221             TemplateElementImpl elDesc1 = interf1.getElementDescription();
222             
223             if (elDesc instanceof Port) {
224                 isOff = ((Port) elDesc).isOffered();
225                 if (isOff) {
226                     if (elDesc1 instanceof Port && !((Port) elDesc1).isOffered()) {
227                         //System.err.println("warning : port " + interf1.getQualifiedName() + " is declared <required> in component " + aComponent.getQualifiedName() + " and <offered> in slot " + getQualifiedName());
228
}
229                 } else {
230                     if (!(elDesc1 instanceof Port) || ((Port) elDesc1).isOffered()) {
231                         //System.err.println("warning : interface " + interf1.getQualifiedName() + " is declared <offered> in component " + aComponent.getQualifiedName() + " and <required> in slot " + getQualifiedName());
232
}
233                 }
234             } else {
235                 if (!(elDesc1 instanceof Port) || !((Port) elDesc1).isOffered()) {
236                     //System.err.println("warning : interface " + interf1.getQualifiedName() + " is declared <required> in component " + aComponent.getQualifiedName() + " and <offered> in slot " + getQualifiedName());
237
}
238             }
239             
240             if (elDesc1 instanceof Port) {
241                 isOff = ((Port) elDesc).isOffered();
242                 isOff1 = ((Port) elDesc1).isOffered();
243                 if (isOff && (elDesc1 instanceof Port && !isOff1)) {
244                     //System.out.println("warning : port " + interf1.getQualifiedName() + " is declared <required> in component " + aComponent.getQualifiedName() + " and <offered> in slot " + getQualifiedName());
245
}
246             }
247             
248             if (isOff) {
249                 interf.bindProvider(interf1, false);
250                 if (isInitialized() || aComponent.isInitialized()) {
251                     if (interf instanceof RtCollectionPort || interf1.hasValue()) {
252                         interf.fireTriggers(Trigger.BIND, interf1.getValue());
253                     }
254                 }
255             } else {
256                 interf1.bindProvider(interf, false);
257                 if (isInitialized() || aComponent.isInitialized()) {
258                     //list.add(new InterfacePair(interf1, interf));
259
if (interf1 instanceof RtCollectionPort || interf.hasValue()) {
260                         interf1.fireTriggers(Trigger.BIND, interf.getValue());
261                     }
262                 }
263             }
264         }
265         
266         plugIns.add(aComponent);
267         ((RtComponent) aComponent).addPlugTo(this);
268     }
269     
270     /**
271      * unplugs a component from the slot.
272      * @param aComponent : the component to be removed.
273      * @throws KilimException : generated if aComponent is null or is not plugged in the slot.
274      */

275     public void unplug(Component aComponent) throws KilimException {
276         if (aComponent == null) {
277             throw new KilimException("attempt to unplug a null component in slot " + getQualifiedName());
278         }
279         
280         if (plugIns == null) {
281             throw new KilimException("attempt to unplug a component \"" + aComponent.getQualifiedName() + "\" from an unplugged slot \"" + getQualifiedName() + "\"");
282         }
283         
284         boolean remove = plugIns.remove(aComponent);
285         if (!remove) {
286             throw new KilimException("attempt to unplug a non plugged component \"" + aComponent.getQualifiedName() + "\" from slot \"" + getQualifiedName() + "\"");
287         }
288         
289         ((RtComponent) aComponent).removePlugTo(this);
290
291         Iterator JavaDoc iter = getInterfaces();
292         List JavaDoc list = new ArrayList JavaDoc();
293         
294         while (iter.hasNext()) {
295             RtComponentInterface interf = (RtComponentInterface) iter.next();
296             String JavaDoc lName = interf.getLocalName();
297             
298             if (interf == null) {
299                 throw new KilimException ("attempt to use an unknown offered port " + lName + " when unplugging a component " + aComponent.getQualifiedName() + " from slot " + getQualifiedName());
300             }
301             
302             RtComponentInterface interf1 = (RtComponentInterface) aComponent.getInterface(lName);
303             TemplateElementImpl elDesc = interf.getElementDescription();
304             boolean isOff = true;
305             
306             if (elDesc instanceof Port) {
307                 isOff = ((Port) elDesc).isOffered();
308             }
309                         
310             if (isOff) {
311                 interf.unbindProvider(interf1);
312                 //list.addLast(new InterfacePair(interf, interf1));
313
if (interf1.hasValue()) {
314                     interf.fireTriggers(Trigger.UNBIND, interf1.specificGetValue());
315                 }
316             } else {
317                 interf1.unbindProvider(interf);
318                 //list.addLast(new InterfacePair(interf1, interf));
319
if (interf.hasValue()) {
320                     interf1.fireTriggers(Trigger.UNBIND, interf.specificGetValue());
321                 }
322             }
323         }
324     }
325 }
326
327 class InterfacePair {
328     RtComponentInterface first;
329     RtComponentInterface second;
330     
331     InterfacePair(RtComponentInterface itf1, RtComponentInterface itf2) {
332         first = itf1;
333         second = itf2;
334     }
335 }
Popular Tags