summaryrefslogtreecommitdiff
path: root/libjava/classpath/gnu/javax/rmi/CORBA/RmiUtilities.java
blob: ac6b9070570282c7571854b2390b3a83586ac1a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
/* RmiUtilities.java --
   Copyright (C) 2005 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package gnu.javax.rmi.CORBA;

import gnu.CORBA.OrbFunctional;
import gnu.CORBA.Minor;
import gnu.CORBA.Unexpected;
import gnu.CORBA.CDR.Vio;
import gnu.CORBA.CDR.gnuRuntime;
import gnu.CORBA.CDR.gnuValueStream;
import gnu.CORBA.CDR.HeadlessInput;

import gnu.java.lang.CPStringBuilder;

import org.omg.CORBA.MARSHAL;
import org.omg.CORBA.StringValueHelper;
import org.omg.CORBA.WStringValueHelper;
import org.omg.CORBA.portable.Delegate;
import org.omg.CORBA.portable.InputStream;
import org.omg.CORBA.portable.ObjectImpl;
import org.omg.CORBA.portable.OutputStream;
import org.omg.CORBA.portable.ValueBase;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import org.omg.PortableServer.Servant;
import org.omg.PortableServer.POAManagerPackage.State;
import org.omg.SendingContext.RunTime;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.rmi.Remote;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
import java.util.WeakHashMap;

import javax.rmi.PortableRemoteObject;
import javax.rmi.CORBA.Stub;
import javax.rmi.CORBA.Tie;
import javax.rmi.CORBA.Util;

/**
 * Defines methods that must be accessible in several derived classes.
 *
 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
 */
public class RmiUtilities
{
  /**
   * The currently used RMI-IIOP version format.
   */
  public static byte VERSION = 1;

  /**
   * The non - writable class fields.
   */
  static final int NON_WRITABLE = Modifier.STATIC | Modifier.TRANSIENT;

  /**
   * The standard String repository Id.
   */
  public static final String RMI_STRING_ID = StringValueHelper.id();

  /**
   * The standard Class repository Id.
   */
  public static final String RMI_CLASS_ID = "RMI:javax.rmi.CORBA.ClassDesc:2BABDA04587ADCCC:CFBF02CF5294176B";

  /**
   * The standard string array repository Id.
   */
  public static final String RMI_STRING_ARRAY_ID = "RMI:[Ljava.lang.String;:071DA8BE7F971128:A0F0A4387A3BB342";

  /**
   * An instance of the wide string value helper for writing strings.
   */
  static WStringValueHelper wStringValueHelper = new WStringValueHelper();

  /**
   * Set of serializable classes that have .writeObject and .readObject defined.
   * Contains weak references to ensure that the classes will be unloadable.
   */
  WeakHashMap io_format = new WeakHashMap();

  /**
   * The standard IO format with no .writeObject and .readObject defined.
   */
  static final Object STANDARD = new Object();

  /**
   * The custom IO format with .writeObject and .readObject defined,
   * defaultWriteObject called.
   */
  static final Object CUSTOM_DWO = new Object();

  /**
   * The custom IO format with .writeObject and .readObject defined,
   * defaultWriteObject has not been called.
   */
  static final Object CUSTOM_NO_DWO = new Object();

  /**
   * The arguments for readObject.
   */
  static final Class[] READ_OBJECT_ARGS = new Class[] { ObjectInputStream.class };

  /**
   * The arguments for writeObject.
   */
  static final Class[] WRITE_OBJECT_ARGS = new Class[] { ObjectOutputStream.class };

  /**
   * The undocumented field that is heading the Sun's object data, written with
   * writeObject.
   */
  static final int S_X = 16908034;

