src/share/classes/com/sun/corba/se/impl/io/OutputStreamHook.java

Tue, 30 Apr 2013 11:53:51 +0100

author
coffeys
date
Tue, 30 Apr 2013 11:53:51 +0100
changeset 478
80161c61aa68
parent 158
91006f157c46
child 553
5ca1b4c282b8
permissions
-rw-r--r--

8000642: Better handling of objects for transportation
Reviewed-by: alanb, mchung, skoivu

duke@1 1 /*
ohair@158 2 * Copyright (c) 1999, 2003, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@158 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@158 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@158 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@158 22 * or visit www.oracle.com if you need additional information or have any
ohair@158 23 * questions.
duke@1 24 */
duke@1 25 /*
duke@1 26 * Licensed Materials - Property of IBM
duke@1 27 * RMI-IIOP v1.0
duke@1 28 * Copyright IBM Corp. 1998 1999 All Rights Reserved
duke@1 29 *
duke@1 30 */
duke@1 31
duke@1 32 package com.sun.corba.se.impl.io;
duke@1 33
duke@1 34 import java.io.IOException;
duke@1 35 import java.io.OutputStream;
duke@1 36 import java.io.ObjectOutputStream;
duke@1 37 import java.io.ObjectOutput;
duke@1 38 import java.util.Hashtable;
duke@1 39
duke@1 40 import org.omg.CORBA.INTERNAL;
duke@1 41
duke@1 42 public abstract class OutputStreamHook extends ObjectOutputStream
duke@1 43 {
duke@1 44 private HookPutFields putFields = null;
duke@1 45
duke@1 46 /**
duke@1 47 * Since ObjectOutputStream.PutField methods specify no exceptions,
duke@1 48 * we are not checking for null parameters on put methods.
duke@1 49 */
duke@1 50 private class HookPutFields extends ObjectOutputStream.PutField
duke@1 51 {
duke@1 52 private Hashtable fields = new Hashtable();
duke@1 53
duke@1 54 /**
duke@1 55 * Put the value of the named boolean field into the persistent field.
duke@1 56 */
duke@1 57 public void put(String name, boolean value){
duke@1 58 fields.put(name, new Boolean(value));
duke@1 59 }
duke@1 60
duke@1 61 /**
duke@1 62 * Put the value of the named char field into the persistent fields.
duke@1 63 */
duke@1 64 public void put(String name, char value){
duke@1 65 fields.put(name, new Character(value));
duke@1 66 }
duke@1 67
duke@1 68 /**
duke@1 69 * Put the value of the named byte field into the persistent fields.
duke@1 70 */
duke@1 71 public void put(String name, byte value){
duke@1 72 fields.put(name, new Byte(value));
duke@1 73 }
duke@1 74
duke@1 75 /**
duke@1 76 * Put the value of the named short field into the persistent fields.
duke@1 77 */
duke@1 78 public void put(String name, short value){
duke@1 79 fields.put(name, new Short(value));
duke@1 80 }
duke@1 81
duke@1 82 /**
duke@1 83 * Put the value of the named int field into the persistent fields.
duke@1 84 */
duke@1 85 public void put(String name, int value){
duke@1 86 fields.put(name, new Integer(value));
duke@1 87 }
duke@1 88
duke@1 89 /**
duke@1 90 * Put the value of the named long field into the persistent fields.
duke@1 91 */
duke@1 92 public void put(String name, long value){
duke@1 93 fields.put(name, new Long(value));
duke@1 94 }
duke@1 95
duke@1 96 /**
duke@1 97 * Put the value of the named float field into the persistent fields.
duke@1 98 *
duke@1 99 */
duke@1 100 public void put(String name, float value){
duke@1 101 fields.put(name, new Float(value));
duke@1 102 }
duke@1 103
duke@1 104 /**
duke@1 105 * Put the value of the named double field into the persistent field.
duke@1 106 */
duke@1 107 public void put(String name, double value){
duke@1 108 fields.put(name, new Double(value));
duke@1 109 }
duke@1 110
duke@1 111 /**
duke@1 112 * Put the value of the named Object field into the persistent field.
duke@1 113 */
duke@1 114 public void put(String name, Object value){
duke@1 115 fields.put(name, value);
duke@1 116 }
duke@1 117
duke@1 118 /**
duke@1 119 * Write the data and fields to the specified ObjectOutput stream.
duke@1 120 */
duke@1 121 public void write(ObjectOutput out) throws IOException {
duke@1 122 OutputStreamHook hook = (OutputStreamHook)out;
duke@1 123
duke@1 124 ObjectStreamField[] osfields = hook.getFieldsNoCopy();
duke@1 125
duke@1 126 // Write the fields to the stream in the order
duke@1 127 // provided by the ObjectStreamClass. (They should
duke@1 128 // be sorted appropriately already.)
duke@1 129 for (int i = 0; i < osfields.length; i++) {
duke@1 130
duke@1 131 Object value = fields.get(osfields[i].getName());
duke@1 132
duke@1 133 hook.writeField(osfields[i], value);
duke@1 134 }
duke@1 135 }
duke@1 136 }
duke@1 137
duke@1 138 abstract void writeField(ObjectStreamField field, Object value) throws IOException;
duke@1 139
duke@1 140 public OutputStreamHook()
duke@1 141 throws java.io.IOException {
duke@1 142 super();
duke@1 143
duke@1 144 }
duke@1 145
duke@1 146 public void defaultWriteObject() throws IOException {
duke@1 147
duke@1 148 writeObjectState.defaultWriteObject(this);
duke@1 149
duke@1 150 defaultWriteObjectDelegate();
duke@1 151 }
duke@1 152
duke@1 153 public abstract void defaultWriteObjectDelegate();
duke@1 154
duke@1 155 public ObjectOutputStream.PutField putFields()
duke@1 156 throws IOException {
duke@1 157 putFields = new HookPutFields();
duke@1 158 return putFields;
duke@1 159 }
duke@1 160
duke@1 161 // Stream format version, saved/restored during recursive calls
duke@1 162 protected byte streamFormatVersion = 1;
duke@1 163
duke@1 164 // Return the stream format version currently being used
duke@1 165 // to serialize an object
duke@1 166 public byte getStreamFormatVersion() {
duke@1 167 return streamFormatVersion;
duke@1 168 }
duke@1 169
duke@1 170 abstract ObjectStreamField[] getFieldsNoCopy();
duke@1 171
duke@1 172 // User uses PutFields to simulate default data.
duke@1 173 // See java.io.ObjectOutputStream.PutFields
duke@1 174 public void writeFields()
duke@1 175 throws IOException {
duke@1 176
duke@1 177 writeObjectState.defaultWriteObject(this);
duke@1 178
duke@1 179 putFields.write(this);
duke@1 180 }
duke@1 181
coffeys@478 182 abstract org.omg.CORBA_2_3.portable.OutputStream getOrbStream();
duke@1 183
duke@1 184 protected abstract void beginOptionalCustomData();
duke@1 185
duke@1 186
duke@1 187 // The following is a State pattern implementation of what
duke@1 188 // should be done when a Serializable has a
duke@1 189 // writeObject method. This was especially necessary for
duke@1 190 // RMI-IIOP stream format version 2. Please see the
duke@1 191 // state diagrams in the docs directory of the workspace.
duke@1 192
duke@1 193 protected WriteObjectState writeObjectState = NOT_IN_WRITE_OBJECT;
duke@1 194
duke@1 195 protected void setState(WriteObjectState newState) {
duke@1 196 writeObjectState = newState;
duke@1 197 }
duke@1 198
duke@1 199 // Description of possible actions
duke@1 200 protected static class WriteObjectState {
duke@1 201 public void enterWriteObject(OutputStreamHook stream) throws IOException {}
duke@1 202 public void exitWriteObject(OutputStreamHook stream) throws IOException {}
duke@1 203 public void defaultWriteObject(OutputStreamHook stream) throws IOException {}
duke@1 204 public void writeData(OutputStreamHook stream) throws IOException {}
duke@1 205 }
duke@1 206
duke@1 207 protected static class DefaultState extends WriteObjectState {
duke@1 208 public void enterWriteObject(OutputStreamHook stream) throws IOException {
duke@1 209 stream.setState(IN_WRITE_OBJECT);
duke@1 210 }
duke@1 211 }
duke@1 212
duke@1 213 protected static final WriteObjectState NOT_IN_WRITE_OBJECT = new DefaultState();
duke@1 214 protected static final WriteObjectState IN_WRITE_OBJECT = new InWriteObjectState();
duke@1 215 protected static final WriteObjectState WROTE_DEFAULT_DATA = new WroteDefaultDataState();
duke@1 216 protected static final WriteObjectState WROTE_CUSTOM_DATA = new WroteCustomDataState();
duke@1 217
duke@1 218 protected static class InWriteObjectState extends WriteObjectState {
duke@1 219
duke@1 220 public void enterWriteObject(OutputStreamHook stream) throws IOException {
duke@1 221 // XXX I18N, logging needed.
duke@1 222 throw new IOException("Internal state failure: Entered writeObject twice");
duke@1 223 }
duke@1 224
duke@1 225 public void exitWriteObject(OutputStreamHook stream) throws IOException {
duke@1 226
duke@1 227 // We didn't write any data, so write the
duke@1 228 // called defaultWriteObject indicator as false
duke@1 229 stream.getOrbStream().write_boolean(false);
duke@1 230
duke@1 231 // If we're in stream format verison 2, we must
duke@1 232 // put the "null" marker to say that there isn't
duke@1 233 // any optional data
duke@1 234 if (stream.getStreamFormatVersion() == 2)
duke@1 235 stream.getOrbStream().write_long(0);
duke@1 236
duke@1 237 stream.setState(NOT_IN_WRITE_OBJECT);
duke@1 238 }
duke@1 239
duke@1 240 public void defaultWriteObject(OutputStreamHook stream) throws IOException {
duke@1 241
duke@1 242 // The writeObject method called defaultWriteObject
duke@1 243 // or writeFields, so put the called defaultWriteObject
duke@1 244 // indicator as true
duke@1 245 stream.getOrbStream().write_boolean(true);
duke@1 246
duke@1 247 stream.setState(WROTE_DEFAULT_DATA);
duke@1 248 }
duke@1 249
duke@1 250 public void writeData(OutputStreamHook stream) throws IOException {
duke@1 251
duke@1 252 // The writeObject method first called a direct
duke@1 253 // write operation. Write the called defaultWriteObject
duke@1 254 // indicator as false, put the special stream format
duke@1 255 // version 2 header (if stream format version 2, of course),
duke@1 256 // and write the data
duke@1 257 stream.getOrbStream().write_boolean(false);
duke@1 258 stream.beginOptionalCustomData();
duke@1 259 stream.setState(WROTE_CUSTOM_DATA);
duke@1 260 }
duke@1 261 }
duke@1 262
duke@1 263 protected static class WroteDefaultDataState extends InWriteObjectState {
duke@1 264
duke@1 265 public void exitWriteObject(OutputStreamHook stream) throws IOException {
duke@1 266
duke@1 267 // We only wrote default data, so if in stream format
duke@1 268 // version 2, put the null indicator to say that there
duke@1 269 // is no optional data
duke@1 270 if (stream.getStreamFormatVersion() == 2)
duke@1 271 stream.getOrbStream().write_long(0);
duke@1 272
duke@1 273 stream.setState(NOT_IN_WRITE_OBJECT);
duke@1 274 }
duke@1 275
duke@1 276 public void defaultWriteObject(OutputStreamHook stream) throws IOException {
duke@1 277 // XXX I18N, logging needed.
duke@1 278 throw new IOException("Called defaultWriteObject/writeFields twice");
duke@1 279 }
duke@1 280
duke@1 281 public void writeData(OutputStreamHook stream) throws IOException {
duke@1 282
duke@1 283 // The writeObject method called a direct write operation.
duke@1 284 // If in stream format version 2, put the fake valuetype
duke@1 285 // header.
duke@1 286 stream.beginOptionalCustomData();
duke@1 287
duke@1 288 stream.setState(WROTE_CUSTOM_DATA);
duke@1 289 }
duke@1 290 }
duke@1 291
duke@1 292 protected static class WroteCustomDataState extends InWriteObjectState {
duke@1 293
duke@1 294 public void exitWriteObject(OutputStreamHook stream) throws IOException {
duke@1 295 // In stream format version 2, we must tell the ORB
duke@1 296 // stream to close the fake custom valuetype.
duke@1 297 if (stream.getStreamFormatVersion() == 2)
duke@1 298 ((org.omg.CORBA.portable.ValueOutputStream)stream.getOrbStream()).end_value();
duke@1 299
duke@1 300 stream.setState(NOT_IN_WRITE_OBJECT);
duke@1 301 }
duke@1 302
duke@1 303 public void defaultWriteObject(OutputStreamHook stream) throws IOException {
duke@1 304 // XXX I18N, logging needed.
duke@1 305 throw new IOException("Cannot call defaultWriteObject/writeFields after writing custom data in RMI-IIOP");
duke@1 306 }
duke@1 307
duke@1 308 // We don't have to do anything special here, just let
duke@1 309 // the stream write the data.
duke@1 310 public void writeData(OutputStreamHook stream) throws IOException {}
duke@1 311 }
duke@1 312 }

mercurial