KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > io > InputStreamHook


1 /*
2  * @(#)InputStreamHook.java 1.20 05/01/04
3  *
4  * Copyright 2005 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
16 package com.sun.corba.se.impl.io;
17
18 import java.io.IOException JavaDoc;
19 import java.io.StreamCorruptedException JavaDoc;
20 import java.io.NotActiveException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.ObjectInputStream JavaDoc;
23 import java.util.*;
24 import java.lang.reflect.Array JavaDoc;
25 import java.lang.reflect.Constructor JavaDoc;
26
27 import org.omg.CORBA.portable.ValueInputStream JavaDoc;
28
29 import com.sun.corba.se.spi.orb.ORB;
30 import com.sun.corba.se.spi.orb.ORBVersion;
31 import com.sun.corba.se.spi.orb.ORBVersionFactory;
32 import com.sun.corba.se.spi.logging.CORBALogDomains;
33 import com.sun.corba.se.impl.logging.UtilSystemException;
34 import com.sun.corba.se.impl.logging.OMGSystemException;
35
36 public abstract class InputStreamHook extends ObjectInputStream JavaDoc
37 {
38     // These should be visible in all the nested classes
39
static final OMGSystemException omgWrapper =
40     OMGSystemException.get( CORBALogDomains.RPC_ENCODING ) ;
41
42     static final UtilSystemException utilWrapper =
43     UtilSystemException.get( CORBALogDomains.RPC_ENCODING ) ;
44
45     private class HookGetFields extends ObjectInputStream.GetField JavaDoc {
46     private Map fields = null;
47
48     HookGetFields(Map fields){
49         this.fields = fields;
50     }
51
52     /**
53      * Get the ObjectStreamClass that describes the fields in the stream.
54          *
55          * REVISIT! This doesn't work since we have our own ObjectStreamClass.
56      */

57     public java.io.ObjectStreamClass JavaDoc getObjectStreamClass() {
58         return null;
59     }
60         
61     /**
62      * Return true if the named field is defaulted and has no value
63      * in this stream.
64      */

65     public boolean defaulted(String JavaDoc name)
66         throws IOException JavaDoc, IllegalArgumentException JavaDoc {
67         return (!fields.containsKey(name));
68     }
69         
70     /**
71      * Get the value of the named boolean field from the persistent field.
72      */

73     public boolean get(String JavaDoc name, boolean defvalue)
74         throws IOException JavaDoc, IllegalArgumentException JavaDoc {
75         if (defaulted(name))
76         return defvalue;
77         else return ((Boolean JavaDoc)fields.get(name)).booleanValue();
78     }
79         
80     /**
81      * Get the value of the named char field from the persistent fields.
82      */

83     public char get(String JavaDoc name, char defvalue)
84         throws IOException JavaDoc, IllegalArgumentException JavaDoc {
85         if (defaulted(name))
86         return defvalue;
87         else return ((Character JavaDoc)fields.get(name)).charValue();
88
89     }
90         
91     /**
92      * Get the value of the named byte field from the persistent fields.
93      */

94     public byte get(String JavaDoc name, byte defvalue)
95         throws IOException JavaDoc, IllegalArgumentException JavaDoc {
96         if (defaulted(name))
97         return defvalue;
98         else return ((Byte JavaDoc)fields.get(name)).byteValue();
99
100     }
101         
102     /**
103      * Get the value of the named short field from the persistent fields.
104      */

105     public short get(String JavaDoc name, short defvalue)
106         throws IOException JavaDoc, IllegalArgumentException JavaDoc {
107         if (defaulted(name))
108         return defvalue;
109         else return ((Short JavaDoc)fields.get(name)).shortValue();
110
111     }
112         
113     /**
114      * Get the value of the named int field from the persistent fields.
115      */

116     public int get(String JavaDoc name, int defvalue)
117         throws IOException JavaDoc, IllegalArgumentException JavaDoc {
118         if (defaulted(name))
119         return defvalue;
120         else return ((Integer JavaDoc)fields.get(name)).intValue();
121
122     }
123         
124     /**
125      * Get the value of the named long field from the persistent fields.
126      */

127     public long get(String JavaDoc name, long defvalue)
128         throws IOException JavaDoc, IllegalArgumentException JavaDoc {
129         if (defaulted(name))
130         return defvalue;
131         else return ((Long JavaDoc)fields.get(name)).longValue();
132
133     }
134         
135     /**
136      * Get the value of the named float field from the persistent fields.
137      */