  /**
   * Write all fields of the passed value.
   */
  void writeFields(OutputStream an_output, Serializable object)
  {
    org.omg.CORBA_2_3.portable.OutputStream output = (org.omg.CORBA_2_3.portable.OutputStream) an_output;
    try
      {
        Class o_class = object.getClass();
        Field[] fields = getWritableFields(o_class);
        Field f;

        Class fc;

        for (int i = 0; i < fields.length; i++)
          {
            f = fields[i];
            fc = f.getType();
            Object v = f.get(object);

            if (fc == String.class)
              {
                output.write_value((Serializable) v, wStringValueHelper);
              }
            else if (fc == int.class)
              output.write_long(((Integer) v).intValue());
            else if (fc == long.class)
              output.write_longlong(((Number) v).longValue());
            else if (fc == double.class)
              output.write_double(((Number) v).doubleValue());
            else if (fc == float.class)
              output.write_float(((Number) v).floatValue());
            else if (fc == boolean.class)
              output.write_boolean(((Boolean) v).booleanValue());
            else if (fc == short.class)
              output.write_short(((Number) v).shortValue());
            else if (fc == byte.class)
              output.write_octet(((Number) v).byteValue());
            else if (fc == char.class)
              output.write_wchar(((Character) v).charValue());
            else
              {
                if (!fc.isInterface() && Remote.class.isAssignableFrom(fc))
                  fc = getExportedInterface(fc);
                writeMember(output, v, fc);
              }
          }
      }
    catch (Exception ex)
      {
        MARSHAL m = new MARSHAL("Cannot write " + object);
        m.minor = Minor.ValueFields;
        m.initCause(ex);
        throw m;
      }
  }

  /**
   * Write a memeber (field) of the data structure.
   */
  void writeMember(org.omg.CORBA_2_3.portable.OutputStream output,
    Object object, Class xClass)
  {
    if (output instanceof gnuValueStream)
      {
        gnuRuntime g = ((gnuValueStream) output).getRunTime();
        // Reset the target as we are already beyond the critical point
        // where is must have the value being written.
        if (g != null)
          g.target = null;
      }
    if (Serializable.class.isAssignableFrom(xClass)
      || Remote.class.isAssignableFrom(xClass))
      {
        // Object handles null reference on its own.
        if (org.omg.CORBA.Object.class.isAssignableFrom(xClass)
          || Remote.class.isAssignableFrom(xClass))
          {
            if (object == null)
              output.write_Object(null);
            else if (isTieRequired(object))
              exportTie(output, object, xClass);
            else
              writeValue(output, (Serializable) object);
          }
        else
          output.write_value((Serializable) object, xClass);
      }
    else
      {
        MARSHAL m = new MARSHAL(xClass + " is not Serializable");
        m.minor = Minor.NonSerializable;
        throw m;
      }
  }

  /**
   * Check if the object must be wrapped into Tie, connected to the ORB and then
   * the corresponding Stub be written.
   */
  public boolean isTieRequired(Object object)
  {
    return object instanceof Remote && !(object instanceof Stub);
  }

  /**
   * Get the interface under that the class of this object must be exposed. The
   * interface must be derived from Remote.
   */
  Class getExportedInterface(Object object)
    throws MARSHAL
  {
    Class fc = null;
    Class[] interfaces = object.getClass().getInterfaces();
    for (int i = 0; i < interfaces.length; i++)
      {
        if (!Remote.class.equals(interfaces[i]))
          if (Remote.class.isAssignableFrom(interfaces[i]))
            {
              if (fc == null)
                fc = interfaces[i];
              else
                {
                  MARSHAL m = new MARSHAL("Both " + fc + " and " + interfaces[i]
                  + " extends Remote");
                  m.minor = Minor.TargetConversion;
                  throw m;
                }
            }
      }
    if (fc == null)
      {
        MARSHAL m = new MARSHAL(object.getClass()
        + " does not implement any interface, derived from Remote");
        m.minor = Minor.TargetConversion;
        throw m;
      }
    return fc;
  }

