1 18 19 package org.objectweb.kilim.model; 20 21 import java.util.Iterator ; 22 import java.util.Stack ; 23 24 import org.objectweb.kilim.KilimException; 25 import org.objectweb.kilim.description.KILIM; 26 import org.objectweb.kilim.description.NamedElement; 27 import org.objectweb.kilim.description.Port; 28 import org.objectweb.kilim.description.TemplateDescription; 29 30 33 public class RtSingleValuePort extends RtComponentInterface implements ComponentInterface { 34 private RuntimeSource provider; 36 private Object resultValue; 37 private boolean gotAValue; 38 39 private boolean firstCall = true; 41 private int nbBound; 42 private boolean msgDisplayed; 43 44 49 public RtSingleValuePort(Port aPort, ContainerElement aContainer) { 50 super(aPort, aContainer); 51 } 52 53 56 public boolean hasValue() { 57 return gotAValue; 58 } 59 60 64 public Object getBufferedValue() { 65 return resultValue; 66 } 67 68 71 public boolean isSingleValuePort() { 72 return true; 73 } 74 75 78 public boolean isCollectionPort() { 79 return false; 80 } 81 82 85 public boolean isProvider() { 86 return false; 87 } 88 89 92 public boolean isProperty() { 93 return false; 94 } 95 96 99 public String getLocalName() { 100 NamedElement elem = (NamedElement) getElementDescription(); 101 return elem.getLocalName(); 102 } 103 104 107 public void bindProvider(RuntimeSource aProvider, boolean jReplace) throws KilimException { 108 if (aProvider == null) { 111 String tmpName = getContainingComponent().getTemplate().getName(); 112 throw new KilimException("attempt to bind a null provider to an unary port " + getQualifiedName() + "(template : " + tmpName + ")"); 113 } 114 115 provider = aProvider; 117 nbBound++; 118 119 if (nbBound > 1 && !jReplace) { 120 if (!msgDisplayed) { 121 msgDisplayed = true; 122 TemplateDescription tmp0 = getElementDescription().getContainingTemplate(); 123 String lName = getLocalName(); 124 String tmpList = ""; 125 do { 126 Iterator iter = tmp0.getBindings(lName, true); 127 if (iter != KILIM.EMPTY_ITERATOR) { 128 tmpList = tmpList + "\"" + tmp0.getName() + "\" "; 129 } 130 } while ((tmp0 = tmp0.getSuperTemplate()) != null); 131 132 TemplateDescription tmp1 = getContainingComponent().getTemplate(); 133 134 if (tmp1 != tmp0) { 135 do { 136 Iterator iter = tmp1.getBindings(lName, true); 137 if (iter != KILIM.EMPTY_ITERATOR) { 138 tmpList = tmpList + " : " + tmp1.getName(); 139 } 140 } while ((tmp1 = tmp1.getSuperTemplate()) != null); 141 } 142 } 143 144 } 146 } 147 148 151 public void unbindProvider(RuntimeSource aProvider) throws KilimException { 152 if (provider == null) { 153 throw new KilimException("attempt to unbind a provider from the empty interface " + getQualifiedName()); 154 } 155 156 provider = null; 158 } 159 160 163 protected Object specificGetValue() throws KilimException { 164 if (gotAValue) { 165 return resultValue; 166 } 167 168 svpStack.push(this); 169 170 if (provider == null) { 173 provider = new RtExternalValue(getLocalName() + KILIM.getUniqueIndex(), null); 174 } else { 175 resultValue = ((RuntimeSource) provider).getValue(); 176 gotAValue = true; 177 } 178 svpStack.pop(); 179 return resultValue; 180 } 181 182 185 protected void specificBindValue(Object aValue) throws KilimException { 186 if (provider != null) { 187 throw new KilimException("attempt to bind a value to a unary port " + getQualifiedName() + " having a provider " + provider); 188 } 189 resultValue = aValue; 190 gotAValue = true; 191 } 192 193 196 protected void specificUnbindValue() throws KilimException { 197 if (!gotAValue) { 198 throw new KilimException("attempt to unbind a Value from a port " + getQualifiedName() + " without value"); 199 } 200 resultValue = null; 201 gotAValue = false; 202 } 203 204 207 public boolean checkValue(Stack exclude) throws KilimException { 208 if (hasValue()) { 209 return true; 210 } 211 212 int eSize = exclude.size(); 213 for (int i = 0; i < eSize; i++) { 214 if (this == exclude.get(i)) { 215 return false; 216 } 217 } 218 219 if (provider == null) { 220 return false; 221 } 222 223 return provider.checkValue(exclude); 224 } 225 } | Popular Tags |