1 18 19 package org.objectweb.kilim.model; 20 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.List ; 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 36 37 public class RtComponentSlot extends RtComponentElement implements ComponentSlot { 38 private SlotFactory factory; 39 private boolean isInitialized; 40 private List interfaces; 41 private List plugIns; 42 43 49 public RtComponentSlot(Slot aSlot, Component aComponent, SlotFactory aFactory) { 50 super(aSlot, aComponent); 51 factory = aFactory; 52 } 53 54 57 public boolean isComponent() { 58 return false; 59 } 60 61 64 public boolean isSlot() { 65 return true; 66 } 67 68 71 public Factory getFactory() { 72 return factory; 73 } 74 75 78 public boolean isInitialized() { 79 return isInitialized; 80 } 81 82 85 public void setInitialized() { 86 isInitialized = true; 87 } 88 89 92 public String getLocalName() { 93 NamedElement elem = (NamedElement) getElementDescription(); 94 return elem.getLocalName(); 95 } 96 97 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 (); 113 } 114 String 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 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 lName = aInterface.getLocalName(); 134 Object 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 144 public Iterator getInterfaces() { 145 if (interfaces == null) { 146 return KILIM.EMPTY_ITERATOR; 147 } 148 return interfaces.iterator(); 149 } 150 151 157 public ComponentInterface getInterface(String 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 176 public Iterator getPlugIns() { 177 if (plugIns == null) { 178 return KILIM.EMPTY_ITERATOR; 179 } 180 return plugIns.listIterator(); 181 } 182 183 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 (); 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 iter = interfaces.iterator(); 206 207 while (iter.hasNext()) { 208 RtComponentInterface interf = (RtComponentInterface) iter.next(); 209 String 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 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 } 229 } else { 230 if (!(elDesc1 instanceof Port) || ((Port) elDesc1).isOffered()) { 231 } 233 } 234 } else { 235 if (!(elDesc1 instanceof Port) || !((Port) elDesc1).isOffered()) { 236 } 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 } 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 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 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 iter = getInterfaces(); 292 List list = new ArrayList (); 293 294 while (iter.hasNext()) { 295 RtComponentInterface interf = (RtComponentInterface) iter.next(); 296 String 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 if (interf1.hasValue()) { 314 interf.fireTriggers(Trigger.UNBIND, interf1.specificGetValue()); 315 } 316 } else { 317 interf1.unbindProvider(interf); 318 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 |