1 7 15 16 package com.sun.corba.se.impl.javax.rmi.CORBA; 18 import java.rmi.RemoteException ; 19 import java.rmi.UnexpectedException ; 20 import java.rmi.MarshalException ; 21 22 import java.rmi.server.RMIClassLoader ; 23 24 import java.util.Hashtable ; 25 import java.util.Enumeration ; 26 import java.util.Properties ; 27 import java.util.Map ; 28 import java.util.WeakHashMap ; 29 30 import java.io.Serializable ; 31 import java.io.NotSerializableException ; 32 33 import java.lang.reflect.Constructor ; 34 35 import javax.rmi.CORBA.ValueHandler ; 36 import javax.rmi.CORBA.Tie ; 37 38 import java.security.AccessController ; 39 import java.security.PrivilegedAction ; 40 41 import java.rmi.MarshalException ; 42 import java.rmi.NoSuchObjectException ; 43 import java.rmi.AccessException ; 44 import java.rmi.Remote ; 45 import java.rmi.ServerError ; 46 import java.rmi.ServerException ; 47 import java.rmi.ServerRuntimeException ; 48 49 import javax.transaction.TransactionRequiredException ; 50 import javax.transaction.TransactionRolledbackException ; 51 import javax.transaction.InvalidTransactionException ; 52 53 import org.omg.CORBA.SystemException ; 54 import org.omg.CORBA.Any ; 55 import org.omg.CORBA.TypeCode ; 56 import org.omg.CORBA.COMM_FAILURE ; 57 import org.omg.CORBA.BAD_PARAM ; 58 import org.omg.CORBA.INV_OBJREF ; 59 import org.omg.CORBA.NO_PERMISSION ; 60 import org.omg.CORBA.MARSHAL ; 61 import org.omg.CORBA.OBJECT_NOT_EXIST ; 62 import org.omg.CORBA.TRANSACTION_REQUIRED ; 63 import org.omg.CORBA.TRANSACTION_ROLLEDBACK ; 64 import org.omg.CORBA.INVALID_TRANSACTION ; 65 import org.omg.CORBA.BAD_OPERATION ; 66 import org.omg.CORBA.ACTIVITY_REQUIRED ; 67 import org.omg.CORBA.ACTIVITY_COMPLETED ; 68 import org.omg.CORBA.INVALID_ACTIVITY ; 69 import org.omg.CORBA.CompletionStatus ; 70 import org.omg.CORBA.TCKind ; 71 import org.omg.CORBA.portable.UnknownException ; 72 import org.omg.CORBA.portable.InputStream ; 73 import org.omg.CORBA.portable.OutputStream ; 74 75 79 import com.sun.corba.se.pept.transport.ContactInfoList ; 80 import com.sun.corba.se.spi.orb.ORB; 81 import com.sun.corba.se.spi.orb.ORBVersionFactory; 82 import com.sun.corba.se.spi.protocol.CorbaClientDelegate; 83 import com.sun.corba.se.spi.transport.CorbaContactInfoList ; 84 import com.sun.corba.se.spi.protocol.LocalClientRequestDispatcher ; 85 import com.sun.corba.se.spi.copyobject.ReflectiveCopyException ; 86 import com.sun.corba.se.spi.copyobject.CopierManager ; 87 import com.sun.corba.se.spi.copyobject.ObjectCopierFactory ; 88 import com.sun.corba.se.spi.copyobject.ObjectCopier ; 89 import com.sun.corba.se.impl.io.ValueHandlerImpl; 90 import com.sun.corba.se.impl.orbutil.ORBConstants; 91 import com.sun.corba.se.impl.orbutil.ORBUtility; 92 import com.sun.corba.se.impl.logging.OMGSystemException; 93 import com.sun.corba.se.impl.util.Utility; 94 import com.sun.corba.se.impl.util.IdentityHashtable; 95 import com.sun.corba.se.impl.util.JDKBridge; 96 import com.sun.corba.se.impl.orbutil.ORBClassLoader; 97 import com.sun.corba.se.impl.logging.UtilSystemException; 98 import com.sun.corba.se.spi.logging.CORBALogDomains; 99 100 104 public class Util implements javax.rmi.CORBA.UtilDelegate 105 { 106 private static KeepAlive keepAlive = null; 108 109 private static IdentityHashtable exportedServants = new IdentityHashtable(); 111 112 private static ValueHandlerImpl valueHandlerSingleton = new ValueHandlerImpl(); 113 114 private UtilSystemException utilWrapper = UtilSystemException.get( 115 CORBALogDomains.RPC_ENCODING); 116 117 public static Util instance = null; 118 119 public Util() { 120 instance = this; 121 } 122 123 public void unregisterTargetsForORB(org.omg.CORBA.ORB orb) 126 { 127 for (Enumeration e = exportedServants.keys(); e.hasMoreElements(); ) 128 { 129 java.lang.Object key = e.nextElement(); 130 Remote target = (Remote )(key instanceof Tie ? ((Tie )key).getTarget() : key); 131 132 try { 135 if (orb == getTie(target).orb()) { 136 try { 137 unexportObject(target); 138 } catch( java.rmi.NoSuchObjectException ex ) { 139 } 142 } 143 } catch (BAD_OPERATION bad) { 144 145 } 146 } 147 } 148 149 154 public RemoteException mapSystemException(SystemException ex) 155 { 156 if (ex instanceof UnknownException ) { 157 Throwable orig = ((UnknownException )ex).originalEx; 158 if (orig instanceof Error ) { 159 return new ServerError ("Error occurred in server thread",(Error )orig); 160 } else if (orig instanceof RemoteException ) { 161 return new ServerException ("RemoteException occurred in server thread", 162 (Exception )orig); 163 } else if (orig instanceof RuntimeException ) { 164 throw (RuntimeException ) orig; 165 } 166 } 167 168 String name = ex.getClass().getName(); 170 String corbaName = name.substring(name.lastIndexOf('.')+1); 171 String status; 172 switch (ex.completed.value()) { 173 case CompletionStatus._COMPLETED_YES: 174 status = "Yes"; 175 break; 176 case CompletionStatus._COMPLETED_NO: 177 status = "No"; 178 break; 179 case CompletionStatus._COMPLETED_MAYBE: 180 default: 181 status = "Maybe"; 182 break; 183 } 184 185 String message = "CORBA " + corbaName + " " + ex.minor + " " + status; 186 187 if (ex instanceof COMM_FAILURE ) { 189 return new MarshalException (message, ex); 190 } else if (ex instanceof INV_OBJREF ) { 191 RemoteException newEx = new NoSuchObjectException (message); 192 newEx.detail = ex; 193 return newEx; 194 } else if (ex instanceof NO_PERMISSION ) { 195 return new AccessException (message, ex); 196 } else if (ex instanceof MARSHAL ) { 197 return new MarshalException (message, ex); 198 } else if (ex instanceof OBJECT_NOT_EXIST ) { 199 RemoteException newEx = new NoSuchObjectException (message); 200 newEx.detail = ex; 201 return newEx; 202 } else if (ex instanceof TRANSACTION_REQUIRED ) { 203 RemoteException newEx = new TransactionRequiredException (message); 204 newEx.detail = ex; 205 return newEx; 206 } else if (ex instanceof TRANSACTION_ROLLEDBACK ) { 207 RemoteException newEx = new TransactionRolledbackException (message); 208 newEx.detail = ex; 209 return newEx; 210 } else if (ex instanceof INVALID_TRANSACTION ) { 211 RemoteException newEx = new InvalidTransactionException (message); 212 newEx.detail = ex; 213 return newEx; 214 } else if (ex instanceof BAD_PARAM ) { 215 Exception inner = ex; 216 217 if (ex.minor == ORBConstants.LEGACY_SUN_NOT_SERIALIZABLE || 220 ex.minor == OMGSystemException.NOT_SERIALIZABLE) { 221 222 if (ex.getMessage() != null) 223 inner = new NotSerializableException (ex.getMessage()); 224 else 225 inner = new NotSerializableException (); 226 227 inner.initCause( ex ) ; 228 } 229 230 return new MarshalException (message,inner); 231 } else if (ex instanceof ACTIVITY_REQUIRED ) { 232 try { 233 Class cl = ORBClassLoader.loadClass( 234 "javax.activity.ActivityRequiredException"); 235 Class [] params = new Class [2]; 236 params[0] = java.lang.String .class; 237 params[1] = java.lang.Throwable .class; 238 Constructor cr = cl.getConstructor(params); 239 Object [] args = new Object [2]; 240 args[0] = message; 241 args[1] = ex; 242 return (RemoteException ) cr.newInstance(args); 243 } catch (Throwable e) { 244 utilWrapper.classNotFound( 245 e, "javax.activity.ActivityRequiredException"); 246 } 247 } else if (ex instanceof ACTIVITY_COMPLETED ) { 248 try { 249 Class cl = ORBClassLoader.loadClass( 250 "javax.activity.ActivityCompletedException"); 251 Class [] params = new Class [2]; 252 params[0] = java.lang.String .class; 253 params[1] = java.lang.Throwable .class; 254 Constructor cr = cl.getConstructor(params); 255 Object [] args = new Object [2]; 256 args[0] = message; 257 args[1] = ex; 258 return (RemoteException ) cr.newInstance(args); 259 } catch (Throwable e) { 260 utilWrapper.classNotFound( 261 e, "javax.activity.ActivityCompletedException"); 262 } 263 } else if (ex instanceof INVALID_ACTIVITY ) { 264 try { 265 Class cl = ORBClassLoader.loadClass( 266 "javax.activity.InvalidActivityException"); 267 Class [] params = new Class [2]; 268 params[0] = java.lang.String .class; 269 params[1] = java.lang.Throwable .class; 270 Constructor cr = cl.getConstructor(params); 271 Object [] args = new Object [2]; 272 args[0] = message; 273 args[1] = ex; 274 return (RemoteException ) cr.newInstance(args); 275 } catch (Throwable e) { 276 utilWrapper.classNotFound( 277 e, "javax.activity.InvalidActivityException"); 278 } 279 } 280 281 return new RemoteException (message, ex); 283 } 284 285 290 public void writeAny( org.omg.CORBA.portable.OutputStream out, 291 java.lang.Object obj) 292 { 293 org.omg.CORBA.ORB orb = out.orb(); 294 295 Any any = orb.create_any(); 297 298 java.lang.Object newObj = Utility.autoConnect(obj,orb,false); 300 301 if (newObj instanceof org.omg.CORBA.Object ) { 302 any.insert_Object((org.omg.CORBA.Object )newObj); 303 } else { 304 if (newObj == null) { 305 any.insert_Value(null, createTypeCodeForNull(orb)); 308 } else { 309 if (newObj instanceof Serializable ) { 310 TypeCode tc = createTypeCode((Serializable )newObj, any, orb); 313 if (tc == null) 314 any.insert_Value((Serializable )newObj); 315 else 316 any.insert_Value((Serializable )newObj, tc); 317 } else if (newObj instanceof Remote ) { 318 ORBUtility.throwNotSerializableForCorba(newObj.getClass().getName()); 319 } else { 320 ORBUtility.throwNotSerializableForCorba(newObj.getClass().getName()); 321 } 322 } 323 } 324 325 out.write_any(any); 326 } 327 328 343 private TypeCode createTypeCode(Serializable obj, 344 org.omg.CORBA.Any any, 345 org.omg.CORBA.ORB orb) { 346 347 if (any instanceof com.sun.corba.se.impl.corba.AnyImpl && 348 orb instanceof ORB) { 349 350 com.sun.corba.se.impl.corba.AnyImpl anyImpl 351 = (com.sun.corba.se.impl.corba.AnyImpl)any; 352 353 ORB ourORB = (ORB)orb; 354 355 return anyImpl.createTypeCodeForClass(obj.getClass(), ourORB); 356 357 } else 358 return null; 359 } 360 361 362 368 private TypeCode createTypeCodeForNull(org.omg.CORBA.ORB orb) 369 { 370 if (orb instanceof ORB) { 371 372 ORB ourORB = (ORB)orb; 373 374 if (!ORBVersionFactory.getFOREIGN().equals(ourORB.getORBVersion()) && 380 ORBVersionFactory.getNEWER().compareTo(ourORB.getORBVersion()) > 0) { 381 382 return orb.get_primitive_tc(TCKind.tk_value); 383 } 384 } 385 386 388 String abstractBaseID = "IDL:omg.org/CORBA/AbstractBase:1.0"; 390 391 return orb.create_abstract_interface_tc(abstractBaseID, ""); 392 } 393 394 399 public Object readAny(InputStream in) 400 { 401 Any any = in.read_any(); 402 if ( any.type().kind().value() == TCKind._tk_objref ) 403 return any.extract_Object (); 404 else 405 return any.extract_Value(); 406 } 407 408 417 public void writeRemoteObject(OutputStream out, java.lang.Object obj) 418 { 419 422 Object newObj = Utility.autoConnect(obj,out.orb(),false); 423 out.write_Object((org.omg.CORBA.Object )newObj); 424 } 425 426 435 public void writeAbstractObject( OutputStream out, java.lang.Object obj ) 436 { 437 440 Object newObj = Utility.autoConnect(obj,out.orb(),false); 441 ((org.omg.CORBA_2_3.portable.OutputStream )out).write_abstract_interface(newObj); 442 } 443 444 450 public void registerTarget(javax.rmi.CORBA.Tie tie, java.rmi.Remote target) 451 { 452 synchronized (exportedServants) { 453 if (lookupTie(target) == null) { 455 exportedServants.put(target,tie); 457 tie.setTarget(target); 458 459 if (keepAlive == null) { 461 keepAlive = (KeepAlive)AccessController.doPrivileged(new PrivilegedAction () { 464 public java.lang.Object run() { 465 return new KeepAlive(); 466 } 467 }); 468 keepAlive.start(); 469 } 470 } 471 } 472 } 473 474 479 public void unexportObject(java.rmi.Remote target) 480 throws java.rmi.NoSuchObjectException 481 { 482 synchronized (exportedServants) { 483 Tie cachedTie = lookupTie(target); 484 if (cachedTie != null) { 485 exportedServants.remove(target); 486 Utility.purgeStubForTie(cachedTie); 487 Utility.purgeTieAndServant(cachedTie); 488 try { 489 cleanUpTie(cachedTie); 490 } catch (BAD_OPERATION e) { 491 } catch (org.omg.CORBA.OBJ_ADAPTER e) { 493 } 496 497 if (exportedServants.isEmpty()) { 499 keepAlive.quit(); 500 keepAlive = null; 501 } 502 } else { 503 throw new java.rmi.NoSuchObjectException ("Tie not found" ); 504 } 505 } 506 } 507 508 protected void cleanUpTie(Tie cachedTie) 509 throws java.rmi.NoSuchObjectException 510 { 511 cachedTie.setTarget(null); 512 cachedTie.deactivate(); 513 } 514 515 519 public Tie getTie (Remote target) 520 { 521 synchronized (exportedServants) { 522 return lookupTie(target); 523 } 524 } 525 526 529 private static Tie lookupTie (Remote target) 530 { 531 Tie result = (Tie )exportedServants.get(target); 532 if (result == null && target instanceof Tie ) { 533 if (exportedServants.contains(target)) { 534 result = (Tie )target; 535 } 536 } 537 return result; 538 } 539 540 545 public ValueHandler createValueHandler() 546 { 547 return valueHandlerSingleton; 548 } 549 550 555 public String getCodebase(java.lang.Class clz) { 556 return RMIClassLoader.getClassAnnotation(clz); 557 } 558 559 569 public Class loadClass( String className, String remoteCodebase, 570 ClassLoader loader) throws ClassNotFoundException 571 { 572 return JDKBridge.loadClass(className,remoteCodebase,loader); 573 } 574 575 595 public boolean isLocal(javax.rmi.CORBA.Stub stub) throws RemoteException 596 { 597 boolean result = false ; 598 599 try { 600 org.omg.CORBA.portable.Delegate delegate = stub._get_delegate() ; 601 if (delegate instanceof CorbaClientDelegate) { 602 CorbaClientDelegate cdel = (CorbaClientDelegate)delegate ; 604 ContactInfoList cil = cdel.getContactInfoList() ; 605 if (cil instanceof CorbaContactInfoList) { 606 CorbaContactInfoList ccil = (CorbaContactInfoList)cil ; 607 LocalClientRequestDispatcher lcs = ccil.getLocalClientRequestDispatcher() ; 608 result = lcs.useLocalInvocation( null ) ; 609 } 610 } else { 611 result = delegate.is_local( stub ) ; 613 } 614 } catch (SystemException e) { 615 throw javax.rmi.CORBA.Util.mapSystemException(e); 616 } 617 618 return result ; 619 } 620 621 627 public RemoteException wrapException(Throwable orig) 628 { 629 if (orig instanceof SystemException ) { 630 return mapSystemException((SystemException )orig); 631 } 632 633 if (orig instanceof Error ) { 634 return new ServerError ("Error occurred in server thread",(Error )orig); 635 } else if (orig instanceof RemoteException ) { 636 return new ServerException ("RemoteException occurred in server thread", 637 (Exception )orig); 638 } else if (orig instanceof RuntimeException ) { 639 throw (RuntimeException ) orig; 640 } 641 642 if (orig instanceof Exception ) 643 return new UnexpectedException ( orig.toString(), (Exception )orig ); 644 else 645 return new UnexpectedException ( orig.toString()); 646 } 647 648 657 public Object [] copyObjects (Object [] obj, org.omg.CORBA.ORB orb) 658 throws RemoteException 659 { 660 if (obj == null) 661 throw new NullPointerException () ; 666 667 Class compType = obj.getClass().getComponentType() ; 668 if (Remote .class.isAssignableFrom( compType ) && !compType.isInterface()) { 669 Remote [] result = new Remote [obj.length] ; 673 System.arraycopy( (Object )obj, 0, (Object )result, 0, obj.length ) ; 674 return (Object [])copyObject( result, orb ) ; 675 } else 676 return (Object [])copyObject( obj, orb ) ; 677 } 678 679 687 public Object copyObject (Object obj, org.omg.CORBA.ORB orb) 688 throws RemoteException 689 { 690 if (orb instanceof ORB) { 691 ORB lorb = (ORB)orb ; 692 693 try { 694 try { 695 return lorb.peekInvocationInfo().getCopierFactory().make().copy( obj ) ; 698 } catch (java.util.EmptyStackException exc) { 699 CopierManager cm = lorb.getCopierManager() ; 704 ObjectCopier copier = cm.getDefaultObjectCopierFactory().make() ; 705 return copier.copy( obj ) ; 706 } 707 } catch (ReflectiveCopyException exc) { 708 RemoteException rexc = new RemoteException () ; 709 rexc.initCause( exc ) ; 710 throw rexc ; 711 } 712 } else { 713 org.omg.CORBA_2_3.portable.OutputStream out = 714 (org.omg.CORBA_2_3.portable.OutputStream )orb.create_output_stream(); 715 out.write_value((Serializable )obj); 716 org.omg.CORBA_2_3.portable.InputStream in = 717 (org.omg.CORBA_2_3.portable.InputStream )out.create_input_stream(); 718 return in.read_value(); 719 } 720 } 721 } 722 723 class KeepAlive extends Thread 724 { 725 boolean quit = false; 726 727 public KeepAlive () 728 { 729 setDaemon(false); 730 } 731 732 public synchronized void run () 733 { 734 while (!quit) { 735 try { 736 wait(); 737 } catch (InterruptedException e) {} 738 } 739 } 740 741 public synchronized void quit () 742 { 743 quit = true; 744 notifyAll(); 745 } 746 } 747 | Popular Tags |