KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)OutputStreamHook.java 1.13 03/12/19
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
16 package com.sun.corba.se.impl.io;
17
18 import java.io.IOException JavaDoc;
19 import java.io.OutputStream JavaDoc;
20 import java.io.ObjectOutputStream JavaDoc;
21 import java.io.ObjectOutput JavaDoc;
22 import java.util.Hashtable JavaDoc;
23
24 import org.omg.CORBA.INTERNAL JavaDoc;
25
26 public abstract class OutputStreamHook extends ObjectOutputStream JavaDoc
27 {
28     private HookPutFields putFields = null;
29
30     /**
31      * Since ObjectOutputStream.PutField methods specify no exceptions,
32      * we are not checking for null parameters on put methods.
33      */

34     private class HookPutFields extends ObjectOutputStream.PutField JavaDoc
35     {
36     private Hashtable JavaDoc fields = new Hashtable JavaDoc();
37
38     /**
39      * Put the value of the named boolean field into the persistent field.
40      */

41     public void put(String JavaDoc name, boolean value){
42         fields.put(name, new Boolean JavaDoc(value));
43     }
44         
45     /**
46      * Put the value of the named char field into the persistent fields.
47      */

48     public void put(String JavaDoc name, char value){
49         fields.put(name, new Character JavaDoc(value));
50     }
51         
52     /**
53      * Put the value of the named byte field into the persistent fields.
54      */

55     public void put(String JavaDoc name, byte value){
56         fields.put(name, new Byte JavaDoc(value));
57     }
58         
59     /**
60      * Put the value of the named short field into the persistent fields.
61      */

62     public void put(String JavaDoc name, short value){
63         fields.put(name, new Short JavaDoc(value));
64     }
65         
66     /**
67      * Put the value of the named int field into the persistent fields.
68      */

69     public void put(String JavaDoc name, int value){
70         fields.put(name, new Integer JavaDoc(value));
71     }
72         
73     /**
74      * Put the value of the named long field into the persistent fields.
75      */

76     public void put(String JavaDoc name, long value){
77         fields.put(name, new Long JavaDoc(value));
78     }
79         
80     /**
81      * Put the value of the named float field into the persistent fields.
82      *
83      */

84     public void put(String JavaDoc name, float value){
85         fields.put(name, new Float JavaDoc(value));
86     }
87         
88     /**
89      * Put the value of the named double field into the persistent field.
90      */

91     public void put(String JavaDoc name, double value){
92         fields.put(name, new Double JavaDoc(value));
93     }
94         
95     /**
96      * Put the value of the named Object field into the persistent field.
97      */

98     public void put(String JavaDoc name, Object JavaDoc value){
99         fields.put(name, value);
100     }
101         
102     /**
103      * Write the data and fields to the specified ObjectOutput stream.
104      */

105     public void write(ObjectOutput JavaDoc out) throws IOException JavaDoc {
106             OutputStreamHook hook = (OutputStreamHook)out;
107
108             ObjectStreamField[] osfields = hook.getFieldsNoCopy();
109
110             // Write the fields to the stream in the order
111
// provided by the ObjectStreamClass. (They should
112
// be sorted appropriately already.)
113
for (int i = 0; i < osfields.length; i++) {
114
115                 Object JavaDoc value = fields.get(osfields[i].getName());
116
117                 hook.writeField(osfields[i], value);
118             }
119     }
120     }
121
122     abstract void writeField(ObjectStreamField field, Object JavaDoc value) throws IOException JavaDoc;
123
124     public OutputStreamHook()
125     throws java.io.IOException JavaDoc {
126     super();
127         
128     }
129
130     public void defaultWriteObject() throws IOException JavaDoc {
131
132         writeObjectState.defaultWriteObject(this);
133
134     defaultWriteObjectDelegate();
135     }
136
137     public abstract void defaultWriteObjectDelegate();
138     
139     public ObjectOutputStream.PutField JavaDoc putFields()
140     throws IOException JavaDoc {
141     putFields = new HookPutFields();
142     return putFields;
143     }
144
145     // Stream format version, saved/restored during recursive calls
146
protected byte streamFormatVersion = 1;
147
148     // Return the stream format version currently being used
149
// to serialize an object
150
public byte getStreamFormatVersion() {
151         return streamFormatVersion;
152     }
153
154     abstract ObjectStreamField[] getFieldsNoCopy();
155
156     // User uses PutFields to simulate default data.
157
// See java.io.ObjectOutputStream.PutFields
158
public void writeFields()
159     throws IOException JavaDoc {
160
161         writeObjectState.defaultWriteObject(this);
162
163         putFields.write(this);
164     }
165
166     public abstract org.omg.CORBA_2_3.portable.OutputStream JavaDoc getOrbStream();
167
168     protected abstract void beginOptionalCustomData();
169
170
171     // The following is a State pattern implementation of what
172
// should be done when a Serializable has a
173
// writeObject method. This was especially necessary for
174
// RMI-IIOP stream format version 2. Please see the
175
// state diagrams in the docs directory of the workspace.
176

177     protected WriteObjectState writeObjectState = NOT_IN_WRITE_OBJECT;
178     
179     protected void setState(WriteObjectState newState) {
180         writeObjectState = newState;
181     }
182
183     // Description of possible actions
184
protected static class WriteObjectState {
185         public void enterWriteObject(OutputStreamHook stream) throws IOException JavaDoc {}
186         public void exitWriteObject(OutputStreamHook stream) throws IOException JavaDoc {}
187         public void defaultWriteObject(OutputStreamHook stream) throws IOException JavaDoc {}
188         public void writeData(OutputStreamHook stream) throws IOException JavaDoc {}
189     }
190
191     protected static class DefaultState extends WriteObjectState {
192         public void enterWriteObject(OutputStreamHook stream) throws IOException JavaDoc {
193             stream.setState(IN_WRITE_OBJECT);
194         }
195     }
196
197     protected static final WriteObjectState NOT_IN_WRITE_OBJECT = new DefaultState();
198     protected static final WriteObjectState IN_WRITE_OBJECT = new InWriteObjectState();
199     protected static final WriteObjectState WROTE_DEFAULT_DATA = new WroteDefaultDataState();
200     protected static final WriteObjectState WROTE_CUSTOM_DATA = new WroteCustomDataState();
201     
202     protected static class InWriteObjectState extends WriteObjectState {
203
204         public void enterWriteObject(OutputStreamHook stream) throws IOException JavaDoc {
205         // XXX I18N, logging needed.
206
throw new IOException JavaDoc("Internal state failure: Entered writeObject twice");
207         }
208         
209         public void exitWriteObject(OutputStreamHook stream) throws IOException JavaDoc {
210
211             // We didn't write any data, so write the
212
// called defaultWriteObject indicator as false
213
stream.getOrbStream().write_boolean(false);
214
215             // If we're in stream format verison 2, we must
216
// put the "null" marker to say that there isn't
217
// any optional data
218
if (stream.getStreamFormatVersion() == 2)
219                 stream.getOrbStream().write_long(0);
220
221             stream.setState(NOT_IN_WRITE_OBJECT);
222         }
223
224         public void defaultWriteObject(OutputStreamHook stream) throws IOException JavaDoc {
225
226             // The writeObject method called defaultWriteObject
227
// or writeFields, so put the called defaultWriteObject
228
// indicator as true
229
stream.getOrbStream().write_boolean(true);
230
231             stream.setState(WROTE_DEFAULT_DATA);
232         }
233
234         public void writeData(OutputStreamHook stream) throws IOException JavaDoc {
235
236             // The writeObject method first called a direct
237
// write operation. Write the called defaultWriteObject
238
// indicator as false, put the special stream format
239
// version 2 header (if stream format version 2, of course),
240
// and write the data
241
stream.getOrbStream().write_boolean(false);
242             stream.beginOptionalCustomData();
243             stream.setState(WROTE_CUSTOM_DATA);
244         }
245     }
246
247     protected static class WroteDefaultDataState extends InWriteObjectState {
248         
249         public void exitWriteObject(OutputStreamHook stream) throws IOException JavaDoc {
250
251             // We only wrote default data, so if in stream format
252
// version 2, put the null indicator to say that there
253
// is no optional data
254
if (stream.getStreamFormatVersion() == 2)
255                 stream.getOrbStream().write_long(0);
256             
257             stream.setState(NOT_IN_WRITE_OBJECT);
258         }
259
260         public void defaultWriteObject(OutputStreamHook stream) throws IOException JavaDoc {
261         // XXX I18N, logging needed.
262
throw new IOException JavaDoc("Called defaultWriteObject/writeFields twice");
263         }
264
265         public void writeData(OutputStreamHook stream) throws IOException JavaDoc {
266
267             // The writeObject method called a direct write operation.
268
// If in stream format version 2, put the fake valuetype
269
// header.
270
stream.beginOptionalCustomData();
271             
272             stream.setState(WROTE_CUSTOM_DATA);
273         }
274     }
275
276     protected static class WroteCustomDataState extends InWriteObjectState {
277
278         public void exitWriteObject(OutputStreamHook stream) throws IOException JavaDoc {
279             // In stream format version 2, we must tell the ORB
280
// stream to close the fake custom valuetype.
281
if (stream.getStreamFormatVersion() == 2)
282                 ((org.omg.CORBA.portable.ValueOutputStream JavaDoc)stream.getOrbStream()).end_value();
283
284             stream.setState(NOT_IN_WRITE_OBJECT);
285         }
286
287         public void defaultWriteObject(OutputStreamHook stream) throws IOException JavaDoc {
288         // XXX I18N, logging needed.
289
throw new IOException JavaDoc("Cannot call defaultWriteObject/writeFields after writing custom data in RMI-IIOP");
290         }
291
292         // We don't have to do anything special here, just let
293
// the stream write the data.
294
public void writeData(OutputStreamHook stream) throws IOException JavaDoc {}
295     }
296 }
297
Popular Tags