1 18 19 package org.objectweb.kilim.model.services; 20 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 24 import org.objectweb.kilim.KilimException; 25 import org.objectweb.kilim.description.KILIM; 26 import org.objectweb.kilim.model.Component; 27 import org.objectweb.kilim.model.ComponentElement; 28 import org.objectweb.kilim.model.RtExternalValue; 29 30 33 public class DefaultNamingContext implements NamingContext { 34 private String localName; 35 private NamingContext parentContext; 36 private HashMap childContexts; 37 private HashMap boundNames; 38 private ExternalValueReferences externalNames; 39 40 47 public DefaultNamingContext(String aName, NamingContext aParent, ExternalValueReferences extNames) throws KilimException { 48 if (aName == null) { 49 throw new KilimException("attempt to create a naming context with a null name"); 50 } 51 localName = aName; 52 parentContext = aParent; 53 externalNames = extNames; 54 } 55 56 61 public DefaultNamingContext(String aName, NamingContext aParent) throws KilimException { 62 this(aName, aParent, null); 63 } 64 65 68 public NamingContext getParentNamingContext() { 69 return parentContext; 70 } 71 72 75 public void addChildNamingContext(String aName, NamingContext aContext) throws KilimException { 76 if (aName == null) { 77 throw new KilimException("attempt to add a child context with a null name in naming context " + getQualifiedName()); 78 } 79 if (aContext == null) { 80 throw new KilimException("attempt to add a null child context " + aName + " in naming context " + getQualifiedName()); 81 } 82 if (childContexts == null) { 83 childContexts = new HashMap (); 84 } 85 if (childContexts.containsKey(aName)) { 86 throw new KilimException("attempt to add a child context with an already existing name " + aName + " in naming context " + getQualifiedName()); 87 } 88 childContexts.put(aName, aContext); 89 } 90 91 94 public void removeChildNamingContext(String aName) throws KilimException { 95 if (aName == null) { 96 throw new KilimException("attempt to remove a child context through a null name in naming context " + getQualifiedName()); 97 } 98 if (childContexts == null) { 99 throw new KilimException("attempt to remove a child context from an empty naming context " + getQualifiedName()); 100 } 101 Object result = childContexts.remove(aName); 102 if (result == null) { 103 throw new KilimException("attempt to remove an unknown child context " + aName + " in naming context " + getQualifiedName()); 104 } 105 } 106 107 110 public Iterator getChildNamingContexts() { 111 if (childContexts == null) { 112 return KILIM.EMPTY_ITERATOR; 113 } 114 return childContexts.keySet().iterator(); 115 } 116 117 120 public void setParentNamingContext(NamingContext aContext) { 121 parentContext = aContext; 122 } 123 124 127 public void setExternalReferences(ExternalValueReferences xReferences) { 128 externalNames = xReferences; 129 } 130 131 134 public ExternalValueReferences getExternalReferenceMap() { 135 return externalNames; 136 } 137 138 141 public String getQualifiedName() { 142 if (parentContext == null) { 143 return localName; 144 } 145 146 String contName = parentContext.getQualifiedName(); 147 148 if ("".equals(contName)) { 149 return localName; 150 } 151 return contName + "/" + localName; 152 } 153 154 157 public String getLocalName() { 158 return localName; 159 } 160 161 164 public String toString() { 165 return "[" + getQualifiedName() + "]"; 166 } 167 168 171 public void addBoundName(String aName, ComponentElement aElement) throws KilimException { 172 if (aName == null) { 173 throw new KilimException("null name in addBoundName of naming context " + getQualifiedName()); 174 } 175 176 if (aElement == null) { 177 throw new KilimException("null element in addBoundName of naming context " + getQualifiedName()); 178 } 179 180 if (boundNames == null) { 181 boundNames = new HashMap (); 182 } 183 184 if (boundNames.containsKey(aName)) { 185 throw new KilimException("Name clash in addBoundName of naming context " + getQualifiedName() + " : \"" + aName + "\""); 186 } 187 188 boundNames.put(aName, aElement); 189 } 190 191 194 public void removeBoundName(String aName) throws KilimException { 195 if (aName == null) { 196 throw new KilimException("null name in removeBoundName of naming context " + getQualifiedName()); 197 } 198 199 if (boundNames == null) { 200 throw new KilimException("no bound names in current naming context " + getQualifiedName()); 201 } 202 203 Object previous = boundNames.remove(aName); 204 if (previous == null) { 205 throw new KilimException("name " + aName + " unknown in current naming context " + getQualifiedName()); 206 } 207 } 208 209 212 public Iterator getBoundNames() { 213 if (boundNames == null) { 214 return KILIM.EMPTY_ITERATOR; 215 } 216 217 return boundNames.keySet().iterator(); 218 } 219 220 223 public ComponentElement resolveReference(String aName, Component origin) throws KilimException { 224 226 if (aName.startsWith("/")) { 227 if (externalNames != null) { 228 Object eResult = externalNames.getExternalValueReference(aName); 229 if (eResult != null) { 230 RtExternalValue result = new RtExternalValue(aName, eResult); 231 boundNames.put(aName, result); 232 return result; 233 } 234 } 235 throw new KilimException("unknown external reference " + aName + " in naming context " + getQualifiedName() + " from component " + origin.getQualifiedName()); 236 } 237 238 241 if (aName.startsWith("../")) { 242 NamingContext parent = getParentNamingContext(); 243 if (parent == null) { 244 if (externalNames != null) { 245 Object result = externalNames.getExternalValueReference(aName); 246 if (result != null) { 247 return new RtExternalValue(aName, result); 249 } 250 } 251 throw new KilimException("unresolvable reference (1) " + aName + " in naming context " + getQualifiedName() + " from component " + origin.getQualifiedName()); 252 } 253 return parent.resolveReference(aName.substring(3), origin); 254 } 255 256 if (aName.startsWith("./")) { 258 aName = aName.substring(2); 259 } 260 261 if (boundNames != null) { 265 ComponentElement result = (ComponentElement) boundNames.get(aName); 266 if (result != null) { 267 return result; 268 } 269 } 270 271 int pos = aName.indexOf("/"); 275 if (pos != -1) { 276 String lName = aName.substring(0, pos); 277 Object child = null; 278 if (childContexts != null) { 279 child = childContexts.get(lName); 280 } 281 if (child == null) { 282 if (externalNames != null) { 283 Object result = externalNames.getExternalValueReference(aName); 284 if (result != null) { 285 return new RtExternalValue(aName, result); 287 } 288 } 289 throw new KilimException("unresolvable reference (2) " + aName + " in naming context " + getQualifiedName() + " from component " + origin.getQualifiedName()); 290 } 291 292 if (!(child instanceof NamingContext)) { 293 throw new KilimException("attempt to use as a naming context an illegal element " + lName + " in naming context " + getQualifiedName() + " from component " + origin.getQualifiedName()); 294 } 295 return ((NamingContext) child).resolveReference(aName.substring(pos + 1), origin); 296 } 297 298 if (externalNames != null) { 300 Object result = externalNames.getExternalValueReference(aName); 301 if (result != null) { 302 return new RtExternalValue(aName, result); 304 } 305 } 306 return null; 307 } 308 } | Popular Tags |