KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > iiop > rmi > marshal > CDRStream


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.iiop.rmi.marshal;
23
24 import java.io.Externalizable JavaDoc;
25 import java.io.Serializable JavaDoc;
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import javax.rmi.CORBA.Util JavaDoc;
29 import javax.rmi.PortableRemoteObject JavaDoc;
30 import org.omg.CORBA.portable.IDLEntity JavaDoc;
31 import org.omg.CORBA_2_3.portable.InputStream JavaDoc;
32 import org.omg.CORBA_2_3.portable.OutputStream JavaDoc;
33
34 import org.jboss.iiop.rmi.RmiIdlUtil;
35
36 /**
37  * Utility class with static methods to:
38  * <ul>
39  * <li>get the <code>CDRStreamReader</code> for a given class</li>
40  * <li>get the <code>CDRStreamWriter</code> for a given class</li>
41  * </ul>
42  *
43  * The <code>CDRStreamReader</code>s and <code>CDRStreamWriter</code>s
44  * returned by these methods are instances of static inner classes
45  * defined by <code>CDRStream</code>.
46  *
47  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
48  * @version $Revision: 37459 $
49  */

50 public class CDRStream
51 {
52    /**
53     * Returns the abbreviated name of the marshaller for given
54     * <code>Class</code>.
55     *
56     * <p>Abbreviated names of marshallers for basic types follow the usual
57     * Java convention:
58     * <br>
59     * <pre>
60     * type abbrev name
61     * boolean "Z"
62     * byte "B"
63     * char "C"
64     * double "D"
65     * float "F"
66     * int "I"
67     * long "J"
68     * short "S"
69     * void "V"
70     * </pre>
71     *
72     * <p>The abbreviated names of marshallers for object types are:
73     * <br>
74     * <pre>
75     * java.lang.String "G" (strinG)
76     * RMI remote interface "R" + interfaceName
77     * RMI abstract interface "A"
78     * serializable "E" (sErializablE)
79     * valuetype "L" + className
80     * externalizable "X" (eXternalizable)
81     * org.omg.CORBA.Object "M" (oMg)
82     * IDL interface "N" + interfaceName
83     * java.lang.Object "O"
84     * </pre>
85     *
86     * <p>As an example: the abbreviated name of a marshaller for a valuetype
87     * class named <code>Foo</code> is the string <code>"LFoo"</code>.
88     */

89    public static String JavaDoc abbrevFor(Class JavaDoc clz)
90    {
91       if (clz == Boolean.TYPE) {
92          return "Z";
93       }
94       else if (clz == Byte.TYPE) {
95          return "B";
96       }
97       else if (clz == Character.TYPE) {
98          return "C";
99       }
100       else if (clz == Double.TYPE) {
101          return "D";
102       }
103       else if (clz == Float.TYPE) {
104          return "F";
105       }
106       else if (clz == Integer.TYPE) {
107          return "I";
108       }
109       else if (clz == Long.TYPE) {
110          return "J";
111       }
112       else if (clz == Short.TYPE) {
113          return "S";
114       }
115       else if (clz == Void.TYPE) {
116          return "V";
117       }
118       else if (clz == String JavaDoc.class) {
119          return "G"; // strinG
120
}
121       else if (RmiIdlUtil.isRMIIDLRemoteInterface(clz)) {
122          return "R" + clz.getName(); // Remote interface
123
}
124       else if (clz == org.omg.CORBA.Object JavaDoc.class) {
125          return "M"; // oMg (CORBA Object)
126
}
127       else if (org.omg.CORBA.Object JavaDoc.class.isAssignableFrom(clz)) {
128          return "N" + clz.getName(); // IDL iNterface
129
}
130       else if (IDLEntity JavaDoc.class.isAssignableFrom(clz)) {
131          return "L" + clz.getName(); // vaLuetype
132
}
133       else if (clz == Serializable JavaDoc.class) {
134          return "E"; // sErializablE
135
}
136       else if (RmiIdlUtil.isAbstractInterface(clz)) {
137          return "A"; // Abstract interface
138
}
139       else if (Serializable JavaDoc.class.isAssignableFrom(clz)) {
140          return "L" + clz.getName(); // vaLuetype
141
}
142       else if (Externalizable JavaDoc.class.isAssignableFrom(clz)) {
143          return "X"; // eXternalizable
144
}
145       else if (clz == Object JavaDoc.class) {
146          return "O"; // Object
147
}
148       else {
149          return "L" + clz.getName(); // vaLuetype
150
}
151    }
152
153    /**
154     * Returns a <code>CDRStreamReader</code> given an abbreviated name
155     * and a <code>ClassLoader</code> for valuetype classes.
156     */

157    public static CDRStreamReader readerFor(String JavaDoc s, ClassLoader JavaDoc cl)
158    {
159       switch (s.charAt(0)) {
160
161       case 'A':
162          return AbstractInterfaceReader.instance;
163       case 'B':
164          return ByteReader.instance;
165       case 'C':
166          return CharReader.instance;
167       case 'D':
168          return DoubleReader.instance;
169       case 'E':
170          return SerializableReader.instance;
171       case 'F':
172          return FloatReader.instance;
173       case 'G':
174          return StringReader.instance;
175       case 'I':
176          return IntReader.instance;
177       case 'J':
178          return LongReader.instance;
179       case 'L':
180          try {
181             // Use Class.forName() (rather than cl.loadClass()), because
182
// Class.forName() loads Java array types (which are valuetypes).
183
return new ValuetypeReader(Class.forName(s.substring(1),
184                                                      true,
185                                                      cl));
186          }
187          catch (ClassNotFoundException JavaDoc e) {
188             throw new RuntimeException JavaDoc("Error loading class "
189                                        + s.substring(1) + ": " + e);
190          }
191       case 'M':
192          return CorbaObjectReader.instance;
193       case 'N':
194          try {
195             return new IdlInterfaceReader(cl.loadClass(s.substring(1)));
196          }
197          catch (ClassNotFoundException JavaDoc e) {
198             throw new RuntimeException JavaDoc("Error loading class "
199                                        + s.substring(1) + ": " + e);
200          }
201       case 'O':
202          return ObjectReader.instance;
203       case 'R':
204          try {
205             return new RemoteReader(cl.loadClass(s.substring(1)));
206          }
207          catch (ClassNotFoundException JavaDoc e) {
208             throw new RuntimeException JavaDoc("Error loading class "
209                                        + s.substring(1) + ": " + e);
210          }
211       case 'S':
212          return ShortReader.instance;
213       case 'V':
214          return null;
215       case 'X':
216          return ExternalizableReader.instance;
217       case 'Z':
218          return BooleanReader.instance;
219       default:
220          return null;
221       }
222    }
223
224    /**
225     * Returns a <code>CDRStreamWriter</code> given an abbreviated name
226     * and a <code>ClassLoader</code> for valuetype classes.
227     */

228    public static CDRStreamWriter writerFor(String JavaDoc s, ClassLoader JavaDoc cl)
229    {
230       switch (s.charAt(0)) {
231
232       case 'A':
233          return AbstractInterfaceWriter.instance;
234       case 'B':
235          return ByteWriter.instance;
236       case 'C':
237          return CharWriter.instance;
238       case 'D':
239          return DoubleWriter.instance;
240       case 'E':
241          return SerializableWriter.instance;
242       case 'F':
243          return FloatWriter.instance;
244       case 'G':
245          return StringWriter.instance;
246       case 'I':
247          return IntWriter.instance;
248       case 'J':
249          return LongWriter.instance;
250       case 'L':
251          try {
252             // Use Class.forName() (rather than cl.loadClass()), because
253
// Class.forName() loads Java array types (which are valuetypes).
254
return new ValuetypeWriter(Class.forName(s.substring(1),
255                                                      true,
256                                                      cl));
257          }
258          catch (ClassNotFoundException JavaDoc e) {
259             throw new RuntimeException JavaDoc("Error loading class "
260                                        + s.substring(1) + ": " + e);
261          }
262       case 'M':
263          return CorbaObjectWriter.instance;
264       case 'N':
265          try {
266             return new IdlInterfaceWriter(cl.loadClass(s.substring(1)));
267          }
268          catch (ClassNotFoundException JavaDoc e) {
269             throw new RuntimeException JavaDoc("Error loading class "
270                                        + s.substring(1) + ": " + e);
271          }
272       case 'O':
273          return ObjectWriter.instance;
274       case 'R':
275          return RemoteWriter.instance;
276       case 'S':
277          return ShortWriter.instance;
278       case 'V':
279          return null;
280       case 'X':
281          return ExternalizableWriter.instance;
282       case 'Z':
283          return BooleanWriter.instance;
284       default:
285          return null;
286       }
287    }
288
289    /**
290     * Returns the <code>CDRStreamReader</code> for a given <code>Class</code>.
291     */

292    public static CDRStreamReader readerFor(Class JavaDoc clz)
293    {
294       if (clz == Boolean.TYPE) {
295          return BooleanReader.instance;
296       }
297       else if (clz == Byte.TYPE) {
298          return ByteReader.instance;
299       }
300       else if (clz == Character.TYPE) {
301          return CharReader.instance;
302       }
303       else if (clz == Double.TYPE) {
304          return DoubleReader.instance;
305       }
306       else if (clz == Float.TYPE) {
307          return FloatReader.instance;
308       }
309       else if (clz == Integer.TYPE) {
310          return IntReader.instance;
311       }
312       else if (clz == Long.TYPE) {
313          return LongReader.instance;
314       }
315       else if (clz == Short.TYPE) {
316          return ShortReader.instance;
317       }
318       else if (clz == Void.TYPE) {
319          return null;
320       }
321       else if (clz == String JavaDoc.class) {
322          return StringReader.instance;
323       }
324       else if (RmiIdlUtil.isRMIIDLRemoteInterface(clz)) {
325          return new RemoteReader(clz);
326       }
327       else if (clz == org.omg.CORBA.Object JavaDoc.class) {
328          return CorbaObjectReader.instance;
329       }
330       else if (org.omg.CORBA.Object JavaDoc.class.isAssignableFrom(clz)) {
331          return new IdlInterfaceReader(clz);
332       }
333       else if (IDLEntity JavaDoc.class.isAssignableFrom(clz)) {
334          return new ValuetypeReader(clz);
335       }
336       else if (clz == Serializable JavaDoc.class) {
337          return SerializableReader.instance;
338       }
339       else if (RmiIdlUtil.isAbstractInterface(clz)) {
340          return AbstractInterfaceReader.instance;
341       }
342       else if (Serializable JavaDoc.class.isAssignableFrom(clz)) {
343          return new ValuetypeReader(clz);
344       }
345       else if (Externalizable JavaDoc.class.isAssignableFrom(clz)) {
346          return ExternalizableReader.instance;
347       }
348       else if (clz == Object JavaDoc.class) {
349          return ObjectReader.instance;
350       }
351       else {
352          return new ValuetypeReader(clz);
353       }
354    }
355
356    /**
357     * Returns the <code>CDRStreamWriter</code> for a given <code>Class</code>.
358     */

359    public static CDRStreamWriter writerFor(Class JavaDoc clz)
360    {
361       if (clz == Boolean.TYPE) {
362          return BooleanWriter.instance;
363       }
364       else if (clz == Byte.TYPE) {
365          return ByteWriter.instance;
366       }
367       else if (clz == Character.TYPE) {
368          return CharWriter.instance;
369       }
370       else if (clz == Double.TYPE) {
371          return DoubleWriter.instance;
372       }
373       else if (clz == Float.TYPE) {
374          return FloatWriter.instance;
375       }
376       else if (clz == Integer.TYPE) {
377          return IntWriter.instance;
378       }
379       else if (clz == Long.TYPE) {
380          return LongWriter.instance;
381       }
382       else if (clz == Short.TYPE) {
383          return ShortWriter.instance;
384       }
385       else if (clz == String JavaDoc.class) {
386          return StringWriter.instance;
387       }
388       else if (clz == Void.TYPE) {
389          return null;
390       }
391       else if (RmiIdlUtil.isRMIIDLRemoteInterface(clz)) {
392          return RemoteWriter.instance;
393       }
394       else if (clz == org.omg.CORBA.Object JavaDoc.class) {
395          return CorbaObjectWriter.instance;
396       }
397       else if (org.omg.CORBA.Object JavaDoc.class.isAssignableFrom(clz)) {
398          return new IdlInterfaceWriter(clz);
399       }
400       else if (IDLEntity JavaDoc.class.isAssignableFrom(clz)) {
401          return new ValuetypeWriter(clz);
402       }
403       else if (clz == Serializable JavaDoc.class) {
404          return SerializableWriter.instance;
405       }
406       else if (RmiIdlUtil.isAbstractInterface(clz)) {
407          return AbstractInterfaceWriter.instance;
408       }
409       else if (Serializable JavaDoc.class.isAssignableFrom(clz)) {
410          return new ValuetypeWriter(clz);
411       }
412       else if (Externalizable JavaDoc.class.isAssignableFrom(clz)) {
413          return ExternalizableWriter.instance;
414       }
415       else if (clz == Object JavaDoc.class) {
416          return ObjectWriter.instance;
417       }
418       else {
419          return new ValuetypeWriter(clz);
420       }
421    }
422
423    // Private -----------------------------------------------------------------
424

425    // Static inner classes (all of them private) ------------------------------
426

427    /**
428     * Singleton class that unmarshals <code>boolean</code>s from a CDR input
429     * stream.
430     */

431    private static final class BooleanReader
432          implements CDRStreamReader
433    {
434       static final CDRStreamReader instance = new BooleanReader();
435       
436       private BooleanReader() { }
437       
438       public Object JavaDoc read(InputStream JavaDoc in)
439       {
440          return new Boolean JavaDoc(in.read_boolean());
441       }
442    }
443
444    /**
445     * Singleton class that unmarshals <code>byte</code>s from a CDR input
446     * stream.
447     */

448    private static final class ByteReader
449          implements CDRStreamReader
450    {
451       static final CDRStreamReader instance = new ByteReader();
452       
453       private ByteReader() { }
454       
455       public Object JavaDoc read(InputStream JavaDoc in)
456       {
457          return new Byte JavaDoc(in.read_octet());
458       }
459    }
460
461    /**
462     * Singleton class that unmarshals <code>char</code>s from a CDR input
463     * stream.
464     */

465    private static final class CharReader
466          implements CDRStreamReader
467    {
468       static final CDRStreamReader instance = new CharReader();
469       
470       private CharReader() { }
471       
472       public Object JavaDoc read(InputStream JavaDoc in)
473       {
474          return new Character JavaDoc(in.read_wchar());
475       }
476    }
477
478    /**
479     * Singleton class that unmarshals <code>double</code>s from a CDR input
480     * stream.
481     */

482    private static final class DoubleReader
483          implements CDRStreamReader
484    {
485       static final CDRStreamReader instance = new DoubleReader();
486       
487       private DoubleReader() { }
488       
489       public Object JavaDoc read(InputStream JavaDoc in)
490       {
491          return new Double JavaDoc(in.read_double());
492       }
493    }
494    
495    /**
496     * Singleton class that unmarshals <code>float</code>s from a CDR input
497     * stream.
498     */

499    private static final class FloatReader
500          implements CDRStreamReader
501    {
502       static final CDRStreamReader instance = new FloatReader();
503       
504       private FloatReader() { }
505       
506       public Object JavaDoc read(InputStream JavaDoc in)
507       {
508          return new Float JavaDoc(in.read_float());
509       }
510    }
511    
512    /**
513     * Singleton class that unmarshals <code>int</code>s from a CDR input
514     * stream.
515     */

516    private static final class IntReader
517          implements CDRStreamReader
518    {
519       static final CDRStreamReader instance = new IntReader();
520       
521       private IntReader() { }
522       
523       public Object JavaDoc read(InputStream JavaDoc in)
524       {
525          return new Integer JavaDoc(in.read_long());
526       }
527    }
528    
529    /**
530     * Singleton class that unmarshals <code>long</code>s from a CDR input
531     * stream.
532     */

533    private static final class LongReader
534          implements CDRStreamReader
535    {
536       static final CDRStreamReader instance = new LongReader();
537       
538       private LongReader() { }
539       
540       public Object JavaDoc read(InputStream JavaDoc in)
541       {
542          return new Long JavaDoc(in.read_longlong());
543       }
544    }
545
546    /**
547     * Singleton class that unmarshals <code>short</code>s from a CDR input
548     * stream.
549     */

550    private static final class ShortReader
551          implements CDRStreamReader
552    {
553       static final CDRStreamReader instance = new ShortReader();
554       
555       private ShortReader() { }
556       
557       public Object JavaDoc read(InputStream JavaDoc in)
558       {
559          return new Short JavaDoc(in.read_short());
560       }
561    }
562    
563    /**
564     * Singleton class that unmarshals <code>String</code>s from a CDR input
565     * stream.
566     */

567    private static final class StringReader
568          implements CDRStreamReader
569    {
570       static final CDRStreamReader instance = new StringReader();
571       
572       private StringReader() { }
573       
574       public Object JavaDoc read(InputStream JavaDoc in)
575       {
576          return in.read_value(String JavaDoc.class);
577       }
578    }
579    
580    /**
581     * Class that unmarshals <code>java.rmi.Remote</code> objects from a CDR
582     * input stream. A <code>RemoteReader</code> is specific for a given
583     * remote interface, which is passed as a parameter to the
584     * <code>RemoteReader</code> constructor.
585     */

586    private static final class RemoteReader
587          implements CDRStreamReader
588    {
589       private Class JavaDoc clz;
590       
591       RemoteReader(Class JavaDoc clz) {
592          this.clz = clz;
593       }
594       
595       public Object JavaDoc read(InputStream JavaDoc in)
596       {
597          // The narrow() call downloads the stub from the codebase embedded
598
// within the IOR of the unmarshalled object.
599
return PortableRemoteObject.narrow(in.read_Object(), clz);
600       }
601    }
602
603    /**
604     * Singleton class that unmarshals objects whose declared type is
605     * <code>java.lang.Object</code> from a CDR input stream.
606     */

607    private static final class ObjectReader
608          implements CDRStreamReader
609    {
610       static final CDRStreamReader instance = new ObjectReader();
611       
612       private ObjectReader() { }
613       
614       public Object JavaDoc read(InputStream JavaDoc in)
615       {
616          return Util.readAny(in);
617       }
618    }
619    
620    /**
621     * Singleton class that unmarshals objects whose declared type is
622     * <code>java.io.Serializable</code> from a CDR input stream.
623     */

624    private static final class SerializableReader
625          implements CDRStreamReader
626    {
627       static final CDRStreamReader instance = new SerializableReader();
628       
629       private SerializableReader() { }
630       
631       public Object JavaDoc read(InputStream JavaDoc in)
632       {
633          return (Serializable JavaDoc)Util.readAny(in);
634       }
635    }
636
637    /**
638     * Singleton class that unmarshals objects whose declared type is
639     * <code>java.io.Externalizable</code> from a CDR input stream.
640     */

641    private static final class ExternalizableReader
642          implements CDRStreamReader
643    {
644       static final CDRStreamReader instance = new ExternalizableReader();
645       
646       private ExternalizableReader() { }
647       
648       public Object JavaDoc read(InputStream JavaDoc in)
649       {
650          return (Externalizable JavaDoc)Util.readAny(in);
651       }
652    }
653
654    /**
655     * Singleton class that unmarshals objects whose declared type is
656     * <code>org.omg.CORBA.Object</code> from a CDR input stream.
657     */

658    private static final class CorbaObjectReader
659          implements CDRStreamReader
660    {
661       static final CDRStreamReader instance = new CorbaObjectReader();
662       
663       private CorbaObjectReader() { }
664       
665       public Object JavaDoc read(InputStream JavaDoc in)
666       {
667          return (org.omg.CORBA.Object JavaDoc)in.read_Object();
668       }
669    }
670    
671    /**
672     * Class that unmarshals IDL interfaces from a CDR input stream.
673     * An <code>IdlInterfaceReader</code> is specific for objects that
674     * implement the Java interface passed as a parameter to the
675     * <code>IdlInterfaceReader</code> constructor. This Java interface
676     * must extend <code>org.omg.CORBA.Object</code>.
677     */

678    private static final class IdlInterfaceReader
679          implements CDRStreamReader
680    {
681       static private Class JavaDoc[] paramTypes =
682          { org.omg.CORBA.portable.InputStream JavaDoc.class };
683
684       // The readMethod for this IdlInterfaceReader.
685
private Method JavaDoc readMethod = null;
686       
687       IdlInterfaceReader(Class JavaDoc clz)
688       {
689          String JavaDoc helperClassName = clz.getName() + "Helper";
690          
691          try {
692             Class JavaDoc helperClass =
693                clz.getClassLoader().loadClass(helperClassName);
694             readMethod = helperClass.getMethod("read", paramTypes);
695          }
696          catch (ClassNotFoundException JavaDoc e) {
697             throw new RuntimeException JavaDoc("Error loading class "
698                                        + helperClassName + ": " + e);
699          }
700          catch (NoSuchMethodException JavaDoc e) {
701             throw new RuntimeException JavaDoc("No read method in helper class "
702                                        + helperClassName + ": " + e);
703          }
704       }
705       
706       public Object JavaDoc read(InputStream JavaDoc in)
707       {
708          try {
709             return readMethod.invoke(null, new Object JavaDoc[] { in });
710          }
711          catch (IllegalAccessException JavaDoc e) {
712             throw new RuntimeException JavaDoc("Internal error: " + e);
713          }
714          catch (InvocationTargetException JavaDoc e) {
715             throw new RuntimeException JavaDoc("Exception unmarshaling CORBA object: "
716                                        + e.getTargetException());
717          }
718       }
719    }
720    
721    /**
722     * Singleton class that unmarshals from a CDR output stream objects whose
723     * declared type is an interface that does not extend
724     * <code>java.rmi.Remote</code> and whose declared methods
725     * (including inherited ones) all throw
726     * <code>java.rmi.RemoteException</code>.
727     */

728    private static final class AbstractInterfaceReader
729          implements CDRStreamReader
730    {
731       static final CDRStreamReader instance = new AbstractInterfaceReader();
732       
733       private AbstractInterfaceReader() { }
734       
735       public Object JavaDoc read(InputStream JavaDoc in)
736       {
737          return in.read_abstract_interface();
738       }
739    }
740
741    /**
742     * Class that unmarshals valuetypes from a CDR input stream.
743     * A <code>ValuetypeReader</code> is specific for objects of a given class,
744     * which is passed as a parameter to the <code>ValuetypeReader</code>
745     * constructor and must implement <code>java.io.Serializable</code>.
746     */

747    private static final class ValuetypeReader
748          implements CDRStreamReader
749    {
750       private Class JavaDoc clz;
751       
752       ValuetypeReader(Class JavaDoc clz) {
753          this.clz = clz;
754       }
755       
756       public Object JavaDoc read(InputStream JavaDoc in)
757       {
758          return in.read_value(clz);
759       }
760    }
761    
762    /**
763     * Singleton class that marshals <code>boolean</code>s into a CDR output
764     * stream.
765     */

766    private static final class BooleanWriter
767          implements CDRStreamWriter
768    {
769       static final CDRStreamWriter instance = new BooleanWriter();
770       
771       private BooleanWriter() { }
772       
773       public void write(OutputStream JavaDoc out, Object JavaDoc obj)
774       {
775          out.write_boolean(((Boolean JavaDoc)obj).booleanValue());
776       }
777    }
778
779    /**
780     * Singleton class that marshals <code>byte</code>s into a CDR output
781     * stream.
782     */

783    private static final class ByteWriter
784          implements CDRStreamWriter
785    {
786       static final CDRStreamWriter instance = new ByteWriter();
787       
788       private ByteWriter() { }
789       
790       public void write(OutputStream JavaDoc out, Object JavaDoc obj)
791       {
792          out.write_octet(((Byte JavaDoc)obj).byteValue());
793       }
794    }
795
796    /**
797     * Singleton class that marshals <code>char</code>s into a CDR output
798     * stream.
799     */

800    private static final class CharWriter
801          implements CDRStreamWriter
802    {
803       
804       static final CDRStreamWriter instance = new CharWriter();
805       
806       private CharWriter() { }
807       
808       public void write(OutputStream JavaDoc out, Object JavaDoc obj)
809       {
810          out.write_wchar(((Character JavaDoc)obj).charValue());
811       }
812    }
813
814    /**
815     * Singleton class that marshals <code>double</code>s into a CDR output
816     * stream.
817     */

818    private static final class DoubleWriter
819          implements CDRStreamWriter
820    {
821       static final CDRStreamWriter instance = new DoubleWriter();
822       
823       private DoubleWriter() { }
824       
825       public void write(OutputStream JavaDoc out, Object JavaDoc obj)
826       {
827          out.write_double(((Double JavaDoc)obj).doubleValue());
828       }
829    }
830
831    /**
832     * Singleton class that marshals <code>float</code>s into a CDR output
833     * stream.
834     */

835    private static final class FloatWriter
836          implements CDRStreamWriter
837    {
838       static final CDRStreamWriter instance = new FloatWriter();
839       
840       private FloatWriter() { }
841       
842       public void write(OutputStream JavaDoc out, Object JavaDoc obj)
843       {
844          out.write_float(((Float JavaDoc)obj).floatValue());
845       }
846    }
847
848    /**
849     * Singleton class that marshals <code>int</code>s into a CDR output
850     * stream.
851     */

852    private static final class IntWriter
853          implements CDRStreamWriter
854    {
855       static final CDRStreamWriter instance = new IntWriter();
856       
857       private IntWriter() { }
858       
859       public void write(OutputStream JavaDoc out, Object JavaDoc obj)
860       {
861          out.write_long(((Integer JavaDoc)obj).intValue());
862       }
863    }
864
865    /**
866     * Singleton class that marshals <code>long</code>s into a CDR output
867     * stream.
868     */

869    private static final class LongWriter
870          implements CDRStreamWriter
871    {
872       static final CDRStreamWriter instance = new LongWriter();
873       
874       private LongWriter() { }
875       
876       public void write(OutputStream JavaDoc out, Object JavaDoc obj)
877       {
878          out.write_longlong(((Long JavaDoc)obj).longValue());
879       }
880    }
881
882    /**
883     * Singleton class that marshals <code>short</code>s into a CDR output
884     * stream.
885     */

886    private static final class ShortWriter
887          implements CDRStreamWriter
888    {
889       static final CDRStreamWriter instance = new ShortWriter();
890       
891       private ShortWriter() { }
892       
893       public void write(OutputStream JavaDoc out, Object JavaDoc obj)
894       {
895          out.write_short(((Short JavaDoc)obj).shortValue());
896       }
897    }
898
899    /**
900     * Singleton class that marshals <code>String</code>s into a CDR output
901     * stream.
902     */

903    private static final class StringWriter
904          implements CDRStreamWriter
905    {
906       static final CDRStreamWriter instance = new StringWriter();
907       
908       private StringWriter() { }
909       
910       public void write(OutputStream JavaDoc out, Object JavaDoc obj)
911       {
912          out.write_value((String JavaDoc)obj, String JavaDoc.class);
913       }
914    }
915
916    /**
917     * Singleton class that marshals <code>java.rmi.Remote</code> objects into
918     * a CDR output stream.
919     */

920    private static final class RemoteWriter
921          implements CDRStreamWriter
922    {
923       static final CDRStreamWriter instance = new RemoteWriter();
924       
925       private RemoteWriter() { }
926       
927       public void write(OutputStream JavaDoc out, Object JavaDoc obj)
928       {
929          out.write_Object((org.omg.CORBA.Object JavaDoc)obj);
930       }
931    }
932    
933    /**
934     * Singleton class that marshals objects whose declared type is
935     * <code>java.lang.Object</code> into a CDR output stream.
936     */

937    private static final class ObjectWriter
938          implements CDRStreamWriter
939    {
940       static final CDRStreamWriter instance = new ObjectWriter();
941       
942       private ObjectWriter() { }
943       
944       public void write(OutputStream JavaDoc out, Object JavaDoc obj)
945       {
946          Util.writeAny(out, obj);
947       }
948    }
949    
950    /**
951     * Singleton class that marshals objects whose declared type is
952     * <code>java.io.Serializable</code> into a CDR output stream.
953     */

954    private static final class SerializableWriter
955          implements CDRStreamWriter
956    {
957       static final CDRStreamWriter instance = new SerializableWriter();
958       
959       private SerializableWriter() { }
960       
961       public void write(OutputStream JavaDoc out, Object JavaDoc obj)
962       {
963          Util.writeAny(out, (Serializable JavaDoc)obj);
964       }
965    }
966    
967    /**
968     * Singleton class that marshals objects whose declared type is
969     * <code>java.io.Externalizable</code> into a CDR output stream.
970     */

971    private static final class ExternalizableWriter
972          implements CDRStreamWriter
973    {
974       static final CDRStreamWriter instance = new ExternalizableWriter();
975       
976       private ExternalizableWriter() { }
977       
978       public void write(OutputStream JavaDoc out, Object JavaDoc obj)
979       {
980          Util.writeAny(out, (Externalizable JavaDoc)obj);
981       }
982    }
983    
984    /**
985     * Singleton class that marshals objects whose declared type is
986     * <code>org.omg.CORBA.Object</code> into a CDR output stream.
987     */

988    private static final class CorbaObjectWriter
989          implements CDRStreamWriter
990    {
991       static final CDRStreamWriter instance = new CorbaObjectWriter();
992       
993       private CorbaObjectWriter() { }
994       
995       public void write(OutputStream JavaDoc out, Object JavaDoc obj)
996       {
997          out.write_Object((org.omg.CORBA.Object JavaDoc)obj);
998       }
999    }
1000   
1001   /**
1002    * Class that marshals IDL interfaces into a CDR output stream.
1003    * An <code>IdlInterfaceWriter</code> is specific for objects that
1004    * implement the Java interface is passed as a parameter to the
1005    * <code>IdlInterfaceWriter</code> constructor. This Java interface
1006    * must extend <code>org.omg.CORBA.Object</code>.
1007    */

1008   private static final class IdlInterfaceWriter
1009         implements CDRStreamWriter
1010   {
1011      // The writeMethod for this IdlInterfaceReader.
1012
private Method JavaDoc writeMethod = null;
1013      
1014      IdlInterfaceWriter(Class JavaDoc clz)
1015      {
1016         String JavaDoc helperClassName = clz.getName() + "Helper";
1017         
1018         try {
1019            Class JavaDoc helperClass =
1020               clz.getClassLoader().loadClass(helperClassName);
1021            Class JavaDoc[] paramTypes = {
1022               org.omg.CORBA.portable.OutputStream JavaDoc.class,
1023               clz
1024            };
1025            writeMethod = helperClass.getMethod("write", paramTypes);
1026         }
1027         catch (ClassNotFoundException JavaDoc e) {
1028            throw new RuntimeException JavaDoc("Error loading class "
1029                                       + helperClassName + ": " + e);
1030         }
1031         catch (NoSuchMethodException JavaDoc e) {
1032            throw new RuntimeException JavaDoc("No write method in helper class "
1033                                       + helperClassName + ": " + e);
1034         }
1035      }
1036      
1037      public void write(OutputStream JavaDoc out, Object JavaDoc obj)
1038      {
1039         try {
1040            writeMethod.invoke(null, new Object JavaDoc[] { out, obj });
1041         }
1042         catch (IllegalAccessException JavaDoc e) {
1043            throw new RuntimeException JavaDoc("Internal error: " + e);
1044         }
1045         catch (InvocationTargetException JavaDoc e) {
1046            throw new RuntimeException JavaDoc("Exception marshaling CORBA object: "
1047                                       + e.getTargetException());
1048         }
1049      }
1050   }
1051      
1052   /**
1053    * Singleton class that marshals into a CDR output stream objects whose
1054    * declared type is an interface that does not extend
1055    * <code>java.rmi.Remote</code> and whose declared methods
1056    * (including inherited ones) all throw
1057    * <code>java.rmi.RemoteException</code>.
1058    */

1059   private static final class AbstractInterfaceWriter
1060         implements CDRStreamWriter
1061   {
1062      static final CDRStreamWriter instance = new AbstractInterfaceWriter();
1063      
1064      private AbstractInterfaceWriter() { }
1065      
1066      public void write(OutputStream JavaDoc out, Object JavaDoc obj)
1067      {
1068         out.write_abstract_interface(obj);
1069      }
1070   }
1071
1072   /**
1073    * Class that marshals valuetypes into a CDR output stream.
1074    * A <code>ValuetypeWriter</code> is specific for objects of a given class,
1075    * which is passed as a parameter to the <code>ValuetypeWriter</code>
1076    * constructor and must implement <code>java.io.Serializable</code>.
1077    */

1078   private static final class ValuetypeWriter
1079         implements CDRStreamWriter
1080   {
1081      private Class JavaDoc clz;
1082      
1083      ValuetypeWriter(Class JavaDoc clz)
1084      {
1085         this.clz = clz;
1086      }
1087      
1088      public void write(OutputStream JavaDoc out, Object JavaDoc obj)
1089      {
1090         out.write_value((java.io.Serializable JavaDoc)obj, clz);
1091      }
1092   }
1093
1094}
1095
Popular Tags