138     public float get(String JavaDoc name, float defvalue)
139         throws IOException JavaDoc, IllegalArgumentException JavaDoc {
140         if (defaulted(name))
141         return defvalue;
142         else return ((Float JavaDoc)fields.get(name)).floatValue();
143
144     }
145         
146     /**
147      * Get the value of the named double field from the persistent field.
148      */

149     public double get(String JavaDoc name, double defvalue)
150         throws IOException JavaDoc, IllegalArgumentException JavaDoc {
151         if (defaulted(name))
152         return defvalue;
153         else return ((Double JavaDoc)fields.get(name)).doubleValue();
154
155     }
156         
157     /**
158      * Get the value of the named Object field from the persistent field.
159      */

160     public Object JavaDoc get(String JavaDoc name, Object JavaDoc defvalue)
161         throws IOException JavaDoc, IllegalArgumentException JavaDoc {
162         if (defaulted(name))
163         return defvalue;
164         else return fields.get(name);
165
166     }
167         
168     public String JavaDoc toString(){
169         return fields.toString();
170     }
171     }
172
173     public InputStreamHook()
174     throws IOException JavaDoc {
175     super();
176     }
177
178     public void defaultReadObject()
179     throws IOException JavaDoc, ClassNotFoundException JavaDoc, NotActiveException JavaDoc
180     {
181         readObjectState.beginDefaultReadObject(this);
182
183         defaultReadObjectDelegate();
184
185         readObjectState.endDefaultReadObject(this);
186     }
187
188     public abstract void defaultReadObjectDelegate();
189
190     abstract void readFields(java.util.Map JavaDoc fieldToValueMap)
191         throws java.io.InvalidClassException JavaDoc, java.io.StreamCorruptedException JavaDoc,
192                ClassNotFoundException JavaDoc, java.io.IOException JavaDoc;
193
194
195     // See java.io.ObjectInputStream.GetField
196
// Remember that this is equivalent to defaultReadObject
197
// in RMI-IIOP
198
public ObjectInputStream.GetField JavaDoc readFields()
199         throws IOException JavaDoc, ClassNotFoundException JavaDoc, NotActiveException JavaDoc {
200
201         HashMap fieldValueMap = new HashMap();
202
203         // We were treating readFields same as defaultReadObject. It is
204
// incorrect if the state is readOptionalData. If this line
205
// is uncommented, it will throw a stream corrupted exception.
206
// _REVISIT_: The ideal fix would be to add a new state. In
207
// writeObject user may do one of the following
208
// 1. Call defaultWriteObject()
209
// 2. Put out optional fields
210
// 3. Call writeFields
211
// We have the state defined for (1) and (2) but not for (3), so
212
// we should ideally introduce a new state for 3 and have the
213
// beginDefaultReadObject do nothing.
214
//readObjectState.beginDefaultReadObject(this);
215

216         readFields(fieldValueMap);
217
218         readObjectState.endDefaultReadObject(this);
219
220     return new HookGetFields(fieldValueMap);
221     }
222
223     // The following is a State pattern implementation of what
224
// should be done when the sender's Serializable has a
225
// writeObject method. This was especially necessary for
226
// RMI-IIOP stream format version 2. Please see the
227
// state diagrams in the docs directory of the workspace.
228
//
229
// On the reader's side, the main factors are whether or not
230
// we have a readObject method and whether or not the
231
// sender wrote default data
232

