1 18 19 package org.objectweb.kilim.model; 20 21 import java.util.Iterator ; 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import java.util.Stack ; 25 import java.util.LinkedHashMap ; 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 38 39 public class RtCollectionPort extends RtComponentInterface { 40 private static final Integer FALSE = new Integer (0); 41 private static final Integer BIND_TRUE = new Integer (1); 42 private static final Integer UNBIND_TRUE = new Integer (2); 43 44 private LinkedHashMap providers; 45 46 51 public RtCollectionPort(Port aPort, ContainerElement aContainer) { 52 super(aPort, aContainer); 53 } 54 55 58 public boolean isSingleValuePort() { 59 return false; 60 } 61 62 65 public boolean isCollectionPort() { 66 return true; 67 } 68 69 72 public boolean isProvider() { 73 return false; 74 } 75 76 79 public boolean isProperty() { 80 return false; 81 } 82 83 86 public boolean hasValue() { 87 return false; 88 } 89 90 93 public boolean checkValue(Stack exclude) throws KilimException { 94 return false; 95 } 96 97 100 public String getLocalName() { 101 NamedElement elem = (NamedElement) getElementDescription(); 102 return elem.getLocalName(); 103 } 104 105 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 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 result = (Integer ) 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 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 (); 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 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 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 226 public Iterator getBoundProviders() { 227 if (providers == null) { 228 return KILIM.EMPTY_ITERATOR; 229 } 230 return providers.keySet().iterator(); 231 } 232 233 236 public void bindValue(Object aValue) throws KilimException { 237 throw new KilimException("attempt to get a value from a multiple value port " + getQualifiedName()); 238 } 239 240 243 public void unbindValue() throws KilimException { 244 throw new KilimException("attempt to unbind a value from a multiple value port " + getQualifiedName()); 245 } 246 247 250 public Object getValue() throws KilimException { 251 throw new KilimException("attempt to get a value from a multiple value port " + getQualifiedName()); 252 } 253 254 257 258 protected Object specificGetValue() throws KilimException { return null; } 259 260 263 protected void specificBindValue(Object aValue) throws KilimException { } 264 265 268 protected void specificUnbindValue() throws KilimException { } 269 270 273 public void update() throws KilimException { 274 Iterator 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 resultValue = provider.getValue(); 283 Iterator iter2 = getTriggers(Trigger.BIND); 284 while (iter2.hasNext()) { 285 RuntimeTrigger rtElem = (RuntimeTrigger) iter2.next(); 286 if (rtElem.getEventKind() == Trigger.BIND) { 287 Iterator 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 |