  /**
   * Get the persistent hash code for the given class, as defined by OMG
   * standard. The inheritance, field names and types (but not the visibility)
   * are taken into consideration as well as the presence of the writeObject
   * method are taken into consideration. The class name and methods, if any,
   * are not taken into consideration.
   */
  public static long getHashCode(Class c)
  {
    Class of = c.isArray() ? c.getComponentType() : null;
    if (c.isArray()
      && ((!Serializable.class.isAssignableFrom(of) || of.isPrimitive() || Remote.class.isAssignableFrom(of))))
      return 0;
    if (!Serializable.class.isAssignableFrom(c))
      return 0;
    try
      {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        DataOutputStream out = new DataOutputStream(bout);

        Class superClass = c.getSuperclass();
        if (superClass != null)
          out.writeLong(getHashCode(superClass));

        int writeObjectPresentCode;
        try
          {
            c.getDeclaredMethod("writeObject",
              new Class[] { ObjectOutputStream.class });
            writeObjectPresentCode = 2; // Exists.
          }
        catch (NoSuchMethodException e)
          {
            writeObjectPresentCode = 1; // Missing.
          }
        out.writeInt(writeObjectPresentCode);

        Field[] fields = c.getDeclaredFields();

        Arrays.sort(fields, new Comparator()
        {
          public int compare(Object a, Object b)
          {
            Field fa = (Field) a;
            Field fb = (Field) b;
            return fa.getName().compareTo(fb.getName());
          }
        });

        Field f;
        for (int i = 0; i < fields.length; i++)
          {
            f = fields[i];
            if ((f.getModifiers() & NON_WRITABLE) == 0)
              {
                out.writeUTF(f.getName());
                out.writeUTF(getDescriptor(f.getType()));
              }
          }

        out.flush();
        out.close();
        MessageDigest shaDigest;
        try
          {
            shaDigest = MessageDigest.getInstance("SHA");
          }
        catch (Exception ex)
          {
            throw new InternalError("SHA digesting algorithm is not available");
          }

        // Return the digest value to the calling
        // method as an array of bytes.
        byte[] sha = shaDigest.digest(bout.toByteArray());

        long hash = 0;
        for (int i = 0; i < Math.min(8, sha.length); i++)
          {
            hash += (long) (sha[i] & 255) << (i * 8);
          }
        return hash;
      }
    catch (IOException ioex)
      {
        throw new Unexpected(ioex);
      }
  }

  /**
   * Converts to hexadecimal string, supplementing leading zeros.
   */
  public static String toHex(long l)
  {
    CPStringBuilder b = new CPStringBuilder();
    b.append(Long.toHexString(l).toUpperCase());
    while (b.length() < 16)
      b.insert(0, '0');
    return b.toString();
  }

  /**
   * Returns a <code>String</code> representing the type-encoding of a class.
   */
  static String getDescriptor(Class type)
  {
    if (type.equals(boolean.class))
      return "Z";
    if (type.equals(byte.class))
      return "B";
    if (type.equals(short.class))
      return "S";
    if (type.equals(char.class))
      return "C";
    if (type.equals(int.class))
      return "I";
    if (type.equals(long.class))
      return "J";
    if (type.equals(float.class))
      return "F";
    if (type.equals(double.class))
      return "D";
    if (type.equals(void.class))
      return "V";
    else if (type.isArray())
      {
        CPStringBuilder l = new CPStringBuilder("[");
        Class component = type.getComponentType();

        while (component.isArray())
          {
            l.append('[');
            component = component.getComponentType();
          }

        l.append('L');
        l.append(component.getName().replace('.', '/'));
        l.append(';');
        return l.toString();
      }
    else
      return "L" + type.getName().replace('.', '/') + ';';
  }

  public static Field[] getWritableFields(Class c)
  {
    TreeSet set = new TreeSet(new Comparator()
    {
      public int compare(Object a, Object b)
      {
        return ((Field) a).getName().compareTo(((Field) b).getName());
      }
    });

    while (!c.equals(Object.class))
      {
        Field[] f = c.getDeclaredFields();
        for (int i = 0; i < f.length; i++)
          {
            if ((f[i].getModifiers() & NON_WRITABLE) == 0)
              {
                f[i].setAccessible(true);
                set.add(f[i]);
              }
          }
        c = c.getSuperclass();
      }

    Field[] r = new Field[set.size()];
    int p = 0;
    Iterator it = set.iterator();
    while (it.hasNext())
      {
        r[p++] = (Field) it.next();
      }
    return r;
  }

