1 17 18 19 package org.apache.naming; 20 21 import java.util.Hashtable ; 22 import javax.naming.Context ; 23 import javax.naming.Name ; 24 import javax.naming.NameParser ; 25 import javax.naming.NamingEnumeration ; 26 import javax.naming.NamingException ; 27 28 34 35 public class SelectorContext implements Context { 36 37 38 40 41 44 public static final String prefix = "java:"; 45 46 47 50 public static final int prefixLength = prefix.length(); 51 52 53 56 public static final String IC_PREFIX = "IC_"; 57 58 59 61 62 65 public SelectorContext(Hashtable env) { 66 this.env = env; 67 } 68 69 70 73 public SelectorContext(Hashtable env, boolean initialContext) { 74 this(env); 75 this.initialContext = initialContext; 76 } 77 78 79 81 82 85 protected Hashtable env; 86 87 88 91 protected StringManager sm = StringManager.getManager(Constants.Package); 92 93 94 97 protected boolean initialContext = false; 98 99 100 102 103 105 106 116 public Object lookup(Name name) 117 throws NamingException { 118 return getBoundContext().lookup(parseName(name)); 122 } 123 124 125 132 public Object lookup(String name) 133 throws NamingException { 134 return getBoundContext().lookup(parseName(name)); 138 } 139 140 141 153 public void bind(Name name, Object obj) 154 throws NamingException { 155 getBoundContext().bind(parseName(name), obj); 156 } 157 158 159 169 public void bind(String name, Object obj) 170 throws NamingException { 171 getBoundContext().bind(parseName(name), obj); 172 } 173 174 175 190 public void rebind(Name name, Object obj) 191 throws NamingException { 192 getBoundContext().rebind(parseName(name), obj); 193 } 194 195 196 205 public void rebind(String name, Object obj) 206 throws NamingException { 207 getBoundContext().rebind(parseName(name), obj); 208 } 209 210 211 225 public void unbind(Name name) 226 throws NamingException { 227 getBoundContext().unbind(parseName(name)); 228 } 229 230 231 239 public void unbind(String name) 240 throws NamingException { 241 getBoundContext().unbind(parseName(name)); 242 } 243 244 245 256 public void rename(Name oldName, Name newName) 257 throws NamingException { 258 getBoundContext().rename(parseName(oldName), parseName(newName)); 259 } 260 261 262 271 public void rename(String oldName, String newName) 272 throws NamingException { 273 getBoundContext().rename(parseName(oldName), parseName(newName)); 274 } 275 276 277 290 public NamingEnumeration list(Name name) 291 throws NamingException { 292 return getBoundContext().list(parseName(name)); 293 } 294 295 296 305 public NamingEnumeration list(String name) 306 throws NamingException { 307 return getBoundContext().list(parseName(name)); 308 } 309 310 311 324 public NamingEnumeration listBindings(Name name) 325 throws NamingException { 326 return getBoundContext().listBindings(parseName(name)); 327 } 328 329 330 339 public NamingEnumeration listBindings(String name) 340 throws NamingException { 341 return getBoundContext().listBindings(parseName(name)); 342 } 343 344 345 370 public void destroySubcontext(Name name) 371 throws NamingException { 372 getBoundContext().destroySubcontext(parseName(name)); 373 } 374 375 376 385 public void destroySubcontext(String name) 386 throws NamingException { 387 getBoundContext().destroySubcontext(parseName(name)); 388 } 389 390 391 404 public Context createSubcontext(Name name) 405 throws NamingException { 406 return getBoundContext().createSubcontext(parseName(name)); 407 } 408 409 410 420 public Context createSubcontext(String name) 421 throws NamingException { 422 return getBoundContext().createSubcontext(parseName(name)); 423 } 424 425 426 436 public Object lookupLink(Name name) 437 throws NamingException { 438 return getBoundContext().lookupLink(parseName(name)); 439 } 440 441 442 451 public Object lookupLink(String name) 452 throws NamingException { 453 return getBoundContext().lookupLink(parseName(name)); 454 } 455 456 457 471 public NameParser getNameParser(Name name) 472 throws NamingException { 473 return getBoundContext().getNameParser(parseName(name)); 474 } 475 476 477 485 public NameParser getNameParser(String name) 486 throws NamingException { 487 return getBoundContext().getNameParser(parseName(name)); 488 } 489 490 491 506 public Name composeName(Name name, Name prefix) 507 throws NamingException { 508 prefix = (Name ) prefix.clone(); 509 return prefix.addAll(name); 510 } 511 512 513 521 public String composeName(String name, String prefix) 522 throws NamingException { 523 return prefix + "/" + name; 524 } 525 526 527 536 public Object addToEnvironment(String propName, Object propVal) 537 throws NamingException { 538 return getBoundContext().addToEnvironment(propName, propVal); 539 } 540 541 542 549 public Object removeFromEnvironment(String propName) 550 throws NamingException { 551 return getBoundContext().removeFromEnvironment(propName); 552 } 553 554 555 565 public Hashtable getEnvironment() 566 throws NamingException { 567 return getBoundContext().getEnvironment(); 568 } 569 570 571 581 public void close() 582 throws NamingException { 583 getBoundContext().close(); 584 } 585 586 587 604 public String getNameInNamespace() 605 throws NamingException { 606 return prefix; 607 } 608 609 610 612 613 616 protected Context getBoundContext() 617 throws NamingException { 618 619 if (initialContext) { 620 String ICName = IC_PREFIX; 621 if (ContextBindings.isThreadBound()) { 622 ICName += ContextBindings.getThreadName(); 623 } else if (ContextBindings.isClassLoaderBound()) { 624 ICName += ContextBindings.getClassLoaderName(); 625 } 626 Context initialContext = ContextBindings.getContext(ICName); 627 if (initialContext == null) { 628 initialContext = new NamingContext(env, ICName); 631 ContextBindings.bindContext(ICName, initialContext); 632 } 633 return initialContext; 634 } else { 635 if (ContextBindings.isThreadBound()) { 636 return ContextBindings.getThread(); 637 } else { 638 return ContextBindings.getClassLoader(); 639 } 640 } 641 642 } 643 644 645 652 protected String parseName(String name) 653 throws NamingException { 654 655 if ((!initialContext) && (name.startsWith(prefix))) { 656 return (name.substring(prefixLength)); 657 } else { 658 if (initialContext) { 659 return (name); 660 } else { 661 throw new NamingException 662 (sm.getString("selectorContext.noJavaUrl")); 663 } 664 } 665 666 } 667 668 669 676 protected Name parseName(Name name) 677 throws NamingException { 678 679 if ((!initialContext) && (!name.isEmpty()) 680 && (name.get(0).equals(prefix))) { 681 return (name.getSuffix(1)); 682 } else { 683 if (initialContext) { 684 return (name); 685 } else { 686 throw new NamingException 687 (sm.getString("selectorContext.noJavaUrl")); 688 } 689 } 690 691 } 692 693 694 } 695 696 | Popular Tags |