1 23 package com.sun.enterprise.naming; 24 25 import java.util.*; 26 import java.io.*; 27 import javax.naming.*; 28 import javax.rmi.PortableRemoteObject ; 29 import java.rmi.*; 30 import com.sun.enterprise.util.ORBManager; 31 import org.omg.CosNaming.NamingContext ; 32 import org.omg.CosNaming.NameComponent ; 33 import org.omg.CosNaming.NamingContextHelper ; 34 import javax.rmi.CORBA.Tie ; 35 36 import java.util.logging.*; 38 import com.sun.logging.*; 39 41 42 47 public class TransientContext implements Context , Serializable { 48 49 static Logger _logger=LogDomains.getLogger(LogDomains.JNDI_LOGGER); 51 53 public static final boolean debug = false; 54 Hashtable myEnv; 55 private Hashtable bindings = new Hashtable (); 56 static NameParser myParser = new SerialNameParser(); 57 58 public TransientContext() { 59 } 60 61 67 public Context createSubcontext(String name) throws NamingException { 68 return drillDownAndCreateSubcontext (name); 69 } 70 71 77 public Context createSubcontext(Name name) throws NamingException { 78 return createSubcontext(name.toString()); 79 } 80 81 86 public void destroySubcontext(String name) throws NamingException { 87 drillDownAndDestroySubcontext (name); 88 } 89 90 95 public void destroySubcontext(Name name) throws NamingException { 96 destroySubcontext(name.toString()); 97 } 98 99 106 public Context drillDownAndCreateSubcontext (String name) 107 throws NamingException { 108 Name n = new CompositeName (name); 109 if (n.size () < 1){ 110 throw new InvalidNameException ("Cannot create empty subcontext"); 111 } 112 if (n.size() == 1){ if (bindings.containsKey (name)){ 114 throw new NameAlreadyBoundException ("Subcontext " + 115 name + "already present"); 116 } 117 118 TransientContext ctx = null; 119 ctx = new TransientContext (); 120 bindings.put (name, ctx); 121 return ctx; 122 } else { 123 String suffix = n.getSuffix(1).toString(); 124 Context retCtx, ctx; try { 126 ctx = resolveContext(n.get(0)); 127 } catch (NameNotFoundException e){ 128 ctx = new TransientContext (); 129 } 130 retCtx = ctx.createSubcontext (suffix); 131 bindings.put (n.get(0), ctx); 132 return retCtx; 133 } 134 } 135 136 141 public void drillDownAndDestroySubcontext (String name) 142 throws NamingException { 143 Name n = new CompositeName (name); 144 if (n.size () < 1){ 145 throw new InvalidNameException ("Cannot destoy empty subcontext"); 146 } 147 if (n.size() == 1){ if (bindings.containsKey (name)){ 149 bindings.remove (name); 150 } 151 else{ 152 throw new NameNotFoundException ("Subcontext: " + name + 153 " not found"); 154 } 155 } else { 156 String suffix = n.getSuffix(1).toString(); 157 Context ctx; ctx = resolveContext(n.get(0)); 159 ctx.destroySubcontext (suffix); 160 } 161 } 162 163 169 public Object lookup(String name) throws NamingException { 170 Name n = new CompositeName(name); 171 if (n.size() < 1) { 172 throw new InvalidNameException("Cannot bind empty name"); 173 } 174 if(n.size() == 1) { return doLookup(n.toString()); 176 } else { 177 String suffix = n.getSuffix(1).toString(); 178 TransientContext ctx = resolveContext(n.get(0)); 179 return ctx.lookup(suffix); 180 } 181 } 182 183 189 public Object lookup(Name name) throws NamingException { 190 return lookup(name.toString()); 191 } 192 193 199 private Object doLookup(String name) throws NamingException 200 { 201 Object answer = bindings.get(name); 202 if (answer == null) { 203 throw new NameNotFoundException(name + " not found"); 204 } 205 return answer; 206 } 207 208 213 public void bind(String name, Object obj) throws NamingException{ 214 Name n = new CompositeName(name); 215 if (n.size() < 1) { 216 throw new InvalidNameException("Cannot bind empty name"); 217 } 218 if(n.size() == 1) { doBindOrRebind(n.toString(), obj, false); 220 } else { 221 String suffix = n.getSuffix(1).toString(); 222 Context ctx; 223 try { 224 ctx = resolveContext(n.get(0)); 225 } catch (NameNotFoundException e){ 226 ctx = createSubcontext (n.get (0)); 227 } 228 ctx.bind(suffix, obj); 229 } 230 } 231 232 237 public void bind(Name name, Object obj) 238 throws NamingException { 239 bind(name.toString(), obj); 240 } 241 242 247 250 private TransientContext resolveContext(String s) throws NamingException { 251 TransientContext ctx; 253 Object obj = bindings.get (s); 254 if(obj == null) { 255 throw new NameNotFoundException(); 256 } 257 if (obj instanceof TransientContext){ 258 ctx = (TransientContext) obj; 259 } 260 else { 261 throw new NameAlreadyBoundException (s); 262 } 263 return ctx; 264 } 265 266 271 private void doBindOrRebind(String name, Object obj, boolean rebind) 272 throws NamingException 273 { 274 if (name.equals("")) { 275 throw new InvalidNameException("Cannot bind empty name"); 276 } 277 if(!rebind) { 278 if (bindings.get(name) != null) { 279 throw new NameAlreadyBoundException( 280 "Use rebind to override"); 281 } 282 } 283 bindings.put(name, obj); 284 } 285 286 287 292 public void rebind(String name, Object obj) 293 throws NamingException { 294 Name n = new CompositeName(name); 295 if (n.size() < 1) { 296 throw new InvalidNameException("Cannot bind empty name"); 297 } 298 if(n.size() == 1) { doBindOrRebind(n.toString(), obj, true); 300 } else { 301 String suffix = n.getSuffix(1).toString(); 302 Context ctx=null; 303 try { 304 ctx = resolveContext(n.get(0)); 305 ctx.rebind(suffix, obj); 306 } catch (NameNotFoundException e){ 307 ctx = createSubcontext (n.get(0)); 308 ctx.rebind(suffix, obj); 309 } 310 } 311 } 312 313 318 public void rebind(Name name, Object obj) 319 throws NamingException { 320 rebind(name.toString(), obj); 321 } 322 323 329 private void doUnbind(String name) throws NamingException { 330 if (name.equals("")) { 331 throw new InvalidNameException("Cannot unbind empty name"); 332 } 333 if (bindings.get(name) == null) { 334 throw new NameNotFoundException( 335 "Cannot find name to unbind"); 336 } 337 bindings.remove(name); 338 } 339 340 346 public void unbind(String name) throws NamingException { 347 Name n = new CompositeName(name); 348 if (n.size() < 1) { 349 throw new InvalidNameException("Cannot unbind empty name"); 350 } 351 if(n.size() == 1) { doUnbind(n.toString()); 353 } else { 354 String suffix = n.getSuffix(1).toString(); 355 TransientContext ctx = resolveContext(n.get(0)); 356 ctx.unbind(suffix); 357 } 358 } 359 360 365 public void unbind(Name name) 366 throws NamingException { 367 unbind(name.toString()); 368 } 369 370 375 public void rename(Name oldname, Name newname) throws NamingException { 376 rename(oldname.toString(), newname.toString()); 377 } 378 379 384 public void rename(String oldname, String newname) 385 throws NamingException { 386 if (oldname.equals("") || newname.equals("")) { 387 throw new InvalidNameException("Cannot rename empty name"); 388 } 389 390 if (bindings.get(newname) != null) { 392 throw new NameAlreadyBoundException(newname + 393 " is already bound"); 394 } 395 396 Object oldBinding = bindings.remove(oldname); 398 if (oldBinding == null) { 399 throw new NameNotFoundException(oldname + " not bound"); 400 } 401 402 bindings.put(newname, oldBinding); 403 } 404 405 410 public Hashtable list() { 411 return bindings; 412 } 413 414 419 public Hashtable listContext(String name) throws NamingException { 420 if(debug) { 421 print(bindings); 422 } 423 if(name.equals("")) 424 return bindings; 425 426 Object target = lookup(name); 427 if(target instanceof TransientContext) { 428 return ((TransientContext)target).listContext(""); 429 } 430 throw new NotContextException(name + " cannot be listed"); 431 } 432 433 438 public NamingEnumeration list(Name name) throws NamingException { 439 return list(name.toString()); 440 } 441 442 447 public NamingEnumeration list(String name) throws NamingException { 448 if(debug) { 449 print(bindings); 450 } 451 if(name.equals("")) 452 return new RepNames(bindings); 453 454 Object target = lookup(name); 455 if(target instanceof Context ) { 456 return ((Context )target).list(""); 457 } 458 throw new NotContextException(name + " cannot be listed"); 459 } 460 461 466 public NamingEnumeration listBindings(String name) throws NamingException { 467 if(name.equals("")) 468 return new RepBindings(bindings); 469 470 Object target = lookup(name); 471 if(target instanceof Context ) { 472 return ((Context )target).listBindings(""); 473 } 474 throw new NotContextException(name + " cannot be listed"); 475 } 476 477 482 public NamingEnumeration listBindings(Name name) throws NamingException { 483 return listBindings(name.toString()); 484 } 485 486 491 public Object lookupLink(String name) throws NamingException { 492 return lookup(name); 494 } 495 496 501 public Object lookupLink(Name name) throws NamingException { 502 return lookupLink(name.toString()); 504 } 505 506 511 public NameParser getNameParser(String name) throws NamingException { 512 return myParser; 513 } 514 515 520 public NameParser getNameParser(Name name) throws NamingException { 521 return getNameParser(name.toString()); 523 } 524 525 531 public String composeName(String name, String prefix) 532 throws NamingException { 533 return null; 534 } 535 536 542 public Name composeName(Name name, Name prefix) 543 throws NamingException { 544 Name result = (Name)(prefix.clone()); 545 result.addAll(name); 546 return result; 547 } 548 549 554 public Object addToEnvironment(String propName, Object propVal) 555 throws NamingException { 556 if (myEnv == null) { 557 myEnv = new Hashtable(5, 0.75f); 558 } 559 return myEnv.put(propName, propVal); 560 } 561 562 567 public Object removeFromEnvironment(String propName) 568 throws NamingException { 569 if (myEnv == null) { 570 return null; 571 } 572 return myEnv.remove(propName); 573 } 574 575 580 public Hashtable getEnvironment() throws NamingException { 581 if (myEnv == null) { 582 myEnv = new Hashtable(3, 0.75f); 584 } 585 return myEnv; 586 } 587 588 593 public void close() throws NamingException { 594 myEnv = null; 595 } 596 597 600 public String getNameInNamespace() throws NamingException { 601 throw new OperationNotSupportedException("getNameInNamespace() " + 602 "not implemented"); 603 } 604 605 608 private static void print(Hashtable ht) { 609 for (Enumeration en = ht.keys(); en.hasMoreElements(); ) { 610 Object key = en.nextElement(); 611 Object value = ht.get(key); 612 617 if(_logger.isLoggable(Level.FINE)) { 619 _logger.log(Level.FINE,"[" + key + ":" + 620 key.getClass().getName() + 621 ", " + value + ":" + value.getClass().getName() + "]"); 622 } 623 } 625 } 626 627 class RepNames implements NamingEnumeration { 629 Hashtable bindings; 630 Enumeration names; 631 632 RepNames (Hashtable bindings) { 633 this.bindings = bindings; 634 this.names = bindings.keys(); 635 } 636 637 public boolean hasMoreElements() { 638 return names.hasMoreElements(); 639 } 640 641 public boolean hasMore() throws NamingException { 642 return hasMoreElements(); 643 } 644 645 public Object nextElement() { 646 if(names.hasMoreElements()) 647 { 648 String name = (String )names.nextElement(); 649 String className = bindings.get(name).getClass().getName(); 650 return new NameClassPair(name, className); 651 } 652 else 653 return null; 654 } 655 656 public Object next() throws NamingException { 657 return nextElement(); 658 } 659 660 public void close() throws NamingException { 662 throw new OperationNotSupportedException("close() not implemented"); 663 } 664 } 665 666 class RepBindings implements NamingEnumeration { 668 Enumeration names; 669 Hashtable bindings; 670 671 RepBindings (Hashtable bindings) { 672 this.bindings = bindings; 673 this.names = bindings.keys(); 674 } 675 676 public boolean hasMoreElements() { 677 return names.hasMoreElements(); 678 } 679 680 public boolean hasMore() throws NamingException { 681 return hasMoreElements(); 682 } 683 684 public Object nextElement() { 685 if(hasMoreElements()) 686 { 687 String name = (String )names.nextElement(); 688 return new Binding(name, bindings.get(name)); 689 } 690 else 691 return null; 692 } 693 public Object next() throws NamingException { 694 return nextElement(); 695 } 696 697 public void close() throws NamingException { 699 throw new 700 OperationNotSupportedException("close() not implemented"); 701 } 702 } 703 } 704 705 706 707 708 709 710 711 712 713 714 | Popular Tags |