1 16 17 package org.apache.naming.modules.java; 18 19 import java.util.Hashtable ; 20 21 import javax.naming.Context ; 22 import javax.naming.Name ; 23 import javax.naming.NameParser ; 24 import javax.naming.NamingEnumeration ; 25 import javax.naming.NamingException ; 26 27 import org.apache.naming.core.BaseContext; 28 import org.apache.naming.modules.memory.MemoryNamingContext; 29 import org.apache.tomcat.util.res.StringManager; 30 31 35 36 41 public class SelectorContext extends BaseContext { 42 43 45 46 49 public static final String prefix = "java:"; 50 51 52 55 public static final int prefixLength = prefix.length(); 56 57 58 61 public static final String IC_PREFIX = "IC_"; 62 63 64 66 67 70 public SelectorContext(Hashtable env) { 71 super( env ); 72 } 73 74 75 78 public SelectorContext(Hashtable env, boolean initialContext) { 79 this(env); 80 this.initialContext = initialContext; 81 } 82 83 84 86 87 90 protected Hashtable env; 91 92 93 96 protected StringManager sm = StringManager.getManager("org.apache.naming.res"); 97 98 99 102 protected boolean initialContext = false; 103 104 105 107 108 110 111 public Object lookup(Name name) 112 throws NamingException { 113 return getBoundContext().lookup(parseName(name)); 117 } 118 119 120 127 public Object lookup(String name) 128 throws NamingException { 129 return getBoundContext().lookup(parseName(name)); 133 } 134 135 136 148 public void bind(Name name, Object obj) 149 throws NamingException { 150 getBoundContext().bind(parseName(name), obj); 151 } 152 153 154 164 public void bind(String name, Object obj) 165 throws NamingException { 166 getBoundContext().bind(parseName(name), obj); 167 } 168 169 170 185 public void rebind(Name name, Object obj) 186 throws NamingException { 187 getBoundContext().rebind(parseName(name), obj); 188 } 189 190 191 200 public void rebind(String name, Object obj) 201 throws NamingException { 202 getBoundContext().rebind(parseName(name), obj); 203 } 204 205 206 220 public void unbind(Name name) 221 throws NamingException { 222 getBoundContext().unbind(parseName(name)); 223 } 224 225 226 234 public void unbind(String name) 235 throws NamingException { 236 getBoundContext().unbind(parseName(name)); 237 } 238 239 240 251 public void rename(Name oldName, Name newName) 252 throws NamingException { 253 getBoundContext().rename(parseName(oldName), parseName(newName)); 254 } 255 256 257 266 public void rename(String oldName, String newName) 267 throws NamingException { 268 getBoundContext().rename(parseName(oldName), parseName(newName)); 269 } 270 271 272 285 public NamingEnumeration list(Name name) 286 throws NamingException { 287 return getBoundContext().list(parseName(name)); 288 } 289 290 291 300 public NamingEnumeration list(String name) 301 throws NamingException { 302 return getBoundContext().list(parseName(name)); 303 } 304 305 306 319 public NamingEnumeration listBindings(Name name) 320 throws NamingException { 321 return getBoundContext().listBindings(parseName(name)); 322 } 323 324 325 334 public NamingEnumeration listBindings(String name) 335 throws NamingException { 336 return getBoundContext().listBindings(parseName(name)); 337 } 338 339 340 365 public void destroySubcontext(Name name) 366 throws NamingException { 367 getBoundContext().destroySubcontext(parseName(name)); 368 } 369 370 371 380 public void destroySubcontext(String name) 381 throws NamingException { 382 getBoundContext().destroySubcontext(parseName(name)); 383 } 384 385 386 399 public Context createSubcontext(Name name) 400 throws NamingException { 401 return getBoundContext().createSubcontext(parseName(name)); 402 } 403 404 405 415 public Context createSubcontext(String name) 416 throws NamingException { 417 return getBoundContext().createSubcontext(parseName(name)); 418 } 419 420 421 431 public Object lookupLink(Name name) 432 throws NamingException { 433 return getBoundContext().lookupLink(parseName(name)); 434 } 435 436 437 446 public Object lookupLink(String name) 447 throws NamingException { 448 return getBoundContext().lookupLink(parseName(name)); 449 } 450 451 452 466 public NameParser getNameParser(Name name) 467 throws NamingException { 468 return getBoundContext().getNameParser(parseName(name)); 469 } 470 471 472 480 public NameParser getNameParser(String name) 481 throws NamingException { 482 return getBoundContext().getNameParser(parseName(name)); 483 } 484 485 486 501 public Name composeName(Name name, Name prefix) 502 throws NamingException { 503 prefix = (Name ) name.clone(); 504 return prefix.addAll(name); 505 } 506 507 508 516 public String composeName(String name, String prefix) 517 throws NamingException { 518 return prefix + "/" + name; 519 } 520 521 522 531 public Object addToEnvironment(String propName, Object propVal) 532 throws NamingException { 533 return getBoundContext().addToEnvironment(propName, propVal); 534 } 535 536 537 544 public Object removeFromEnvironment(String propName) 545 throws NamingException { 546 return getBoundContext().removeFromEnvironment(propName); 547 } 548 549 550 560 public Hashtable getEnvironment() 561 throws NamingException { 562 return getBoundContext().getEnvironment(); 563 } 564 565 566 576 public void close() 577 throws NamingException { 578 getBoundContext().close(); 579 } 580 581 582 599 public String getNameInNamespace() 600 throws NamingException { 601 return prefix; 602 } 603 604 605 607 608 611 protected Context getBoundContext() 612 throws NamingException { 613 614 if (initialContext) { 615 String ICName = IC_PREFIX; 616 if (ContextBindings.isThreadBound()) { 617 ICName += ContextBindings.getThreadName(); 618 } else if (ContextBindings.isClassLoaderBound()) { 619 ICName += ContextBindings.getClassLoaderName(); 620 } 621 Context initialContext = ContextBindings.getContext(ICName); 622 if (initialContext == null) { 623 initialContext = new MemoryNamingContext(env); 628 ContextBindings.bindContext(ICName, initialContext); 629 } 630 return initialContext; 631 } else { 632 if (ContextBindings.isThreadBound()) { 633 return ContextBindings.getThread(); 634 } else { 635 return ContextBindings.getClassLoader(); 636 } 637 } 638 639 } 640 641 642 649 protected String parseName(String name) 650 throws NamingException { 651 652 if ((!initialContext) && (name.startsWith(prefix))) { 653 return (name.substring(prefixLength)); 654 } else { 655 if (initialContext) { 656 return (name); 657 } else { 658 throw new NamingException 659 (sm.getString("selectorContext.noJavaUrl")); 660 } 661 } 662 663 } 664 665 666 673 protected Name parseName(Name name) 674 throws NamingException { 675 676 if ((!initialContext) && (!name.isEmpty()) 677 && (name.get(0).equals(prefix))) { 678 return (name.getSuffix(1)); 679 } else { 680 if (initialContext) { 681 return (name); 682 } else { 683 throw new NamingException 684 (sm.getString("selectorContext.noJavaUrl")); 685 } 686 } 687 688 } 689 } 690 691 | Popular Tags |