1 25 26 package org.objectweb.jonas.naming; 27 28 import java.util.Enumeration ; 29 import java.util.Hashtable ; 30 import java.util.NoSuchElementException ; 31 32 import javax.naming.Binding ; 33 import javax.naming.CompositeName ; 34 import javax.naming.Context ; 35 import javax.naming.InitialContext ; 36 import javax.naming.InvalidNameException ; 37 import javax.naming.LinkRef ; 38 import javax.naming.Name ; 39 import javax.naming.NameAlreadyBoundException ; 40 import javax.naming.NameClassPair ; 41 import javax.naming.NameNotFoundException ; 42 import javax.naming.NameParser ; 43 import javax.naming.NamingEnumeration ; 44 import javax.naming.NamingException ; 45 import javax.naming.NotContextException ; 46 import javax.naming.OperationNotSupportedException ; 47 import javax.naming.RefAddr ; 48 import javax.naming.Reference ; 49 50 import org.objectweb.jonas.common.Log; 51 import org.objectweb.util.monolog.api.BasicLevel; 52 import org.objectweb.util.monolog.api.Logger; 53 54 62 public class CompNamingContext implements Context { 63 64 67 private static Logger logger = null; 68 69 72 private Hashtable myEnv = null; 73 74 77 private Hashtable bindings = new Hashtable (); 78 79 82 private static NameParser myParser = new EJBNameParser(); 83 84 87 private String compId; 88 89 94 public CompNamingContext(String id, Hashtable env) { 95 if (env != null) { 96 myEnv = (Hashtable ) (env.clone()); 98 } 99 compId = id; 100 logger = Log.getLogger(Log.JONAS_NAMING_PREFIX); 101 } 102 103 107 public CompNamingContext(String id) { 108 myEnv = new Hashtable (); 109 compId = id; 110 logger = Log.getLogger(Log.JONAS_NAMING_PREFIX); 111 } 112 113 117 125 public Object lookup(Name name) throws NamingException { 126 return lookup(name.toString()); 128 } 129 130 137 public Object lookup(String name) throws NamingException { 138 if (logger.isLoggable(BasicLevel.DEBUG)) { 139 logger.log(BasicLevel.DEBUG, name); 140 } 141 142 Name n = new CompositeName (name); 143 if (n.size() < 1) { 144 if (logger.isLoggable(BasicLevel.DEBUG)) { 146 logger.log(BasicLevel.DEBUG, "empty name"); 147 } 148 return this; 149 } 150 151 if (n.size() == 1) { 152 Object ret = bindings.get(name); 154 if (ret == null) { 155 if (logger.isLoggable(BasicLevel.DEBUG)) { 156 logger.log(BasicLevel.DEBUG, " " + name + " not found."); 157 } 158 throw new NameNotFoundException (name); 159 } 160 if (ret instanceof LinkRef ) { 161 165 InitialContext ictx = NamingManager.getInstance().getInitialContext(); 166 RefAddr ra = ((Reference ) ret).get(0); 167 try { 168 ret = ictx.lookup((String ) ra.getContent()); 169 } catch (Exception e) { 170 NamingException ne = new NamingException (e.getMessage()); 171 ne.setRootCause(e); 172 logger.log(BasicLevel.WARN, "unexpected exception " + e.getMessage()); 173 throw ne; 174 } 175 } else if (ret instanceof Reference ) { 176 try { 178 Object o = javax.naming.spi.NamingManager.getObjectInstance(ret, n, this, myEnv); 179 ret = o; 180 } catch (NamingException e) { 181 throw e; 182 } catch (Exception e) { 183 NamingException ne = new NamingException (e.getMessage()); 184 ne.setRootCause(e); 185 throw ne; 186 } 187 if (ret == null) { 188 logger.log(BasicLevel.WARN, "Can not build an object with the reference " + name); 189 throw new NamingException ("Can not build an object with the reference '" + name + "'"); 190 } 191 } 192 return ret; 193 } else { 194 String suffix = n.getSuffix(1).toString(); 196 Context subctx = lookupCtx(n.get(0)); 198 return subctx.lookup(suffix); 199 } 200 } 201 202 212 public void bind(Name name, Object obj) throws NamingException { 213 bind(name.toString(), obj); 215 } 216 217 226 public void bind(String name, Object obj) throws NamingException { 227 228 if (logger.isLoggable(BasicLevel.DEBUG)) { 229 logger.log(BasicLevel.DEBUG, name); 230 } 231 232 Name n = new CompositeName (name); 233 if (n.size() < 1) { 234 logger.log(BasicLevel.ERROR, "CompNamingContext bind empty name ?"); 235 throw new InvalidNameException ("CompNamingContext cannot bind empty name"); 236 } 237 238 if (n.size() == 1) { 239 if (bindings.get(name) != null) { 241 logger.log(BasicLevel.ERROR, "CompNamingContext: trying to overbind"); 242 throw new NameAlreadyBoundException ("CompNamingContext: Use rebind to bind over a name"); 243 } 244 bindings.put(name, obj); 245 } else { 246 String suffix = n.getSuffix(1).toString(); 248 Context subctx; 250 try { 251 subctx = lookupCtx(n.get(0)); 252 } catch (NameNotFoundException e) { 253 subctx = createSubcontext(n.get(0)); 254 } 255 subctx.bind(suffix, obj); 256 } 257 } 258 259 269 public void rebind(Name name, Object obj) throws NamingException { 270 rebind(name.toString(), obj); 272 } 273 274 285 public void rebind(String name, Object obj) throws NamingException { 286 287 if (logger.isLoggable(BasicLevel.DEBUG)) { 288 logger.log(BasicLevel.DEBUG, name); 289 } 290 291 Name n = new CompositeName (name); 292 if (n.size() < 1) { 293 logger.log(BasicLevel.ERROR, "CompNamingContext rebind empty name ?"); 294 throw new InvalidNameException ("CompNamingContext cannot rebind empty name"); 295 } 296 297 if (n.size() == 1) { 298 bindings.put(name, obj); 300 } else { 301 String suffix = n.getSuffix(1).toString(); 303 Context subctx; 305 try { 306 subctx = lookupCtx(n.get(0)); 307 } catch (NameNotFoundException e) { 308 subctx = createSubcontext(n.get(0)); 309 } 310 subctx.rebind(suffix, obj); 311 } 312 } 313 314 321 public void unbind(Name name) throws NamingException { 322 unbind(name.toString()); 324 } 325 326 334 public void unbind(String name) throws NamingException { 335 336 if (logger.isLoggable(BasicLevel.DEBUG)) { 337 logger.log(BasicLevel.DEBUG, name); 338 } 339 340 Name n = new CompositeName (name); 341 if (n.size() < 1) { 342 logger.log(BasicLevel.ERROR, "CompNamingContext unbind empty name ?"); 343 throw new InvalidNameException ("CompNamingContext cannot unbind empty name"); 344 } 345 346 if (n.size() == 1) { 347 if (bindings.get(name) == null) { 349 logger.log(BasicLevel.ERROR, "CompNamingContext nothing to unbind"); 350 throw new NameNotFoundException (name); 351 } 352 bindings.remove(name); 353 } else { 354 String suffix = n.getSuffix(1).toString(); 356 Context subctx = lookupCtx(n.get(0)); 358 subctx.unbind(suffix); 359 } 360 } 361 362 372 public void rename(Name oldName, Name newName) throws NamingException { 373 rename(oldName.toString(), newName.toString()); 375 } 376 377 385 public void rename(String oldName, String newName) throws NamingException { 386 387 logger.log(BasicLevel.ERROR, "CompNamingContext rename " + oldName + " in " + newName); 388 389 Object obj = lookup(oldName); 390 rebind(newName, obj); 391 unbind(oldName); 392 } 393 394 405 public NamingEnumeration list(Name name) throws NamingException { 406 return list(name.toString()); 408 } 409 410 420 public NamingEnumeration list(String name) throws NamingException { 421 422 if (logger.isLoggable(BasicLevel.DEBUG)) { 423 logger.log(BasicLevel.DEBUG, name); 424 } 425 426 if (name.length() == 0) { 427 return new ListOfNames(bindings); 429 } 430 Object obj = lookup(name); 431 if (obj instanceof Context ) { 432 return ((Context ) obj).list(""); 433 } else { 434 logger.log(BasicLevel.ERROR, "CompNamingContext: can only list a Context"); 435 throw new NotContextException (name); 436 } 437 } 438 439 455 public NamingEnumeration listBindings(Name name) throws NamingException { 456 return listBindings(name.toString()); 458 } 459 460 470 public NamingEnumeration listBindings(String name) throws NamingException { 471 472 if (logger.isLoggable(BasicLevel.DEBUG)) { 473 logger.log(BasicLevel.DEBUG, name); 474 } 475 476 if (name.length() == 0) { 477 return new ListOfBindings(bindings); 479 } 480 Object obj = lookup(name); 481 if (obj instanceof Context ) { 482 return ((Context ) obj).listBindings(""); 483 } else { 484 logger.log(BasicLevel.ERROR, "CompNamingContext: can only list a Context"); 485 throw new NotContextException (name); 486 } 487 } 488 489 495 public void destroySubcontext(Name name) throws NamingException { 496 destroySubcontext(name.toString()); 498 } 499 500 506 public void destroySubcontext(String name) throws NamingException { 507 508 logger.log(BasicLevel.ERROR, "CompNamingContext try to destroySubcontext " + name); 509 510 throw new OperationNotSupportedException ("CompNamingContext: destroySubcontext"); 511 } 512 513 525 public Context createSubcontext(Name name) throws NamingException { 526 return createSubcontext(name.toString()); 528 } 529 530 540 public Context createSubcontext(String name) throws NamingException { 541 542 if (logger.isLoggable(BasicLevel.DEBUG)) { 543 logger.log(BasicLevel.DEBUG, name); 544 } 545 546 Name n = new CompositeName (name); 547 if (n.size() < 1) { 548 logger.log(BasicLevel.ERROR, "CompNamingContext createSubcontext with empty name ?"); 549 throw new InvalidNameException ("CompNamingContext cannot create empty Subcontext"); 550 } 551 552 Context ctx = null; if (n.size() == 1) { 554 ctx = new CompNamingContext(compId, myEnv); 556 bindings.put(name, ctx); 557 } else { 558 String suffix = n.getSuffix(1).toString(); 561 Context subctx; 562 name = n.get(0); 563 try { 564 subctx = lookupCtx(name); 565 } catch (NameNotFoundException e) { 566 subctx = createSubcontext(name); 567 } 568 ctx = subctx.createSubcontext(suffix); 569 } 570 return ctx; 571 } 572 573 584 public Object lookupLink(Name name) throws NamingException { 585 return lookupLink(name.toString()); 587 } 588 589 601 public Object lookupLink(String name) throws NamingException { 602 603 if (logger.isLoggable(BasicLevel.DEBUG)) { 604 logger.log(BasicLevel.DEBUG, name); 605 } 606 607 logger.log(BasicLevel.ERROR, "CompNamingContext lookupLink not implemented yet!"); 609 return lookup(name); 610 } 611 612 621 public NameParser getNameParser(Name name) throws NamingException { 622 return myParser; 623 } 624 625 634 public NameParser getNameParser(String name) throws NamingException { 635 return myParser; 636 } 637 638 647 public Name composeName(Name name, Name prefix) throws NamingException { 648 649 logger.log(BasicLevel.ERROR, "CompNamingContext composeName not implemented!"); 650 throw new OperationNotSupportedException ("CompNamingContext composeName"); 651 } 652 653 662 public String composeName(String name, String prefix) throws NamingException { 663 664 logger.log(BasicLevel.ERROR, "CompNamingContext composeName " + name + " " + prefix); 665 666 throw new OperationNotSupportedException ("CompNamingContext composeName"); 667 } 668 669 681 public Object addToEnvironment(String propName, Object propVal) throws NamingException { 682 683 if (logger.isLoggable(BasicLevel.DEBUG)) { 684 logger.log(BasicLevel.DEBUG, propName); 685 } 686 687 if (myEnv == null) { 688 myEnv = new Hashtable (); 689 } 690 return myEnv.put(propName, propVal); 691 } 692 693 702 public Object removeFromEnvironment(String propName) throws NamingException { 703 704 if (logger.isLoggable(BasicLevel.DEBUG)) { 705 logger.log(BasicLevel.DEBUG, propName); 706 } 707 708 if (myEnv == null) { 709 return null; 710 } 711 return myEnv.remove(propName); 712 } 713 714 720 public Hashtable getEnvironment() throws NamingException { 721 722 if (logger.isLoggable(BasicLevel.DEBUG)) { 723 logger.log(BasicLevel.DEBUG, ""); 724 } 725 726 if (myEnv == null) { 727 myEnv = new Hashtable (); 728 } 729 return myEnv; 730 } 731 732 737 public void close() throws NamingException { 738 myEnv = null; 739 } 740 741 746 public String getNameInNamespace() { 747 return compId; 749 } 750 751 755 763 private Context lookupCtx(String name) throws NamingException { 764 Object obj = bindings.get(name); 765 if (obj == null) { 766 throw new NameNotFoundException (); 767 } 768 if (obj instanceof CompNamingContext) { 769 return (Context ) obj; 770 } else { 771 throw new NameAlreadyBoundException (name); 772 } 773 } 774 775 779 783 class ListOfNames implements NamingEnumeration { 784 protected Enumeration names; 785 protected Hashtable bindings; 786 787 791 ListOfNames (Hashtable bindings) { 792 this.bindings = bindings; 793 this.names = bindings.keys(); 794 } 795 796 public boolean hasMore() throws NamingException { 801 return names.hasMoreElements(); 802 } 803 804 public Object next() throws NamingException { 805 String name = (String ) names.nextElement(); 806 String className = bindings.get(name).getClass().getName(); 807 return new NameClassPair (name, className); 808 } 809 810 public void close() { 811 } 812 813 817 public Object nextElement() { 818 try { 819 return next(); 820 } catch (NamingException e) { 821 throw new NoSuchElementException (e.toString()); 822 } 823 } 824 825 public boolean hasMoreElements() { 826 try { 827 return hasMore(); 828 } catch (NamingException e) { 829 return false; 830 } 831 } 832 833 } 834 835 838 class ListOfBindings extends ListOfNames { 839 840 ListOfBindings (Hashtable bindings) { 841 super(bindings); 842 } 843 844 public Object next() throws NamingException { 847 String name = (String ) names.nextElement(); 848 return new Binding (name, this.bindings.get(name)); 849 } 850 } 851 } 852 | Popular Tags |