1 23 24 package com.rift.coad.lib.naming.cos; 26 27 import java.util.Enumeration ; 29 import java.util.Hashtable ; 30 import javax.naming.Context ; 31 import javax.naming.InvalidNameException ; 32 import javax.naming.Name ; 33 import javax.naming.NameAlreadyBoundException ; 34 import javax.naming.NameParser ; 35 import javax.naming.NamingEnumeration ; 36 import javax.naming.NamingException ; 37 import javax.naming.NameNotFoundException ; 38 39 44 public class MemoryContext implements Context { 45 46 private Hashtable env = null; 48 private Hashtable contextEnv = new Hashtable (); 49 private Name nameSpace = null; 50 51 56 public MemoryContext(Hashtable env,Name nameSpace) { 57 this.env = (Hashtable )env.clone(); 58 this.nameSpace = nameSpace; 59 } 60 61 62 69 public Object addToEnvironment(String propName, Object propVal) throws 70 NamingException { 71 Object original = null; 72 synchronized(env) { 73 if (env.containsKey(propName)) { 74 original = env.get(propName); 75 } 76 env.put(propName,propVal); 77 } 78 return original; 79 } 80 81 82 89 public void bind(Name name, Object obj) throws NamingException { 90 if (name.size() <= 0) { 91 throw new InvalidNameException ("Invalid name has been passed"); 92 } 93 String key = name.get(0); 94 MemoryContext subContext = null; 95 synchronized(contextEnv) { 96 boolean contains = contextEnv.containsKey(key); 97 if ((name.size() == 1) && contains) { 98 throw new NameAlreadyBoundException ("The name [" + key 99 + "] is already bound"); 100 } else if (name.size() == 1 ) { 101 contextEnv.put(key,obj); 102 return; 103 } else if (contains) { 104 subContext = (MemoryContext)contextEnv.get(key); 105 } else { 106 subContext = new MemoryContext(env,composeName(nameSpace, 107 new NamingParser().parse(key))); 108 contextEnv.put(key,subContext); 109 } 110 } 111 subContext.bind(name.getSuffix(1),obj); 112 } 113 114 115 118 public void bind(String name, Object obj) throws NamingException { 119 bind(new NamingParser().parse(name),obj); 120 } 121 122 123 126 public void close() throws NamingException { 127 128 } 129 130 131 139 public Name composeName(Name name, Name prefix) throws NamingException { 140 Name newName = (Name )prefix.clone(); 141 newName.addAll(name); 142 return newName; 143 } 144 145 146 153 public String composeName(String name, String prefix) throws 154 NamingException { 155 return composeName(new NamingParser().parse(name), 156 new NamingParser().parse(prefix)).toString(); 157 } 158 159 160 167 public Context createSubcontext(Name name) throws NamingException { 168 if (name.size() <= 0) { 169 throw new InvalidNameException ("Invalid name has been passed"); 170 } 171 String key = name.get(0); 172 MemoryContext subContext = null; 173 synchronized(contextEnv) { 174 if (contextEnv.containsKey(key)) { 175 subContext = (MemoryContext)contextEnv.get(key); 176 } else { 177 subContext = new MemoryContext(env,composeName( 178 new NamingParser().parse(key),nameSpace)); 179 contextEnv.put(key,subContext); 180 } 181 } 182 if (name.size() > 1) { 183 return subContext.createSubcontext(name.getSuffix(1)); 184 } 185 return subContext; 186 } 187 188 189 196 public Context createSubcontext(String name) throws NamingException { 197 return createSubcontext(new NamingParser().parse(name)); 198 } 199 200 201 207 public void destroySubcontext(Name name) throws NamingException { 208 if (name.size() <= 0) { 209 throw new InvalidNameException ("Invalid name has been passed"); 210 } 211 String key = name.get(0); 212 MemoryContext subContext = null; 213 synchronized(contextEnv) { 214 boolean contains = contextEnv.containsKey(key); 215 if ((name.size() == 1) && contains) { 216 contextEnv.remove(key); 217 return; 218 } else if (contains) { 219 subContext = (MemoryContext)contextEnv.get(key); 220 } else { 221 throw new NameNotFoundException ("The name [" + key 222 + "] not found"); 223 } 224 } 225 if (name.size() > 1) { 226 subContext.destroySubcontext(name.getSuffix(1)); 227 } 228 } 229 230 231 236 public void destroySubcontext(String name) throws NamingException { 237 destroySubcontext(new NamingParser().parse(name)); 238 } 239 240 241 247 public Hashtable getEnvironment() throws NamingException { 248 return env; 249 } 250 251 252 255 public String getNameInNamespace() throws NamingException { 256 return nameSpace.toString(); 257 } 258 259 260 267 public NameParser getNameParser(Name name) throws NamingException { 268 return new NamingParser(); 269 } 270 271 272 275 public NameParser getNameParser(String name) throws NamingException { 276 return new NamingParser(); 277 } 278 279 280 284 public NamingEnumeration list(Name name) throws NamingException { 285 return new MemoryNamingEnumeration(name.getAll()); 286 } 287 288 289 297 public NamingEnumeration list(String name) throws NamingException { 298 return new MemoryNamingEnumeration(new NamingParser().parse(name). 299 getAll()); 300 } 301 302 303 311 public NamingEnumeration listBindings(Name name) throws NamingException { 312 if (name.size() <= 0) { 313 synchronized(contextEnv) { 314 return new MemoryNamingEnumeration(contextEnv.elements()); 315 } 316 } 317 String key = name.get(0); 318 MemoryContext subContext = null; 319 synchronized(contextEnv) { 320 if (contextEnv.containsKey(key)) { 321 subContext = (MemoryContext)contextEnv.get(key); 322 } else { 323 throw new NameNotFoundException ("The name [" + key 324 + "] not found"); 325 } 326 } 327 return subContext.listBindings(name.getSuffix(1)); 328 } 329 330 331 339 public NamingEnumeration listBindings(String name) throws NamingException { 340 return listBindings(new NamingParser().parse(name)); 341 } 342 343 344 351 public Object lookup(Name name) throws NamingException { 352 if (name.size() <= 0) { 353 throw new InvalidNameException ("Invalid name has been passed"); 354 } 355 String key = name.get(0); 356 MemoryContext subContext = null; 357 synchronized(contextEnv) { 358 boolean contains = contextEnv.containsKey(key); 359 if ((name.size() == 1) && contains) { 360 return contextEnv.get(key); 361 } else if (contains) { 362 subContext = (MemoryContext)contextEnv.get(key); 363 } else { 364 throw new NameNotFoundException ("The name [" + key 365 + "] not found"); 366 } 367 } 368 return subContext.lookup(name.getSuffix(1)); 369 } 370 371 372 379 public Object lookup(String name) throws NamingException { 380 return lookup(new NamingParser().parse(name)); 381 } 382 383 384 392 public Object lookupLink(Name name) throws NamingException { 393 Name currentName = name; 394 while(true) { 395 Object result = lookup(currentName); 396 if (!(result instanceof Name )) { 397 return result; 398 } 399 currentName = (Name )result; 400 } 401 } 402 403 404 412 public Object lookupLink(String name) throws NamingException { 413 return lookupLink(new NamingParser().parse(name)); 414 } 415 416 417 424 public void rebind(Name name, Object obj) throws NamingException { 425 if (name.size() <= 0) { 426 throw new InvalidNameException ("Invalid name has been passed"); 427 } 428 String key = name.get(0); 429 MemoryContext subContext = null; 430 synchronized(contextEnv) { 431 boolean contains = contextEnv.containsKey(key); 432 if (name.size() == 1 ) { 433 contextEnv.put(key,obj); 434 return; 435 } else if (contains) { 436 subContext = (MemoryContext)contextEnv.get(key); 437 } else { 438 subContext = new MemoryContext(env,composeName( 439 new NamingParser().parse(key),nameSpace)); 440 contextEnv.put(key,subContext); 441 } 442 } 443 subContext.rebind(name.getSuffix(1),obj); 444 } 445 446 447 454 public void rebind(String name, Object obj) throws NamingException { 455 rebind(new NamingParser().parse(name),obj); 456 } 457 458 459 466 public Object removeFromEnvironment(String propName) throws NamingException { 467 Object originalValue = null; 468 synchronized(env) { 469 if (env.containsKey(propName)) { 470 originalValue = env.get(propName); 471 env.remove(propName); 472 } 473 } 474 return originalValue; 475 } 476 477 478 486 public void rename(Name oldName, Name newName) throws NamingException { 487 Object value = lookup(oldName); 488 unbind(oldName); 489 bind(newName,value); 490 } 491 492 493 501 public void rename(String oldName, String newName) throws NamingException { 502 rename(new NamingParser().parse(oldName),new NamingParser(). 503 parse(newName)); 504 } 505 506 507 513 public void unbind(Name name) throws NamingException { 514 if (name.size() <= 0) { 515 throw new InvalidNameException ("Invalid name has been passed"); 516 } 517 String key = name.get(0); 518 MemoryContext subContext = null; 519 synchronized(contextEnv) { 520 boolean contains = contextEnv.containsKey(key); 521 if (contains && (name.size() == 1 )) { 522 contextEnv.remove(key); 523 return; 524 } else if (contains) { 525 subContext = (MemoryContext)contextEnv.get(key); 526 } else { 527 throw new NameNotFoundException ("The name [" + name.toString() 528 + "] was not found."); 529 } 530 } 531 subContext.unbind(name.getSuffix(1)); 532 } 533 534 535 541 public void unbind(String name) throws NamingException { 542 unbind(new NamingParser().parse(name)); 543 } 544 } 545 | Popular Tags |