233     protected void setState(ReadObjectState newState) {
234         readObjectState = newState;
235     }
236
237     protected abstract byte getStreamFormatVersion();
238     protected abstract org.omg.CORBA_2_3.portable.InputStream JavaDoc getOrbStream();
239
240     // Description of possible actions
241
protected static class ReadObjectState {
242         public void beginUnmarshalCustomValue(InputStreamHook stream,
243                                               boolean calledDefaultWriteObject,
244                                               boolean hasReadObject) throws IOException JavaDoc {}
245
246         public void endUnmarshalCustomValue(InputStreamHook stream) throws IOException JavaDoc {}
247         public void beginDefaultReadObject(InputStreamHook stream) throws IOException JavaDoc {}
248         public void endDefaultReadObject(InputStreamHook stream) throws IOException JavaDoc {}
249         public void readData(InputStreamHook stream) throws IOException JavaDoc {}
250     }
251
252     protected ReadObjectState readObjectState = DEFAULT_STATE;
253     
254     protected static final ReadObjectState DEFAULT_STATE = new DefaultState();
255     protected static final ReadObjectState IN_READ_OBJECT_OPT_DATA
256         = new InReadObjectOptionalDataState();
257     protected static final ReadObjectState IN_READ_OBJECT_NO_MORE_OPT_DATA
258         = new InReadObjectNoMoreOptionalDataState();
259     protected static final ReadObjectState IN_READ_OBJECT_DEFAULTS_SENT
260         = new InReadObjectDefaultsSentState();
261     protected static final ReadObjectState NO_READ_OBJECT_DEFAULTS_SENT
262         = new NoReadObjectDefaultsSentState();
263
264     protected static final ReadObjectState IN_READ_OBJECT_REMOTE_NOT_CUSTOM_MARSHALED
265         = new InReadObjectRemoteDidNotUseWriteObjectState();
266     protected static final ReadObjectState IN_READ_OBJECT_PAST_DEFAULTS_REMOTE_NOT_CUSTOM
267         = new InReadObjectPastDefaultsRemoteDidNotUseWOState();
268
269     protected static class DefaultState extends ReadObjectState {
270
271         public void beginUnmarshalCustomValue(InputStreamHook stream,
272                                               boolean calledDefaultWriteObject,
273                                               boolean hasReadObject)
274             throws IOException JavaDoc {
275
276             if (hasReadObject) {
277                 if (calledDefaultWriteObject)
278                     stream.setState(IN_READ_OBJECT_DEFAULTS_SENT);
279                 else {
280                     try {
281                         if (stream.getStreamFormatVersion() == 2)
282                             ((ValueInputStream JavaDoc)stream.getOrbStream()).start_value();
283                     } catch( Exception JavaDoc e ) {
284                         // This will happen for Big Integer which uses
285
// writeFields in it's writeObject. We should be past
286
// start_value by now.
287
// NOTE: If we don't log any exception here we should
288
// be fine. If there is an error, it will be caught
289
// while reading the optional data.
290

291                     }
292                     stream.setState(IN_READ_OBJECT_OPT_DATA);
293                 }
294             } else {
295                 if (calledDefaultWriteObject)
296                     stream.setState(NO_READ_OBJECT_DEFAULTS_SENT);
297                 else
298             // XXX I18N and logging needed.
299
throw new StreamCorruptedException JavaDoc("No default data sent");
300             }
301         }
302     }
303
304     // REVISIT. If a readObject exits here without reading
305
// default data, we won't skip it. This could be done automatically
306
// as in line 1492 in IIOPInputStream.
307
protected static class InReadObjectRemoteDidNotUseWriteObjectState extends ReadObjectState {
308
309         public void beginUnmarshalCustomValue(InputStreamHook stream,
310                                               boolean calledDefaultWriteObject,
311                                               boolean hasReadObject)
312     {
313         throw utilWrapper.badBeginUnmarshalCustomValue() ;
314         }
315
316         public void endDefaultReadObject(InputStreamHook stream) {
317             stream.setState(IN_READ_OBJECT_PAST_DEFAULTS_REMOTE_NOT_CUSTOM);
318         }
319
320         public void readData(InputStreamHook stream) {
321             stream.throwOptionalDataIncompatibleException();
322         }
323     }
324
325     protected static class InReadObjectPastDefaultsRemoteDidNotUseWOState extends ReadObjectState {
326
327         public void beginUnmarshalCustomValue(InputStreamHook stream,
328                                               boolean calledDefaultWriteObject,
329                                               boolean hasReadObject)
330     {
331         throw utilWrapper.badBeginUnmarshalCustomValue() ;
332         }
333
334         public void beginDefaultReadObject(InputStreamHook stream) throws IOException JavaDoc
335     {
336         // XXX I18N and logging needed.
337
throw new StreamCorruptedException JavaDoc("Default data already read");
338         }
339
340
341         public void readData(InputStreamHook stream) {
342             stream.throwOptionalDataIncompatibleException();
343         }
344     }
345
346     protected void throwOptionalDataIncompatibleException()
347     {
348     throw omgWrapper.rmiiiopOptionalDataIncompatible2() ;
349     }
350
351
352     protected static class InReadObjectDefaultsSentState extends ReadObjectState {
353         
354         public void beginUnmarshalCustomValue(InputStreamHook stream,
355                                               boolean calledDefaultWriteObject,
356                                               boolean hasReadObject) {
357             // This should never happen.
358
throw utilWrapper.badBeginUnmarshalCustomValue() ;
359         }
360
361         public void endUnmarshalCustomValue(InputStreamHook stream) {
362
363             // In stream format version 2, we can skip over
364
// the optional data this way. In stream format version 1,
365
// we will probably wind up with an error if we're
366
// unmarshaling a superclass.
367
if (stream.getStreamFormatVersion() == 2) {
368                 ((ValueInputStream JavaDoc)stream.getOrbStream()).start_value();
369                 ((ValueInputStream JavaDoc)stream.getOrbStream()).end_value();
370             }
371
372             stream.setState(DEFAULT_STATE);
373         }
374
375         public void endDefaultReadObject(InputStreamHook stream) throws IOException JavaDoc {
376
377             // Read the fake valuetype header in stream format version 2
378
if (stream.getStreamFormatVersion() == 2)
379                 ((ValueInputStream JavaDoc)stream.getOrbStream()).start_value();
380
381             stream.setState(IN_READ_OBJECT_OPT_DATA);
382         }
383
384         public void readData(InputStreamHook stream) throws IOException JavaDoc {
385         org.omg.CORBA.ORB JavaDoc orb = stream.getOrbStream().orb();
386         if ((orb == null) ||
387             !(orb instanceof com.sun.corba.se.spi.orb.ORB)) {
388         throw new StreamCorruptedException JavaDoc(
389                      "Default data must be read first");
390         }
391         ORBVersion clientOrbVersion =
392         ((com.sun.corba.se.spi.orb.ORB)orb).getORBVersion();
393
394         // Fix Date interop bug. For older versions of the ORB don't do
395
// anything for readData(). Before this used to throw
396
// StreamCorruptedException for older versions of the ORB where
397
// calledDefaultWriteObject always returns true.
398
if ((ORBVersionFactory.getPEORB().compareTo(clientOrbVersion) <= 0) ||
399             (clientOrbVersion.equals(ORBVersionFactory.getFOREIGN()))) {
400         // XXX I18N and logging needed.
401
throw new StreamCorruptedException JavaDoc("Default data must be read first");
402         }
403         }
404     }
405
406     protected static class InReadObjectOptionalDataState extends ReadObjectState {
407
408         public void beginUnmarshalCustomValue(InputStreamHook stream,
409                                               boolean calledDefaultWriteObject,
410                                               boolean hasReadObject)
411     {
412             // This should never happen.
413
throw utilWrapper.badBeginUnmarshalCustomValue() ;
414         }
415
416         public void endUnmarshalCustomValue(InputStreamHook stream) throws IOException JavaDoc
417     {
418             if (stream.getStreamFormatVersion() == 2) {
419                 ((ValueInputStream JavaDoc)stream.getOrbStream()).end_value();
420             }
421             stream.setState(DEFAULT_STATE);
422         }
423         
424         public void beginDefaultReadObject(InputStreamHook stream) throws IOException JavaDoc
425     {
426         // XXX I18N and logging needed.
427
throw new StreamCorruptedException JavaDoc("Default data not sent or already read/passed");
428         }
429
430         
431     }
432
433     protected static class InReadObjectNoMoreOptionalDataState
434         extends InReadObjectOptionalDataState {
435
436         public void readData(InputStreamHook stream) throws IOException JavaDoc {
437             stream.throwOptionalDataIncompatibleException();
438         }
439     }
440
441     protected static class NoReadObjectDefaultsSentState extends ReadObjectState {
442         public void endUnmarshalCustomValue(InputStreamHook stream) throws IOException JavaDoc {
443             // Code should read default fields before calling this
444

445             if (stream.getStreamFormatVersion() == 2) {
446                 ((ValueInputStream JavaDoc)stream.getOrbStream()).start_value();
447                 ((ValueInputStream JavaDoc)stream.getOrbStream()).end_value();
448             }
449
450             stream.setState(DEFAULT_STATE);
451         }
452     }
453 }
454
Popular Tags