1 7 8 16 17 package com.sun.corba.se.impl.util; 18 19 import org.omg.CORBA.SystemException ; 20 import org.omg.CORBA.CompletionStatus ; 21 import org.omg.CORBA.BAD_OPERATION ; 22 import org.omg.CORBA.BAD_INV_ORDER ; 23 import org.omg.CORBA.BAD_PARAM ; 24 import org.omg.CORBA.ORB ; 25 import org.omg.CORBA.Any ; 26 import org.omg.CORBA.TypeCode ; 27 import org.omg.CORBA.Principal ; 28 import org.omg.CORBA.portable.InputStream ; 29 import org.omg.CORBA.portable.OutputStream ; 30 import org.omg.CORBA.portable.BoxedValueHelper ; 31 import org.omg.CORBA.portable.ValueFactory ; 32 import org.omg.CORBA.portable.Streamable ; 33 import org.omg.CORBA.portable.Delegate ; 34 35 36 import java.util.Hashtable ; 37 import java.util.NoSuchElementException ; 38 39 import java.rmi.Remote ; 40 import java.rmi.NoSuchObjectException ; 41 import java.rmi.RemoteException ; 42 import java.rmi.server.RemoteStub ; 43 44 import javax.rmi.PortableRemoteObject ; 45 import javax.rmi.CORBA.Stub ; 46 import javax.rmi.CORBA.Tie ; 47 import javax.rmi.CORBA.Util ; 48 49 import java.io.Serializable ; 50 import java.io.File ; 51 import java.io.FileInputStream ; 52 53 import org.omg.PortableServer.POA ; 54 55 import com.sun.org.omg.SendingContext.CodeBase; 56 57 import com.sun.corba.se.spi.logging.CORBALogDomains ; 58 import com.sun.corba.se.spi.presentation.rmi.PresentationManager; 59 import com.sun.corba.se.spi.presentation.rmi.StubAdapter ; 60 61 import com.sun.corba.se.impl.logging.UtilSystemException ; 62 import com.sun.corba.se.impl.logging.OMGSystemException ; 63 64 67 public final class Utility { 68 69 public static final String STUB_PREFIX = "_"; 70 public static final String RMI_STUB_SUFFIX = "_Stub"; 71 public static final String DYNAMIC_STUB_SUFFIX = "_DynamicStub" ; 72 public static final String IDL_STUB_SUFFIX = "Stub"; 73 public static final String TIE_SUFIX = "_Tie"; 74 private static IdentityHashtable tieCache = new IdentityHashtable(); 75 private static IdentityHashtable tieToStubCache = new IdentityHashtable(); 76 private static IdentityHashtable stubToTieCache = new IdentityHashtable(); 77 private static Object CACHE_MISS = new Object (); 78 private static UtilSystemException wrapper = UtilSystemException.get( 79 CORBALogDomains.UTIL ) ; 80 private static OMGSystemException omgWrapper = OMGSystemException.get( 81 CORBALogDomains.UTIL ) ; 82 83 95 public static Object autoConnect(Object obj, ORB orb, boolean convertToStub) 96 { 97 if (obj == null) { 98 return obj; 99 } 100 101 if (StubAdapter.isStub(obj)) { 102 try { 103 StubAdapter.getDelegate(obj) ; 104 } catch (BAD_OPERATION okay) { 105 try { 106 StubAdapter.connect( obj, orb ) ; 107 } catch (RemoteException e) { 108 throw wrapper.objectNotConnected( e, 111 obj.getClass().getName() ) ; 112 } 113 } 114 115 return obj; 116 } 117 118 if (obj instanceof Remote ) { 119 Remote remoteObj = (Remote )obj; 120 Tie theTie = Util.getTie(remoteObj); 121 if (theTie != null) { 122 try { 123 theTie.orb(); 124 } catch (SystemException okay) { 125 theTie.orb(orb); 126 } 127 128 if (convertToStub) { 129 Object result = loadStub(theTie,null,null,true); 130 if (result != null) { 131 return result; 132 } else { 133 throw wrapper.couldNotLoadStub(obj.getClass().getName()); 134 } 135 } else { 136 return StubAdapter.activateTie( theTie ); 137 } 138 } else { 139 throw wrapper.objectNotExported( obj.getClass().getName() ) ; 143 } 144 } 145 146 148 return obj; 149 } 150 151 155 public static Tie loadTie(Remote obj) { 156 Tie result = null; 157 Class objClass = obj.getClass(); 158 159 161 synchronized (tieCache) { 162 163 Object it = tieCache.get(obj); 164 165 if (it == null) { 166 167 169 try { 170 171 173 result = loadTie(objClass); 174 175 180 while (result == null && 181 (objClass = objClass.getSuperclass()) != null && 182 objClass != PortableRemoteObject .class && 183 objClass != Object .class) { 184 185 result = loadTie(objClass); 186 } 187 } catch (Exception ex) { 188 wrapper.loadTieFailed( ex, objClass.getName() ) ; 189 } 190 191 193 if (result == null) { 194 195 197 tieCache.put(obj,CACHE_MISS); 198 199 } else { 200 201 203 tieCache.put(obj,result); 204 } 205 } else { 206 207 210 if (it != CACHE_MISS) { 211 try { 212 result = (Tie ) it.getClass().newInstance(); 213 } catch (Exception e) { 214 } 215 } 216 } 217 } 218 219 return result; 220 } 221 222 225 private static Tie loadTie(Class theClass) 226 { 227 return com.sun.corba.se.spi.orb.ORB.getStubFactoryFactory(). 228 getTie( theClass ) ; 229 } 230 231 235 public static void clearCaches() { 236 synchronized (tieToStubCache) { 237 tieToStubCache.clear(); 238 } 239 synchronized (tieCache) { 240 tieCache.clear(); 241 } 242 synchronized (stubToTieCache) { 243 stubToTieCache.clear(); 244 } 245 } 246 247 255 static Class loadClassOfType(String className, String remoteCodebase, 256 ClassLoader loader, Class expectedType, 257 ClassLoader expectedTypeClassLoader) throws ClassNotFoundException 258 { 259 Class loadedClass = null; 260 261 try { 262 try{ 264 if (!PackagePrefixChecker.hasOffendingPrefix( 271 PackagePrefixChecker.withoutPackagePrefix(className))){ 272 loadedClass = Util.loadClass( 273 PackagePrefixChecker.withoutPackagePrefix(className), 274 remoteCodebase, 275 loader); 276 } else { 277 loadedClass = Util.loadClass(className, remoteCodebase, 278 loader); 279 } 280 } catch (ClassNotFoundException cnfe) { 281 loadedClass = Util.loadClass(className, remoteCodebase, 282 loader); 283 } 284 if (expectedType == null) 285 return loadedClass; 286 } catch (ClassNotFoundException cnfe) { 287 if (expectedType == null) 288 throw cnfe; 289 } 290 291 if (loadedClass == null || !expectedType.isAssignableFrom(loadedClass)){ 299 if (expectedType.getClassLoader() != expectedTypeClassLoader) 300 throw new IllegalArgumentException ( 301 "expectedTypeClassLoader not class loader of " + 302 "expected Type."); 303 304 if (expectedTypeClassLoader != null) 305 loadedClass = expectedTypeClassLoader.loadClass(className); 306 else { 307 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 308 if (cl == null) 309 cl = ClassLoader.getSystemClassLoader(); 310 311 loadedClass = cl.loadClass(className); 312 } 313 } 314 315 return loadedClass; 316 } 317 318 326 public static Class loadClassForClass (String className, 327 String remoteCodebase, 328 ClassLoader loader, 329 Class relatedType, 330 ClassLoader relatedTypeClassLoader) 331 throws ClassNotFoundException 332 { 333 if (relatedType == null) 334 return Util.loadClass(className, remoteCodebase, loader); 335 336 Class loadedClass = null; 337 try { 338 loadedClass = Util.loadClass(className, remoteCodebase, loader); 339 } catch (ClassNotFoundException cnfe) { 340 if (relatedType.getClassLoader() == null) 341 throw cnfe; 342 } 343 344 if (loadedClass == null || 352 (loadedClass.getClassLoader() != null && 353 loadedClass.getClassLoader().loadClass(relatedType.getName()) != 354 relatedType)) 355 { 356 if (relatedType.getClassLoader() != relatedTypeClassLoader) 357 throw new IllegalArgumentException ( 358 "relatedTypeClassLoader not class loader of relatedType."); 359 360 if (relatedTypeClassLoader != null) 361 loadedClass = relatedTypeClassLoader.loadClass(className); 362 } 363 364 return loadedClass; 365 } 366 367 372 public static BoxedValueHelper getHelper(Class clazz, String codebase, 373 String repId) 374 { 375 String className = null; 376 if (clazz != null) { 377 className = clazz.getName(); 378 if (codebase == null) 379 codebase = Util.getCodebase(clazz); 380 } else { 381 if (repId != null) 382 className = RepositoryId.cache.getId(repId).getClassName(); 383 if (className == null) throw wrapper.unableLocateValueHelper( 385 CompletionStatus.COMPLETED_MAYBE); 386 } 387 388 try { 389 ClassLoader clazzLoader = 390 (clazz == null ? null : clazz.getClassLoader()); 391 Class helperClass = 392 loadClassForClass(className+"Helper", codebase, clazzLoader, 393 clazz, clazzLoader); 394 return (BoxedValueHelper )helperClass.newInstance(); 395 396 } catch (ClassNotFoundException cnfe) { 397 throw wrapper.unableLocateValueHelper( CompletionStatus.COMPLETED_MAYBE, 398 cnfe ); 399 } catch (IllegalAccessException iae) { 400 throw wrapper.unableLocateValueHelper( CompletionStatus.COMPLETED_MAYBE, 401 iae ); 402 } catch (InstantiationException ie) { 403 throw wrapper.unableLocateValueHelper( CompletionStatus.COMPLETED_MAYBE, 404 ie ); 405 } catch (ClassCastException cce) { 406 throw wrapper.unableLocateValueHelper( CompletionStatus.COMPLETED_MAYBE, 407 cce ); 408 } 409 } 410 411 416 public static ValueFactory getFactory(Class clazz, String codebase, 417 ORB orb, String repId) 418 { 419 ValueFactory factory = null; 420 if ((orb != null) && (repId != null)) { 421 try { 422 factory = ((org.omg.CORBA_2_3.ORB )orb).lookup_value_factory( 423 repId); 424 } catch (org.omg.CORBA.BAD_PARAM ex) { 425 } 427 } 428 429 String className = null; 430 if (clazz != null) { 431 className = clazz.getName(); 432 if (codebase == null) 433 codebase = Util.getCodebase(clazz); 434 } else { 435 if (repId != null) 436 className = RepositoryId.cache.getId(repId).getClassName(); 437 if (className == null) throw omgWrapper.unableLocateValueFactory( 439 CompletionStatus.COMPLETED_MAYBE); 440 } 441 442 if (factory != null && 445 (!factory.getClass().getName().equals(className+"DefaultFactory") || 446 (clazz == null && codebase == null))) 447 return factory; 448 449 try { 450 ClassLoader clazzLoader = 451 (clazz == null ? null : clazz.getClassLoader()); 452 Class factoryClass = 453 loadClassForClass(className+"DefaultFactory", codebase, 454 clazzLoader, clazz, clazzLoader); 455 return (ValueFactory )factoryClass.newInstance(); 456 457 } catch (ClassNotFoundException cnfe) { 458 throw omgWrapper.unableLocateValueFactory( 459 CompletionStatus.COMPLETED_MAYBE, cnfe); 460 } catch (IllegalAccessException iae) { 461 throw omgWrapper.unableLocateValueFactory( 462 CompletionStatus.COMPLETED_MAYBE, iae); 463 } catch (InstantiationException ie) { 464 throw omgWrapper.unableLocateValueFactory( 465 CompletionStatus.COMPLETED_MAYBE, ie); 466 } catch (ClassCastException cce) { 467 throw omgWrapper.unableLocateValueFactory( 468 CompletionStatus.COMPLETED_MAYBE, cce); 469 } 470 } 471 472 481 482 public static Remote loadStub(Tie tie, 483 PresentationManager.StubFactory stubFactory, 484 String remoteCodebase, 485 boolean onlyMostDerived) 486 { 487 StubEntry entry = null; 488 489 synchronized (tieToStubCache) { 491 Object cached = tieToStubCache.get(tie); 492 if (cached == null) { 493 entry = loadStubAndUpdateCache( 495 tie, stubFactory, remoteCodebase, onlyMostDerived); 496 } else { 497 if (cached != CACHE_MISS) { 500 entry = (StubEntry) cached; 502 503 if (!entry.mostDerived && onlyMostDerived) { 510 entry = loadStubAndUpdateCache(tie,null, 516 remoteCodebase,true); 517 } else if (stubFactory != null && 518 !StubAdapter.getTypeIds(entry.stub)[0].equals( 519 stubFactory.getTypeIds()[0]) ) 520 { 521 entry = loadStubAndUpdateCache(tie,null, 525 remoteCodebase,true); 526 527 if (entry == null) { 530 entry = loadStubAndUpdateCache(tie,stubFactory, 531 remoteCodebase,onlyMostDerived); 532 } 533 } else { 534 try { 536 Delegate stubDel = StubAdapter.getDelegate( 537 entry.stub ) ; 538 } catch (Exception e2) { 539 try { 541 Delegate del = StubAdapter.getDelegate( 542 tie ) ; 543 StubAdapter.setDelegate( entry.stub, 544 del ) ; 545 } catch (Exception e) {} 546 } 547 } 548 } 549 } 550 } 551 552 if (entry != null) { 553 return (Remote )entry.stub; 554 } else { 555 return null; 556 } 557 } 558 559 569 private static StubEntry loadStubAndUpdateCache ( 570 Tie tie, PresentationManager.StubFactory stubFactory, 571 String remoteCodebase, boolean onlyMostDerived) 572 { 573 org.omg.CORBA.Object stub = null; 574 StubEntry entry = null; 575 boolean tieIsStub = StubAdapter.isStub( tie ) ; 576 577 if (stubFactory != null) { 578 try { 579 stub = stubFactory.makeStub(); 580 } catch (Throwable e) { 581 wrapper.stubFactoryCouldNotMakeStub( e ) ; 582 if (e instanceof ThreadDeath ) { 583 throw (ThreadDeath ) e; 584 } 585 } 586 } else { 587 String [] ids = null; 588 if (tieIsStub) { 589 ids = StubAdapter.getTypeIds( tie ) ; 590 } else { 591 ids = ((org.omg.PortableServer.Servant )tie). 594 _all_interfaces( null, null ); 595 } 596 597 if (remoteCodebase == null) { 598 remoteCodebase = Util.getCodebase(tie.getClass()); 599 } 600 601 if (ids.length == 0) { 602 stub = new org.omg.stub.java.rmi._Remote_Stub (); 603 } else { 604 for (int i = 0; i < ids.length; i++) { 606 if (ids[i].length() == 0) { 607 stub = new org.omg.stub.java.rmi._Remote_Stub (); 608 break; 609 } 610 611 try { 612 PresentationManager.StubFactoryFactory stubFactoryFactory = 613 com.sun.corba.se.spi.orb.ORB.getStubFactoryFactory(); 614 RepositoryId rid = RepositoryId.cache.getId( ids[i] ) ; 615 String className = rid.getClassName() ; 616 boolean isIDLInterface = rid.isIDLType() ; 617 stubFactory = stubFactoryFactory.createStubFactory( 618 className, isIDLInterface, remoteCodebase, null, 619 tie.getClass().getClassLoader() ) ; 620 stub = stubFactory.makeStub(); 621 break; 622 } catch (Exception e) { 623 wrapper.errorInMakeStubFromRepositoryId( e ) ; 624 } 625 626 if (onlyMostDerived) 627 break; 628 } 629 } 630 } 631 632 if (stub == null) { 633 tieToStubCache.put(tie,CACHE_MISS); 635 } else { 636 if (tieIsStub) { 637 try { 638 Delegate del = StubAdapter.getDelegate( tie ) ; 639 StubAdapter.setDelegate( stub, del ) ; 640 } catch( Exception e1 ) { 641 646 synchronized (stubToTieCache) { 647 stubToTieCache.put(stub,tie); 648 } 649 } 650 } else { 651 try { 653 Delegate delegate = StubAdapter.getDelegate( tie ) ; 654 StubAdapter.setDelegate( stub, delegate ) ; 655 } catch( org.omg.CORBA.BAD_INV_ORDER bad) { 656 synchronized (stubToTieCache) { 657 stubToTieCache.put(stub,tie); 658 } 659 } catch( Exception e ) { 660 throw wrapper.noPoa( e ) ; 666 } 667 } 668 entry = new StubEntry(stub,onlyMostDerived); 670 tieToStubCache.put(tie,entry); 671 } 672 673 return entry; 674 } 675 676 681 public static Tie getAndForgetTie (org.omg.CORBA.Object stub) { 682 synchronized (stubToTieCache) { 683 return (Tie ) stubToTieCache.remove(stub); 684 } 685 } 686 687 690 public static void purgeStubForTie (Tie tie) { 691 StubEntry entry; 692 synchronized (tieToStubCache) { 693 entry = (StubEntry)tieToStubCache.remove(tie); 694 } 695 if (entry != null) { 696 synchronized (stubToTieCache) { 697 stubToTieCache.remove(entry.stub); 698 } 699 } 700 } 701 702 705 public static void purgeTieAndServant (Tie tie) { 706 synchronized (tieCache) { 707 Object target = tie.getTarget(); 708 if (target != null) 709 tieCache.remove(target); 710 } 711 } 712 713 716 public static String stubNameFromRepID (String repID) { 717 718 721 RepositoryId id = RepositoryId.cache.getId(repID); 722 String className = id.getClassName(); 723 724 if (id.isIDLType()) { 725 className = idlStubName(className); 726 } else { 727 className = stubName(className); 728 } 729 return className; 730 } 731 732 733 736 public static Remote loadStub (org.omg.CORBA.Object narrowFrom, 737 Class narrowTo) 738 { 739 Remote result = null; 740 741 try { 742 String codebase = null; 745 try { 746 Delegate delegate = StubAdapter.getDelegate( narrowFrom ) ; 750 codebase = ((org.omg.CORBA_2_3.portable.Delegate )delegate). 751 get_codebase(narrowFrom); 752 753 } catch (ClassCastException e) { 754 wrapper.classCastExceptionInLoadStub( e ) ; 755 } 756 757 PresentationManager.StubFactoryFactory sff = 758 com.sun.corba.se.spi.orb.ORB.getStubFactoryFactory() ; 759 PresentationManager.StubFactory sf = sff.createStubFactory( 760 narrowTo.getName(), false, codebase, narrowTo, 761 narrowTo.getClassLoader() ) ; 762 result = (Remote )sf.makeStub() ; 763 StubAdapter.setDelegate( result, 764 StubAdapter.getDelegate( narrowFrom ) ) ; 765 } catch (Exception err) { 766 wrapper.exceptionInLoadStub( err ) ; 767 } 768 769 return result; 770 } 771 772 776 public static Class loadStubClass(String repID, 777 String remoteCodebase, 778 Class expectedType) 779 throws ClassNotFoundException 780 { 781 785 if (repID.length() == 0) { 786 throw new ClassNotFoundException (); 787 } 788 789 String className = Utility.stubNameFromRepID(repID); 793 ClassLoader expectedTypeClassLoader = (expectedType == null ? null : 794 expectedType.getClassLoader()); 795 796 try { 797 return loadClassOfType(className, 798 remoteCodebase, 799 expectedTypeClassLoader, 800 expectedType, 801 expectedTypeClassLoader); 802 } catch (ClassNotFoundException e) { 803 return loadClassOfType(PackagePrefixChecker.packagePrefix() + className, 804 remoteCodebase, 805 expectedTypeClassLoader, 806 expectedType, 807 expectedTypeClassLoader); 808 } 809 } 810 811 814 public static String stubName (String className) 815 { 816 return stubName( className, false ) ; 817 } 818 819 public static String dynamicStubName( String className ) 820 { 821 return stubName( className, true ) ; 822 } 823 824 private static String stubName( String className, 825 boolean isDynamic ) 826 { 827 String name = stubNameForCompiler( className, isDynamic ) ; 828 if (PackagePrefixChecker.hasOffendingPrefix( name )) 829 name = PackagePrefixChecker.packagePrefix() + name ; 830 return name ; 831 } 832 833 public static String stubNameForCompiler (String className) 834 { 835 return stubNameForCompiler( className, false ) ; 836 } 837 838 private static String stubNameForCompiler( String className, 839 boolean isDynamic ) 840 { 841 int index = className.indexOf('$'); 842 if (index < 0) { 843 index = className.lastIndexOf('.'); 844 } 845 846 String suffix = isDynamic ? DYNAMIC_STUB_SUFFIX : 847 RMI_STUB_SUFFIX ; 848 849 if (index > 0) { 850 return className.substring(0,index+1) + STUB_PREFIX + 851 className.substring(index+1) + suffix; 852 } else { 853 return STUB_PREFIX + className + suffix; 854 } 855 } 856 857 860 public static String tieName (String className) 861 { 862 return 863 PackagePrefixChecker.hasOffendingPrefix(tieNameForCompiler(className)) ? 864 PackagePrefixChecker.packagePrefix() + tieNameForCompiler(className) : 865 tieNameForCompiler(className); 866 } 867 868 public static String tieNameForCompiler (String className) 869 { 870 int index = className.indexOf('$'); 871 if (index < 0) { 872 index = className.lastIndexOf('.'); 873 } 874 if (index > 0) { 875 return className.substring(0,index+1) + 876 STUB_PREFIX + 877 className.substring(index+1) + 878 TIE_SUFIX; 879 } else { 880 return STUB_PREFIX + 881 className + 882 TIE_SUFIX; 883 } 884 } 885 886 889 public static void throwNotSerializableForCorba(String className) { 890 throw omgWrapper.notSerializable( CompletionStatus.COMPLETED_MAYBE, 891 className ) ; 892 } 893 894 897 public static String idlStubName(String className) 898 { 899 String result = null; 900 int index = className.lastIndexOf('.'); 901 if (index > 0) { 902 result = className.substring(0,index+1) + 903 STUB_PREFIX + 904 className.substring(index+1) + 905 IDL_STUB_SUFFIX; 906 } else { 907 result = STUB_PREFIX + 908 className + 909 IDL_STUB_SUFFIX; 910 } 911 return result; 912 } 913 914 public static void printStackTrace() 915 { 916 Throwable thr = new Throwable ( "Printing stack trace:" ) ; 917 thr.fillInStackTrace() ; 918 thr.printStackTrace() ; 919 } 920 921 927 public static Object readObjectAndNarrow(InputStream in, 928 Class narrowTo) 929 throws ClassCastException 930 { 931 Object result = in.read_Object(); 932 if (result != null) 933 return PortableRemoteObject.narrow(result, narrowTo); 934 else 935 return null; 936 } 937 938 944 public static Object readAbstractAndNarrow( 945 org.omg.CORBA_2_3.portable.InputStream in, Class narrowTo) 946 throws ClassCastException 947 { 948 Object result = in.read_abstract_interface(); 949 if (result != null) 950 return PortableRemoteObject.narrow(result, narrowTo); 951 else 952 return null; 953 } 954 955 956 958 static int hexOf( char x ) 959 { 960 int val; 961 962 val = x - '0'; 963 if (val >=0 && val <= 9) 964 return val; 965 966 val = (x - 'a') + 10; 967 if (val >= 10 && val <= 15) 968 return val; 969 970 val = (x - 'A') + 10; 971 if (val >= 10 && val <= 15) 972 return val; 973 974 throw wrapper.badHexDigit() ; 975 } 976 } 977 978 class StubEntry { 979 org.omg.CORBA.Object stub; 980 boolean mostDerived; 981 982 StubEntry(org.omg.CORBA.Object stub, boolean mostDerived) { 983 this.stub = stub; 984 this.mostDerived = mostDerived; 985 } 986 } 987 | Popular Tags |