1. /*
  2. * @(#)Util.java 1.45 04/06/21
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. /*
  8. * Licensed Materials - Property of IBM
  9. * RMI-IIOP v1.0
  10. * Copyright IBM Corp. 1998 1999 All Rights Reserved
  11. *
  12. * US Government Users Restricted Rights - Use, duplication or
  13. * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  14. */
  15. package com.sun.corba.se.impl.javax.rmi.CORBA; // Util (sed marker, don't remove!)
  16. import java.rmi.RemoteException;
  17. import java.rmi.UnexpectedException;
  18. import java.rmi.MarshalException;
  19. import java.rmi.server.RMIClassLoader;
  20. import java.util.Hashtable;
  21. import java.util.Enumeration;
  22. import java.util.Properties;
  23. import java.util.Map;
  24. import java.util.WeakHashMap;
  25. import java.io.Serializable;
  26. import java.io.NotSerializableException;
  27. import java.lang.reflect.Constructor;
  28. import javax.rmi.CORBA.ValueHandler;
  29. import javax.rmi.CORBA.Tie;
  30. import java.security.AccessController;
  31. import java.security.PrivilegedAction;
  32. import java.rmi.MarshalException;
  33. import java.rmi.NoSuchObjectException;
  34. import java.rmi.AccessException;
  35. import java.rmi.Remote;
  36. import java.rmi.ServerError;
  37. import java.rmi.ServerException;
  38. import java.rmi.ServerRuntimeException;
  39. import javax.transaction.TransactionRequiredException;
  40. import javax.transaction.TransactionRolledbackException;
  41. import javax.transaction.InvalidTransactionException;
  42. import org.omg.CORBA.SystemException;
  43. import org.omg.CORBA.Any;
  44. import org.omg.CORBA.TypeCode;
  45. import org.omg.CORBA.COMM_FAILURE;
  46. import org.omg.CORBA.BAD_PARAM;
  47. import org.omg.CORBA.INV_OBJREF;
  48. import org.omg.CORBA.NO_PERMISSION;
  49. import org.omg.CORBA.MARSHAL;
  50. import org.omg.CORBA.OBJECT_NOT_EXIST;
  51. import org.omg.CORBA.TRANSACTION_REQUIRED;
  52. import org.omg.CORBA.TRANSACTION_ROLLEDBACK;
  53. import org.omg.CORBA.INVALID_TRANSACTION;
  54. import org.omg.CORBA.BAD_OPERATION;
  55. import org.omg.CORBA.ACTIVITY_REQUIRED;
  56. import org.omg.CORBA.ACTIVITY_COMPLETED;
  57. import org.omg.CORBA.INVALID_ACTIVITY;
  58. import org.omg.CORBA.CompletionStatus;
  59. import org.omg.CORBA.TCKind;
  60. import org.omg.CORBA.portable.UnknownException;
  61. import org.omg.CORBA.portable.InputStream;
  62. import org.omg.CORBA.portable.OutputStream;
  63. // This class must be able to function with non-Sun ORBs.
  64. // This means that any of the following com.sun.corba classes
  65. // must only occur in contexts that also handle the non-Sun case.
  66. import com.sun.corba.se.pept.transport.ContactInfoList ;
  67. import com.sun.corba.se.spi.orb.ORB;
  68. import com.sun.corba.se.spi.orb.ORBVersionFactory;
  69. import com.sun.corba.se.spi.protocol.CorbaClientDelegate;
  70. import com.sun.corba.se.spi.transport.CorbaContactInfoList ;
  71. import com.sun.corba.se.spi.protocol.LocalClientRequestDispatcher ;
  72. import com.sun.corba.se.spi.copyobject.ReflectiveCopyException ;
  73. import com.sun.corba.se.spi.copyobject.CopierManager ;
  74. import com.sun.corba.se.spi.copyobject.ObjectCopierFactory ;
  75. import com.sun.corba.se.spi.copyobject.ObjectCopier ;
  76. import com.sun.corba.se.impl.io.ValueHandlerImpl;
  77. import com.sun.corba.se.impl.orbutil.ORBConstants;
  78. import com.sun.corba.se.impl.orbutil.ORBUtility;
  79. import com.sun.corba.se.impl.logging.OMGSystemException;
  80. import com.sun.corba.se.impl.util.Utility;
  81. import com.sun.corba.se.impl.util.IdentityHashtable;
  82. import com.sun.corba.se.impl.util.JDKBridge;
  83. import com.sun.corba.se.impl.orbutil.ORBClassLoader;
  84. import com.sun.corba.se.impl.logging.UtilSystemException;
  85. import com.sun.corba.se.spi.logging.CORBALogDomains;
  86. /**
  87. * Provides utility methods that can be used by stubs and ties to
  88. * perform common operations.
  89. */
  90. public class Util implements javax.rmi.CORBA.UtilDelegate
  91. {
  92. // Runs as long as there are exportedServants
  93. private static KeepAlive keepAlive = null;
  94. // Maps targets to ties.
  95. private static IdentityHashtable exportedServants = new IdentityHashtable();
  96. private static ValueHandlerImpl valueHandlerSingleton = new ValueHandlerImpl();
  97. private UtilSystemException utilWrapper = UtilSystemException.get(
  98. CORBALogDomains.RPC_ENCODING);
  99. public static Util instance = null;
  100. public Util() {
  101. instance = this;
  102. }
  103. // Used by TOAFactory.shutdown to unexport all targets for this
  104. // particular ORB. This happens during ORB shutdown.
  105. public void unregisterTargetsForORB(org.omg.CORBA.ORB orb)
  106. {
  107. for (Enumeration e = exportedServants.keys(); e.hasMoreElements(); )
  108. {
  109. java.lang.Object key = e.nextElement();
  110. Remote target = (Remote)(key instanceof Tie ? ((Tie)key).getTarget() : key);
  111. // Bug 4476347: BAD_OPERATION is thrown if the ties delegate isn't set.
  112. // We can ignore this because it means the tie is not connected to an ORB.
  113. try {
  114. if (orb == getTie(target).orb()) {
  115. try {
  116. unexportObject(target);
  117. } catch( java.rmi.NoSuchObjectException ex ) {
  118. // We neglect this exception if at all if it is
  119. // raised. It is not harmful.
  120. }
  121. }
  122. } catch (BAD_OPERATION bad) {
  123. /* Ignore */
  124. }
  125. }
  126. }
  127. /**
  128. * Maps a SystemException to a RemoteException.
  129. * @param ex the SystemException to map.
  130. * @return the mapped exception.
  131. */
  132. public RemoteException mapSystemException(SystemException ex)
  133. {
  134. if (ex instanceof UnknownException) {
  135. Throwable orig = ((UnknownException)ex).originalEx;
  136. if (orig instanceof Error) {
  137. return new ServerError("Error occurred in server thread",(Error)orig);
  138. } else if (orig instanceof RemoteException) {
  139. return new ServerException("RemoteException occurred in server thread",
  140. (Exception)orig);
  141. } else if (orig instanceof RuntimeException) {
  142. throw (RuntimeException) orig;
  143. }
  144. }
  145. // Build the message string...
  146. String name = ex.getClass().getName();
  147. String corbaName = name.substring(name.lastIndexOf('.')+1);
  148. String status;
  149. switch (ex.completed.value()) {
  150. case CompletionStatus._COMPLETED_YES:
  151. status = "Yes";
  152. break;
  153. case CompletionStatus._COMPLETED_NO:
  154. status = "No";
  155. break;
  156. case CompletionStatus._COMPLETED_MAYBE:
  157. default:
  158. status = "Maybe";
  159. break;
  160. }
  161. String message = "CORBA " + corbaName + " " + ex.minor + " " + status;
  162. // Now map to the correct RemoteException type...
  163. if (ex instanceof COMM_FAILURE) {
  164. return new MarshalException(message, ex);
  165. } else if (ex instanceof INV_OBJREF) {
  166. RemoteException newEx = new NoSuchObjectException(message);
  167. newEx.detail = ex;
  168. return newEx;
  169. } else if (ex instanceof NO_PERMISSION) {
  170. return new AccessException(message, ex);
  171. } else if (ex instanceof MARSHAL) {
  172. return new MarshalException(message, ex);
  173. } else if (ex instanceof OBJECT_NOT_EXIST) {
  174. RemoteException newEx = new NoSuchObjectException(message);
  175. newEx.detail = ex;
  176. return newEx;
  177. } else if (ex instanceof TRANSACTION_REQUIRED) {
  178. RemoteException newEx = new TransactionRequiredException(message);
  179. newEx.detail = ex;
  180. return newEx;
  181. } else if (ex instanceof TRANSACTION_ROLLEDBACK) {
  182. RemoteException newEx = new TransactionRolledbackException(message);
  183. newEx.detail = ex;
  184. return newEx;
  185. } else if (ex instanceof INVALID_TRANSACTION) {
  186. RemoteException newEx = new InvalidTransactionException(message);
  187. newEx.detail = ex;
  188. return newEx;
  189. } else if (ex instanceof BAD_PARAM) {
  190. Exception inner = ex;
  191. // Pre-Merlin Sun ORBs used the incorrect minor code for
  192. // this case. See Java to IDL ptc-00-01-08 1.4.8.
  193. if (ex.minor == ORBConstants.LEGACY_SUN_NOT_SERIALIZABLE ||
  194. ex.minor == OMGSystemException.NOT_SERIALIZABLE) {
  195. if (ex.getMessage() != null)
  196. inner = new NotSerializableException(ex.getMessage());
  197. else
  198. inner = new NotSerializableException();
  199. inner.initCause( ex ) ;
  200. }
  201. return new MarshalException(message,inner);
  202. } else if (ex instanceof ACTIVITY_REQUIRED) {
  203. try {
  204. Class cl = ORBClassLoader.loadClass(
  205. "javax.activity.ActivityRequiredException");
  206. Class[] params = new Class[2];
  207. params[0] = java.lang.String.class;
  208. params[1] = java.lang.Throwable.class;
  209. Constructor cr = cl.getConstructor(params);
  210. Object[] args = new Object[2];
  211. args[0] = message;
  212. args[1] = ex;
  213. return (RemoteException) cr.newInstance(args);
  214. } catch (Throwable e) {
  215. utilWrapper.classNotFound(
  216. e, "javax.activity.ActivityRequiredException");
  217. }
  218. } else if (ex instanceof ACTIVITY_COMPLETED) {
  219. try {
  220. Class cl = ORBClassLoader.loadClass(
  221. "javax.activity.ActivityCompletedException");
  222. Class[] params = new Class[2];
  223. params[0] = java.lang.String.class;
  224. params[1] = java.lang.Throwable.class;
  225. Constructor cr = cl.getConstructor(params);
  226. Object[] args = new Object[2];
  227. args[0] = message;
  228. args[1] = ex;
  229. return (RemoteException) cr.newInstance(args);
  230. } catch (Throwable e) {
  231. utilWrapper.classNotFound(
  232. e, "javax.activity.ActivityCompletedException");
  233. }
  234. } else if (ex instanceof INVALID_ACTIVITY) {
  235. try {
  236. Class cl = ORBClassLoader.loadClass(
  237. "javax.activity.InvalidActivityException");
  238. Class[] params = new Class[2];
  239. params[0] = java.lang.String.class;
  240. params[1] = java.lang.Throwable.class;
  241. Constructor cr = cl.getConstructor(params);
  242. Object[] args = new Object[2];
  243. args[0] = message;
  244. args[1] = ex;
  245. return (RemoteException) cr.newInstance(args);
  246. } catch (Throwable e) {
  247. utilWrapper.classNotFound(
  248. e, "javax.activity.InvalidActivityException");
  249. }
  250. }
  251. // Just map to a generic RemoteException...
  252. return new RemoteException(message, ex);
  253. }
  254. /**
  255. * Writes any java.lang.Object as a CORBA any.
  256. * @param out the stream in which to write the any.
  257. * @param obj the object to write as an any.
  258. */
  259. public void writeAny( org.omg.CORBA.portable.OutputStream out,
  260. java.lang.Object obj)
  261. {
  262. org.omg.CORBA.ORB orb = out.orb();
  263. // Create Any
  264. Any any = orb.create_any();
  265. // Make sure we have a connected object...
  266. java.lang.Object newObj = Utility.autoConnect(obj,orb,false);
  267. if (newObj instanceof org.omg.CORBA.Object) {
  268. any.insert_Object((org.omg.CORBA.Object)newObj);
  269. } else {
  270. if (newObj == null) {
  271. // Handle the null case, including backwards
  272. // compatibility issues
  273. any.insert_Value(null, createTypeCodeForNull(orb));
  274. } else {
  275. if (newObj instanceof Serializable) {
  276. // If they're our Any and ORB implementations,
  277. // we may want to do type code related versioning.
  278. TypeCode tc = createTypeCode((Serializable)newObj, any, orb);
  279. if (tc == null)
  280. any.insert_Value((Serializable)newObj);
  281. else
  282. any.insert_Value((Serializable)newObj, tc);
  283. } else if (newObj instanceof Remote) {
  284. ORBUtility.throwNotSerializableForCorba(newObj.getClass().getName());
  285. } else {
  286. ORBUtility.throwNotSerializableForCorba(newObj.getClass().getName());
  287. }
  288. }
  289. }
  290. out.write_any(any);
  291. }
  292. /**
  293. * When using our own ORB and Any implementations, we need to get
  294. * the ORB version and create the type code appropriately. This is
  295. * to overcome a bug in which the JDK 1.3.x ORBs used a tk_char
  296. * rather than a tk_wchar to describe a Java char field.
  297. *
  298. * This only works in RMI-IIOP with Util.writeAny since we actually
  299. * know what ORB and stream we're writing with when we insert
  300. * the value.
  301. *
  302. * Returns null if it wasn't possible to create the TypeCode (means
  303. * it wasn't our ORB or Any implementation).
  304. *
  305. * This does not handle null objs.
  306. */
  307. private TypeCode createTypeCode(Serializable obj,
  308. org.omg.CORBA.Any any,
  309. org.omg.CORBA.ORB orb) {
  310. if (any instanceof com.sun.corba.se.impl.corba.AnyImpl &&
  311. orb instanceof ORB) {
  312. com.sun.corba.se.impl.corba.AnyImpl anyImpl
  313. = (com.sun.corba.se.impl.corba.AnyImpl)any;
  314. ORB ourORB = (ORB)orb;
  315. return anyImpl.createTypeCodeForClass(obj.getClass(), ourORB);
  316. } else
  317. return null;
  318. }
  319. /**
  320. * This is used to create the TypeCode for a null reference.
  321. * It also handles backwards compatibility with JDK 1.3.x.
  322. *
  323. * This method will not return null.
  324. */
  325. private TypeCode createTypeCodeForNull(org.omg.CORBA.ORB orb)
  326. {
  327. if (orb instanceof ORB) {
  328. ORB ourORB = (ORB)orb;
  329. // Preserve backwards compatibility with Kestrel and Ladybird
  330. // by not fully implementing interop issue resolution 3857,
  331. // and returning a null TypeCode with a tk_value TCKind.
  332. // If we're not talking to Kestrel or Ladybird, fall through
  333. // to the abstract interface case (also used for foreign ORBs).
  334. if (!ORBVersionFactory.getFOREIGN().equals(ourORB.getORBVersion()) &&
  335. ORBVersionFactory.getNEWER().compareTo(ourORB.getORBVersion()) > 0) {
  336. return orb.get_primitive_tc(TCKind.tk_value);
  337. }
  338. }
  339. // Use tk_abstract_interface as detailed in the resolution
  340. // REVISIT: Define this in IDL and get the ID in generated code
  341. String abstractBaseID = "IDL:omg.org/CORBA/AbstractBase:1.0";
  342. return orb.create_abstract_interface_tc(abstractBaseID, "");
  343. }
  344. /**
  345. * Reads a java.lang.Object as a CORBA any.
  346. * @param in the stream from which to read the any.
  347. * @return the object read from the stream.
  348. */
  349. public Object readAny(InputStream in)
  350. {
  351. Any any = in.read_any();
  352. if ( any.type().kind().value() == TCKind._tk_objref )
  353. return any.extract_Object ();
  354. else
  355. return any.extract_Value();
  356. }
  357. /**
  358. * Writes a java.lang.Object as a CORBA Object. If <code>obj</code> is
  359. * an exported RMI-IIOP server object, the tie is found
  360. * and wired to <code>obj</code>, then written to <code>out.write_Object(org.omg.CORBA.Object)</code>.
  361. * If <code>obj</code> is a CORBA Object, it is written to
  362. * <code>out.write_Object(org.omg.CORBA.Object)</code>.
  363. * @param out the stream in which to write the object.
  364. * @param obj the object to write.
  365. */
  366. public void writeRemoteObject(OutputStream out, java.lang.Object obj)
  367. {
  368. // Make sure we have a connected object, then
  369. // write it out...
  370. Object newObj = Utility.autoConnect(obj,out.orb(),false);
  371. out.write_Object((org.omg.CORBA.Object)newObj);
  372. }
  373. /**
  374. * Writes a java.lang.Object as either a value or a CORBA Object.
  375. * If <code>obj</code> is a value object or a stub object, it is written to
  376. * <code>out.write_abstract_interface(java.lang.Object)</code>. If <code>obj</code> is an exported
  377. * RMI-IIOP server object, the tie is found and wired to <code>obj</code>,
  378. * then written to <code>out.write_abstract_interface(java.lang.Object)</code>.
  379. * @param out the stream in which to write the object.
  380. * @param obj the object to write.
  381. */
  382. public void writeAbstractObject( OutputStream out, java.lang.Object obj )
  383. {
  384. // Make sure we have a connected object, then
  385. // write it out...
  386. Object newObj = Utility.autoConnect(obj,out.orb(),false);
  387. ((org.omg.CORBA_2_3.portable.OutputStream)out).write_abstract_interface(newObj);
  388. }
  389. /**
  390. * Registers a target for a tie. Adds the tie to an internal table and calls
  391. * {@link Tie#setTarget} on the tie object.
  392. * @param tie the tie to register.
  393. * @param target the target for the tie.
  394. */
  395. public void registerTarget(javax.rmi.CORBA.Tie tie, java.rmi.Remote target)
  396. {
  397. synchronized (exportedServants) {
  398. // Do we already have this target registered?
  399. if (lookupTie(target) == null) {
  400. // No, so register it and set the target...
  401. exportedServants.put(target,tie);
  402. tie.setTarget(target);
  403. // Do we need to instantiate our keep-alive thread?
  404. if (keepAlive == null) {
  405. // Yes. Instantiate our keep-alive thread and start
  406. // it up...
  407. keepAlive = (KeepAlive)AccessController.doPrivileged(new PrivilegedAction() {
  408. public java.lang.Object run() {
  409. return new KeepAlive();
  410. }
  411. });
  412. keepAlive.start();
  413. }
  414. }
  415. }
  416. }
  417. /**
  418. * Removes the associated tie from an internal table and calls {@link Tie#deactivate}
  419. * to deactivate the object.
  420. * @param target the object to unexport.
  421. */
  422. public void unexportObject(java.rmi.Remote target)
  423. throws java.rmi.NoSuchObjectException
  424. {
  425. synchronized (exportedServants) {
  426. Tie cachedTie = lookupTie(target);
  427. if (cachedTie != null) {
  428. exportedServants.remove(target);
  429. Utility.purgeStubForTie(cachedTie);
  430. Utility.purgeTieAndServant(cachedTie);
  431. try {
  432. cleanUpTie(cachedTie);
  433. } catch (BAD_OPERATION e) {
  434. // ignore
  435. } catch (org.omg.CORBA.OBJ_ADAPTER e) {
  436. // This can happen when the target was never associated with a POA.
  437. // We can safely ignore this case.
  438. }
  439. // Is it time to shut down our keep alive thread?
  440. if (exportedServants.isEmpty()) {
  441. keepAlive.quit();
  442. keepAlive = null;
  443. }
  444. } else {
  445. throw new java.rmi.NoSuchObjectException("Tie not found" );
  446. }
  447. }
  448. }
  449. protected void cleanUpTie(Tie cachedTie)
  450. throws java.rmi.NoSuchObjectException
  451. {
  452. cachedTie.setTarget(null);
  453. cachedTie.deactivate();
  454. }
  455. /**
  456. * Returns the tie (if any) for a given target object.
  457. * @return the tie or null if no tie is registered for the given target.
  458. */
  459. public Tie getTie (Remote target)
  460. {
  461. synchronized (exportedServants) {
  462. return lookupTie(target);
  463. }
  464. }
  465. /**
  466. * An unsynchronized version of getTie() for internal use.
  467. */
  468. private static Tie lookupTie (Remote target)
  469. {
  470. Tie result = (Tie)exportedServants.get(target);
  471. if (result == null && target instanceof Tie) {
  472. if (exportedServants.contains(target)) {
  473. result = (Tie)target;
  474. }
  475. }
  476. return result;
  477. }
  478. /**
  479. * Returns a singleton instance of a class that implements the
  480. * {@link ValueHandler} interface.
  481. * @return a class which implements the ValueHandler interface.
  482. */
  483. public ValueHandler createValueHandler()
  484. {
  485. return valueHandlerSingleton;
  486. }
  487. /**
  488. * Returns the codebase, if any, for the given class.
  489. * @param clz the class to get a codebase for.
  490. * @return a space-separated list of URLs, or null.
  491. */
  492. public String getCodebase(java.lang.Class clz) {
  493. return RMIClassLoader.getClassAnnotation(clz);
  494. }
  495. /**
  496. * Returns a class instance for the specified class.
  497. * @param className the name of the class.
  498. * @param remoteCodebase a space-separated list of URLs at which
  499. * the class might be found. May be null.
  500. * @param loadingContext a class whose ClassLoader may be used to
  501. * load the class if all other methods fail.
  502. * @return the <code>Class</code> object representing the loaded class.
  503. * @exception ClassNotFoundException if class cannot be loaded.
  504. */
  505. public Class loadClass( String className, String remoteCodebase,
  506. ClassLoader loader) throws ClassNotFoundException
  507. {
  508. return JDKBridge.loadClass(className,remoteCodebase,loader);
  509. }
  510. /**
  511. * The <tt>isLocal</tt> method has the same semantics as the
  512. * ObjectImpl._is_local method, except that it can throw a RemoteException.
  513. * (no it doesn't but the spec says it should.)
  514. *
  515. * The <tt>_is_local()</tt> method is provided so that stubs may determine
  516. * if a particular object is implemented by a local servant and hence local
  517. * invocation APIs may be used.
  518. *
  519. * @param stub the stub to test.
  520. *
  521. * @return The <tt>_is_local()</tt> method returns true if
  522. * the servant incarnating the object is located in the same process as
  523. * the stub and they both share the same ORB instance. The <tt>_is_local()</tt>
  524. * method returns false otherwise. The default behavior of <tt>_is_local()</tt> is
  525. * to return false.
  526. *
  527. * @throws RemoteException The Java to IDL specification does to
  528. * specify the conditions that cause a RemoteException to be thrown.
  529. */
  530. public boolean isLocal(javax.rmi.CORBA.Stub stub) throws RemoteException
  531. {
  532. boolean result = false ;
  533. try {
  534. org.omg.CORBA.portable.Delegate delegate = stub._get_delegate() ;
  535. if (delegate instanceof CorbaClientDelegate) {
  536. // For the Sun ORB
  537. CorbaClientDelegate cdel = (CorbaClientDelegate)delegate ;
  538. ContactInfoList cil = cdel.getContactInfoList() ;
  539. if (cil instanceof CorbaContactInfoList) {
  540. CorbaContactInfoList ccil = (CorbaContactInfoList)cil ;
  541. LocalClientRequestDispatcher lcs = ccil.getLocalClientRequestDispatcher() ;
  542. result = lcs.useLocalInvocation( null ) ;
  543. }
  544. } else {
  545. // For a non-Sun ORB
  546. result = delegate.is_local( stub ) ;
  547. }
  548. } catch (SystemException e) {
  549. throw javax.rmi.CORBA.Util.mapSystemException(e);
  550. }
  551. return result ;
  552. }
  553. /**
  554. * Wraps an exception thrown by an implementation
  555. * method. It returns the corresponding client-side exception.
  556. * @param orig the exception to wrap.
  557. * @return the wrapped exception.
  558. */
  559. public RemoteException wrapException(Throwable orig)
  560. {
  561. if (orig instanceof SystemException) {
  562. return mapSystemException((SystemException)orig);
  563. }
  564. if (orig instanceof Error) {
  565. return new ServerError("Error occurred in server thread",(Error)orig);
  566. } else if (orig instanceof RemoteException) {
  567. return new ServerException("RemoteException occurred in server thread",
  568. (Exception)orig);
  569. } else if (orig instanceof RuntimeException) {
  570. throw (RuntimeException) orig;
  571. }
  572. if (orig instanceof Exception)
  573. return new UnexpectedException( orig.toString(), (Exception)orig );
  574. else
  575. return new UnexpectedException( orig.toString());
  576. }
  577. /**
  578. * Copies or connects an array of objects. Used by local stubs
  579. * to copy any number of actual parameters, preserving sharing
  580. * across parameters as necessary to support RMI semantics.
  581. * @param obj the objects to copy or connect.
  582. * @param orb the ORB.
  583. * @return the copied or connected objects.
  584. * @exception RemoteException if any object could not be copied or connected.
  585. */
  586. public Object[] copyObjects (Object[] obj, org.omg.CORBA.ORB orb)
  587. throws RemoteException
  588. {
  589. if (obj == null)
  590. // Bug fix for 5018613: JCK test expects copyObjects to throw
  591. // NPE when obj==null. This is actually not in the spec, since
  592. // obj is not really an RMI-IDL data type, but we follow our
  593. // test here, and force this error to be thrown.
  594. throw new NullPointerException() ;
  595. Class compType = obj.getClass().getComponentType() ;
  596. if (Remote.class.isAssignableFrom( compType ) && !compType.isInterface()) {
  597. // obj is an array of remote impl types. This
  598. // causes problems with stream copier, so we copy
  599. // it over to an array of Remotes instead.
  600. Remote[] result = new Remote[obj.length] ;
  601. System.arraycopy( (Object)obj, 0, (Object)result, 0, obj.length ) ;
  602. return (Object[])copyObject( result, orb ) ;
  603. } else
  604. return (Object[])copyObject( obj, orb ) ;
  605. }
  606. /**
  607. * Copies or connects an object. Used by local stubs to copy
  608. * an actual parameter, result object, or exception.
  609. * @param obj the object to copy.
  610. * @param orb the ORB.
  611. * @return the copy or connected object.
  612. * @exception RemoteException if the object could not be copied or connected.
  613. */
  614. public Object copyObject (Object obj, org.omg.CORBA.ORB orb)
  615. throws RemoteException
  616. {
  617. if (orb instanceof ORB) {
  618. ORB lorb = (ORB)orb ;
  619. try {
  620. try {
  621. // This gets the copier for the current invocation, which was
  622. // previously set by preinvoke.
  623. return lorb.peekInvocationInfo().getCopierFactory().make().copy( obj ) ;
  624. } catch (java.util.EmptyStackException exc) {
  625. // copyObject was invoked outside of an invocation, probably by
  626. // a test. Get the default copier from the ORB.
  627. // XXX should we just make the default copier available directly
  628. // and avoid constructing one on each call?
  629. CopierManager cm = lorb.getCopierManager() ;
  630. ObjectCopier copier = cm.getDefaultObjectCopierFactory().make() ;
  631. return copier.copy( obj ) ;
  632. }
  633. } catch (ReflectiveCopyException exc) {
  634. RemoteException rexc = new RemoteException() ;
  635. rexc.initCause( exc ) ;
  636. throw rexc ;
  637. }
  638. } else {
  639. org.omg.CORBA_2_3.portable.OutputStream out =
  640. (org.omg.CORBA_2_3.portable.OutputStream)orb.create_output_stream();
  641. out.write_value((Serializable)obj);
  642. org.omg.CORBA_2_3.portable.InputStream in =
  643. (org.omg.CORBA_2_3.portable.InputStream)out.create_input_stream();
  644. return in.read_value();
  645. }
  646. }
  647. }
  648. class KeepAlive extends Thread
  649. {
  650. boolean quit = false;
  651. public KeepAlive ()
  652. {
  653. setDaemon(false);
  654. }
  655. public synchronized void run ()
  656. {
  657. while (!quit) {
  658. try {
  659. wait();
  660. } catch (InterruptedException e) {}
  661. }
  662. }
  663. public synchronized void quit ()
  664. {
  665. quit = true;
  666. notifyAll();
  667. }
  668. }