RmiUtilities.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. /* RmiUtilities.java --
  2. Copyright (C) 2005 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.CORBA.OrbFunctional;
  33. import gnu.CORBA.Minor;
  34. import gnu.CORBA.Unexpected;
  35. import gnu.CORBA.CDR.Vio;
  36. import gnu.CORBA.CDR.gnuRuntime;
  37. import gnu.CORBA.CDR.gnuValueStream;
  38. import gnu.CORBA.CDR.HeadlessInput;
  39. import gnu.java.lang.CPStringBuilder;
  40. import org.omg.CORBA.MARSHAL;
  41. import org.omg.CORBA.StringValueHelper;
  42. import org.omg.CORBA.WStringValueHelper;
  43. import org.omg.CORBA.portable.Delegate;
  44. import org.omg.CORBA.portable.InputStream;
  45. import org.omg.CORBA.portable.ObjectImpl;
  46. import org.omg.CORBA.portable.OutputStream;
  47. import org.omg.CORBA.portable.ValueBase;
  48. import org.omg.PortableServer.POA;
  49. import org.omg.PortableServer.POAHelper;
  50. import org.omg.PortableServer.Servant;
  51. import org.omg.PortableServer.POAManagerPackage.State;
  52. import org.omg.SendingContext.RunTime;
  53. import java.io.ByteArrayOutputStream;
  54. import java.io.DataOutputStream;
  55. import java.io.Externalizable;
  56. import java.io.IOException;
  57. import java.io.ObjectInputStream;
  58. import java.io.ObjectOutputStream;
  59. import java.io.Serializable;
  60. import java.lang.reflect.Field;
  61. import java.lang.reflect.Method;
  62. import java.lang.reflect.Modifier;
  63. import java.rmi.Remote;
  64. import java.security.MessageDigest;
  65. import java.util.Arrays;
  66. import java.util.Comparator;
  67. import java.util.Iterator;
  68. import java.util.TreeSet;
  69. import java.util.WeakHashMap;
  70. import javax.rmi.PortableRemoteObject;
  71. import javax.rmi.CORBA.Stub;
  72. import javax.rmi.CORBA.Tie;
  73. import javax.rmi.CORBA.Util;
  74. /**
  75. * Defines methods that must be accessible in several derived classes.
  76. *
  77. * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  78. */
  79. public class RmiUtilities
  80. {
  81. /**
  82. * The currently used RMI-IIOP version format.
  83. */
  84. public static byte VERSION = 1;
  85. /**
  86. * The non - writable class fields.
  87. */
  88. static final int NON_WRITABLE = Modifier.STATIC | Modifier.TRANSIENT;
  89. /**
  90. * The standard String repository Id.
  91. */
  92. public static final String RMI_STRING_ID = StringValueHelper.id();
  93. /**
  94. * The standard Class repository Id.
  95. */
  96. public static final String RMI_CLASS_ID = "RMI:javax.rmi.CORBA.ClassDesc:2BABDA04587ADCCC:CFBF02CF5294176B";
  97. /**
  98. * The standard string array repository Id.
  99. */
  100. public static final String RMI_STRING_ARRAY_ID = "RMI:[Ljava.lang.String;:071DA8BE7F971128:A0F0A4387A3BB342";
  101. /**
  102. * An instance of the wide string value helper for writing strings.
  103. */
  104. static WStringValueHelper wStringValueHelper = new WStringValueHelper();
  105. /**
  106. * Set of serializable classes that have .writeObject and .readObject defined.
  107. * Contains weak references to ensure that the classes will be unloadable.
  108. */
  109. WeakHashMap io_format = new WeakHashMap();
  110. /**
  111. * The standard IO format with no .writeObject and .readObject defined.
  112. */
  113. static final Object STANDARD = new Object();
  114. /**
  115. * The custom IO format with .writeObject and .readObject defined,
  116. * defaultWriteObject called.
  117. */
  118. static final Object CUSTOM_DWO = new Object();
  119. /**
  120. * The custom IO format with .writeObject and .readObject defined,
  121. * defaultWriteObject has not been called.
  122. */
  123. static final Object CUSTOM_NO_DWO = new Object();
  124. /**
  125. * The arguments for readObject.
  126. */
  127. static final Class[] READ_OBJECT_ARGS = new Class[] { ObjectInputStream.class };
  128. /**
  129. * The arguments for writeObject.
  130. */
  131. static final Class[] WRITE_OBJECT_ARGS = new Class[] { ObjectOutputStream.class };
  132. /**
  133. * The undocumented field that is heading the Sun's object data, written with
  134. * writeObject.
  135. */
  136. static final int S_X = 16908034;
  137. /**
  138. * Write all fields of the passed value.
  139. */
  140. void writeFields(OutputStream an_output, Serializable object)
  141. {
  142. org.omg.CORBA_2_3.portable.OutputStream output = (org.omg.CORBA_2_3.portable.OutputStream) an_output;
  143. try
  144. {
  145. Class o_class = object.getClass();
  146. Field[] fields = getWritableFields(o_class);
  147. Field f;
  148. Class fc;
  149. for (int i = 0; i < fields.length; i++)
  150. {
  151. f = fields[i];
  152. fc = f.getType();
  153. Object v = f.get(object);
  154. if (fc == String.class)
  155. {
  156. output.write_value((Serializable) v, wStringValueHelper);
  157. }
  158. else if (fc == int.class)
  159. output.write_long(((Integer) v).intValue());
  160. else if (fc == long.class)
  161. output.write_longlong(((Number) v).longValue());
  162. else if (fc == double.class)
  163. output.write_double(((Number) v).doubleValue());
  164. else if (fc == float.class)
  165. output.write_float(((Number) v).floatValue());
  166. else if (fc == boolean.class)
  167. output.write_boolean(((Boolean) v).booleanValue());
  168. else if (fc == short.class)
  169. output.write_short(((Number) v).shortValue());
  170. else if (fc == byte.class)
  171. output.write_octet(((Number) v).byteValue());
  172. else if (fc == char.class)
  173. output.write_wchar(((Character) v).charValue());
  174. else
  175. {
  176. if (!fc.isInterface() && Remote.class.isAssignableFrom(fc))
  177. fc = getExportedInterface(fc);
  178. writeMember(output, v, fc);
  179. }
  180. }
  181. }
  182. catch (Exception ex)
  183. {
  184. MARSHAL m = new MARSHAL("Cannot write " + object);
  185. m.minor = Minor.ValueFields;
  186. m.initCause(ex);
  187. throw m;
  188. }
  189. }
  190. /**
  191. * Write a memeber (field) of the data structure.
  192. */
  193. void writeMember(org.omg.CORBA_2_3.portable.OutputStream output,
  194. Object object, Class xClass)
  195. {
  196. if (output instanceof gnuValueStream)
  197. {
  198. gnuRuntime g = ((gnuValueStream) output).getRunTime();
  199. // Reset the target as we are already beyond the critical point
  200. // where is must have the value being written.
  201. if (g != null)
  202. g.target = null;
  203. }
  204. if (Serializable.class.isAssignableFrom(xClass)
  205. || Remote.class.isAssignableFrom(xClass))
  206. {
  207. // Object handles null reference on its own.
  208. if (org.omg.CORBA.Object.class.isAssignableFrom(xClass)
  209. || Remote.class.isAssignableFrom(xClass))
  210. {
  211. if (object == null)
  212. output.write_Object(null);
  213. else if (isTieRequired(object))
  214. exportTie(output, object, xClass);
  215. else
  216. writeValue(output, (Serializable) object);
  217. }
  218. else
  219. output.write_value((Serializable) object, xClass);
  220. }
  221. else
  222. {
  223. MARSHAL m = new MARSHAL(xClass + " is not Serializable");
  224. m.minor = Minor.NonSerializable;
  225. throw m;
  226. }
  227. }
  228. /**
  229. * Check if the object must be wrapped into Tie, connected to the ORB and then
  230. * the corresponding Stub be written.
  231. */
  232. public boolean isTieRequired(Object object)
  233. {
  234. return object instanceof Remote && !(object instanceof Stub);
  235. }
  236. /**
  237. * Get the interface under that the class of this object must be exposed. The
  238. * interface must be derived from Remote.
  239. */
  240. Class getExportedInterface(Object object)
  241. throws MARSHAL
  242. {
  243. Class fc = null;
  244. Class[] interfaces = object.getClass().getInterfaces();
  245. for (int i = 0; i < interfaces.length; i++)
  246. {
  247. if (!Remote.class.equals(interfaces[i]))
  248. if (Remote.class.isAssignableFrom(interfaces[i]))
  249. {
  250. if (fc == null)
  251. fc = interfaces[i];
  252. else
  253. {
  254. MARSHAL m = new MARSHAL("Both " + fc + " and " + interfaces[i]
  255. + " extends Remote");
  256. m.minor = Minor.TargetConversion;
  257. throw m;
  258. }
  259. }
  260. }
  261. if (fc == null)
  262. {
  263. MARSHAL m = new MARSHAL(object.getClass()
  264. + " does not implement any interface, derived from Remote");
  265. m.minor = Minor.TargetConversion;
  266. throw m;
  267. }
  268. return fc;
  269. }
  270. /**
  271. * Get the persistent hash code for the given class, as defined by OMG
  272. * standard. The inheritance, field names and types (but not the visibility)
  273. * are taken into consideration as well as the presence of the writeObject
  274. * method are taken into consideration. The class name and methods, if any,
  275. * are not taken into consideration.
  276. */
  277. public static long getHashCode(Class c)
  278. {
  279. Class of = c.isArray() ? c.getComponentType() : null;
  280. if (c.isArray()
  281. && ((!Serializable.class.isAssignableFrom(of) || of.isPrimitive() || Remote.class.isAssignableFrom(of))))
  282. return 0;
  283. if (!Serializable.class.isAssignableFrom(c))
  284. return 0;
  285. try
  286. {
  287. ByteArrayOutputStream bout = new ByteArrayOutputStream();
  288. DataOutputStream out = new DataOutputStream(bout);
  289. Class superClass = c.getSuperclass();
  290. if (superClass != null)
  291. out.writeLong(getHashCode(superClass));
  292. int writeObjectPresentCode;
  293. try
  294. {
  295. c.getDeclaredMethod("writeObject",
  296. new Class[] { ObjectOutputStream.class });
  297. writeObjectPresentCode = 2; // Exists.
  298. }
  299. catch (NoSuchMethodException e)
  300. {
  301. writeObjectPresentCode = 1; // Missing.
  302. }
  303. out.writeInt(writeObjectPresentCode);
  304. Field[] fields = c.getDeclaredFields();
  305. Arrays.sort(fields, new Comparator()
  306. {
  307. public int compare(Object a, Object b)
  308. {
  309. Field fa = (Field) a;
  310. Field fb = (Field) b;
  311. return fa.getName().compareTo(fb.getName());
  312. }
  313. });
  314. Field f;
  315. for (int i = 0; i < fields.length; i++)
  316. {
  317. f = fields[i];
  318. if ((f.getModifiers() & NON_WRITABLE) == 0)
  319. {
  320. out.writeUTF(f.getName());
  321. out.writeUTF(getDescriptor(f.getType()));
  322. }
  323. }
  324. out.flush();
  325. out.close();
  326. MessageDigest shaDigest;
  327. try
  328. {
  329. shaDigest = MessageDigest.getInstance("SHA");
  330. }
  331. catch (Exception ex)
  332. {
  333. throw new InternalError("SHA digesting algorithm is not available");
  334. }
  335. // Return the digest value to the calling
  336. // method as an array of bytes.
  337. byte[] sha = shaDigest.digest(bout.toByteArray());
  338. long hash = 0;
  339. for (int i = 0; i < Math.min(8, sha.length); i++)
  340. {
  341. hash += (long) (sha[i] & 255) << (i * 8);
  342. }
  343. return hash;
  344. }
  345. catch (IOException ioex)
  346. {
  347. throw new Unexpected(ioex);
  348. }
  349. }
  350. /**
  351. * Converts to hexadecimal string, supplementing leading zeros.
  352. */
  353. public static String toHex(long l)
  354. {
  355. CPStringBuilder b = new CPStringBuilder();
  356. b.append(Long.toHexString(l).toUpperCase());
  357. while (b.length() < 16)
  358. b.insert(0, '0');
  359. return b.toString();
  360. }
  361. /**
  362. * Returns a <code>String</code> representing the type-encoding of a class.
  363. */
  364. static String getDescriptor(Class type)
  365. {
  366. if (type.equals(boolean.class))
  367. return "Z";
  368. if (type.equals(byte.class))
  369. return "B";
  370. if (type.equals(short.class))
  371. return "S";
  372. if (type.equals(char.class))
  373. return "C";
  374. if (type.equals(int.class))
  375. return "I";
  376. if (type.equals(long.class))
  377. return "J";
  378. if (type.equals(float.class))
  379. return "F";
  380. if (type.equals(double.class))
  381. return "D";
  382. if (type.equals(void.class))
  383. return "V";
  384. else if (type.isArray())
  385. {
  386. CPStringBuilder l = new CPStringBuilder("[");
  387. Class component = type.getComponentType();
  388. while (component.isArray())
  389. {
  390. l.append('[');
  391. component = component.getComponentType();
  392. }
  393. l.append('L');
  394. l.append(component.getName().replace('.', '/'));
  395. l.append(';');
  396. return l.toString();
  397. }
  398. else
  399. return "L" + type.getName().replace('.', '/') + ';';
  400. }
  401. public static Field[] getWritableFields(Class c)
  402. {
  403. TreeSet set = new TreeSet(new Comparator()
  404. {
  405. public int compare(Object a, Object b)
  406. {
  407. return ((Field) a).getName().compareTo(((Field) b).getName());
  408. }
  409. });
  410. while (!c.equals(Object.class))
  411. {
  412. Field[] f = c.getDeclaredFields();
  413. for (int i = 0; i < f.length; i++)
  414. {
  415. if ((f[i].getModifiers() & NON_WRITABLE) == 0)
  416. {
  417. f[i].setAccessible(true);
  418. set.add(f[i]);
  419. }
  420. }
  421. c = c.getSuperclass();
  422. }
  423. Field[] r = new Field[set.size()];
  424. int p = 0;
  425. Iterator it = set.iterator();
  426. while (it.hasNext())
  427. {
  428. r[p++] = (Field) it.next();
  429. }
  430. return r;
  431. }
  432. /**
  433. * The method is called for Remotes that are not Stubs. It is assumed, that
  434. * the Remote is an implementation. The method searches for the suitable tie
  435. * and, if found, exports it by creating and connecting the stub. Such export
  436. * is supported since jdk 1.5.
  437. */
  438. void exportTie(org.omg.CORBA_2_3.portable.OutputStream output,
  439. Object implementation, Class interfaceClass)
  440. {
  441. try
  442. {
  443. // Remote, but non - stub class (implementation)
  444. // must be replaced by stub.
  445. Tie t = Util.getTie((Remote) implementation);
  446. if (t instanceof Servant)
  447. {
  448. POA rootPoa = POAHelper.narrow(output.orb().resolve_initial_references(
  449. "RootPOA"));
  450. org.omg.CORBA.Object co = rootPoa.servant_to_reference((Servant) t);
  451. Stub stub = (Stub) PortableRemoteObject.narrow(co, interfaceClass);
  452. writeRemoteObject(output, stub);
  453. if (rootPoa.the_POAManager().get_state().value() == State._HOLDING)
  454. rootPoa.the_POAManager().activate();
  455. }
  456. else if (t instanceof org.omg.CORBA.Object)
  457. {
  458. org.omg.CORBA.Object co = (org.omg.CORBA.Object) t;
  459. output.orb().connect(co);
  460. Stub stub = (Stub) PortableRemoteObject.narrow(co, interfaceClass);
  461. writeRemoteObject(output, stub);
  462. }
  463. }
  464. catch (Exception ex)
  465. {
  466. MARSHAL m = new MARSHAL("Unable to export " + implementation);
  467. m.minor = Minor.TargetConversion;
  468. m.initCause(ex);
  469. throw m;
  470. }
  471. }
  472. /**
  473. * Start the ORB, if it is not already runnning.
  474. */
  475. void ensureOrbRunning(org.omg.CORBA_2_3.portable.OutputStream output)
  476. {
  477. // Ensure ORB is running.
  478. if (output.orb() instanceof OrbFunctional)
  479. {
  480. ((OrbFunctional) output.orb()).ensureRunning();
  481. }
  482. }
  483. /**
  484. * Write data to the CORBA output stream. Writes the object contents only; the
  485. * header must be already written. For object, containing objects, may be
  486. * called recursively.
  487. *
  488. * @param an_output a stream to write to, must be
  489. * org.omg.CORBA_2_3.portable.OutputStream
  490. * @param object an object to write.
  491. */
  492. public void writeRemoteObject(OutputStream an_output, Object object)
  493. {
  494. org.omg.CORBA_2_3.portable.OutputStream output = (org.omg.CORBA_2_3.portable.OutputStream) an_output;
  495. if (isTieRequired(object))
  496. {
  497. // Find the interface that is implemented by the object and extends
  498. // Remote.
  499. Class fc = getExportedInterface(object);
  500. exportTie(output, object, fc);
  501. }
  502. else if (object instanceof org.omg.CORBA.Object)
  503. {
  504. ensureOrbRunning(output);
  505. an_output.write_Object((org.omg.CORBA.Object) object);
  506. }
  507. else if (object != null && object instanceof Serializable)
  508. writeFields(an_output, (Serializable) object);
  509. }
  510. /**
  511. * Write data to the CORBA output stream. Writes the object contents only; the
  512. * header must be already written. For object, containing objects, may be
  513. * called recursively.
  514. *
  515. * @param an_output a stream to write to, must be
  516. * org.omg.CORBA_2_3.portable.OutputStream
  517. * @param object an object to write.
  518. */
  519. public void writeValue(OutputStream an_output, Serializable object)
  520. {
  521. org.omg.CORBA_2_3.portable.OutputStream output = (org.omg.CORBA_2_3.portable.OutputStream) an_output;
  522. if (isTieRequired(object))
  523. {
  524. // Find the interface that is implemented by the object and extends
  525. // Remote.
  526. Class fc = getExportedInterface(object);
  527. exportTie(output, object, fc);
  528. }
  529. else if (object instanceof org.omg.CORBA.Object)
  530. {
  531. ensureOrbRunning(output);
  532. an_output.write_Object((org.omg.CORBA.Object) object);
  533. }
  534. else if (object instanceof Externalizable)
  535. {
  536. try
  537. {
  538. ObjectOutputStream stream = new CorbaOutput(output, object,
  539. this);
  540. stream.write(VERSION);
  541. ((Externalizable) object).writeExternal(stream);
  542. }
  543. catch (Exception ex)
  544. {
  545. MARSHAL m = new MARSHAL("writeExternal failed");
  546. m.minor = Minor.Value;
  547. m.initCause(ex);
  548. throw m;
  549. }
  550. }
  551. else if (object instanceof Serializable)
  552. {
  553. Object mode = null;
  554. synchronized (io_format)
  555. {
  556. mode = io_format.get(object.getClass());
  557. if (mode == STANDARD)
  558. {
  559. writeFields(an_output, (Serializable) object);
  560. return;
  561. }
  562. }
  563. try
  564. {
  565. Method m = object.getClass().getDeclaredMethod("writeObject",
  566. WRITE_OBJECT_ARGS);
  567. m.setAccessible(true); // May be private.
  568. try
  569. {
  570. ObjectOutputStream stream = new CorbaOutput(output,
  571. object, this);
  572. // Write version.
  573. stream.write(VERSION);
  574. if (mode == CUSTOM_DWO)
  575. // Write true, supposing that the defaultWriteObject
  576. // has been called.
  577. stream.write(1);
  578. else if (mode == CUSTOM_NO_DWO)
  579. // Write false (has not been called)
  580. stream.write(0);
  581. else
  582. {
  583. // Measure.
  584. DefaultWriteObjectTester tester = new DefaultWriteObjectTester(object);
  585. m.invoke(object, new Object[] { tester });
  586. synchronized (io_format)
  587. {
  588. io_format.put(object.getClass(),
  589. tester.dwo_called ? CUSTOM_DWO : CUSTOM_NO_DWO);
  590. stream.write(tester.dwo_called ? 1 : 0);
  591. }
  592. }
  593. m.invoke(object, new Object[] { stream });
  594. stream.flush();
  595. }
  596. catch (Exception ex)
  597. {
  598. MARSHAL mx = new MARSHAL(object.getClass().getName()
  599. + ".writeObject failed");
  600. mx.initCause(ex);
  601. throw mx;
  602. }
  603. }
  604. catch (NoSuchMethodException e)
  605. {
  606. // Write in a standard way.
  607. writeFields(an_output, (Serializable) object);
  608. synchronized (io_format)
  609. {
  610. io_format.put(object.getClass(), STANDARD);
  611. }
  612. }
  613. }
  614. }
  615. /**
  616. * Read data from the CDR input stream. Reads the object contents only; the
  617. * header must be already read (the repository id or ids ara passed). For
  618. * object, containing objects, may be called recursively.
  619. *
  620. * @param an_input the stream to read from, must be
  621. * org.omg.CORBA_2_3.portable.InputStream
  622. * @param object the instance of the object being read.
  623. * @param id the repository Id from the stream in the case when single id was
  624. * specified.
  625. * @param ids the repository Ids from the stream in the case when multiple ids
  626. * were specified.
  627. * @param codebase the codebase, if it was included in the header of the value
  628. * type. Null if not codebase was included.
  629. *
  630. * @return the object, extracted from the stream.
  631. */
  632. /**
  633. * Read value from the input stream in the case when the value is not
  634. * Streamable or CustomMarshalled.
  635. */
  636. public Serializable readValue(InputStream in, int offset, Class clz,
  637. String repositoryID, RunTime sender)
  638. {
  639. if (in instanceof HeadlessInput)
  640. ((HeadlessInput) in).subsequentCalls = true;
  641. gnuRuntime g = null;
  642. Serializable object = null;
  643. try
  644. {
  645. g = (gnuRuntime) sender;
  646. if (sender != null)
  647. object = g.target;
  648. }
  649. catch (ClassCastException e)
  650. {
  651. // Working with the other CORBA implementation.
  652. g = null;
  653. }
  654. org.omg.CORBA_2_3.portable.InputStream input = (org.omg.CORBA_2_3.portable.InputStream) in;
  655. if (Remote.class.isAssignableFrom(clz)
  656. || ValueBase.class.isAssignableFrom(clz))
  657. {
  658. // Interface is narrowed into Stub.
  659. if (clz.isInterface())
  660. try
  661. {
  662. clz = Util.loadClass(
  663. PortableRemoteObjectDelegateImpl.getStubClassName(clz.getName()),
  664. null, clz.getClassLoader());
  665. }
  666. catch (ClassNotFoundException e)
  667. {
  668. MARSHAL m = new MARSHAL("Cannot get stub from interface "
  669. + clz.getClass().getName());
  670. m.minor = Minor.TargetConversion;
  671. m.initCause(e);
  672. throw m;
  673. }
  674. // Remote needs special handling.
  675. if (ObjectImpl.class.isAssignableFrom(clz))
  676. {
  677. // First read CORBA object reference.
  678. Object ro = input.read_Object();
  679. ObjectImpl obj = (ObjectImpl) ro;
  680. if (obj == null)
  681. return null;
  682. Delegate delegate = obj._get_delegate();
  683. object = instantiate(offset, clz, g);
  684. ((ObjectImpl) object)._set_delegate(delegate);
  685. }
  686. // The object - specific data follows.
  687. }
  688. else if (org.omg.CORBA.Object.class.isAssignableFrom(clz))
  689. object = (Serializable) input.read_Object();
  690. if (object == null)
  691. object = instantiate(offset, clz, g);
  692. // The sentence below prevents attempt to read the internal fields of the
  693. // ObjectImpl (or RMI Stub) that might follow the object definition.
  694. // Sun's jre 1.5 does not write this information. The stubs, generated
  695. // by rmic, does not contain such fields.
  696. if (object instanceof ObjectImpl)
  697. return object;
  698. if (object instanceof Externalizable)
  699. {
  700. try
  701. {
  702. CorbaInput stream = new CorbaInput(input, object, this,
  703. offset, repositoryID, g);
  704. byte version = stream.readByte();
  705. if (version != 1)
  706. throw new MARSHAL("Unsuported RMI-IIOP version " + version);
  707. ((Externalizable) object).readExternal(stream);
  708. }
  709. catch (Exception ex)
  710. {
  711. MARSHAL m = new MARSHAL("readExternal failed");
  712. m.initCause(ex);
  713. throw m;
  714. }
  715. }
  716. else
  717. {
  718. Object mode = null;
  719. synchronized (io_format)
  720. {
  721. mode = io_format.get(object.getClass());
  722. }
  723. if (mode == STANDARD)
  724. {
  725. readFields(offset, repositoryID, object, input, g);
  726. }
  727. else
  728. {
  729. try
  730. {
  731. Method m = object.getClass().getDeclaredMethod("readObject",
  732. READ_OBJECT_ARGS);
  733. try
  734. {
  735. m.setAccessible(true); // May be private.
  736. CorbaInput stream = new CorbaInput(input,
  737. object, this, offset, repositoryID, g);
  738. byte version = stream.readByte();
  739. if (version != 1)
  740. throw new MARSHAL("Unsuported RMI-IIOP version "
  741. + version);
  742. // This would indicate is defaultWriteObject has been
  743. // called,
  744. // but the readObject method normally takes care about this.
  745. boolean dwo = stream.readByte() != 0;
  746. m.invoke(object, new Object[] { stream });
  747. synchronized (io_format)
  748. {
  749. io_format.put(object.getClass(), dwo ? CUSTOM_DWO
  750. : CUSTOM_NO_DWO);
  751. }
  752. }
  753. catch (Exception ex)
  754. {
  755. ex.printStackTrace();
  756. MARSHAL mx = new MARSHAL(object.getClass().getName()
  757. + ".readObject failed");
  758. mx.initCause(ex);
  759. throw mx;
  760. }
  761. }
  762. catch (NoSuchMethodException e)
  763. {
  764. // Read in a standard way.
  765. synchronized (io_format)
  766. {
  767. io_format.put(object.getClass(), STANDARD);
  768. readFields(offset, repositoryID, object, input, g);
  769. }
  770. }
  771. }
  772. }
  773. return object;
  774. }
  775. /**
  776. * Create an instance.
  777. */
  778. Serializable instantiate(int offset, Class clz, gnuRuntime g)
  779. throws MARSHAL
  780. {
  781. Serializable object;
  782. try
  783. {
  784. object = (Serializable) Vio.instantiateAnyWay(clz);
  785. g.objectWritten(object, offset);
  786. }
  787. catch (Exception e)
  788. {
  789. MARSHAL m = new MARSHAL("Unable to instantiate " + clz);
  790. m.minor = Minor.Instantiation;
  791. m.initCause(e);
  792. throw m;
  793. }
  794. return object;
  795. }
  796. /**
  797. * Read fields of the object.
  798. */
  799. void readFields(int offset, String repositoryID, Serializable object,
  800. org.omg.CORBA_2_3.portable.InputStream input, gnuRuntime r)
  801. throws MARSHAL
  802. {
  803. Field f = null;
  804. Class o_class = object.getClass();
  805. try
  806. {
  807. // The returned field array must already be in canonical order.
  808. Field[] fields = getWritableFields(o_class);
  809. Class fc;
  810. for (int i = 0; i < fields.length; i++)
  811. {
  812. // Full value type header expected ahead.
  813. if (input instanceof HeadlessInput)
  814. ((HeadlessInput) input).subsequentCalls = true;
  815. f = fields[i];
  816. fc = f.getType();
  817. Object v;
  818. if (fc == String.class)
  819. {
  820. v = input.read_value(wStringValueHelper);
  821. }
  822. else if (fc == int.class)
  823. v = new Integer(input.read_long());
  824. else if (fc == long.class)
  825. v = new Long(input.read_longlong());
  826. else if (fc == double.class)
  827. v = new Double(input.read_double());
  828. else if (fc == float.class)
  829. v = new Float(input.read_float());
  830. else if (fc == boolean.class)
  831. v = input.read_boolean() ? Boolean.TRUE : Boolean.FALSE;
  832. else if (fc == short.class)
  833. v = new Short(input.read_short());
  834. else if (fc == byte.class)
  835. v = new Byte(input.read_octet());
  836. else if (fc == char.class)
  837. v = new Character(input.read_char());
  838. else if (org.omg.CORBA.Object.class.isAssignableFrom(fc)
  839. || Remote.class.isAssignableFrom(fc))
  840. {
  841. v = readValue(input, offset, fc, null, r);
  842. }
  843. else
  844. {
  845. v = Vio.read(input, fc);
  846. }
  847. f.set(object, v);
  848. }
  849. }
  850. catch (Exception ex)
  851. {
  852. MARSHAL m = new MARSHAL("Cannot read " + o_class.getName() + " field "
  853. + f);
  854. m.initCause(ex);
  855. m.minor = Minor.ValueFields;
  856. throw m;
  857. }
  858. }
  859. }