1 18 19 package org.objectweb.kilim.model; 20 21 import java.io.FileWriter ; 22 import java.io.PrintWriter ; 23 import java.util.Iterator ; 24 import java.util.ArrayList ; 25 import java.util.List ; 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 36 public abstract class RtComponentInterface extends RtComponentSource implements ComponentInterface, RuntimeSource { 37 private List bTriggers; 38 private List uTriggers; 39 private boolean bTriggersDone; 40 private boolean uTriggersDone; 41 private boolean isModified = true; 42 43 48 protected RtComponentInterface(TemplateElementImpl aElement, ContainerElement aComponent) { 49 super(aElement, aComponent); 50 } 51 52 57 public boolean isModified() throws KilimException { 58 return isModified; 59 } 60 61 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 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 103 public abstract void bindProvider(RuntimeSource aProvider, boolean jReplace) throws KilimException; 104 105 110 public abstract void unbindProvider(RuntimeSource aProvider) throws KilimException; 111 112 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 (); 124 } 125 bTriggers.add(aTrigger); 126 } else { 127 if (uTriggers == null) { 128 uTriggers = new ArrayList (); 129 } 130 uTriggers.add(aTrigger); 131 } 132 } 133 134 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 168 public List getTriggerList(int aEventKind) { 169 List list = (aEventKind == Trigger.BIND) ? bTriggers : uTriggers; 170 if (list == null) { 171 return KILIM.EMPTY_LIST; 172 } 173 return list; 174 } 175 176 181 public Iterator getTriggers(int aEventKind) { 182 return getTriggerList(aEventKind).iterator(); 183 } 184 185 188 public boolean isEventSource() { 189 return false; 190 } 191 192 195 public void setEventSourceValue(Object aValue) throws KilimException { 196 throw new KilimException("attempt to set an event source to an interface " + getQualifiedName()); 197 } 198 199 202 public Object getEventSourceValue() throws KilimException { 203 throw new KilimException("attempt to get an event source from an interface " + getQualifiedName()); 204 } 205 206 209 210 PrintWriter pW=null; 211 FileWriter fW = null; 212 213 public Object getValue() throws KilimException { 214 initializeInstanciation(); 215 boolean hasAValue = hasValue(); 218 Object resultValue = specificGetValue(); 219 220 229 243 if (hasAValue && !isModified) { 244 return resultValue; 245 } 246 if (triggersDone(Trigger.BIND)) { 248 return resultValue; 249 } 250 251 isModified = false; 253 setTriggersDone(Trigger.BIND, true); 254 fireTriggers(Trigger.BIND, resultValue); 255 return resultValue; 256 } 257 258 261 public void unbindValue() throws KilimException { 262 initializeInstanciation(); 263 Object aValue = getValue(); 264 specificUnbindValue(); 265 isModified = true; 267 if (triggersDone(Trigger.UNBIND)) { 268 return; 269 } 270 setTriggersDone(Trigger.UNBIND, true); 271 fireTriggers(Trigger.UNBIND, aValue); 272 } 273 274 277 public void bindValue(Object aValue) throws KilimException { 278 if (hasValue()) { 279 unbindValue(); 280 } else { 281 initializeInstanciation(); 282 } 283 specificBindValue(aValue); 284 isModified = true; 286 setTriggersDone(Trigger.BIND, true); 287 fireTriggers(Trigger.BIND, aValue); 288 } 289 290 293 public void update() throws KilimException { 294 initializeInstanciation(); 295 boolean hasAValue = hasValue(); 298 Object resultValue = specificGetValue(); 299 isModified = false; 301 setTriggersDone(Trigger.BIND, true); 302 fireTriggers(Trigger.BIND, resultValue); 303 } 304 305 protected void fireTriggers(int eventKind, Object resultValue) throws KilimException { 306 List 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 iter1 = trig.getTransformers(); 315 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 if (!contain.isInitialized()) { 335 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 iter = contain.getSlots(); 348 while (iter.hasNext()) { 349 ComponentSlot slot = (ComponentSlot) iter.next(); 350 Iterator 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 366 protected abstract Object specificGetValue() throws KilimException; 367 368 373 protected abstract void specificBindValue(Object aValue) throws KilimException; 374 375 379 protected abstract void specificUnbindValue() throws KilimException; 380 } | Popular Tags |