1 23 package com.sun.enterprise.naming; 24 25 import java.util.*; 26 import java.io.*; 27 import javax.naming.*; 28 import org.omg.CORBA.ORB ; 29 import com.sun.enterprise.util.ORBManager; 30 import com.sun.enterprise.Switch; 31 import com.sun.enterprise.naming.java.javaURLContext; 32 import javax.rmi.PortableRemoteObject ; 33 import java.rmi.RemoteException ; 34 import org.omg.CosNaming.NamingContext ; 35 import org.omg.CosNaming.NameComponent ; 36 import org.omg.CosNaming.NamingContextHelper ; 37 import com.sun.enterprise.server.ondemand.entry.*; 38 39 import java.util.logging.*; 40 import com.sun.logging.*; 41 42 43 51 public class SerialContext implements Context { 52 53 static Logger _logger=LogDomains.getLogger(LogDomains.JNDI_LOGGER); 54 55 static private NameParser myParser = new SerialNameParser(); 56 57 private Hashtable myEnv; 58 private SerialContextProvider provider; 59 private static Hashtable providerCache = new Hashtable(); 60 private String myName; 61 private javaURLContext javaUrlContext = null; 62 63 private final static String JAVA_URL = "java:"; 64 private InitialContext cosContext; 65 private static Boolean threadlock = new Boolean (true); 66 67 private static ThreadLocal stickyContext = new ThreadLocal (); 68 69 86 public static void setSticky(ThreadLocalIC var) { 87 stickyContext.set(var); 88 } 89 90 public static ThreadLocalIC getSticky() { 91 return (ThreadLocalIC) stickyContext.get(); 92 } 93 94 98 public static Context getStickyContext() { 99 return getSticky().getStickyContext(); 100 } 101 102 103 107 private SerialContextProvider narrowProvider(org.omg.CORBA.Object ref) 108 throws Exception { 109 110 NamingContext nctx = NamingContextHelper.narrow(ref); 111 NameComponent [] path = 112 { new NameComponent ("SerialContextProvider", "") }; 113 synchronized (threadlock) { 114 provider = (SerialContextProvider) 115 PortableRemoteObject.narrow(nctx.resolve(path), 116 SerialContextProvider.class); 117 } 118 return provider; 119 } 120 121 122 private SerialContextProvider getProvider() 123 throws NamingException { 124 125 try { 126 if (provider == null) { 127 if (Switch.getSwitch().getContainerType() == 128 Switch.EJBWEB_CONTAINER) { 129 if (_logger.isLoggable(Level.FINE)) { 130 _logger.fine("lookup call inside the server VM..."); 131 } 132 provider = Switch.getSwitch().getProviderManager().getLocalProvider(); 133 134 } else { 135 if (_logger.isLoggable(Level.FINE)) { 136 _logger.fine("lookup call outside the server VM..."); 137 } 138 139 141 ORB orb = (ORB ) myEnv.get(ORBManager.JNDI_CORBA_ORB_PROPERTY); 143 144 if ( orb == null ) { 145 orb = ORBManager.getORB(); 146 } 147 148 if (_logger.isLoggable(Level.FINE)) 149 print(orb); 150 151 org.omg.CORBA.Object ref = null; 152 if (myEnv.get("com.sun.appserv.ee.iiop.endpointslist") != null) { 155 156 ref = orb.string_to_object((String )(myEnv.get( 157 "com.sun.appserv.ee.iiop.endpointslist"))); 158 provider = narrowProvider(ref); 159 160 } else { 161 provider = (SerialContextProvider) providerCache.get(orb); 162 if (provider == null) { 163 ref = orb.resolve_initial_references("NameService"); 164 provider = narrowProvider(ref); 165 providerCache.put(orb, provider); 166 } 167 } 168 } 169 } 170 } catch (Exception ex) { 171 setSticky(null); 172 CommunicationException ce = new CommunicationException 173 ("Can't find SerialContextProvider"); 174 ce.initCause(ex); 175 throw ce; 176 } 177 178 return provider; 179 } 180 181 public void print(ORB orb) { 183 _logger.fine("SerialContext ==> SerialContext instance created : " + this); 184 185 if (orb != null) { 186 _logger.fine("SerialContext ==> ORB instance : " + orb); 187 188 String host = ((com.sun.corba.ee.impl.orb.ORBImpl)orb). 189 getORBData().getORBInitialHost(); 190 int port = ((com.sun.corba.ee.impl.orb.ORBImpl)orb). 191 getORBData().getORBInitialPort(); 192 193 _logger.fine("SerialContext ==> ORB HOST : " + host + 194 ", ORB PORT : " + port); 195 } else _logger.fine("orb is null"); 196 } 197 198 202 public SerialContext(Hashtable environment) throws NamingException { 203 204 myEnv = (environment != null) 205 ? (Hashtable)(environment.clone()) 206 : null; 207 208 212 this.myName = ""; 213 if (_logger.isLoggable(Level.FINE)) 214 _logger.fine("SerialContext ==> SerialContext instance created : " + this); 215 javaUrlContext = new javaURLContext(myEnv, this); 216 } 217 218 222 public SerialContext(String name, Hashtable env) throws NamingException { 223 this(env); 224 this.myName = name; 225 } 226 227 228 232 public String getNameInNamespace() throws NamingException { 233 return myName; 234 } 235 236 239 private boolean isjavaURL(String name) { 240 241 if (name.startsWith(JAVA_URL)) { 242 return true; 243 } else return false; 244 } 245 246 249 public void generateEntryContext(Object context) { 250 ServerEntryHelper.generateJndiEntryContext((String ) context); 251 } 252 253 257 private void resetSticky() { 258 if (getSticky() != null) { 259 getSticky().decrementCount(); 260 261 if (getSticky().getStickyCount() == 0) { 262 setSticky(null); 263 } 264 } 265 } 266 267 273 public Object lookup(String name) throws NamingException { 274 275 generateEntryContext(name); 277 278 285 if (myEnv.get("com.sun.appserv.ee.iiop.endpointslist") != null) { 286 if (getSticky() == null) { 287 ThreadLocalIC threadLocal = new ThreadLocalIC(this,1); 288 setSticky(threadLocal); 289 } else getSticky().incrementCount(); 290 } 291 292 if (_logger.isLoggable(Level.FINE)) 293 _logger.fine("SerialContext ==> doing lookup with " + this); 294 if (name.equals("")) { 295 resetSticky(); 296 return (new SerialContext(myName, myEnv)); 299 } 300 name = getRelativeName(name); 301 if (_logger.isLoggable(Level.FINE)) 302 _logger.fine("SerialContext ==> looking up : " + name); 303 304 try { 305 if (isjavaURL(name)) { 306 resetSticky(); 307 return javaUrlContext.lookup(name); 308 } else { 309 Object obj = getProvider().lookup(name); 310 if(obj instanceof Context) { 311 resetSticky(); 312 return new SerialContext(name, myEnv); 313 } 314 Object retObj = 315 javax.naming.spi.NamingManager.getObjectInstance(obj, 316 new CompositeName(name), 317 null, myEnv); 318 resetSticky(); 319 320 return retObj; 321 } 322 } catch (NamingException nnfe) { 323 setSticky(null); 324 throw nnfe; 325 } catch (Exception ex) { 326 setSticky(null); 327 _logger.log(Level.SEVERE, 328 "enterprise_naming.serialctx_communication_exception", 329 ex); 330 if (ex instanceof java.rmi.MarshalException && 333 ex.getCause() instanceof org.omg.CORBA.COMM_FAILURE ) { 334 provider = null; 335 _logger.fine("Resetting provider to NULL. Will get new obj ref for provider since previous obj ref was stale..."); 336 return lookup(name); 337 } else { 338 CommunicationException ce = 339 new CommunicationException("serial context communication ex"); 340 ce.initCause(ex); 341 throw ce; 342 } 343 } 344 345 } 346 347 354 public Object lookup(Name name) throws NamingException { 355 return lookup(name.toString()); 357 } 358 359 365 public void bind(String name, Object obj) throws NamingException { 366 367 name = getRelativeName(name); 368 if (isjavaURL(name)) { 369 javaUrlContext.bind(name, obj); 370 } else { 371 try { 372 getProvider().bind(name, obj); 373 } catch (RemoteException ex) { 374 throw new CommunicationException(ex.toString()); 375 } 376 } 377 } 378 379 385 public void bind(Name name, Object obj) throws NamingException { 386 bind(name.toString(), obj); 388 } 389 390 396 public void rebind(String name, Object obj) throws NamingException { 397 398 name = getRelativeName(name); 399 if (isjavaURL(name)) { 400 javaUrlContext.rebind(name, obj); 401 } else { 402 try { 403 getProvider().rebind(name, obj); 404 } catch (RemoteException ex) { 405 throw new CommunicationException(ex.toString()); 406 } 407 } 408 } 409 410 416 public void rebind(Name name, Object obj) throws NamingException { 417 rebind(name.toString(), obj); 419 } 420 421 426 public void unbind(String name) throws NamingException { 427 name = getRelativeName(name); 428 if (isjavaURL(name)) { 429 javaUrlContext.unbind(name); 430 } else { 431 try { 432 getProvider().unbind(name); 433 } catch (RemoteException ex) { 434 throw new CommunicationException(ex.toString()); 435 } 436 } 437 } 438 439 444 public void unbind(Name name) throws NamingException { 445 unbind(name.toString()); 447 } 448 449 455 public void rename(String oldname, String newname) throws NamingException { 456 oldname = getRelativeName(oldname); 457 newname = getRelativeName(newname); 458 if (isjavaURL(oldname)) { 459 javaUrlContext.rename(oldname, newname); 460 } else { 461 try { 462 getProvider().rename(oldname, newname); 463 } catch (RemoteException ex) { 464 throw new CommunicationException(ex.toString()); 465 } 466 } 467 } 468 469 475 public void rename(Name oldname, Name newname) 476 throws NamingException { 477 rename(oldname.toString(), newname.toString()); 479 } 480 481 487 public NamingEnumeration list(String name) throws NamingException { 488 if (name.equals("")) { 489 try { 491 Hashtable bindings = getProvider().list(myName); 492 return new RepNames(bindings); 493 } catch (RemoteException ex) { 494 throw new CommunicationException(ex.toString()); 495 } 496 } 497 498 name = getRelativeName(name); 499 if (isjavaURL(name)) { 500 return javaUrlContext.list(name); 501 } else { 502 Object target = lookup(name); 504 if (target instanceof Context) { 505 return ((Context)target).list(""); 506 } 507 throw new NotContextException(name + " cannot be listed"); 508 } 509 } 510 511 517 public NamingEnumeration list(Name name) 518 throws NamingException { 519 return list(name.toString()); 521 } 522 523 529 public NamingEnumeration listBindings(String name) 530 throws NamingException { 531 if (name.equals("")) { 532 try { 534 Hashtable bindings = getProvider().list(myName); 535 return new RepBindings(bindings); 536 } catch (RemoteException ex) { 537 CommunicationException ce = 538 new CommunicationException(ex.toString()); 539 ce.initCause(ex); 540 throw ce; 541 } 542 } 543 544 name = getRelativeName(name); 545 if (isjavaURL(name)) { 546 return javaUrlContext.listBindings(name); 547 } else { 548 Object target = lookup(name); 550 if (target instanceof Context) { 551 return ((Context)target).listBindings(""); 552 } 553 throw new NotContextException(name + " cannot be listed"); 554 } 555 } 556 557 563 public NamingEnumeration listBindings(Name name) 564 throws NamingException { 565 return listBindings(name.toString()); 567 } 568 569 574 public void destroySubcontext(String name) throws NamingException { 575 name = getRelativeName (name); 576 if (isjavaURL(name)) { 577 javaUrlContext.destroySubcontext(name); 578 } else { 579 try { 580 getProvider().destroySubcontext(name); 581 } catch(RemoteException e) { 582 CommunicationException ce = 583 new CommunicationException(e.toString()); 584 ce.initCause(e); 585 throw ce; 586 } 587 } 588 } 589 590 595 public void destroySubcontext(Name name) throws NamingException { 596 destroySubcontext(name.toString()); 598 } 599 600 606 public Context createSubcontext(String name) 607 throws NamingException { 608 Context c = null; 609 name = getRelativeName (name); 610 if (isjavaURL(name)) { 611 return javaUrlContext.createSubcontext(name); 612 } else { 613 try { 614 c = getProvider().createSubcontext(name); 615 619 if (c instanceof Context){ 620 c = new SerialContext (name, myEnv); 621 } 622 } catch(RemoteException e) { 623 CommunicationException ce = 624 new CommunicationException(e.toString()); 625 ce.initCause(e); 626 throw ce; 627 } 628 return c; 629 } 630 } 631 632 638 public Context createSubcontext(Name name) throws NamingException { 639 return createSubcontext(name.toString()); 641 } 642 643 649 public Object lookupLink(String name) throws NamingException { 650 name = getRelativeName(name); 651 if (isjavaURL(name)) { 652 return javaUrlContext.lookupLink(name); 653 } else { 654 return lookup(name); 656 } 657 } 658 659 665 public Object lookupLink(Name name) throws NamingException { 666 return lookupLink(name.toString()); 668 } 669 670 677 public NameParser getNameParser(String name) 678 throws NamingException { 679 return myParser; 680 } 681 682 689 public NameParser getNameParser(Name name) throws NamingException { 690 return getNameParser(name.toString()); 692 } 693 694 public String composeName(String name, String prefix) 695 throws NamingException { 696 Name result = composeName(new CompositeName(name), 697 new CompositeName(prefix)); 698 return result.toString(); 699 } 700 701 public Name composeName(Name name, Name prefix) 702 throws NamingException { 703 Name result = (Name)(prefix.clone()); 704 result.addAll(name); 705 return result; 706 } 707 708 712 public Object addToEnvironment(String propName, Object propVal) 713 throws NamingException { 714 if (myEnv == null) { 715 myEnv = new Hashtable(5, 0.75f); 716 } 717 return myEnv.put(propName, propVal); 718 } 719 720 724 public Object removeFromEnvironment(String propName) 725 throws NamingException { 726 if (myEnv == null) { 727 return null; 728 } 729 return myEnv.remove(propName); 730 } 731 732 736 public Hashtable getEnvironment() throws NamingException { 737 if (myEnv == null) { 738 myEnv = new Hashtable(3, 0.75f); 740 } 741 return myEnv; 742 } 743 744 749 public void close() throws NamingException { 750 myEnv = null; 751 } 752 753 private String getRelativeName(String name) { 754 if(!myName.equals("")) { 755 name = myName + "/" + name; 756 } 757 return name; 758 } 759 760 class RepNames implements NamingEnumeration { 762 Hashtable bindings; 763 Enumeration names; 764 765 RepNames (Hashtable bindings) { 766 this.bindings = bindings; 767 this.names = bindings.keys(); 768 } 769 770 public boolean hasMoreElements() { 771 return names.hasMoreElements(); 772 } 773 774 public boolean hasMore() throws NamingException { 775 return hasMoreElements(); 776 } 777 778 public Object nextElement() { 779 if(names.hasMoreElements()) 780 { 781 String name = (String )names.nextElement(); 782 String className = bindings.get(name).getClass().getName(); 783 return new NameClassPair(name, className); 784 } 785 else 786 return null; 787 } 788 789 public Object next() throws NamingException { 790 return nextElement(); 791 } 792 793 public void close() throws NamingException { 795 throw new OperationNotSupportedException("close() not implemented"); 796 } 797 } 798 799 class RepBindings implements NamingEnumeration { 801 Enumeration names; 802 Hashtable bindings; 803 804 RepBindings (Hashtable bindings) { 805 this.bindings = bindings; 806 this.names = bindings.keys(); 807 } 808 809 public boolean hasMoreElements() { 810 return names.hasMoreElements(); 811 } 812 813 public boolean hasMore() throws NamingException { 814 return hasMoreElements(); 815 } 816 817 public Object nextElement() { 818 if(hasMoreElements()) 819 { 820 String name = (String )names.nextElement(); 821 return new Binding(name, bindings.get(name)); 822 } 823 else 824 return null; 825 } 826 827 public Object next() throws NamingException { 828 return nextElement(); 829 } 830 831 public void close() throws NamingException { 833 throw new OperationNotSupportedException("close() not implemented"); 834 } 835 } 836 837 838 845 class ThreadLocalIC { 846 847 Context ctx; 848 int count = 0; 849 850 public ThreadLocalIC(Context ctx, int count) { 851 this.ctx = ctx; 852 this.count = count; 853 } 854 855 public void setStickyContext(Context ctx) { 856 ctx = ctx; 857 } 858 859 public Context getStickyContext() { 860 return ctx; 861 } 862 863 public void setStickyCount(int count) { 864 count = count; 865 866 } 867 868 public int getStickyCount() { 869 return count; 870 } 871 872 public void incrementCount() { 873 count++; 874 } 875 876 public void decrementCount() { 877 count--; 878 } 879 } 880 }; 881 882 883 | Popular Tags |