1 18 19 package org.apache.activemq.jndi; 20 21 import javax.naming.*; 22 import javax.naming.spi.NamingManager ; 23 import java.io.Serializable ; 24 import java.util.Collections ; 25 import java.util.HashMap ; 26 import java.util.Hashtable ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 30 50 public class ReadOnlyContext implements Context, Serializable { 51 private static final long serialVersionUID = -5754338187296859149L; 52 protected static final NameParser nameParser = new NameParserImpl(); 53 54 protected final Hashtable environment; protected final Map bindings; protected final Map treeBindings; 58 private boolean frozen = false; 59 private String nameInNamespace = ""; 60 public static final String SEPARATOR = "/"; 61 62 public ReadOnlyContext() { 63 environment = new Hashtable (); 64 bindings = new HashMap (); 65 treeBindings = new HashMap (); 66 } 67 68 public ReadOnlyContext(Hashtable env) { 69 if (env == null) { 70 this.environment = new Hashtable (); 71 } 72 else { 73 this.environment = new Hashtable (env); 74 } 75 this.bindings = Collections.EMPTY_MAP; 76 this.treeBindings = Collections.EMPTY_MAP; 77 } 78 79 public ReadOnlyContext(Hashtable environment, Map bindings) { 80 if (environment == null) { 81 this.environment = new Hashtable (); 82 } 83 else { 84 this.environment = new Hashtable (environment); 85 } 86 this.bindings = bindings; 87 treeBindings = new HashMap (); 88 frozen = true; 89 } 90 91 public ReadOnlyContext(Hashtable environment, Map bindings, String nameInNamespace) { 92 this(environment, bindings); 93 this.nameInNamespace = nameInNamespace; 94 } 95 96 protected ReadOnlyContext(ReadOnlyContext clone, Hashtable env) { 97 this.bindings = clone.bindings; 98 this.treeBindings = clone.treeBindings; 99 this.environment = new Hashtable (env); 100 } 101 102 protected ReadOnlyContext(ReadOnlyContext clone, Hashtable env, String nameInNamespace) { 103 this(clone, env); 104 this.nameInNamespace = nameInNamespace; 105 } 106 107 public void freeze() { 108 frozen = true; 109 } 110 111 boolean isFrozen() { 112 return frozen; 113 } 114 115 128 protected Map internalBind(String name, Object value) throws NamingException { 129 assert name != null && name.length() > 0; 130 assert !frozen; 131 132 Map newBindings = new HashMap (); 133 int pos = name.indexOf('/'); 134 if (pos == -1) { 135 if (treeBindings.put(name, value) != null) { 136 throw new NamingException("Something already bound at " + name); 137 } 138 bindings.put(name, value); 139 newBindings.put(name, value); 140 } 141 else { 142 String segment = name.substring(0, pos); 143 assert segment != null; 144 assert !segment.equals(""); 145 Object o = treeBindings.get(segment); 146 if (o == null) { 147 o = newContext(); 148 treeBindings.put(segment, o); 149 bindings.put(segment, o); 150 newBindings.put(segment, o); 151 } 152 else if (!(o instanceof ReadOnlyContext)) { 153 throw new NamingException("Something already bound where a subcontext should go"); 154 } 155 ReadOnlyContext readOnlyContext = (ReadOnlyContext) o; 156 String remainder = name.substring(pos + 1); 157 Map subBindings = readOnlyContext.internalBind(remainder, value); 158 for (Iterator iterator = subBindings.entrySet().iterator(); iterator.hasNext();) { 159 Map.Entry entry = (Map.Entry ) iterator.next(); 160 String subName = segment + "/" + (String ) entry.getKey(); 161 Object bound = entry.getValue(); 162 treeBindings.put(subName, bound); 163 newBindings.put(subName, bound); 164 } 165 } 166 return newBindings; 167 } 168 169 protected ReadOnlyContext newContext() { 170 return new ReadOnlyContext(); 171 } 172 173 public Object addToEnvironment(String propName, Object propVal) throws NamingException { 174 return environment.put(propName, propVal); 175 } 176 177 public Hashtable getEnvironment() throws NamingException { 178 return (Hashtable ) environment.clone(); 179 } 180 181 public Object removeFromEnvironment(String propName) throws NamingException { 182 return environment.remove(propName); 183 } 184 185 public Object lookup(String name) throws NamingException { 186 if (name.length() == 0) { 187 return this; 188 } 189 Object result = treeBindings.get(name); 190 if (result == null) { 191 result = bindings.get(name); 192 } 193 if (result == null) { 194 int pos = name.indexOf(':'); 195 if (pos > 0) { 196 String scheme = name.substring(0, pos); 197 Context ctx = NamingManager.getURLContext(scheme, environment); 198 if (ctx == null) { 199 throw new NamingException("scheme " + scheme + " not recognized"); 200 } 201 return ctx.lookup(name); 202 } 203 else { 204 CompositeName path = new CompositeName(name); 207 208 if (path.size() == 0) { 209 return this; 210 } 211 else { 212 String first = path.get(0); 213 Object obj = bindings.get(first); 214 if (obj == null) { 215 throw new NameNotFoundException(name); 216 } 217 else if (obj instanceof Context && path.size() > 1) { 218 Context subContext = (Context) obj; 219 obj = subContext.lookup(path.getSuffix(1)); 220 } 221 return obj; 222 } 223 } 224 } 225 if (result instanceof LinkRef) { 226 LinkRef ref = (LinkRef) result; 227 result = lookup(ref.getLinkName()); 228 } 229 if (result instanceof Reference) { 230 try { 231 result = NamingManager.getObjectInstance(result, null, null, this.environment); 232 } 233 catch (NamingException e) { 234 throw e; 235 } 236 catch (Exception e) { 237 throw (NamingException) new NamingException("could not look up : " + name).initCause(e); 238 } 239 } 240 if (result instanceof ReadOnlyContext) { 241 String prefix = getNameInNamespace(); 242 if (prefix.length() > 0) { 243 prefix = prefix + SEPARATOR; 244 } 245 result = new ReadOnlyContext((ReadOnlyContext) result, environment, prefix + name); 246 } 247 return result; 248 } 249 250 public Object lookup(Name name) throws NamingException { 251 return lookup(name.toString()); 252 } 253 254 public Object lookupLink(String name) throws NamingException { 255 return lookup(name); 256 } 257 258 public Name composeName(Name name, Name prefix) throws NamingException { 259 Name result = (Name) prefix.clone(); 260 result.addAll(name); 261 return result; 262 } 263 264 public String composeName(String name, String prefix) throws NamingException { 265 CompositeName result = new CompositeName(prefix); 266 result.addAll(new CompositeName(name)); 267 return result.toString(); 268 } 269 270 public NamingEnumeration list(String name) throws NamingException { 271 Object o = lookup(name); 272 if (o == this) { 273 return new ListEnumeration(); 274 } 275 else if (o instanceof Context) { 276 return ((Context) o).list(""); 277 } 278 else { 279 throw new NotContextException(); 280 } 281 } 282 283 public NamingEnumeration listBindings(String name) throws NamingException { 284 Object o = lookup(name); 285 if (o == this) { 286 return new ListBindingEnumeration(); 287 } 288 else if (o instanceof Context) { 289 return ((Context) o).listBindings(""); 290 } 291 else { 292 throw new NotContextException(); 293 } 294 } 295 296 public Object lookupLink(Name name) throws NamingException { 297 return lookupLink(name.toString()); 298 } 299 300 public NamingEnumeration list(Name name) throws NamingException { 301 return list(name.toString()); 302 } 303 304 public NamingEnumeration listBindings(Name name) throws NamingException { 305 return listBindings(name.toString()); 306 } 307 308 public void bind(Name name, Object obj) throws NamingException { 309 throw new OperationNotSupportedException(); 310 } 311 312 public void bind(String name, Object obj) throws NamingException { 313 throw new OperationNotSupportedException(); 314 } 315 316 public void close() throws NamingException { 317 } 319 320 public Context createSubcontext(Name name) throws NamingException { 321 throw new OperationNotSupportedException(); 322 } 323 324 public Context createSubcontext(String name) throws NamingException { 325 throw new OperationNotSupportedException(); 326 } 327 328 public void destroySubcontext(Name name) throws NamingException { 329 throw new OperationNotSupportedException(); 330 } 331 332 public void destroySubcontext(String name) throws NamingException { 333 throw new OperationNotSupportedException(); 334 } 335 336 public String getNameInNamespace() throws NamingException { 337 return nameInNamespace; 338 } 339 340 public NameParser getNameParser(Name name) throws NamingException { 341 return nameParser; 342 } 343 344 public NameParser getNameParser(String name) throws NamingException { 345 return nameParser; 346 } 347 348 public void rebind(Name name, Object obj) throws NamingException { 349 throw new OperationNotSupportedException(); 350 } 351 352 public void rebind(String name, Object obj) throws NamingException { 353 throw new OperationNotSupportedException(); 354 } 355 356 public void rename(Name oldName, Name newName) throws NamingException { 357 throw new OperationNotSupportedException(); 358 } 359 360 public void rename(String oldName, String newName) throws NamingException { 361 throw new OperationNotSupportedException(); 362 } 363 364 public void unbind(Name name) throws NamingException { 365 throw new OperationNotSupportedException(); 366 } 367 368 public void unbind(String name) throws NamingException { 369 throw new OperationNotSupportedException(); 370 } 371 372 private abstract class LocalNamingEnumeration implements NamingEnumeration { 373 private Iterator i = bindings.entrySet().iterator(); 374 375 public boolean hasMore() throws NamingException { 376 return i.hasNext(); 377 } 378 379 public boolean hasMoreElements() { 380 return i.hasNext(); 381 } 382 383 protected Map.Entry getNext() { 384 return (Map.Entry ) i.next(); 385 } 386 387 public void close() throws NamingException { 388 } 389 } 390 391 private class ListEnumeration extends LocalNamingEnumeration { 392 public Object next() throws NamingException { 393 return nextElement(); 394 } 395 396 public Object nextElement() { 397 Map.Entry entry = getNext(); 398 return new NameClassPair((String ) entry.getKey(), entry.getValue().getClass().getName()); 399 } 400 } 401 402 private class ListBindingEnumeration extends LocalNamingEnumeration { 403 public Object next() throws NamingException { 404 return nextElement(); 405 } 406 407 public Object nextElement() { 408 Map.Entry entry = getNext(); 409 return new Binding((String ) entry.getKey(), entry.getValue()); 410 } 411 } 412 } 413 | Popular Tags |