1 25 26 package org.objectweb.carol.jndi.enc.java; 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.carol.util.configuration.TraceCarol; 51 52 59 public class CompNamingContext implements Context { 60 61 64 private Hashtable myEnv = null; 65 66 69 private Hashtable bindings = new Hashtable (); 70 71 74 private static NameParser myParser = new JavaNameParser(); 75 76 79 private String compId; 80 81 86 public CompNamingContext(String id, Hashtable env) { 87 if (env != null) { 88 myEnv = (Hashtable ) (env.clone()); 90 } 91 compId = id; 92 } 93 94 98 public CompNamingContext(String id) { 99 this(id, new Hashtable ()); 100 } 101 102 108 public Object lookup(Name name) throws NamingException { 109 return lookup(name.toString()); 111 } 112 113 119 public Object lookup(String name) throws NamingException { 120 if (TraceCarol.isDebugjndiEncCarol()) { 121 TraceCarol.debugjndiEncCarol(name); 122 } 123 124 Name n = new CompositeName (name); 125 if (n.size() < 1) { 126 if (TraceCarol.isDebugjndiEncCarol()) { 128 TraceCarol.debugjndiEncCarol("empty name"); 129 } 130 return this; 131 } 132 133 if (n.size() == 1) { 134 Object ret = bindings.get(name); 136 if (ret == null) { 137 if (TraceCarol.isDebugjndiEncCarol()) { 138 TraceCarol.debugjndiEncCarol(" " + name + " not found."); 139 } 140 throw new NameNotFoundException (name); 141 } 142 if (ret instanceof LinkRef ) { 143 147 InitialContext ictx = new InitialContext (); 148 RefAddr ra = ((Reference ) ret).get(0); 149 try { 150 ret = ictx.lookup((String ) ra.getContent()); 151 } catch (Exception e) { 152 NamingException ne = new NamingException (e.getMessage()); 153 ne.setRootCause(e); 154 TraceCarol.error("unexpected exception " + e.getMessage(), e); 155 throw ne; 156 } 157 } else if (ret instanceof Reference ) { 158 try { 160 Object o = javax.naming.spi.NamingManager.getObjectInstance(ret, n, this, myEnv); 161 ret = o; 162 } catch (NamingException e) { 163 throw e; 164 } catch (Exception e) { 165 NamingException ne = new NamingException (e.getMessage()); 166 ne.setRootCause(e); 167 throw ne; 168 } 169 if (ret == null) { 170 TraceCarol.error("Can not build an object with the reference " + name); 171 throw new NamingException ("Can not build an object with the reference '" + name + "'"); 172 } 173 } 174 return ret; 175 } else { 176 String suffix = n.getSuffix(1).toString(); 178 Context subctx = lookupCtx(n.get(0)); 180 return subctx.lookup(suffix); 181 } 182 } 183 184 192 public void bind(Name name, Object obj) throws NamingException { 193 bind(name.toString(), obj); 195 } 196 197 205 public void bind(String name, Object obj) throws NamingException { 206 if (TraceCarol.isDebugjndiEncCarol()) { 207 TraceCarol.debugjndiEncCarol(name); 208 } 209 210 Name n = new CompositeName (name); 211 if (n.size() < 1) { 212 TraceCarol.error("CompNamingContext bind empty name ?"); 213 214 throw new InvalidNameException ("CompNamingContext cannot bind empty name"); 215 } 216 217 if (n.size() == 1) { 218 if (bindings.get(name) != null) { 220 TraceCarol.error("CompNamingContext: trying to overbind"); 221 throw new NameAlreadyBoundException ("CompNamingContext: Use rebind to bind over a name"); 222 } 223 bindings.put(name, obj); 224 } else { 225 String suffix = n.getSuffix(1).toString(); 227 Context subctx; 229 try { 230 subctx = lookupCtx(n.get(0)); 231 } catch (NameNotFoundException e) { 232 subctx = createSubcontext(n.get(0)); 233 } 234 subctx.bind(suffix, obj); 235 } 236 } 237 238 245 public void rebind(Name name, Object obj) throws NamingException { 246 rebind(name.toString(), obj); 248 } 249 250 258 public void rebind(String name, Object obj) throws NamingException { 259 260 if (TraceCarol.isDebugjndiEncCarol()) { 261 TraceCarol.debugjndiEncCarol(name); 262 } 263 264 Name n = new CompositeName (name); 265 if (n.size() < 1) { 266 TraceCarol.error("CompNamingContext rebind empty name ?"); 267 throw new InvalidNameException ("CompNamingContext cannot rebind empty name"); 268 } 269 270 if (n.size() == 1) { 271 bindings.put(name, obj); 273 } else { 274 String suffix = n.getSuffix(1).toString(); 276 Context subctx; 278 try { 279 subctx = lookupCtx(n.get(0)); 280 } catch (NameNotFoundException e) { 281 subctx = createSubcontext(n.get(0)); 282 } 283 subctx.rebind(suffix, obj); 284 } 285 } 286 287 293 public void unbind(Name name) throws NamingException { 294 unbind(name.toString()); 296 } 297 298 305 public void unbind(String name) throws NamingException { 306 307 if (TraceCarol.isDebugjndiEncCarol()) { 308 TraceCarol.debugjndiEncCarol(name); 309 } 310 311 Name n = new CompositeName (name); 312 if (n.size() < 1) { 313 TraceCarol.error("CompNamingContext unbind empty name ?"); 314 throw new InvalidNameException ("CompNamingContext cannot unbind empty name"); 315 } 316 317 if (n.size() == 1) { 318 if (bindings.get(name) == null) { 320 TraceCarol.error("CompNamingContext nothing to unbind"); 321 throw new NameNotFoundException (name); 322 } 323 bindings.remove(name); 324 } else { 325 String suffix = n.getSuffix(1).toString(); 327 Context subctx = lookupCtx(n.get(0)); 329 subctx.unbind(suffix); 330 } 331 } 332 333 340 public void rename(Name oldName, Name newName) throws NamingException { 341 rename(oldName.toString(), newName.toString()); 343 } 344 345 352 public void rename(String oldName, String newName) throws NamingException { 353 if (TraceCarol.isDebugjndiEncCarol()) { 354 TraceCarol.debugjndiEncCarol("CompNamingContext rename " + oldName + " in " + newName); 355 } 356 Object obj = lookup(oldName); 357 rebind(newName, obj); 358 unbind(oldName); 359 } 360 361 371 public NamingEnumeration list(Name name) throws NamingException { 372 return list(name.toString()); 374 } 375 376 385 public NamingEnumeration list(String name) throws NamingException { 386 if (TraceCarol.isDebugjndiEncCarol()) { 387 TraceCarol.debugjndiEncCarol(name); 388 } 389 390 if (name.length() == 0) { 391 return new ListOfNames(bindings); 393 } 394 Object obj = lookup(name); 395 if (obj instanceof Context ) { 396 return ((Context ) obj).list(""); 397 } else { 398 TraceCarol.error("CompNamingContext: can only list a Context"); 399 throw new NotContextException (name); 400 } 401 } 402 403 413 public NamingEnumeration listBindings(Name name) throws NamingException { 414 return listBindings(name.toString()); 416 } 417 418 426 public NamingEnumeration listBindings(String name) throws NamingException { 427 if (TraceCarol.isDebugjndiEncCarol()) { 428 TraceCarol.debugjndiEncCarol(name); 429 } 430 431 if (name.length() == 0) { 432 return new ListOfBindings(bindings); 434 } 435 Object obj = lookup(name); 436 if (obj instanceof Context ) { 437 return ((Context ) obj).listBindings(""); 438 } else { 439 TraceCarol.error("CompNamingContext: can only list a Context"); 440 throw new NotContextException (name); 441 } 442 } 443 444 450 public void destroySubcontext(Name name) throws NamingException { 451 destroySubcontext(name.toString()); 453 } 454 455 461 public void destroySubcontext(String name) throws NamingException { 462 TraceCarol.error("CompNamingContext try to destroySubcontext " + name); 463 throw new OperationNotSupportedException ("CompNamingContext: destroySubcontext"); 464 } 465 466 475 public Context createSubcontext(Name name) throws NamingException { 476 return createSubcontext(name.toString()); 478 } 479 480 488 public Context createSubcontext(String name) throws NamingException { 489 if (TraceCarol.isDebugjndiEncCarol()) { 490 TraceCarol.debugjndiEncCarol(name); 491 } 492 493 Name n = new CompositeName (name); 494 if (n.size() < 1) { 495 TraceCarol.error("CompNamingContext createSubcontext with empty name ?"); 496 throw new InvalidNameException ("CompNamingContext cannot create empty Subcontext"); 497 } 498 499 Context ctx = null; if (n.size() == 1) { 501 ctx = new CompNamingContext(compId, myEnv); 503 bindings.put(name, ctx); 504 } else { 505 String suffix = n.getSuffix(1).toString(); 508 Context subctx; 509 name = n.get(0); 510 try { 511 subctx = lookupCtx(name); 512 } catch (NameNotFoundException e) { 513 subctx = createSubcontext(name); 514 } 515 ctx = subctx.createSubcontext(suffix); 516 } 517 return ctx; 518 } 519 520 529 public Object lookupLink(Name name) throws NamingException { 530 return lookupLink(name.toString()); 532 } 533 534 543 public Object lookupLink(String name) throws NamingException { 544 if (TraceCarol.isDebugjndiEncCarol()) { 545 TraceCarol.debugjndiEncCarol(name); 546 } 547 548 TraceCarol.error("CompNamingContext lookupLink not implemented yet!"); 550 551 return lookup(name); 552 } 553 554 561 public NameParser getNameParser(Name name) throws NamingException { 562 return myParser; 563 } 564 565 572 public NameParser getNameParser(String name) throws NamingException { 573 return myParser; 574 } 575 576 583 public Name composeName(Name name, Name prefix) throws NamingException { 584 TraceCarol.error("CompNamingContext composeName not implemented!"); 585 586 throw new OperationNotSupportedException ("CompNamingContext composeName"); 587 } 588 589 597 public String composeName(String name, String prefix) throws NamingException { 598 TraceCarol.error("CompNamingContext composeName " + name + " " + prefix); 599 600 throw new OperationNotSupportedException ("CompNamingContext composeName"); 601 } 602 603 613 public Object addToEnvironment(String propName, Object propVal) throws NamingException { 614 615 TraceCarol.debugjndiEncCarol(propName); 616 617 if (myEnv == null) { 618 myEnv = new Hashtable (); 619 } 620 return myEnv.put(propName, propVal); 621 } 622 623 631 public Object removeFromEnvironment(String propName) throws NamingException { 632 633 TraceCarol.debugjndiEncCarol(propName); 634 635 if (myEnv == null) { 636 return null; 637 } 638 return myEnv.remove(propName); 639 } 640 641 646 public Hashtable getEnvironment() throws NamingException { 647 if (TraceCarol.isDebugjndiEncCarol()) { 648 TraceCarol.debugjndiEncCarol(""); 649 } 650 651 if (myEnv == null) { 652 myEnv = new Hashtable (); 653 } 654 return myEnv; 655 } 656 657 661 public void close() throws NamingException { 662 myEnv = null; 663 } 664 665 669 public String getNameInNamespace() { 670 return compId; 672 } 673 674 678 686 private Context lookupCtx(String name) throws NamingException { 687 Object obj = bindings.get(name); 688 if (obj == null) { 689 throw new NameNotFoundException (); 690 } 691 if (obj instanceof CompNamingContext) { 692 return (Context ) obj; 693 } else { 694 throw new NameAlreadyBoundException (name); 695 } 696 } 697 698 702 706 protected class ListOfNames implements NamingEnumeration { 707 708 711 private Enumeration names; 712 713 716 private Hashtable bindings; 717 718 722 ListOfNames(Hashtable bindings) { 723 this.bindings = bindings; 724 this.names = bindings.keys(); 725 } 726 727 734 public boolean hasMore() throws NamingException { 735 return names.hasMoreElements(); 736 } 737 738 748 public Object next() throws NamingException { 749 String name = (String ) names.nextElement(); 750 String className = bindings.get(name).getClass().getName(); 751 return new NameClassPair (name, className); 752 } 753 754 757 public void close() { 758 } 759 760 765 public Object nextElement() { 766 try { 767 return next(); 768 } catch (NamingException e) { 769 throw new NoSuchElementException (e.toString()); 770 } 771 } 772 773 779 public boolean hasMoreElements() { 780 try { 781 return hasMore(); 782 } catch (NamingException e) { 783 return false; 784 } 785 } 786 787 790 protected Hashtable getBindings() { 791 return bindings; 792 } 793 794 797 protected Enumeration getNames() { 798 return names; 799 } 800 } 801 802 805 protected class ListOfBindings extends ListOfNames { 806 807 811 ListOfBindings(Hashtable bindings) { 812 super(bindings); 813 } 814 815 826 public Object next() throws NamingException { 827 String name = (String ) getNames().nextElement(); 828 return new Binding (name, getBindings().get(name)); 829 } 830 } 831 } 832 | Popular Tags |