  /**
   * The method is called for Remotes that are not Stubs. It is assumed, that
   * the Remote is an implementation. The method searches for the suitable tie
   * and, if found, exports it by creating and connecting the stub. Such export
   * is supported since jdk 1.5.
   */
  void exportTie(org.omg.CORBA_2_3.portable.OutputStream output,
    Object implementation, Class interfaceClass)
  {
    try
      {
        // Remote, but non - stub class (implementation)
        // must be replaced by stub.
        Tie t = Util.getTie((Remote) implementation);
        if (t instanceof Servant)
          {
            POA rootPoa = POAHelper.narrow(output.orb().resolve_initial_references(
              "RootPOA"));
            org.omg.CORBA.Object co = rootPoa.servant_to_reference((Servant) t);
            Stub stub = (Stub) PortableRemoteObject.narrow(co, interfaceClass);
            writeRemoteObject(output, stub);

            if (rootPoa.the_POAManager().get_state().value() == State._HOLDING)
              rootPoa.the_POAManager().activate();
          }
        else if (t instanceof org.omg.CORBA.Object)
          {
            org.omg.CORBA.Object co = (org.omg.CORBA.Object) t;
            output.orb().connect(co);

            Stub stub = (Stub) PortableRemoteObject.narrow(co, interfaceClass);
            writeRemoteObject(output, stub);
          }
      }
    catch (Exception ex)
      {
        MARSHAL m = new MARSHAL("Unable to export " + implementation);
        m.minor = Minor.TargetConversion;
        m.initCause(ex);
        throw m;
      }
  }

  /**
   * Start the ORB, if it is not already runnning.
   */
  void ensureOrbRunning(org.omg.CORBA_2_3.portable.OutputStream output)
  {
    // Ensure ORB is running.
    if (output.orb() instanceof OrbFunctional)
      {
        ((OrbFunctional) output.orb()).ensureRunning();
      }
  }

  /**
   * Write data to the CORBA output stream. Writes the object contents only; the
   * header must be already written. For object, containing objects, may be
   * called recursively.
   *
   * @param an_output a stream to write to, must be
   * org.omg.CORBA_2_3.portable.OutputStream
   * @param object an object to write.
   */
  public void writeRemoteObject(OutputStream an_output, Object object)
  {
    org.omg.CORBA_2_3.portable.OutputStream output = (org.omg.CORBA_2_3.portable.OutputStream) an_output;

    if (isTieRequired(object))
      {
        // Find the interface that is implemented by the object and extends
        // Remote.
        Class fc = getExportedInterface(object);
        exportTie(output, object, fc);
      }
    else if (object instanceof org.omg.CORBA.Object)
      {
        ensureOrbRunning(output);
        an_output.write_Object((org.omg.CORBA.Object) object);
      }
    else if (object != null && object instanceof Serializable)
      writeFields(an_output, (Serializable) object);
  }

  /**
   * Write data to the CORBA output stream. Writes the object contents only; the
   * header must be already written. For object, containing objects, may be
   * called recursively.
   *
   * @param an_output a stream to write to, must be
   * org.omg.CORBA_2_3.portable.OutputStream
   * @param object an object to write.
   */
  public void writeValue(OutputStream an_output, Serializable object)
  {
    org.omg.CORBA_2_3.portable.OutputStream output = (org.omg.CORBA_2_3.portable.OutputStream) an_output;

    if (isTieRequired(object))
      {
        // Find the interface that is implemented by the object and extends
        // Remote.
        Class fc = getExportedInterface(object);
        exportTie(output, object, fc);
      }
    else if (object instanceof org.omg.CORBA.Object)
      {
        ensureOrbRunning(output);
        an_output.write_Object((org.omg.CORBA.Object) object);
      }
    else if (object instanceof Externalizable)
      {
        try
          {
            ObjectOutputStream stream = new CorbaOutput(output, object,
              this);
            stream.write(VERSION);
            ((Externalizable) object).writeExternal(stream);
          }
        catch (Exception ex)
          {
            MARSHAL m = new MARSHAL("writeExternal failed");
            m.minor = Minor.Value;
            m.initCause(ex);
            throw m;
          }
      }
    else if (object instanceof Serializable)
      {
        Object mode = null;
        synchronized (io_format)
          {
            mode = io_format.get(object.getClass());
            if (mode == STANDARD)
              {
                writeFields(an_output, (Serializable) object);
                return;
              }
          }
        try
          {
            Method m = object.getClass().getDeclaredMethod("writeObject",
              WRITE_OBJECT_ARGS);
            m.setAccessible(true); // May be private.

            try
              {
                ObjectOutputStream stream = new CorbaOutput(output,
                  object, this);

                // Write version.
                stream.write(VERSION);

                if (mode == CUSTOM_DWO)
                  // Write true, supposing that the defaultWriteObject
                  // has been called.
                  stream.write(1);
                else if (mode == CUSTOM_NO_DWO)
                  // Write false (has not been called)
                  stream.write(0);
                else
                  {
                    // Measure.
                    DefaultWriteObjectTester tester = new DefaultWriteObjectTester(object);
                    m.invoke(object, new Object[] { tester });

                    synchronized (io_format)
                      {
                        io_format.put(object.getClass(),
                          tester.dwo_called ? CUSTOM_DWO : CUSTOM_NO_DWO);
                        stream.write(tester.dwo_called ? 1 : 0);
                      }
                  }

                m.invoke(object, new Object[] { stream });
                stream.flush();
              }
            catch (Exception ex)
              {
                MARSHAL mx = new MARSHAL(object.getClass().getName()
                  + ".writeObject failed");
                mx.initCause(ex);
                throw mx;
              }
          }
        catch (NoSuchMethodException e)
          {
            // Write in a standard way.
            writeFields(an_output, (Serializable) object);
            synchronized (io_format)
              {
                io_format.put(object.getClass(), STANDARD);
              }
          }
      }
  }

