1. /*
  2. * @(#)CDRInputStream_1_0.java 1.83 04/01/13
  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.internal.iiop;
  16. import java.io.IOException;
  17. import java.io.Serializable;
  18. import java.io.ByteArrayInputStream;
  19. import java.io.ObjectInputStream;
  20. import java.io.IOException;
  21. import java.io.StreamCorruptedException;
  22. import java.io.OptionalDataException;
  23. import java.io.IOException;
  24. import java.util.Stack;
  25. import java.net.URL;
  26. import java.net.MalformedURLException;
  27. import java.lang.reflect.InvocationTargetException;
  28. import java.lang.reflect.Method;
  29. import java.math.BigDecimal;
  30. import java.rmi.Remote;
  31. import java.rmi.StubNotFoundException;
  32. import org.omg.CORBA.SystemException;
  33. import org.omg.CORBA.INTERNAL;
  34. import org.omg.CORBA.MARSHAL;
  35. import org.omg.CORBA.DATA_CONVERSION;
  36. import org.omg.CORBA.BAD_PARAM;
  37. import org.omg.CORBA.Object;
  38. import org.omg.CORBA.Principal;
  39. import org.omg.CORBA.TypeCode;
  40. import org.omg.CORBA.Any;
  41. import org.omg.CORBA.portable.Delegate;
  42. import org.omg.CORBA.portable.ValueBase;
  43. import org.omg.CORBA.portable.IndirectionException;
  44. import org.omg.CORBA.NO_IMPLEMENT;
  45. import org.omg.CORBA.CompletionStatus;
  46. import org.omg.CORBA.TCKind;
  47. import org.omg.CORBA.TypeCodePackage.BadKind;
  48. import org.omg.CORBA.CustomMarshal;
  49. import org.omg.CORBA.TypeCode;
  50. import org.omg.CORBA.Principal;
  51. import org.omg.CORBA.Any;
  52. import org.omg.CORBA.portable.BoxedValueHelper;
  53. import org.omg.CORBA.portable.ValueFactory;
  54. import org.omg.CORBA.portable.CustomValue;
  55. import org.omg.CORBA.portable.StreamableValue;
  56. import org.omg.CORBA.portable.ObjectImpl;
  57. import org.omg.CORBA.MARSHAL;
  58. import javax.rmi.PortableRemoteObject;
  59. import javax.rmi.CORBA.Tie;
  60. import javax.rmi.CORBA.Util;
  61. import javax.rmi.CORBA.ValueHandler;
  62. import com.sun.corba.se.internal.corba.ServerDelegate;
  63. import com.sun.corba.se.internal.corba.PrincipalImpl;
  64. import com.sun.corba.se.internal.corba.TypeCodeImpl;
  65. import com.sun.corba.se.internal.core.IOR;
  66. import com.sun.corba.se.internal.core.SubcontractRegistry;
  67. import com.sun.corba.se.internal.core.ServerSubcontract;
  68. import com.sun.corba.se.internal.core.ClientSubcontract;
  69. import com.sun.corba.se.internal.core.GIOPVersion;
  70. import com.sun.corba.se.internal.core.ORBVersionImpl;
  71. import com.sun.corba.se.internal.core.ORBVersion;
  72. import com.sun.corba.se.internal.core.CodeSetConversion;
  73. import com.sun.corba.se.internal.util.Utility;
  74. import com.sun.corba.se.internal.orbutil.RepositoryIdStrings;
  75. import com.sun.corba.se.internal.orbutil.RepositoryIdInterface;
  76. import com.sun.corba.se.internal.orbutil.RepositoryIdUtility;
  77. import com.sun.corba.se.internal.orbutil.RepositoryIdFactory;
  78. import com.sun.corba.se.internal.orbutil.ORBUtility;
  79. import com.sun.corba.se.internal.orbutil.CacheTable;
  80. import com.sun.corba.se.internal.orbutil.MinorCodes;
  81. import com.sun.corba.se.internal.ior.ObjectKeyTemplate ;
  82. import com.sun.org.omg.CORBA.portable.ValueHelper;
  83. import com.sun.org.omg.SendingContext.CodeBase;
  84. import java.security.AccessController;
  85. import java.security.PrivilegedExceptionAction;
  86. import java.security.PrivilegedActionException;
  87. public class CDRInputStream_1_0 extends CDRInputStreamBase
  88. implements RestorableInputStream
  89. {
  90. private static final String kReadMethod = "read";
  91. private static final int maxBlockLength = 0x7fffff00;
  92. protected BufferManagerRead bufferManagerRead;
  93. protected ByteBufferWithInfo bbwi;
  94. // Set to the ORB's transportDebugFlag value. This value is
  95. // used if the ORB is null.
  96. private boolean debug = false;
  97. protected boolean littleEndian;
  98. protected com.sun.corba.se.internal.corba.ORB orb;
  99. protected ValueHandler valueHandler = null;
  100. // Value cache
  101. private CacheTable valueCache = null;
  102. // Repository ID cache
  103. private CacheTable repositoryIdCache = null;
  104. // codebase cache
  105. private CacheTable codebaseCache = null;
  106. // Current Class Stack (repository Ids of current class being read)
  107. // private Stack currentStack = null;
  108. // Length of current chunk, or a large positive number if not in a chunk
  109. protected int blockLength = maxBlockLength;
  110. // Read end flag (value nesting depth)
  111. protected int end_flag = 0;
  112. // Beginning with the resolution to interop issue 4328,
  113. // only enclosing chunked valuetypes are taken into account
  114. // when computing the nesting level. However, we still need
  115. // the old computation around for interoperability with our
  116. // older ORBs.
  117. private int chunkedValueNestingLevel = 0;
  118. // Flag used to determine whether blocksize was zero
  119. // private int checkForNullBlock = -1;
  120. // In block flag
  121. // private boolean inBlock = false;
  122. // Indicates whether we are inside a value
  123. // private boolean outerValueDone = true;
  124. // Int used by read_value(Serializable) that is set by this class
  125. // before calling ValueFactory.read_value
  126. protected int valueIndirection = 0;
  127. // Int set by readStringOrIndirection to communicate the actual
  128. // offset of the string length field back to the caller
  129. protected int stringIndirection = 0;
  130. // Flag indicating whether we are unmarshalling a chunked value
  131. protected boolean isChunked = false;
  132. // Repository ID handlers
  133. private RepositoryIdUtility repIdUtil;
  134. private RepositoryIdStrings repIdStrs;
  135. // Code set converters (created when first needed)
  136. private CodeSetConversion.BTCConverter charConverter;
  137. private CodeSetConversion.BTCConverter wcharConverter;
  138. // Template method
  139. public CDRInputStreamBase dup() {
  140. CDRInputStreamBase result;
  141. try {
  142. result = (CDRInputStreamBase)this.getClass().newInstance();
  143. } catch (InstantiationException e) {
  144. debugPrintThrowable(e);
  145. throw new INTERNAL();
  146. } catch (IllegalAccessException e) {
  147. debugPrintThrowable(e);
  148. throw new INTERNAL();
  149. }
  150. result.init(this.orb,
  151. this.bbwi.buf,
  152. this.bbwi.buflen,
  153. this.littleEndian,
  154. this.bufferManagerRead);
  155. ((CDRInputStream_1_0)result).bbwi.index = this.bbwi.index;
  156. return result;
  157. }
  158. /**
  159. * NOTE: size passed to init means buffer size
  160. */
  161. public void init(org.omg.CORBA.ORB orb,
  162. byte[] data,
  163. int size,
  164. boolean littleEndian,
  165. BufferManagerRead bufferManager)
  166. {
  167. this.orb = (com.sun.corba.se.internal.corba.ORB)orb;
  168. this.littleEndian = littleEndian;
  169. this.bufferManagerRead = bufferManager;
  170. this.bbwi = new ByteBufferWithInfo(data, 0);
  171. this.bbwi.buflen = size;
  172. this.markAndResetHandler = bufferManagerRead.getMarkAndResetHandler();
  173. // The ORB seems to be null in some cases due to primitive
  174. // TypeCodeImpls (which are singletons) not having ORB instances.
  175. if (orb != null)
  176. debug = ((com.sun.corba.se.internal.corba.ORB)orb).transportDebugFlag;
  177. }
  178. // See description in CDRInputStream
  179. void performORBVersionSpecificInit() {
  180. createRepositoryIdHandlers();
  181. }
  182. private final void createRepositoryIdHandlers()
  183. {
  184. if (orb != null) {
  185. // Get the appropriate versions based on the ORB version. The
  186. // ORB versioning info is only in the corba ORB.
  187. repIdUtil = RepositoryIdFactory.getRepIdUtility(orb);
  188. repIdStrs = RepositoryIdFactory.getRepIdStringsFactory(orb);
  189. } else {
  190. // Get the latest versions
  191. repIdUtil = RepositoryIdFactory.getRepIdUtility();
  192. repIdStrs = RepositoryIdFactory.getRepIdStringsFactory();
  193. }
  194. }
  195. public GIOPVersion getGIOPVersion() {
  196. return GIOPVersion.V1_0;
  197. }
  198. protected final int computeAlignment(int align) {
  199. if (align > 1) {
  200. int incr = bbwi.index & (align - 1);
  201. if (incr != 0)
  202. return align - incr;
  203. }
  204. return 0;
  205. }
  206. public int getSize()
  207. {
  208. return bbwi.index;
  209. }
  210. protected void checkBlockLength() {
  211. // Since chunks can end at arbitrary points (though not within
  212. // primitive CDR types, arrays of primitives, strings, or wstrings),
  213. // we must check here for termination of the current chunk.
  214. // This also takes care of terminating an open chunk when a
  215. // value tag is encountered.
  216. if (!isChunked)
  217. return;
  218. if (blockLength == get_offset()) {
  219. blockLength = maxBlockLength;
  220. start_block();
  221. } else if (blockLength < get_offset()) {
  222. // current chunk has overflowed
  223. throw new MARSHAL("Chunk overflow at offset " + get_offset(),
  224. com.sun.corba.se.internal.orbutil.MinorCodes.CHUNK_OVERFLOW,
  225. CompletionStatus.COMPLETED_NO);
  226. }
  227. }
  228. protected void alignAndCheck(int align, int n) {
  229. checkBlockLength();
  230. bbwi.index += computeAlignment(align);
  231. if (bbwi.index + n > bbwi.buflen)
  232. grow(align, n);
  233. }
  234. //
  235. // This can be overridden....
  236. //
  237. protected void grow(int align, int n) {
  238. bbwi.needed = n;
  239. bbwi = bufferManagerRead.underflow(bbwi);
  240. }
  241. //
  242. // Marshal primitives.
  243. //
  244. public final void consumeEndian() {
  245. littleEndian = read_boolean();
  246. }
  247. // No such type in java
  248. public final double read_longdouble() {
  249. throw new NO_IMPLEMENT(com.sun.corba.se.internal.orbutil.MinorCodes.SEND_DEFERRED_NOTIMPLEMENTED,
  250. CompletionStatus.COMPLETED_MAYBE);
  251. }
  252. public final boolean read_boolean() {
  253. return (read_octet() != 0);
  254. }
  255. public final char read_char() {
  256. alignAndCheck(1, 1);
  257. return getConvertedChars(1, getCharConverter())[0];
  258. }
  259. public char read_wchar() {
  260. // Don't allow transmission of wchar/wstring data with
  261. // foreign ORBs since it's against the spec.
  262. if (ORBUtility.isForeignORB((com.sun.corba.se.internal.corba.ORB)orb)) {
  263. throw new MARSHAL(MinorCodes.WCHAR_DATA_IN_GIOP_1_0,
  264. CompletionStatus.COMPLETED_MAYBE);
  265. }
  266. // If we're talking to one of our legacy ORBs, do what
  267. // they did:
  268. int b1, b2;
  269. alignAndCheck(2, 2);
  270. if (littleEndian) {
  271. b2 = bbwi.buf[bbwi.index++] & 0x00FF;
  272. b1 = bbwi.buf[bbwi.index++] & 0x00FF;
  273. } else {
  274. b1 = bbwi.buf[bbwi.index++] & 0x00FF;
  275. b2 = bbwi.buf[bbwi.index++] & 0x00FF;
  276. }
  277. return (char)((b1 << 8) + (b2 << 0));
  278. }
  279. public final byte read_octet() {
  280. alignAndCheck(1, 1);
  281. return bbwi.buf[bbwi.index++];
  282. }
  283. public final short read_short() {
  284. int b1, b2;
  285. alignAndCheck(2, 2);
  286. if (littleEndian) {
  287. b2 = (bbwi.buf[bbwi.index++] << 0) & 0x000000FF;
  288. b1 = (bbwi.buf[bbwi.index++] << 8) & 0x0000FF00;
  289. } else {
  290. b1 = (bbwi.buf[bbwi.index++] << 8) & 0x0000FF00;
  291. b2 = (bbwi.buf[bbwi.index++] << 0) & 0x000000FF;
  292. }
  293. return (short)(b1 | b2);
  294. }
  295. public final short read_ushort() {
  296. return read_short();
  297. }
  298. public final int read_long() {
  299. int b1, b2, b3, b4;
  300. alignAndCheck(4, 4);
  301. if (littleEndian) {
  302. b4 = bbwi.buf[bbwi.index++] & 0xFF;
  303. b3 = bbwi.buf[bbwi.index++] & 0xFF;
  304. b2 = bbwi.buf[bbwi.index++] & 0xFF;
  305. b1 = bbwi.buf[bbwi.index++] & 0xFF;
  306. } else {
  307. b1 = bbwi.buf[bbwi.index++] & 0xFF;
  308. b2 = bbwi.buf[bbwi.index++] & 0xFF;
  309. b3 = bbwi.buf[bbwi.index++] & 0xFF;
  310. b4 = bbwi.buf[bbwi.index++] & 0xFF;
  311. }
  312. return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
  313. }
  314. public final int read_ulong() {
  315. return read_long();
  316. }
  317. public final long read_longlong() {
  318. long i1, i2;
  319. alignAndCheck(8, 8);
  320. if (littleEndian) {
  321. i2 = read_long() & 0xFFFFFFFFL;
  322. i1 = (long)read_long() << 32;
  323. } else {
  324. i1 = (long)read_long() << 32;
  325. i2 = read_long() & 0xFFFFFFFFL;
  326. }
  327. return (i1 | i2);
  328. }
  329. public final long read_ulonglong() {
  330. return read_longlong();
  331. }
  332. public final float read_float() {
  333. return Float.intBitsToFloat(read_long());
  334. }
  335. public final double read_double() {
  336. return Double.longBitsToDouble(read_longlong());
  337. }
  338. protected final void checkForNegativeLength(int length) {
  339. if (length < 0)
  340. throw new MARSHAL("Bad string length: " + length,
  341. MinorCodes.NEGATIVE_STRING_LENGTH,
  342. CompletionStatus.COMPLETED_MAYBE);
  343. }
  344. protected final String readStringOrIndirection(boolean allowIndirection) {
  345. int len = read_long();
  346. //
  347. // Check for indirection
  348. //
  349. if (allowIndirection) {
  350. if (len == 0xffffffff)
  351. return null;
  352. else
  353. stringIndirection = get_offset() - 4;
  354. }
  355. checkForNegativeLength(len);
  356. if (orb != null && ORBUtility.isLegacyORB((com.sun.corba.se.internal.corba.ORB)orb))
  357. return legacyReadString(len);
  358. else
  359. return internalReadString(len);
  360. }
  361. private final String internalReadString(int len) {
  362. // Workaround for ORBs which send string lengths of
  363. // zero to mean empty string.
  364. // IMPORTANT: Do not replace 'new String("")' with "", it may result
  365. // in a Serialization bug (See serialization.zerolengthstring) and
  366. // bug id: 4728756 for details
  367. if (len == 0)
  368. return new String("");
  369. char[] result = getConvertedChars(len - 1, getCharConverter());
  370. // Skip over the 1 byte null
  371. read_octet();
  372. return new String(result, 0, getCharConverter().getNumChars());
  373. }
  374. private final String legacyReadString(int len) {
  375. //
  376. // Workaround for ORBs which send string lengths of
  377. // zero to mean empty string.
  378. //
  379. // IMPORTANT: Do not replace 'new String("")' with "", it may result
  380. // in a Serialization bug (See serialization.zerolengthstring) and
  381. // bug id: 4728756 for details
  382. if (len == 0)
  383. return new String("");
  384. len--;
  385. char[] c = new char[len];
  386. int n = 0;
  387. while (n < len) {
  388. int avail;
  389. int bytes;
  390. int wanted;
  391. avail = bbwi.buflen - bbwi.index;
  392. if (avail <= 0) {
  393. grow(1, 1);
  394. avail = bbwi.buflen - bbwi.index;
  395. }
  396. wanted = len - n;
  397. bytes = (wanted < avail) ? wanted : avail;
  398. for (int i=0; i<bytes; i++) {
  399. c[n+i] = (char) (bbwi.buf[bbwi.index+i] & 0xFF);
  400. }
  401. bbwi.index += bytes;
  402. n += bytes;
  403. }
  404. //
  405. // Skip past terminating null byte
  406. //
  407. if (bbwi.index + 1 > bbwi.buflen)
  408. alignAndCheck(1, 1);
  409. bbwi.index++;
  410. return new String(c);
  411. }
  412. public final String read_string() {
  413. return readStringOrIndirection(false);
  414. }
  415. public String read_wstring() {
  416. // Don't allow transmission of wchar/wstring data with
  417. // foreign ORBs since it's against the spec.
  418. if (ORBUtility.isForeignORB((com.sun.corba.se.internal.corba.ORB)orb)) {
  419. throw new MARSHAL(MinorCodes.WCHAR_DATA_IN_GIOP_1_0,
  420. CompletionStatus.COMPLETED_MAYBE);
  421. }
  422. int len = read_long();
  423. //
  424. // Workaround for ORBs which send string lengths of
  425. // zero to mean empty string.
  426. //
  427. // IMPORTANT: Do not replace 'new String("")' with "", it may result
  428. // in a Serialization bug (See serialization.zerolengthstring) and
  429. // bug id: 4728756 for details
  430. if (len == 0)
  431. return new String("");
  432. checkForNegativeLength(len);
  433. len--;
  434. char[] c = new char[len];
  435. for (int i = 0; i < len; i++)
  436. c[i] = read_wchar();
  437. // skip the two null terminator bytes
  438. read_wchar();
  439. // bbwi.index += 2;
  440. return new String(c);
  441. }
  442. public final void read_octet_array(byte[] b, int offset, int length) {
  443. if ( b == null )
  444. throw new BAD_PARAM();
  445. // Must call alignAndCheck at least once to ensure
  446. // we aren't at the end of a chunk. Of course, we
  447. // should only call it if we actually need to read
  448. // something, otherwise we might end up with an
  449. // exception at the end of the stream.
  450. if (length == 0)
  451. return;
  452. alignAndCheck(1, 1);
  453. int n = offset;
  454. while (n < length+offset) {
  455. int avail;
  456. int bytes;
  457. int wanted;
  458. avail = bbwi.buflen - bbwi.index;
  459. if (avail <= 0) {
  460. grow(1, 1);
  461. avail = bbwi.buflen - bbwi.index;
  462. }
  463. wanted = (length + offset) - n;
  464. bytes = (wanted < avail) ? wanted : avail;
  465. System.arraycopy(bbwi.buf, bbwi.index, b, n, bytes);
  466. bbwi.index += bytes;
  467. n += bytes;
  468. }
  469. }
  470. public Principal read_Principal() {
  471. int len = read_long();
  472. byte[] pvalue = new byte[len];
  473. read_octet_array(pvalue,0,len);
  474. Principal p = new PrincipalImpl();
  475. p.name(pvalue);
  476. return p;
  477. }
  478. public TypeCode read_TypeCode() {
  479. TypeCodeImpl tc = new TypeCodeImpl(orb);
  480. tc.read_value(parent);
  481. return tc;
  482. }
  483. public Any read_any() {
  484. Any any = orb.create_any();
  485. TypeCodeImpl tc = new TypeCodeImpl(orb);
  486. // read off the typecode
  487. // REVISIT We could avoid this try-catch if we could peek the typecode kind
  488. // off this stream and see if it is a tk_value.
  489. // Looking at the code we know that for tk_value the Any.read_value() below
  490. // ignores the tc argument anyway (except for the kind field).
  491. // But still we would need to make sure that the whole typecode, including
  492. // encapsulations, is read off.
  493. try {
  494. tc.read_value(parent);
  495. } catch (MARSHAL ex) {
  496. if (tc.kind().value() != TCKind._tk_value)
  497. throw ex;
  498. // We can be sure that the whole typecode encapsulation has been read off.
  499. //System.out.println("Error reading value tc " + tc + ", falling back on ValueHandler");
  500. debugPrintThrowable(ex);
  501. }
  502. // read off the value of the any
  503. any.read_value(parent, tc);
  504. return any;
  505. }
  506. public org.omg.CORBA.Object read_Object() {
  507. return read_Object(null);
  508. }
  509. // ------------ RMI related methods --------------------------
  510. // IDL to Java ptc-00-01-08 1.21.4.1
  511. //
  512. // The clz argument to read_Object can be either a stub
  513. // Class or the "Class object for the RMI/IDL interface type
  514. // that is statically expected."
  515. public org.omg.CORBA.Object read_Object(Class clz) {
  516. IOR ior = new IOR(parent) ;
  517. if (clz == null || ObjectImpl.class.isAssignableFrom(clz)) {
  518. // We were given null or a stub class
  519. return CDRInputStream_1_0.internalIORToObject(ior, clz, orb);
  520. } else {
  521. // We must have been given the Class object for the RMI/IDL
  522. // interface
  523. try {
  524. Class stubClass
  525. = Utility.loadStubClass(ior.getTypeId(),
  526. ior.getCodebase(),
  527. clz);
  528. return CDRInputStream_1_0.internalIORToObject(ior,
  529. stubClass,
  530. orb);
  531. } catch (ClassNotFoundException cnfe) {
  532. // Failed to load the stub class.
  533. throw new MARSHAL("Failed to load stub for "
  534. + ior.getTypeId()
  535. + " with Class "
  536. + (clz == null ? "null" : clz.getName()),
  537. MinorCodes.READ_OBJECT_EXCEPTION,
  538. CompletionStatus.COMPLETED_NO);
  539. }
  540. }
  541. }
  542. /*
  543. * This is used as a general utility (e.g., the PortableInterceptor
  544. * implementation uses it. NOTE: The Class passed in must be the
  545. * Stub Class.
  546. */
  547. public static org.omg.CORBA.Object internalIORToObject(
  548. IOR ior, Class stubClass, com.sun.corba.se.internal.core.ORB orb)
  549. {
  550. if (ior.is_nil())
  551. return null;
  552. if (ior.isLocal()) {
  553. // Ok so far. Can we get a valid servant?
  554. ServerSubcontract sc = ior.getServerSubcontract() ;
  555. if (sc != null && (sc.isServantSupported())) {
  556. java.lang.Object servant = sc.getServant(ior);
  557. if (servant != null ) {
  558. // Got a valid servant. Is it a Tie?
  559. if (servant instanceof Tie) {
  560. // Yes, so it is a local servant. Load a stub
  561. // for it using the codebase from the IOR and
  562. // the delegate from the tie.
  563. String codebase = ior.getCodebase();
  564. org.omg.CORBA.Object objref = (org.omg.CORBA.Object)
  565. Utility.loadStub((Tie)servant,stubClass,codebase,false);
  566. // If we managed to load a stub, return it, otherwise we
  567. // must fail...
  568. if (objref != null) {
  569. return objref;
  570. } else {
  571. throw new MARSHAL(
  572. com.sun.corba.se.internal.orbutil.MinorCodes.READ_OBJECT_EXCEPTION,
  573. CompletionStatus.COMPLETED_NO);
  574. }
  575. } else if (servant instanceof org.omg.CORBA.Object) {
  576. if (servant instanceof org.omg.CORBA.portable.InvokeHandler) {
  577. // Old ImplBase stubs are CORBA.Objects.
  578. // However, they are not stubs.
  579. return createDelegate(ior, stubClass, orb);
  580. } else {
  581. // No, so assume IDL style stub...
  582. return (org.omg.CORBA.Object) servant;
  583. }
  584. } else
  585. throw new INTERNAL( MinorCodes.BAD_SERVANT_READ_OBJECT,
  586. CompletionStatus.COMPLETED_NO ) ;
  587. }
  588. }
  589. }
  590. return createDelegate(ior, stubClass, orb);
  591. }
  592. protected static org.omg.CORBA.Object createDelegate(
  593. IOR ior, Class stubClass, com.sun.corba.se.internal.core.ORB orb)
  594. {
  595. SubcontractRegistry registry = orb.getSubcontractRegistry() ;
  596. ObjectKeyTemplate temp = ior.getProfile().getTemplate().getObjectKeyTemplate() ;
  597. ClientSubcontract rep = registry.getClientSubcontract(temp);
  598. rep.unmarshal(ior);
  599. rep.setOrb(orb);
  600. // Load stub, set the delegate and return it...
  601. return loadStub(ior,stubClass,(Delegate)rep);
  602. }
  603. protected static ObjectImpl loadStub(IOR ior,
  604. Class stubClass,
  605. Delegate delegate) {
  606. // Use the stubClass, if we have it...
  607. if (stubClass != null) {
  608. try {
  609. return newStub(stubClass,delegate);
  610. } catch (Throwable e) {
  611. if (e instanceof ThreadDeath) {
  612. throw (ThreadDeath) e;
  613. }
  614. }
  615. } else {
  616. // Try to load from the ior...
  617. try {
  618. String repID = ior.getTypeId();
  619. // If the repID is "", fall thru to returning the default
  620. // stub, otherwise try to load the class...
  621. if (repID.length() > 0) {
  622. String codebase = ior.getCodebase();
  623. Class clz = Utility.loadStubClass(repID, codebase, null); //d11638
  624. return newStub(clz,delegate);
  625. }
  626. } catch (Throwable e) {
  627. if (e instanceof ThreadDeath) {
  628. throw (ThreadDeath) e;
  629. }
  630. }
  631. // Return the "default" stub...
  632. ObjectImpl objref = new org.omg.CORBA_2_3.portable.ObjectImpl() {
  633. public String[] _ids() {
  634. String[] typeids = new String[1];
  635. typeids[0] = "IDL:omg.org/CORBA/Object:1.0";
  636. return typeids;
  637. }
  638. };
  639. objref._set_delegate(delegate);
  640. return objref;
  641. }
  642. // We failed...
  643. throw new MARSHAL(com.sun.corba.se.internal.orbutil.MinorCodes.READ_OBJECT_EXCEPTION,
  644. CompletionStatus.COMPLETED_NO);
  645. }
  646. protected static ObjectImpl newStub(Class stubClass,Delegate delegate)
  647. throws InstantiationException,
  648. IllegalAccessException,
  649. NoSuchMethodException,
  650. InvocationTargetException {
  651. // What kind of stub do we have?
  652. if (java.rmi.Remote.class.isAssignableFrom(stubClass)) {
  653. // RMI. Instantiate, set delegate and return...
  654. ObjectImpl objref = (ObjectImpl) stubClass.newInstance();
  655. objref._set_delegate(delegate);
  656. return objref;
  657. }
  658. // RMI Abstract OR IDL. Instantiate, set delegate and return...
  659. try {
  660. // Try creating the stub using the default constructor...
  661. ObjectImpl result = (ObjectImpl)stubClass.newInstance();
  662. result._set_delegate(delegate);
  663. return result;
  664. } catch (Throwable e) {
  665. if (e instanceof ThreadDeath) {
  666. throw (ThreadDeath) e;
  667. }
  668. }
  669. // Try creating the stub using the delegate constructor...
  670. Class[] intArgsClass = new Class[] {org.omg.CORBA.portable.Delegate.class};
  671. java.lang.Object[]intArgs = new java.lang.Object[] {delegate};
  672. java.lang.reflect.Constructor intArgsConstructor;
  673. intArgsConstructor = stubClass.getConstructor(intArgsClass);
  674. return (ObjectImpl)intArgsConstructor.newInstance(intArgs);
  675. }
  676. public java.lang.Object read_abstract_interface() {
  677. return read_abstract_interface(null);
  678. }
  679. public java.lang.Object read_abstract_interface(java.lang.Class clz) {
  680. boolean object = read_boolean();
  681. if (object) {
  682. return read_Object(clz);
  683. } else {
  684. return read_value();
  685. }
  686. }
  687. public Serializable read_value() {
  688. // Read value tag
  689. int vType = readValueTag();
  690. if (vType == 0)
  691. return null; // value is null
  692. else if (vType == 0xffffffff) { // Indirection tag
  693. int indirection = read_long() + get_offset() - 4;
  694. if (valueCache != null && valueCache.containsVal(indirection))
  695. {
  696. java.io.Serializable cachedValue = (java.io.Serializable)valueCache.getKey(indirection);
  697. return cachedValue;
  698. }
  699. else {
  700. throw new IndirectionException(indirection);
  701. }
  702. }
  703. else {
  704. int indirection = get_offset() - 4;
  705. // end_block();
  706. boolean saveIsChunked = isChunked;
  707. isChunked = repIdUtil.isChunkedEncoding(vType);
  708. java.lang.Object value = null;
  709. String codebase_URL = null;
  710. if (repIdUtil.isCodeBasePresent(vType)){
  711. codebase_URL = read_codebase_URL();
  712. }
  713. // Read repository id
  714. String repositoryIDString = null;
  715. switch(repIdUtil.getTypeInfo(vType)){
  716. case RepositoryIdUtility.NO_TYPE_INFO :
  717. throw new MARSHAL("read_value() with no repository ID info",
  718. MinorCodes.READ_VALUE_AND_NO_REP_ID,
  719. CompletionStatus.COMPLETED_MAYBE);
  720. case RepositoryIdUtility.SINGLE_REP_TYPE_INFO :
  721. repositoryIDString = read_repositoryId();
  722. break;
  723. case RepositoryIdUtility.PARTIAL_LIST_TYPE_INFO :
  724. repositoryIDString = read_repositoryIds();
  725. break;
  726. }
  727. // indirection = get_offset();
  728. start_block();
  729. end_flag--;
  730. if (isChunked)
  731. chunkedValueNestingLevel--;
  732. if (repositoryIDString.equals(repIdStrs.getWStringValueRepId()))
  733. {
  734. value = read_wstring();
  735. }
  736. else if (repositoryIDString.equals(repIdStrs.getClassDescValueRepId())) {
  737. // read the class either with the wrong RepId or the
  738. // correct RepId for the classDesc
  739. value = readClass();
  740. }
  741. else {
  742. Class valueClass = getClassFromString(repositoryIDString,
  743. codebase_URL);
  744. if ((valueClass != null) &&
  745. org.omg.CORBA.portable.IDLEntity.class.isAssignableFrom(valueClass)) {
  746. value = readIDLValue(indirection, repositoryIDString, valueClass, codebase_URL);
  747. } else {
  748. try {
  749. // cannot cache this since this value will be different
  750. if (valueHandler == null) {
  751. valueHandler = ORBUtility.createValueHandler(orb);
  752. }
  753. value = valueHandler.readValue(parent, indirection, valueClass,
  754. repositoryIDString, getCodeBase());
  755. } catch(Exception ex) {
  756. debugPrintThrowable(ex);
  757. throw new org.omg.CORBA.MARSHAL("Unable to read value from underlying bridge : "
  758. + ex.getMessage(),
  759. MinorCodes.VALUEHANDLER_READ_EXCEPTION,
  760. CompletionStatus.COMPLETED_MAYBE);
  761. } catch(Error e) {
  762. debugPrintThrowable(e);
  763. throw new org.omg.CORBA.MARSHAL("Unable to read value from underlying bridge : "
  764. + e.getMessage(),
  765. MinorCodes.VALUEHANDLER_READ_ERROR,
  766. CompletionStatus.COMPLETED_MAYBE);
  767. }
  768. }
  769. }
  770. handleEndOfValue();
  771. readEndTag();
  772. // Put into valueCache
  773. if (valueCache == null)
  774. valueCache = new CacheTable(false);
  775. valueCache.put(value, indirection);
  776. // allow for possible continuation chunk
  777. isChunked = saveIsChunked;
  778. start_block();
  779. return (java.io.Serializable)value;
  780. }
  781. }
  782. public Serializable read_value(Class expectedType) {
  783. // Read value tag
  784. int vType = readValueTag();
  785. if (vType == 0)
  786. return null; // value is null
  787. else if (vType == 0xffffffff) { // Indirection tag
  788. int indirection = read_long() + get_offset() - 4;
  789. if (valueCache != null && valueCache.containsVal(indirection))
  790. {
  791. java.io.Serializable cachedValue = (java.io.Serializable)valueCache.getKey(indirection);
  792. return cachedValue;
  793. }
  794. else {
  795. throw new IndirectionException(indirection);
  796. }
  797. }
  798. else {
  799. int indirection = get_offset() - 4;
  800. // end_block();
  801. boolean saveIsChunked = isChunked;
  802. isChunked = repIdUtil.isChunkedEncoding(vType);
  803. java.lang.Object value = null;
  804. String codebase_URL = null;
  805. if (repIdUtil.isCodeBasePresent(vType)){
  806. codebase_URL = read_codebase_URL();
  807. }
  808. // Read repository id
  809. String repositoryIDString = null;
  810. switch(repIdUtil.getTypeInfo(vType)){
  811. case RepositoryIdUtility.NO_TYPE_INFO :
  812. // Throw an exception if we have no repository ID info and
  813. // no expectedType to work with. Otherwise, how would we
  814. // know what to unmarshal?
  815. if (expectedType == null)
  816. throw new MARSHAL("Expected type null and no repository ID info",
  817. MinorCodes.EXPECTED_TYPE_NULL_AND_NO_REP_ID,
  818. CompletionStatus.COMPLETED_MAYBE);
  819. repositoryIDString = repIdStrs.createForAnyType(expectedType);
  820. break;
  821. case RepositoryIdUtility.SINGLE_REP_TYPE_INFO :
  822. repositoryIDString = read_repositoryId();
  823. break;
  824. case RepositoryIdUtility.PARTIAL_LIST_TYPE_INFO :
  825. repositoryIDString = read_repositoryIds();
  826. break;
  827. }
  828. // indirection = get_offset();
  829. start_block();
  830. end_flag--;
  831. if (isChunked)
  832. chunkedValueNestingLevel--;
  833. if (repositoryIDString.equals(repIdStrs.getWStringValueRepId()))
  834. {
  835. value = read_wstring();
  836. }
  837. else if (repositoryIDString.equals(repIdStrs.getClassDescValueRepId()))
  838. {
  839. // read in the class whether with the old ClassDesc or the
  840. // new one
  841. value = readClass();
  842. }
  843. else {
  844. Class valueClass = expectedType;
  845. // By this point, either the expectedType or repositoryIDString
  846. // is guaranteed to be non-null.
  847. if (expectedType == null ||
  848. !repositoryIDString.equals(repIdStrs.createForAnyType(expectedType))) {
  849. valueClass = getClassFromString(repositoryIDString,
  850. codebase_URL,
  851. expectedType);
  852. }
  853. if ((valueClass != null) &&
  854. org.omg.CORBA.portable.IDLEntity.class.isAssignableFrom(valueClass)) {
  855. value = readIDLValue(indirection, repositoryIDString, valueClass, codebase_URL);
  856. } else {
  857. try {
  858. if (valueHandler == null) {
  859. valueHandler = ORBUtility.createValueHandler(orb);
  860. }
  861. value = valueHandler.readValue(parent, indirection, valueClass,
  862. repositoryIDString, getCodeBase());
  863. } catch(Exception ex) {
  864. debugPrintThrowable(ex);
  865. throw new org.omg.CORBA.MARSHAL("Unable to read value from underlying bridge : "
  866. + ex.getMessage(),
  867. MinorCodes.VALUEHANDLER_READ_EXCEPTION,
  868. CompletionStatus.COMPLETED_MAYBE);
  869. } catch(Error e){
  870. debugPrintThrowable(e);
  871. throw new org.omg.CORBA.MARSHAL("Unable to read value from underlying bridge : "
  872. + e.getMessage(),
  873. MinorCodes.VALUEHANDLER_READ_ERROR,
  874. CompletionStatus.COMPLETED_MAYBE);
  875. }
  876. }
  877. }
  878. handleEndOfValue();
  879. readEndTag();
  880. // Put into valueCache
  881. if (valueCache == null)
  882. valueCache = new CacheTable(false);
  883. valueCache.put(value, indirection);
  884. // allow for possible continuation chunk
  885. isChunked = saveIsChunked;
  886. start_block();
  887. return (java.io.Serializable)value;
  888. }
  889. }
  890. public Serializable read_value(BoxedValueHelper factory) {
  891. // Read value tag
  892. int vType = readValueTag();
  893. if (vType == 0)
  894. return null; // value is null
  895. else if (vType == 0xffffffff) { // Indirection tag
  896. int indirection = read_long() + get_offset() - 4;
  897. if (valueCache != null && valueCache.containsVal(indirection))
  898. {
  899. java.io.Serializable cachedValue = (java.io.Serializable)valueCache.getKey(indirection);
  900. return cachedValue;
  901. }
  902. else {
  903. throw new IndirectionException(indirection);
  904. }
  905. }
  906. else {
  907. int indirection = get_offset() - 4;
  908. // end_block();
  909. boolean saveIsChunked = isChunked;
  910. isChunked = repIdUtil.isChunkedEncoding(vType);
  911. java.lang.Object value = null;
  912. String codebase_URL = null;
  913. if (repIdUtil.isCodeBasePresent(vType)){
  914. codebase_URL = read_codebase_URL();
  915. }
  916. // Read repository id
  917. String repositoryIDString = null;
  918. switch(repIdUtil.getTypeInfo(vType)){
  919. case RepositoryIdUtility.NO_TYPE_INFO :
  920. throw new org.omg.CORBA.MARSHAL("No class description available (value_tag indicates no type information present)");
  921. case RepositoryIdUtility.SINGLE_REP_TYPE_INFO :
  922. repositoryIDString = read_repositoryId();
  923. break;
  924. case RepositoryIdUtility.PARTIAL_LIST_TYPE_INFO :
  925. repositoryIDString = read_repositoryIds();
  926. break;
  927. }
  928. // Compare rep. ids to see if we should use passed helper
  929. if (!repositoryIDString.equals(factory.get_id()))
  930. factory = Utility.getHelper(null, codebase_URL, repositoryIDString);
  931. start_block();
  932. end_flag--;
  933. if (isChunked)
  934. chunkedValueNestingLevel--;
  935. if (factory instanceof ValueHelper) {
  936. value = readIDLValueWithHelper((ValueHelper)factory, indirection);
  937. } else {
  938. valueIndirection = indirection; // for callback
  939. value = factory.read_value(parent);
  940. }
  941. handleEndOfValue();
  942. readEndTag();
  943. // Put into valueCache
  944. if (valueCache == null)
  945. valueCache = new CacheTable(false);
  946. valueCache.put(value, indirection);
  947. // allow for possible continuation chunk
  948. isChunked = saveIsChunked;
  949. start_block();
  950. return (java.io.Serializable)value;
  951. }
  952. }
  953. private boolean isCustomType(ValueHelper helper) {
  954. try{
  955. TypeCode tc = helper.get_type();
  956. int kind = tc.kind().value();
  957. if (kind == TCKind._tk_value) {
  958. return (tc.type_modifier() == org.omg.CORBA.VM_CUSTOM.value);
  959. }
  960. }
  961. catch(BadKind ex) {
  962. throw new org.omg.CORBA.MARSHAL(MinorCodes.BAD_KIND,
  963. CompletionStatus.COMPLETED_MAYBE);
  964. }
  965. return false;
  966. }
  967. // This method is actually called indirectly by
  968. // read_value(String repositoryId).
  969. // Therefore, it is not a truly independent read call that handles
  970. // header information itself.
  971. public java.io.Serializable read_value(java.io.Serializable value) {
  972. // Put into valueCache using valueIndirection
  973. if (valueCache == null)
  974. valueCache = new CacheTable(false);
  975. valueCache.put(value, valueIndirection);
  976. if (value instanceof StreamableValue)
  977. ((StreamableValue)value)._read(parent);
  978. else if (value instanceof CustomValue)
  979. ((CustomValue)value).unmarshal(parent);
  980. return value;
  981. }
  982. public java.io.Serializable read_value(java.lang.String repositoryId) {
  983. // if (inBlock)
  984. // end_block();
  985. // Read value tag
  986. int vType = readValueTag();
  987. if (vType == 0)
  988. return null; // value is null
  989. else if (vType == 0xffffffff) { // Indirection tag
  990. int indirection = read_long() + get_offset() - 4;
  991. if (valueCache != null && valueCache.containsVal(indirection))
  992. {
  993. java.io.Serializable cachedValue = (java.io.Serializable)valueCache.getKey(indirection);
  994. return cachedValue;
  995. }
  996. else {
  997. throw new IndirectionException(indirection);
  998. }
  999. }
  1000. else {
  1001. int indirection = get_offset() - 4;
  1002. // end_block();
  1003. boolean saveIsChunked = isChunked;
  1004. isChunked = repIdUtil.isChunkedEncoding(vType);
  1005. java.lang.Object value = null;
  1006. String codebase_URL = null;
  1007. if (repIdUtil.isCodeBasePresent(vType)){
  1008. codebase_URL = read_codebase_URL();
  1009. }
  1010. // Read repository id
  1011. String repositoryIDString = null;
  1012. switch(repIdUtil.getTypeInfo(vType)){
  1013. case RepositoryIdUtility.NO_TYPE_INFO :
  1014. repositoryIDString = repositoryId;
  1015. break;
  1016. case RepositoryIdUtility.SINGLE_REP_TYPE_INFO :
  1017. repositoryIDString = read_repositoryId();
  1018. break;
  1019. case RepositoryIdUtility.PARTIAL_LIST_TYPE_INFO :
  1020. repositoryIDString = read_repositoryIds();
  1021. break;
  1022. }
  1023. ValueFactory factory = Utility.getFactory(null, codebase_URL, orb, repositoryIDString);
  1024. start_block();
  1025. end_flag--;
  1026. if (isChunked)
  1027. chunkedValueNestingLevel--;
  1028. valueIndirection = indirection; // for callback
  1029. value = factory.read_value(parent);
  1030. handleEndOfValue();
  1031. readEndTag();
  1032. // Put into valueCache
  1033. if (valueCache == null)
  1034. valueCache = new CacheTable(false);
  1035. valueCache.put(value, indirection);
  1036. // allow for possible continuation chunk
  1037. isChunked = saveIsChunked;
  1038. start_block();
  1039. return (java.io.Serializable)value;
  1040. }
  1041. }
  1042. private Class readClass() {
  1043. String codebases = null, classRepId = null;
  1044. if (orb == null ||
  1045. ORBVersionImpl.FOREIGN.equals(orb.getORBVersion()) ||
  1046. ORBVersionImpl.NEWER.compareTo(orb.getORBVersion()) <= 0) {
  1047. codebases = (String)read_value(java.lang.String.class);
  1048. classRepId = (String)read_value(java.lang.String.class);
  1049. } else {
  1050. // Pre-Merlin/J2EE 1.3 ORBs wrote the repository ID
  1051. // and codebase strings in the wrong order.
  1052. classRepId = (String)read_value(java.lang.String.class);
  1053. codebases = (String)read_value(java.lang.String.class);
  1054. }
  1055. if (debug) {
  1056. debugPrintMessage("readClass codebases: "
  1057. + codebases
  1058. + " rep Id: "
  1059. + classRepId);
  1060. }
  1061. Class cl = null;
  1062. RepositoryIdInterface repositoryID
  1063. = repIdStrs.getFromString(classRepId);
  1064. try {
  1065. cl = repositoryID.getClassFromType(codebases);
  1066. } catch(ClassNotFoundException cnfe){
  1067. debugPrintThrowable(cnfe);
  1068. throw new org.omg.CORBA.MARSHAL("Unable to load Class "
  1069. + repositoryID.getClassName()
  1070. + " : " + cnfe.getMessage(),
  1071. MinorCodes.CNFE_READ_CLASS,
  1072. CompletionStatus.COMPLETED_MAYBE);
  1073. }
  1074. catch(MalformedURLException me){
  1075. debugPrintThrowable(me);
  1076. throw new org.omg.CORBA.MARSHAL("Unable to load Class "
  1077. + repositoryID.getClassName()
  1078. + " : " + me.getMessage(),
  1079. MinorCodes.MALFORMED_URL,
  1080. CompletionStatus.COMPLETED_MAYBE);
  1081. }
  1082. return cl;
  1083. }
  1084. private java.lang.Object readIDLValueWithHelper(ValueHelper helper, int indirection) {
  1085. // look for two-argument static read method
  1086. Method readMethod;
  1087. try {
  1088. Class argTypes[] = {org.omg.CORBA.portable.InputStream.class, helper.get_class()};
  1089. readMethod = helper.getClass().getDeclaredMethod(kReadMethod, argTypes);
  1090. }
  1091. catch(NoSuchMethodException nsme) { // must be boxed value helper
  1092. java.lang.Object result = helper.read_value(parent);
  1093. return result;
  1094. }
  1095. // found two-argument read method, so must be non-boxed value...
  1096. // ...create a blank instance
  1097. java.lang.Object val = null;
  1098. try {
  1099. val = helper.get_class().newInstance();
  1100. }
  1101. catch(java.lang.InstantiationException ie){
  1102. debugPrintThrowable(ie);
  1103. throw new org.omg.CORBA.MARSHAL(ie.getMessage());
  1104. }
  1105. catch(IllegalAccessException iae){
  1106. // Value's constructor is protected or private
  1107. //
  1108. // So, use the helper to read the value.
  1109. //
  1110. // NOTE : This means that in this particular case a recursive ref.
  1111. // would fail.
  1112. return helper.read_value(parent);
  1113. }
  1114. // add blank instance to cache table
  1115. if (valueCache == null)
  1116. valueCache = new CacheTable(false);
  1117. valueCache.put(val, indirection);
  1118. // if custom type, call unmarshal method
  1119. if (val instanceof CustomMarshal && isCustomType(helper)) {
  1120. ((CustomMarshal)val).unmarshal(parent);
  1121. return val;
  1122. }
  1123. // call two-argument read method using reflection
  1124. try {
  1125. java.lang.Object args[] = {parent, val};
  1126. readMethod.invoke(helper, args);
  1127. return val;
  1128. }
  1129. catch(IllegalAccessException iae2){
  1130. debugPrintThrowable(iae2);
  1131. throw new org.omg.CORBA.MARSHAL(iae2.getMessage());
  1132. }
  1133. catch(InvocationTargetException ite){
  1134. debugPrintThrowable(ite);
  1135. throw new org.omg.CORBA.MARSHAL(ite.getMessage());
  1136. }
  1137. }
  1138. private java.lang.Object readBoxedIDLEntity(Class clazz, String codebase)
  1139. {
  1140. try {
  1141. ClassLoader clazzLoader = (clazz == null ? null : clazz.getClassLoader());
  1142. final Class helperClass = Utility.loadClassForClass(clazz.getName()+"Helper", codebase,
  1143. clazzLoader, clazz, clazzLoader