1 17 18 19 package org.apache.naming; 20 21 import java.util.HashMap ; 22 import java.util.Hashtable ; 23 import java.util.Enumeration ; 24 import javax.naming.Context ; 25 import javax.naming.Name ; 26 import javax.naming.LinkRef ; 27 import javax.naming.CompositeName ; 28 import javax.naming.NameParser ; 29 import javax.naming.Referenceable ; 30 import javax.naming.Reference ; 31 import javax.naming.NamingEnumeration ; 32 import javax.naming.NamingException ; 33 import javax.naming.NameAlreadyBoundException ; 34 import javax.naming.NameNotFoundException ; 35 import javax.naming.NotContextException ; 36 import javax.naming.InitialContext ; 37 import javax.naming.OperationNotSupportedException ; 38 import javax.naming.spi.NamingManager ; 39 40 46 public class NamingContext implements Context { 47 48 49 51 52 55 protected static final NameParser nameParser = new NameParserImpl(); 56 57 58 private static org.apache.commons.logging.Log log = 59 org.apache.commons.logging.LogFactory.getLog(NamingContext.class); 60 61 62 64 65 68 public NamingContext(Hashtable env, String name) 69 throws NamingException { 70 this.bindings = new HashMap (); 71 this.env = new Hashtable (); 72 this.name = name; 74 if (env != null ) { 76 Enumeration envEntries = env.keys(); 77 while (envEntries.hasMoreElements()) { 78 String entryName = (String ) envEntries.nextElement(); 79 addToEnvironment(entryName, env.get(entryName)); 80 } 81 } 82 } 83 84 85 88 public NamingContext(Hashtable env, String name, HashMap bindings) 89 throws NamingException { 90 this(env, name); 91 this.bindings = bindings; 92 } 93 94 95 97 98 101 protected Hashtable env; 102 103 104 107 protected StringManager sm = StringManager.getManager(Constants.Package); 108 109 110 113 protected HashMap bindings; 114 115 116 119 protected String name; 120 121 122 124 125 127 128 138 public Object lookup(Name name) 139 throws NamingException { 140 return lookup(name, true); 141 } 142 143 144 151 public Object lookup(String name) 152 throws NamingException { 153 return lookup(new CompositeName (name), true); 154 } 155 156 157 169 public void bind(Name name, Object obj) 170 throws NamingException { 171 bind(name, obj, false); 172 } 173 174 175 185 public void bind(String name, Object obj) 186 throws NamingException { 187 bind(new CompositeName (name), obj); 188 } 189 190 191 206 public void rebind(Name name, Object obj) 207 throws NamingException { 208 bind(name, obj, true); 209 } 210 211 212 221 public void rebind(String name, Object obj) 222 throws NamingException { 223 rebind(new CompositeName (name), obj); 224 } 225 226 227 241 public void unbind(Name name) 242 throws NamingException { 243 checkWritable(); 244 245 while ((!name.isEmpty()) && (name.get(0).length() == 0)) 246 name = name.getSuffix(1); 247 if (name.isEmpty()) 248 throw new NamingException 249 (sm.getString("namingContext.invalidName")); 250 251 NamingEntry entry = (NamingEntry) bindings.get(name.get(0)); 252 253 if (entry == null) { 254 throw new NameNotFoundException 255 (sm.getString("namingContext.nameNotBound", name.get(0))); 256 } 257 258 if (name.size() > 1) { 259 if (entry.type == NamingEntry.CONTEXT) { 260 ((Context ) entry.value).unbind(name.getSuffix(1)); 261 } else { 262 throw new NamingException 263 (sm.getString("namingContext.contextExpected")); 264 } 265 } else { 266 bindings.remove(name.get(0)); 267 } 268 269 } 270 271 272 280 public void unbind(String name) 281 throws NamingException { 282 unbind(new CompositeName (name)); 283 } 284 285 286 297 public void rename(Name oldName, Name newName) 298 throws NamingException { 299 Object value = lookup(oldName); 300 bind(newName, value); 301 unbind(oldName); 302 } 303 304 305 314 public void rename(String oldName, String newName) 315 throws NamingException { 316 rename(new CompositeName (oldName), new CompositeName (newName)); 317 } 318 319 320 333 public NamingEnumeration list(Name name) 334 throws NamingException { 335 while ((!name.isEmpty()) && (name.get(0).length() == 0)) 337 name = name.getSuffix(1); 338 if (name.isEmpty()) { 339 return new NamingContextEnumeration(bindings.values().iterator()); 340 } 341 342 NamingEntry entry = (NamingEntry) bindings.get(name.get(0)); 343 344 if (entry == null) { 345 throw new NameNotFoundException 346 (sm.getString("namingContext.nameNotBound", name.get(0))); 347 } 348 349 if (entry.type != NamingEntry.CONTEXT) { 350 throw new NamingException 351 (sm.getString("namingContext.contextExpected")); 352 } 353 return ((Context ) entry.value).list(name.getSuffix(1)); 354 } 355 356 357 366 public NamingEnumeration list(String name) 367 throws NamingException { 368 return list(new CompositeName (name)); 369 } 370 371 372 385 public NamingEnumeration listBindings(Name name) 386 throws NamingException { 387 while ((!name.isEmpty()) && (name.get(0).length() == 0)) 389 name = name.getSuffix(1); 390 if (name.isEmpty()) { 391 return new NamingContextBindingsEnumeration(bindings.values().iterator(), this); 392 } 393 394 NamingEntry entry = (NamingEntry) bindings.get(name.get(0)); 395 396 if (entry == null) { 397 throw new NameNotFoundException 398 (sm.getString("namingContext.nameNotBound", name.get(0))); 399 } 400 401 if (entry.type != NamingEntry.CONTEXT) { 402 throw new NamingException 403 (sm.getString("namingContext.contextExpected")); 404 } 405 return ((Context ) entry.value).listBindings(name.getSuffix(1)); 406 } 407 408 409 418 public NamingEnumeration listBindings(String name) 419 throws NamingException { 420 return listBindings(new CompositeName (name)); 421 } 422 423 424 449 public void destroySubcontext(Name name) 450 throws NamingException { 451 452 checkWritable(); 453 454 while ((!name.isEmpty()) && (name.get(0).length() == 0)) 455 name = name.getSuffix(1); 456 if (name.isEmpty()) 457 throw new NamingException 458 (sm.getString("namingContext.invalidName")); 459 460 NamingEntry entry = (NamingEntry) bindings.get(name.get(0)); 461 462 if (entry == null) { 463 throw new NameNotFoundException 464 (sm.getString("namingContext.nameNotBound", name.get(0))); 465 } 466 467 if (name.size() > 1) { 468 if (entry.type == NamingEntry.CONTEXT) { 469 ((Context ) entry.value).unbind(name.getSuffix(1)); 470 } else { 471 throw new NamingException 472 (sm.getString("namingContext.contextExpected")); 473 } 474 } else { 475 if (entry.type == NamingEntry.CONTEXT) { 476 ((Context ) entry.value).close(); 477 bindings.remove(name.get(0)); 478 } else { 479 throw new NotContextException 480 (sm.getString("namingContext.contextExpected")); 481 } 482 } 483 484 } 485 486 487 496 public void destroySubcontext(String name) 497 throws NamingException { 498 destroySubcontext(new CompositeName (name)); 499 } 500 501 502 515 public Context createSubcontext(Name name) 516 throws NamingException { 517 checkWritable(); 518 519 Context newContext = new NamingContext(env, this.name); 520 bind(name, newContext); 521 522 return newContext; 523 } 524 525 526 536 public Context createSubcontext(String name) 537 throws NamingException { 538 return createSubcontext(new CompositeName (name)); 539 } 540 541 542 552 public Object lookupLink(Name name) 553 throws NamingException { 554 return lookup(name, false); 555 } 556 557 558 567 public Object lookupLink(String name) 568 throws NamingException { 569 return lookup(new CompositeName (name), false); 570 } 571 572 573 587 public NameParser getNameParser(Name name) 588 throws NamingException { 589 590 while ((!name.isEmpty()) && (name.get(0).length() == 0)) 591 name = name.getSuffix(1); 592 if (name.isEmpty()) 593 return nameParser; 594 595 if (name.size() > 1) { 596 Object obj = bindings.get(name.get(0)); 597 if (obj instanceof Context ) { 598 return ((Context ) obj).getNameParser(name.getSuffix(1)); 599 } else { 600 throw new NotContextException 601 (sm.getString("namingContext.contextExpected")); 602 } 603 } 604 605 return nameParser; 606 607 } 608 609 610 618 public NameParser getNameParser(String name) 619 throws NamingException { 620 return getNameParser(new CompositeName (name)); 621 } 622 623 624 639 public Name composeName(Name name, Name prefix) 640 throws NamingException { 641 prefix = (Name ) prefix.clone(); 642 return prefix.addAll(name); 643 } 644 645 646 654 public String composeName(String name, String prefix) 655 throws NamingException { 656 return prefix + "/" + name; 657 } 658 659 660 669 public Object addToEnvironment(String propName, Object propVal) 670 throws NamingException { 671 return env.put(propName, propVal); 672 } 673 674 675 682 public Object removeFromEnvironment(String propName) 683 throws NamingException { 684 return env.remove(propName); 685 } 686 687 688 698 public Hashtable getEnvironment() 699 throws NamingException { 700 return env; 701 } 702 703 704 714 public void close() 715 throws NamingException { 716 env.clear(); 717 } 718 719 720 737 public String getNameInNamespace() 738 throws NamingException { 739 throw new OperationNotSupportedException 740 (sm.getString("namingContext.noAbsoluteName")); 741 } 743 744 745 747 748 756 protected Object lookup(Name name, boolean resolveLinks) 757 throws NamingException { 758 759 while ((!name.isEmpty()) && (name.get(0).length() == 0)) 761 name = name.getSuffix(1); 762 if (name.isEmpty()) { 763 return new NamingContext(env, this.name, bindings); 765 } 766 767 NamingEntry entry = (NamingEntry) bindings.get(name.get(0)); 768 769 if (entry == null) { 770 throw new NameNotFoundException 771 (sm.getString("namingContext.nameNotBound", name.get(0))); 772 } 773 774 if (name.size() > 1) { 775 if (entry.type != NamingEntry.CONTEXT) { 778 throw new NamingException 779 (sm.getString("namingContext.contextExpected")); 780 } 781 return ((Context ) entry.value).lookup(name.getSuffix(1)); 782 } else { 783 if ((resolveLinks) && (entry.type == NamingEntry.LINK_REF)) { 784 String link = ((LinkRef ) entry.value).getLinkName(); 785 if (link.startsWith(".")) { 786 return lookup(link.substring(1)); 788 } else { 789 return (new InitialContext (env)).lookup(link); 790 } 791 } else if (entry.type == NamingEntry.REFERENCE) { 792 try { 793 Object obj = NamingManager.getObjectInstance 794 (entry.value, name, this, env); 795 if (obj != null) { 796 entry.value = obj; 797 entry.type = NamingEntry.ENTRY; 798 } 799 return obj; 800 } catch (NamingException e) { 801 throw e; 802 } catch (Exception e) { 803 log.warn(sm.getString 804 ("namingContext.failResolvingReference"), e); 805 throw new NamingException (e.getMessage()); 806 } 807 } else { 808 return entry.value; 809 } 810 } 811 812 } 813 814 815 828 protected void bind(Name name, Object obj, boolean rebind) 829 throws NamingException { 830 831 checkWritable(); 832 833 while ((!name.isEmpty()) && (name.get(0).length() == 0)) 834 name = name.getSuffix(1); 835 if (name.isEmpty()) 836 throw new NamingException 837 (sm.getString("namingContext.invalidName")); 838 839 NamingEntry entry = (NamingEntry) bindings.get(name.get(0)); 840 841 if (name.size() > 1) { 842 if (entry == null) { 843 throw new NameNotFoundException 844 (sm.getString("namingContext.nameNotBound", name.get(0))); 845 } 846 if (entry.type == NamingEntry.CONTEXT) { 847 if (rebind) { 848 ((Context ) entry.value).rebind(name.getSuffix(1), obj); 849 } else { 850 ((Context ) entry.value).bind(name.getSuffix(1), obj); 851 } 852 } else { 853 throw new NamingException 854 (sm.getString("namingContext.contextExpected")); 855 } 856 } else { 857 if ((!rebind) && (entry != null)) { 858 throw new NameAlreadyBoundException 859 (sm.getString("namingContext.alreadyBound", name.get(0))); 860 } else { 861 Object toBind = 864 NamingManager.getStateToBind(obj, name, this, env); 865 if (toBind instanceof Context ) { 866 entry = new NamingEntry(name.get(0), toBind, 867 NamingEntry.CONTEXT); 868 } else if (toBind instanceof LinkRef ) { 869 entry = new NamingEntry(name.get(0), toBind, 870 NamingEntry.LINK_REF); 871 } else if (toBind instanceof Reference ) { 872 entry = new NamingEntry(name.get(0), toBind, 873 NamingEntry.REFERENCE); 874 } else if (toBind instanceof Referenceable ) { 875 toBind = ((Referenceable ) toBind).getReference(); 876 entry = new NamingEntry(name.get(0), toBind, 877 NamingEntry.REFERENCE); 878 } else { 879 entry = new NamingEntry(name.get(0), toBind, 880 NamingEntry.ENTRY); 881 } 882 bindings.put(name.get(0), entry); 883 } 884 } 885 886 } 887 888 889 892 protected boolean isWritable() { 893 return ContextAccessController.isWritable(name); 894 } 895 896 897 900 protected void checkWritable() 901 throws NamingException { 902 if (!isWritable()) 903 throw new NamingException (sm.getString("namingContext.readOnly")); 904 } 905 906 907 } 908 909 | Popular Tags |