  /**
   * Read data from the CDR input stream. Reads the object contents only; the
   * header must be already read (the repository id or ids ara passed). For
   * object, containing objects, may be called recursively.
   *
   * @param an_input the stream to read from, must be
   * org.omg.CORBA_2_3.portable.InputStream
   * @param object the instance of the object being read.
   * @param id the repository Id from the stream in the case when single id was
   * specified.
   * @param ids the repository Ids from the stream in the case when multiple ids
   * were specified.
   * @param codebase the codebase, if it was included in the header of the value
   * type. Null if not codebase was included.
   *
   * @return the object, extracted from the stream.
   */
  /**
   * Read value from the input stream in the case when the value is not
   * Streamable or CustomMarshalled.
   */
  public Serializable readValue(InputStream in, int offset, Class clz,
    String repositoryID, RunTime sender)
  {
    if (in instanceof HeadlessInput)
      ((HeadlessInput) in).subsequentCalls = true;

    gnuRuntime g = null;
    Serializable object = null;

    try
      {
        g = (gnuRuntime) sender;
        if (sender != null)
          object = g.target;
      }
    catch (ClassCastException e)
      {
        // Working with the other CORBA implementation.
        g = null;
      }

    org.omg.CORBA_2_3.portable.InputStream input = (org.omg.CORBA_2_3.portable.InputStream) in;

    if (Remote.class.isAssignableFrom(clz)
      || ValueBase.class.isAssignableFrom(clz))
      {
        // Interface is narrowed into Stub.
        if (clz.isInterface())
          try
            {
              clz = Util.loadClass(
                PortableRemoteObjectDelegateImpl.getStubClassName(clz.getName()),
                null, clz.getClassLoader());
            }
          catch (ClassNotFoundException e)
            {
              MARSHAL m = new MARSHAL("Cannot get stub from interface "
                + clz.getClass().getName());
              m.minor = Minor.TargetConversion;
              m.initCause(e);
              throw m;
            }

        // Remote needs special handling.
        if (ObjectImpl.class.isAssignableFrom(clz))
          {
            // First read CORBA object reference.
            Object ro = input.read_Object();

            ObjectImpl obj = (ObjectImpl) ro;
            if (obj == null)
              return null;

            Delegate delegate = obj._get_delegate();
            object = instantiate(offset, clz, g);
            ((ObjectImpl) object)._set_delegate(delegate);
          }
        // The object - specific data follows.
      }
    else if (org.omg.CORBA.Object.class.isAssignableFrom(clz))
      object = (Serializable) input.read_Object();

    if (object == null)
      object = instantiate(offset, clz, g);

    // The sentence below prevents attempt to read the internal fields of the
    // ObjectImpl (or RMI Stub) that might follow the object definition.
    // Sun's jre 1.5 does not write this information. The stubs, generated
    // by rmic, does not contain such fields.
    if (object instanceof ObjectImpl)
      return object;

    if (object instanceof Externalizable)
      {
        try
          {
            CorbaInput stream = new CorbaInput(input, object, this,
              offset, repositoryID, g);

            byte version = stream.readByte();
            if (version != 1)
              throw new MARSHAL("Unsuported RMI-IIOP version " + version);

            ((Externalizable) object).readExternal(stream);
          }
        catch (Exception ex)
          {
            MARSHAL m = new MARSHAL("readExternal failed");
            m.initCause(ex);
            throw m;
          }
      }
    else
      {
        Object mode = null;
        synchronized (io_format)
          {
            mode = io_format.get(object.getClass());
          }

        if (mode == STANDARD)
          {
            readFields(offset, repositoryID, object, input, g);
          }
        else
          {
            try
              {
                Method m = object.getClass().getDeclaredMethod("readObject",
                  READ_OBJECT_ARGS);
                try
                  {
                    m.setAccessible(true); // May be private.

                    CorbaInput stream = new CorbaInput(input,
                      object, this, offset, repositoryID, g);

                    byte version = stream.readByte();
                    if (version != 1)
                      throw new MARSHAL("Unsuported RMI-IIOP version "
                        + version);

                    // This would indicate is defaultWriteObject has been
                    // called,
                    // but the readObject method normally takes care about this.
                    boolean dwo = stream.readByte() != 0;

                    m.invoke(object, new Object[] { stream });
                    synchronized (io_format)
                      {
                        io_format.put(object.getClass(), dwo ? CUSTOM_DWO
                          : CUSTOM_NO_DWO);
                      }
                  }
                catch (Exception ex)
                  {
                    ex.printStackTrace();
                    MARSHAL mx = new MARSHAL(object.getClass().getName()
                      + ".readObject failed");
                    mx.initCause(ex);
                    throw mx;
                  }
              }
            catch (NoSuchMethodException e)
              {
                // Read in a standard way.
                synchronized (io_format)
                  {
                    io_format.put(object.getClass(), STANDARD);
                    readFields(offset, repositoryID, object, input, g);
                  }
              }
          }
      }
    return object;
  }

