UtilDelegateImpl.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /* UtilDelegateImpl.java --
  2. Copyright (C) 2002, 2005, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.rmi.CORBA;
  32. import gnu.classpath.VMStackWalker;
  33. import gnu.CORBA.Minor;
  34. import gnu.CORBA.ObjectCreator;
  35. import gnu.CORBA.Poa.ORB_1_4;
  36. import gnu.CORBA.Poa.AOM;
  37. import gnu.CORBA.Poa.gnuPOA;
  38. import gnu.CORBA.typecodes.GeneralTypeCode;
  39. import org.omg.CORBA.Any;
  40. import org.omg.CORBA.BAD_PARAM;
  41. import org.omg.CORBA.COMM_FAILURE;
  42. import org.omg.CORBA.CompletionStatus;
  43. import org.omg.CORBA.INVALID_TRANSACTION;
  44. import org.omg.CORBA.INV_OBJREF;
  45. import org.omg.CORBA.MARSHAL;
  46. import org.omg.CORBA.NO_PERMISSION;
  47. import org.omg.CORBA.OBJECT_NOT_EXIST;
  48. import org.omg.CORBA.OMGVMCID;
  49. import org.omg.CORBA.ORB;
  50. import org.omg.CORBA.SystemException;
  51. import org.omg.CORBA.TCKind;
  52. import org.omg.CORBA.TRANSACTION_REQUIRED;
  53. import org.omg.CORBA.TRANSACTION_ROLLEDBACK;
  54. import org.omg.CORBA.TypeCode;
  55. import org.omg.CORBA.UNKNOWN;
  56. import org.omg.CORBA.portable.InputStream;
  57. import org.omg.CORBA.portable.OutputStream;
  58. import java.io.ByteArrayInputStream;
  59. import java.io.ByteArrayOutputStream;
  60. import java.io.ObjectInputStream;
  61. import java.io.ObjectOutputStream;
  62. import java.io.Serializable;
  63. import java.net.MalformedURLException;
  64. import java.rmi.AccessException;
  65. import java.rmi.MarshalException;
  66. import java.rmi.NoSuchObjectException;
  67. import java.rmi.Remote;
  68. import java.rmi.RemoteException;
  69. import java.rmi.ServerError;
  70. import java.rmi.ServerException;
  71. import java.rmi.UnexpectedException;
  72. import java.rmi.server.RMIClassLoader;
  73. import java.util.Hashtable;
  74. import javax.rmi.CORBA.Stub;
  75. import javax.rmi.CORBA.Tie;
  76. import javax.rmi.CORBA.Util;
  77. import javax.rmi.CORBA.UtilDelegate;
  78. import javax.rmi.CORBA.ValueHandler;
  79. import javax.transaction.InvalidTransactionException;
  80. import javax.transaction.TransactionRequiredException;
  81. import javax.transaction.TransactionRolledbackException;
  82. /**
  83. * The implementation of UtilDelegate.
  84. *
  85. * @author Wu Gansha (gansha.wu@intel.com) (stub)
  86. * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) (implementation)
  87. */
  88. public class UtilDelegateImpl
  89. extends RmiUtilities
  90. implements UtilDelegate
  91. {
  92. /**
  93. * The instance of the value handler, requested once.
  94. */
  95. static ValueHandler m_ValueHandler;
  96. /**
  97. * The global map of all ties to they records.
  98. */
  99. static Hashtable m_Ties = new Hashtable();
  100. /**
  101. * The global map of all targets to they records.
  102. */
  103. static Hashtable m_Targets = new Hashtable();
  104. /**
  105. * The standard package for that the exception names are omitted.
  106. */
  107. static final String m_StandardPackage = "org.omg.CORBA.";
  108. /**
  109. * Make a deep copy of the object.
  110. */
  111. public Object copyObject(Object obj, ORB orb)
  112. throws RemoteException
  113. {
  114. // Strings are immutable, can be shared.
  115. if (obj instanceof String)
  116. return obj;
  117. else if (obj == null)
  118. return null;
  119. else if (obj instanceof String[] || obj instanceof String[][]
  120. || obj instanceof String[][][])
  121. {
  122. // String arrays can be just cloned.
  123. return ((Object[]) obj).clone();
  124. }
  125. else if (obj instanceof Serializable)
  126. {
  127. try
  128. {
  129. ByteArrayOutputStream a = new ByteArrayOutputStream();
  130. ObjectOutputStream ou = new ObjectOutputStream(a);
  131. ou.writeObject(obj);
  132. ou.close();
  133. ObjectInputStream input = new ObjectInputStream(
  134. new ByteArrayInputStream(a.toByteArray()));
  135. return input.readObject();
  136. }
  137. catch (Exception ex)
  138. {
  139. RemoteException rex = new RemoteException("Cannot copy " + obj);
  140. throw rex;
  141. }
  142. }
  143. else
  144. return obj;
  145. }
  146. /**
  147. * Make a deep copy of the object array.
  148. */
  149. public Object[] copyObjects(Object[] obj, ORB orb)
  150. throws RemoteException
  151. {
  152. return (Object[]) copyObject(obj, orb);
  153. }
  154. public ValueHandler createValueHandler()
  155. {
  156. if (m_ValueHandler == null)
  157. m_ValueHandler = (ValueHandler) DelegateFactory.getInstance(DelegateFactory.VALUEHANDLER);
  158. return m_ValueHandler;
  159. }
  160. /**
  161. * Returns the codebase of the given class.
  162. */
  163. public String getCodebase(Class clz)
  164. {
  165. return RMIClassLoader.getClassAnnotation(clz);
  166. }
  167. /**
  168. * Get the Tie that handles invocations on the given target. If the target/Tie
  169. * pair has not been previously registered using {@link #registerTarget},
  170. * this method tries to locate a tie class by the name pattern. If this
  171. * succeeds, the tie-target pair is also registered.
  172. *
  173. * @return the Tie.
  174. */
  175. public Tie getTie(Remote target)
  176. {
  177. synchronized (m_Targets)
  178. {
  179. Tie tie;
  180. TieTargetRecord r = ((TieTargetRecord) m_Targets.get(target));
  181. if (r == null)
  182. {
  183. if (target instanceof Stub)
  184. {
  185. tie = StubDelegateImpl.getTieFromStub(target);
  186. registerTarget(tie, target);
  187. }
  188. else
  189. {
  190. // Treat this as implementation.
  191. String tieClassName = getTieClassName(target.getClass().getName());
  192. try
  193. {
  194. Class tieClass = Util.loadClass(tieClassName, null,
  195. target.getClass().getClassLoader());
  196. tie = (Tie) tieClass.newInstance();
  197. }
  198. catch (Exception e)
  199. {
  200. MARSHAL m = new MARSHAL("Unable to instantiate "
  201. + tieClassName);
  202. m.minor = Minor.TargetConversion;
  203. m.initCause(e);
  204. throw m;
  205. }
  206. tie.setTarget(target);
  207. registerTarget(tie, target);
  208. }
  209. }
  210. else
  211. tie = r.tie;
  212. return tie;
  213. }
  214. }
  215. /**
  216. * Get the Stub class name for the name, representing the given interface.
  217. */
  218. private String getTieClassName(String interf)
  219. {
  220. String stubClassName;
  221. int p = interf.lastIndexOf('.');
  222. if (p < 0)
  223. // The interface is defined in the default package.
  224. stubClassName = "_" + interf + "_Tie";
  225. else
  226. stubClassName = interf.substring(0, p + 1) + "_"
  227. + interf.substring(p + 1) + "_Tie";
  228. return stubClassName;
  229. }
  230. /**
  231. * Register the Tie-target pair. As the Tie is a Servant, it can potentially
  232. * be connected to several objects and hence may be registered with several
  233. * targets.
  234. */
  235. public void registerTarget(Tie tie, Remote target)
  236. {
  237. synchronized (m_Ties)
  238. {
  239. synchronized (m_Targets)
  240. {
  241. TieTargetRecord r = (TieTargetRecord) m_Ties.get(tie);
  242. if (r == null)
  243. {
  244. // First registration for this Tie.
  245. r = new TieTargetRecord(tie);
  246. m_Ties.put(tie, r);
  247. }
  248. if (target != null)
  249. {
  250. r.add(target);
  251. m_Targets.put(target, r);
  252. }
  253. }
  254. }
  255. }
  256. /**
  257. * Deactivate the associated Tie, if it is found and is not connected to other
  258. * registered targets. Independing from the POA policies, the transparent
  259. * reactivation will not be possible.
  260. */
  261. public void unexportObject(Remote target)
  262. throws NoSuchObjectException
  263. {
  264. synchronized (m_Ties)
  265. {
  266. synchronized (m_Targets)
  267. {
  268. TieTargetRecord r = ((TieTargetRecord) m_Targets.get(target));
  269. if (r != null)
  270. {
  271. if (target instanceof org.omg.CORBA.Object)
  272. r.tie.orb().disconnect((org.omg.CORBA.Object) target);
  273. if (r.unused())
  274. {
  275. m_Targets.remove(target);
  276. m_Ties.remove(r.tie);
  277. r.tie.deactivate();
  278. if (r.tie.orb() instanceof ORB_1_4)
  279. {
  280. // Standard case, when more deep cleanup is possible.
  281. // Independing from the POA policies, the object will
  282. // not be activable transparently.
  283. ORB_1_4 orb = (ORB_1_4) r.tie.orb();
  284. if (target instanceof org.omg.CORBA.Object)
  285. {
  286. AOM.Obj record = orb.rootPOA.findObject((org.omg.CORBA.Object) target);
  287. if (record != null && record.servant == r.tie
  288. && record.poa instanceof gnuPOA)
  289. {
  290. ((gnuPOA) record.poa).aom.remove(record.key);
  291. record.deactivated = true;
  292. record.servant = null;
  293. }
  294. }
  295. }
  296. }
  297. }
  298. }
  299. }
  300. }
  301. /**
  302. * Checks if the given stub is local.
  303. *
  304. * @param stub a stub to check.
  305. * @return true if the stub is local, false otherwise.
  306. */
  307. public boolean isLocal(Stub stub)
  308. throws RemoteException
  309. {
  310. try
  311. {
  312. return stub._is_local();
  313. }
  314. catch (SystemException e)
  315. {
  316. RemoteException rex = new RemoteException();
  317. rex.initCause(e);
  318. throw rex;
  319. }
  320. }
  321. /**
  322. * Load the class. The method uses class loaders from the call stact first. If
  323. * this fails, the further behaviour depends on the System Property
  324. * "java.rmi.server.useCodebaseOnly" with default value "false".
  325. *
  326. * <ul>
  327. * <li>Try the current thread context class loader first.</li>
  328. * <li>If remoteCodebase is non-null and useCodebaseOnly is "false" then call
  329. * java.rmi.server.RMIClassLoader.loadClass (remoteCodebase, className)</li>
  330. * <li> If remoteCodebase is null or useCodebaseOnly is true then call
  331. * java.rmi.server.RMIClassLoader.loadClass(className)</li>
  332. * <li>If a class is still not successfully loaded and the loader != null
  333. * then try Class.forName(className, false, loader). </li>
  334. * </ul>
  335. *
  336. * @param className the name of the class.
  337. * @param remoteCodebase the codebase.
  338. * @param loader the class loader.
  339. * @return the loaded class.
  340. *
  341. * @throws ClassNotFoundException of the class cannot be loaded.
  342. */
  343. public Class loadClass(String className, String remoteCodebase,
  344. ClassLoader loader)
  345. throws ClassNotFoundException
  346. {
  347. if (loader == null)
  348. loader = VMStackWalker.firstNonNullClassLoader();
  349. String p_useCodebaseOnly = System.getProperty("java.rmi.server.useCodebaseOnly");
  350. boolean useCodebaseOnly = p_useCodebaseOnly != null
  351. && p_useCodebaseOnly.trim().equalsIgnoreCase("true");
  352. if (useCodebaseOnly)
  353. remoteCodebase = null;
  354. try
  355. {
  356. return RMIClassLoader.loadClass(remoteCodebase, className, loader);
  357. }
  358. catch (MalformedURLException x)
  359. {
  360. throw new ClassNotFoundException(className, x);
  361. }
  362. }
  363. /**
  364. * Converts CORBA {@link SystemException} into RMI {@link RemoteException}.
  365. * The exception is converted as defined in the following table:
  366. * <p>
  367. * <table border = "1">
  368. * <tr>
  369. * <th>CORBA Exception</th>
  370. * <th>RMI Exception</th>
  371. * </tr>
  372. * <tr>
  373. * <td>{@link COMM_FAILURE}</td>
  374. * <td>{@link MarshalException}</td>
  375. * </tr>
  376. * <tr>
  377. * <td>{@link INV_OBJREF}</td>
  378. * <td>{@link NoSuchObjectException}</td>
  379. * </tr>
  380. * <tr>
  381. * <td>{@link NO_PERMISSION}</td>
  382. * <td>{@link AccessException}</td>
  383. * </tr>
  384. * <tr>
  385. * <td>{@link MARSHAL}</td>
  386. * <td>{@link MarshalException}</td>
  387. * </tr>
  388. * <tr>
  389. * <td>{@link BAD_PARAM} (all other cases)</td>
  390. * <td>{@link MarshalException}</td>
  391. * </tr>
  392. * <tr>
  393. * <td>{@link OBJECT_NOT_EXIST}</td>
  394. * <td>{@link NoSuchObjectException}</td>
  395. * </tr>
  396. * <tr>
  397. * <td>{@link TRANSACTION_REQUIRED}</td>
  398. * <td>{@link TransactionRequiredException}</td>
  399. * </tr>
  400. * <tr>
  401. * <td>{@link TRANSACTION_ROLLEDBACK}</td>
  402. * <td>{@link TransactionRolledbackException}</td>
  403. * </tr>
  404. * <tr>
  405. * <td>{@link INVALID_TRANSACTION}</td>
  406. * <td>{@link InvalidTransactionException}</td>
  407. * </tr>
  408. * <tr>
  409. * <td bgcolor="lightgray">Any other {@link SystemException}</td>
  410. * <td bgcolor="lightgray">{@link RemoteException}</td>
  411. * </tr>
  412. * </table>
  413. * </p>
  414. * <p>
  415. * The exception detailed message always consists of
  416. * <ol>
  417. * <li>the string "CORBA "</li>
  418. * <li>the CORBA name of the system exception</li>
  419. * <li>single space</li>
  420. * <li>the hexadecimal value of the system exception's minor code, preceeded
  421. * by 0x (higher bits contain {@link OMGVMCID}).</li>
  422. * <li>single space</li>
  423. * <li>the {@link CompletionStatus} of the exception: "Yes", "No" or "Maybe".</li>
  424. * </ol>
  425. * <p>
  426. * For instance, if the Internet connection was refused:
  427. * </p>
  428. * <p>
  429. * <pre>
  430. * <code>CORBA COMM_FAILURE 0x535500C9 No</code>
  431. * </p>
  432. * <p>
  433. * The original CORBA exception is set as the cause of the RemoteException
  434. * being created.
  435. * </p>
  436. */
  437. public RemoteException mapSystemException(SystemException ex)
  438. {
  439. RemoteException rex;
  440. String status;
  441. switch (ex.completed.value())
  442. {
  443. case CompletionStatus._COMPLETED_MAYBE:
  444. status = "Maybe";
  445. break;
  446. case CompletionStatus._COMPLETED_NO:
  447. status = "No";
  448. break;
  449. case CompletionStatus._COMPLETED_YES:
  450. status = "Yes";
  451. break;
  452. default:
  453. status = "Unexpected completion status " + ex.completed.value();
  454. }
  455. String name = ex.getClass().getName();
  456. if (name.startsWith(m_StandardPackage))
  457. name = name.substring(m_StandardPackage.length());
  458. String message = "CORBA " + name + " 0x" + Integer.toHexString(ex.minor)
  459. + " " + status;
  460. if (ex instanceof COMM_FAILURE)
  461. rex = new MarshalException(message, ex);
  462. else if (ex instanceof INV_OBJREF)
  463. {
  464. rex = new NoSuchObjectException(message);
  465. rex.detail = ex;
  466. }
  467. else if (ex instanceof NO_PERMISSION)
  468. rex = new AccessException(message, ex);
  469. else if (ex instanceof MARSHAL)
  470. rex = new MarshalException(message, ex);
  471. else if (ex instanceof BAD_PARAM)
  472. rex = new MarshalException(message, ex);
  473. else if (ex instanceof OBJECT_NOT_EXIST)
  474. {
  475. rex = new NoSuchObjectException(message);
  476. rex.detail = ex;
  477. }
  478. else if (ex instanceof TRANSACTION_REQUIRED)
  479. {
  480. rex = new TransactionRequiredException(message);
  481. rex.detail = ex;
  482. }
  483. else if (ex instanceof TRANSACTION_ROLLEDBACK)
  484. {
  485. rex = new TransactionRolledbackException(message);
  486. rex.detail = ex;
  487. }
  488. else if (ex instanceof INVALID_TRANSACTION)
  489. {
  490. rex = new InvalidTransactionException(message);
  491. rex.detail = ex;
  492. }
  493. else if (ex instanceof UNKNOWN)
  494. rex = wrapException(ex.getCause());
  495. else
  496. rex = new RemoteException(message, ex);
  497. return rex;
  498. }
  499. /**
  500. * Converts the exception that was thrown by the implementation method on a
  501. * server side into RemoteException that can be transferred and re-thrown on a
  502. * client side. The method converts exceptions as defined in the following
  503. * table: <table border = "1">
  504. * <tr>
  505. * <th>Exception to map (or subclass)</th>
  506. * <th>Maps into</th>
  507. * </tr>
  508. * <tr>
  509. * <td>{@link Error}</td>
  510. * <td>{@link ServerError}</td>
  511. * </tr>
  512. * <tr>
  513. * <td>{@link RemoteException}</td>
  514. * <td>{@link ServerException}</td>
  515. * </tr>
  516. * <tr>
  517. * <td>{@link SystemException}</td>
  518. * <td>wrapException({@link #mapSystemException})</td>
  519. * </tr>
  520. * <tr>
  521. * <td>{@link RuntimeException}</td>
  522. * <td><b>rethrows</b></td>
  523. * </tr>
  524. * <tr>
  525. * <td>Any other exception</td>
  526. * <td>{@link UnexpectedException}</td>
  527. * </tr>
  528. * </table>
  529. *
  530. * @param ex an exception that was thrown on a server side implementation.
  531. *
  532. * @return the corresponding RemoteException unless it is a RuntimeException.
  533. *
  534. * @throws RuntimeException the passed exception if it is an instance of
  535. * RuntimeException.
  536. *
  537. * @specnote It is the same behavior, as in Suns implementations 1.4.0-1.5.0.
  538. */
  539. public RemoteException wrapException(Throwable ex)
  540. throws RuntimeException
  541. {
  542. if (ex instanceof RuntimeException)
  543. throw (RuntimeException) ex;
  544. else if (ex instanceof Error)
  545. return new ServerError(ex.getMessage(), (Error) ex);
  546. else if (ex instanceof RemoteException)
  547. return new ServerException(ex.getMessage(), (Exception) ex);
  548. else if (ex instanceof SystemException)
  549. return wrapException(mapSystemException((SystemException) ex));
  550. else
  551. return new UnexpectedException("Unexpected", (Exception) ex);
  552. }
  553. /**
  554. * Write abstract interface to the CORBA output stream. The write format is
  555. * matching CORBA abstract interface. Remotes and CORBA objects are written as
  556. * objects, other classes are supposed to be value types and are written as
  557. * such. {@link Remote}s are processed as defined in
  558. * {@link #writeRemoteObject}. The written data contains discriminator,
  559. * defining, that was written. Another method that writes the same content is
  560. * {@link org.omg.CORBA_2_3.portable.OutputStream#write_abstract_interface(java.lang.Object)}.
  561. *
  562. * @param output a stream to write to, must be
  563. * {@link org.omg.CORBA_2_3.portable.OutputStream}.
  564. *
  565. * @param object an object to write, must be CORBA object, Remote
  566. */
  567. public void writeAbstractObject(OutputStream output, Object object)
  568. {
  569. ((org.omg.CORBA_2_3.portable.OutputStream) output).write_abstract_interface(object);
  570. }
  571. /**
  572. * Write the passed java object to the output stream in the form of the CORBA
  573. * {@link Any}. This includes creating an writing the object {@link TypeCode}
  574. * first. Such Any can be later read by a non-RMI-IIOP CORBA implementation
  575. * and manipulated, for instance, by means, provided in
  576. * {@link org.omg.DynamicAny.DynAny}. Depending from the passed value, this
  577. * method writes CORBA object, value type or value box. For value types Null
  578. * is written with the abstract interface, its typecode having repository id
  579. * "IDL:omg.org/CORBA/AbstractBase:1.0" and the empty string name.
  580. *
  581. * @param output the object to write.
  582. * @param object the java object that must be written in the form of the CORBA
  583. * {@link Any}.
  584. */
  585. public void writeAny(OutputStream output, Object object)
  586. {
  587. Any any = output.orb().create_any();
  588. if (object == null)
  589. {
  590. GeneralTypeCode t = new GeneralTypeCode(TCKind.tk_abstract_interface);
  591. t.setId("IDL:omg.org/CORBA/AbstractBase:1.0");
  592. t.setName("");
  593. any.type(t);
  594. output.write_any(any);
  595. return;
  596. }
  597. else if (object instanceof org.omg.CORBA.Object
  598. && !(object instanceof Remote))
  599. {
  600. // Write as value type.
  601. boolean inserted = ObjectCreator.insertWithHelper(any, object);
  602. if (inserted)
  603. {
  604. output.write_any(any);
  605. return;
  606. }
  607. }
  608. if (object instanceof org.omg.CORBA.Object)
  609. writeAnyAsRemote(output, object);
  610. else if (object instanceof Serializable)
  611. {
  612. any.insert_Value((Serializable) object);
  613. output.write_any(any);
  614. }
  615. else
  616. {
  617. MARSHAL m = new MARSHAL(object.getClass().getName()
  618. + " must be CORBA Object, Remote or Serializable");
  619. m.minor = Minor.NonSerializable;
  620. throw m;
  621. }
  622. }
  623. /**
  624. * Write Any as for remote object.
  625. */
  626. void writeAnyAsRemote(OutputStream output, Object object)
  627. {
  628. GeneralTypeCode t = new GeneralTypeCode(TCKind.tk_objref);
  629. t.setId(m_ValueHandler.getRMIRepositoryID(object.getClass()));
  630. t.setName(object.getClass().getName());
  631. // Writing Any (typecode, followed by value).
  632. output.write_TypeCode(t);
  633. writeRemoteObject(output, object);
  634. }
  635. /**
  636. * Get the class name excluding the package name.
  637. */
  638. String getName(String n)
  639. {
  640. int p = n.lastIndexOf('.');
  641. if (p < 0)
  642. return n;
  643. else
  644. return n.substring(p + 1);
  645. }
  646. /**
  647. * Read Any from the input stream.
  648. */
  649. public Object readAny(InputStream input)
  650. {
  651. return input.read_any();
  652. }
  653. /**
  654. * Write the passed parameter to the output stream as CORBA object. If the
  655. * parameter is an instance of Remote and not an instance of Stub, the method
  656. * instantiates a suitable Tie, connects the parameter to this Tie and then
  657. * connects that Tie to the ORB that is requested from the output stream. Then
  658. * the object reference is written to the stream, making remote invocations
  659. * possible. This method is used in write_value(..) method group in
  660. * {@link org.omg.CORBA_2_3.portable.OutputStream} and also may be called
  661. * directly from generated Stubs and Ties.
  662. *
  663. * @param output a stream to write to, must be
  664. * org.omg.CORBA_2_3.portable.OutputStream
  665. * @param object an object to write.
  666. */
  667. public void writeRemoteObject(OutputStream an_output, Object object)
  668. {
  669. org.omg.CORBA_2_3.portable.OutputStream output = (org.omg.CORBA_2_3.portable.OutputStream) an_output;
  670. if (object == null)
  671. an_output.write_Object(null);
  672. else if (isTieRequired(object))
  673. {
  674. // Find the interface that is implemented by the object and extends
  675. // Remote.
  676. Class fc = getExportedInterface(object);
  677. exportTie(output, object, fc);
  678. }
  679. else if (object instanceof org.omg.CORBA.Object)
  680. {
  681. ensureOrbRunning(output);
  682. an_output.write_Object((org.omg.CORBA.Object) object);
  683. }
  684. else if (object != null && object instanceof Serializable)
  685. writeFields(an_output, (Serializable) object);
  686. }
  687. }