1 22 23 package org.objectweb.perseus.jndi; 24 25 import java.io.Serializable ; 26 import java.util.Enumeration ; 27 import java.util.Hashtable ; 28 import java.util.NoSuchElementException ; 29 30 import javax.naming.Binding ; 31 import javax.naming.Context ; 32 import javax.naming.InitialContext ; 33 import javax.naming.InvalidNameException ; 34 import javax.naming.LinkRef ; 35 import javax.naming.Name ; 36 import javax.naming.NameAlreadyBoundException ; 37 import javax.naming.NameClassPair ; 38 import javax.naming.NameNotFoundException ; 39 import javax.naming.NameParser ; 40 import javax.naming.NamingEnumeration ; 41 import javax.naming.NamingException ; 42 import javax.naming.NotContextException ; 43 import javax.naming.OperationNotSupportedException ; 44 import javax.naming.RefAddr ; 45 import javax.naming.Reference ; 46 47 50 public class ContextImpl implements Context , Serializable { 51 52 private Hashtable myEnv = null; 53 private Hashtable bindings = new Hashtable (); 54 static private NameParser myParser = new NameParserImpl(); 55 private String compId; 56 57 60 public ContextImpl(String id, Hashtable env, Hashtable _bindings) throws NamingException { 61 if (env != null) { 62 myEnv = (Hashtable ) (env.clone()); 64 } 65 if (_bindings != null ) { 66 bindings = (Hashtable ) (_bindings.clone()); 67 } 68 compId = id; 69 } 70 71 74 public ContextImpl(String id, Hashtable env) throws NamingException { 75 if (env != null) { 76 myEnv = (Hashtable ) (env.clone()); 78 } 79 compId = id; 80 } 81 82 85 public ContextImpl(String id) throws NamingException { 86 myEnv = new Hashtable (); 87 compId = id; 88 } 89 90 93 public ContextImpl() throws NamingException { 94 myEnv = new Hashtable (); 95 compId = ""; 96 } 97 98 public String toString() { 99 String res = ""; 100 Enumeration en = bindings.keys(); 101 while (en.hasMoreElements()) { 102 String current = (String ) en.nextElement() ; 103 res += current+"=>"+bindings.get(current) + "\n"; 104 } 105 return res; 106 } 107 108 112 120 public Object lookup(Name name) throws NamingException { 121 return lookup(name.toString()); 123 } 124 125 132 public Object lookup(String name) throws NamingException { 133 134 Name n = new DottedName(name); 135 if (n.size() < 1) { 136 return this; 138 } 139 140 if (n.size() == 1) { 141 Object ret = bindings.get(name); 143 if (ret == null) 144 throw new NameNotFoundException (name); 145 if (ret instanceof LinkRef ) { 146 InitialContext ictx = NamingManager.getInstance().getInitialContext(); 150 RefAddr ra = ((Reference ) ret).get(0); 151 ret = ictx.lookup((String )ra.getContent()); 152 } 153 return ret; 154 } else { 155 String suffix = n.getSuffix(1).toString(); 157 Context subctx = lookupCtx(n.get(0)); 159 return subctx.lookup(suffix); 160 } 161 } 162 163 174 public void bind(Name name, Object obj) throws NamingException { 175 bind(name.toString(), obj); 177 } 178 179 189 public void bind(String name, Object obj) throws NamingException { 190 191 Name n = new DottedName(name); 192 if (n.size() < 1) { 193 throw new InvalidNameException ("CompNamingContext cannot bind empty name"); 194 } 195 196 if (n.size() == 1) { 197 if (bindings.get(name) != null) { 199 throw new NameAlreadyBoundException ("CompNamingContext: Use rebind to bind over a name"); 200 } 201 bindings.put(name, obj); 202 } else { 203 String suffix = n.getSuffix(1).toString(); 205 Context subctx; 207 try { 208 subctx = lookupCtx(n.get(0)); 209 } catch (NameNotFoundException e) { 210 subctx = createSubcontext(n.get(0)); 211 } 212 subctx.bind(suffix, obj); 213 } 214 } 215 216 228 public void rebind(Name name, Object obj) throws NamingException { 229 rebind(name.toString(), obj); 231 } 232 233 244 public void rebind(String name, Object obj) throws NamingException { 245 246 Name n = new DottedName(name); 247 if (n.size() < 1) { 248 throw new InvalidNameException ("CompNamingContext cannot rebind empty name"); 249 } 250 251 if (n.size() == 1) { 252 bindings.put(name, obj); 254 } else { 255 String suffix = n.getSuffix(1).toString(); 257 Context subctx; 259 try { 260 subctx = lookupCtx(n.get(0)); 261 } catch (NameNotFoundException e) { 262 subctx = createSubcontext(n.get(0)); 263 } 264 subctx.rebind(suffix, obj); 265 } 266 } 267 268 275 public void unbind(Name name) throws NamingException { 276 unbind(name.toString()); 278 } 279 280 287 public void unbind(String name) throws NamingException { 288 289 Name n = new DottedName(name); 290 if (n.size() < 1) { 291 throw new InvalidNameException ("CompNamingContext cannot unbind empty name"); 292 } 293 294 if (n.size() == 1) { 295 if (bindings.get(name) == null) { 297 throw new NameNotFoundException (name); 298 } 299 bindings.remove(name); 300 } else { 301 String suffix = n.getSuffix(1).toString(); 303 Context subctx = lookupCtx(n.get(0)); 305 subctx.unbind(suffix); 306 } 307 } 308 309 319 public void rename(Name oldName, Name newName) throws NamingException { 320 rename(oldName.toString(), newName.toString()); 322 } 323 324 332 public void rename(String oldName, String newName) throws NamingException { 333 334 Object obj = lookup(oldName); 335 rebind(newName, obj); 336 unbind(oldName); 337 } 338 339 350 public NamingEnumeration list(Name name) throws NamingException { 351 return list(name.toString()); 353 } 354 355 365 public NamingEnumeration list(String name) throws NamingException { 366 367 if (name.length() == 0) { 368 return new ListOfNames(bindings); 370 } 371 Object obj = lookup(name); 372 if (obj instanceof Context ) { 373 return ((Context )obj).list(""); 374 } else { 375 throw new NotContextException (name); 376 } 377 } 378 379 395 public NamingEnumeration listBindings(Name name) throws NamingException { 396 return listBindings(name.toString()); 398 } 399 400 410 public NamingEnumeration listBindings(String name) throws NamingException { 411 412 if (name.length() == 0) { 413 return new ListOfBindings(bindings); 415 } 416 Object obj = lookup(name); 417 if (obj instanceof Context ) { 418 return ((Context )obj).listBindings(""); 419 } else { 420 throw new NotContextException (name); 421 } 422 } 423 424 430 public void destroySubcontext(Name name) throws NamingException { 431 destroySubcontext(name.toString()); 433 } 434 435 441 public void destroySubcontext(String name) throws NamingException { 442 throw new OperationNotSupportedException ("CompNamingContext: destroySubcontext"); 443 } 444 445 459 public Context createSubcontext(Name name) throws NamingException { 460 return createSubcontext(name.toString()); 462 } 463 464 476 public Context createSubcontext(String name) throws NamingException { 477 478 Name n = new DottedName(name); 479 if (n.size() < 1) { 480 throw new InvalidNameException ("CompNamingContext cannot create empty Subcontext"); 481 } 482 483 Context ctx = null; if (n.size() == 1) { 485 ctx = new ContextImpl(compId, myEnv); 487 bindings.put(name, ctx); 488 } else { 489 String suffix = n.getSuffix(1).toString(); 492 Context subctx; 493 name = n.get(0); 494 try { 495 subctx = lookupCtx(name); 496 } catch (NameNotFoundException e) { 497 subctx = createSubcontext(name); 498 } 499 ctx = subctx.createSubcontext(suffix); 500 } 501 return ctx; 502 } 503 504 515 public Object lookupLink(Name name) throws NamingException { 516 return lookupLink(name.toString()); 518 } 519 520 532 public Object lookupLink(String name) throws NamingException { 533 534 return lookup(name); 536 } 537 538 547 public NameParser getNameParser(Name name) throws NamingException { 548 return myParser; 549 } 550 551 560 public NameParser getNameParser(String name) throws NamingException { 561 return myParser; 562 } 563 564 573 public Name composeName(Name name, Name prefix) throws NamingException { 574 throw new OperationNotSupportedException ("CompNamingContext composeName"); 575 } 576 577 586 public String composeName(String name, String prefix) throws NamingException { 587 throw new OperationNotSupportedException ("CompNamingContext composeName"); 588 } 589 590 602 public Object addToEnvironment(String propName, Object propVal) throws NamingException { 603 604 if (myEnv == null) { 605 myEnv = new Hashtable (); 606 } 607 return myEnv.put(propName, propVal); 608 } 609 610 619 public Object removeFromEnvironment(String propName) throws NamingException { 620 621 if (myEnv == null) { 622 return null; 623 } 624 return myEnv.remove(propName); 625 } 626 627 633 public Hashtable getEnvironment() throws NamingException { 634 635 if (myEnv == null) { 636 myEnv = new Hashtable (); 637 } 638 return myEnv; 639 } 640 641 646 public void close() throws NamingException { 647 myEnv = null; 648 } 649 650 658 public String getNameInNamespace() throws NamingException { 659 return compId; 661 } 662 663 667 670 private Context lookupCtx(String name) throws NamingException { 671 Object obj = bindings.get(name); 672 if (obj == null) { 673 throw new NameNotFoundException (); 674 } 675 if (obj instanceof ContextImpl) { 676 return (Context ) obj; 677 } else { 678 throw new NameAlreadyBoundException (name); 679 } 680 } 681 682 686 690 class ListOfNames implements NamingEnumeration { 691 protected Enumeration names; 692 protected Hashtable bindings; 693 694 698 ListOfNames (Hashtable bindings) { 699 this.bindings = bindings; 700 this.names = bindings.keys(); 701 } 702 703 public boolean hasMore() throws NamingException { 708 return names.hasMoreElements(); 709 } 710 711 public Object next() throws NamingException { 712 String name = (String ) names.nextElement(); 713 String className = bindings.get(name).getClass().getName(); 714 return new NameClassPair (name, className); 715 } 716 717 public void close() { 718 } 719 720 724 public Object nextElement() { 725 try { 726 return next(); 727 } catch (NamingException e) { 728 throw new NoSuchElementException (e.toString()); 729 } 730 } 731 732 public boolean hasMoreElements() { 733 try { 734 return hasMore(); 735 } catch (NamingException e) { 736 return false; 737 } 738 } 739 } 740 741 742 745 class ListOfBindings extends ListOfNames { 746 747 ListOfBindings (Hashtable bindings) { 748 super(bindings); 749 } 750 751 public Object next() throws NamingException { 754 String name = (String )names.nextElement(); 755 return new Binding (name, this.bindings.get(name)); 756 } 757 } 758 } 759 | Popular Tags |