  /**
   * Create an instance.
   */
  Serializable instantiate(int offset, Class clz, gnuRuntime g)
    throws MARSHAL
  {
    Serializable object;
    try
      {
        object = (Serializable) Vio.instantiateAnyWay(clz);
        g.objectWritten(object, offset);
      }
    catch (Exception e)
      {
        MARSHAL m = new MARSHAL("Unable to instantiate " + clz);
        m.minor = Minor.Instantiation;
        m.initCause(e);
        throw m;
      }
    return object;
  }

  /**
   * Read fields of the object.
   */
  void readFields(int offset, String repositoryID, Serializable object,
    org.omg.CORBA_2_3.portable.InputStream input, gnuRuntime r)
    throws MARSHAL
  {
    Field f = null;
    Class o_class = object.getClass();

    try
      {
        // The returned field array must already be in canonical order.
        Field[] fields = getWritableFields(o_class);

        Class fc;

        for (int i = 0; i < fields.length; i++)
          {
            // Full value type header expected ahead.
            if (input instanceof HeadlessInput)
              ((HeadlessInput) input).subsequentCalls = true;

            f = fields[i];
            fc = f.getType();

            Object v;

            if (fc == String.class)
              {
                v = input.read_value(wStringValueHelper);
              }
            else if (fc == int.class)
              v = new Integer(input.read_long());
            else if (fc == long.class)
              v = new Long(input.read_longlong());
            else if (fc == double.class)
              v = new Double(input.read_double());
            else if (fc == float.class)
              v = new Float(input.read_float());
            else if (fc == boolean.class)
              v = input.read_boolean() ? Boolean.TRUE : Boolean.FALSE;
            else if (fc == short.class)
              v = new Short(input.read_short());
            else if (fc == byte.class)
              v = new Byte(input.read_octet());
            else if (fc == char.class)
              v = new Character(input.read_char());
            else if (org.omg.CORBA.Object.class.isAssignableFrom(fc)
              || Remote.class.isAssignableFrom(fc))
              {
                v = readValue(input, offset, fc, null, r);
              }
            else
              {
                v = Vio.read(input, fc);
              }

            f.set(object, v);
          }
      }
    catch (Exception ex)
      {
        MARSHAL m = new MARSHAL("Cannot read " + o_class.getName() + " field "
          + f);
        m.initCause(ex);
        m.minor = Minor.ValueFields;
        throw m;
      }
  }

}