1 7 15 16 package com.sun.corba.se.impl.io; 17 18 import java.io.IOException ; 19 import java.io.StreamCorruptedException ; 20 import java.io.NotActiveException ; 21 import java.io.InputStream ; 22 import java.io.ObjectInputStream ; 23 import java.util.*; 24 import java.lang.reflect.Array ; 25 import java.lang.reflect.Constructor ; 26 27 import org.omg.CORBA.portable.ValueInputStream ; 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 37 { 38 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 { 46 private Map fields = null; 47 48 HookGetFields(Map fields){ 49 this.fields = fields; 50 } 51 52 57 public java.io.ObjectStreamClass getObjectStreamClass() { 58 return null; 59 } 60 61 65 public boolean defaulted(String name) 66 throws IOException , IllegalArgumentException { 67 return (!fields.containsKey(name)); 68 } 69 70 73 public boolean get(String name, boolean defvalue) 74 throws IOException , IllegalArgumentException { 75 if (defaulted(name)) 76 return defvalue; 77 else return ((Boolean )fields.get(name)).booleanValue(); 78 } 79 80 83 public char get(String name, char defvalue) 84 throws IOException , IllegalArgumentException { 85 if (defaulted(name)) 86 return defvalue; 87 else return ((Character )fields.get(name)).charValue(); 88 89 } 90 91 94 public byte get(String name, byte defvalue) 95 throws IOException , IllegalArgumentException { 96 if (defaulted(name)) 97 return defvalue; 98 else return ((Byte )fields.get(name)).byteValue(); 99 100 } 101 102 105 public short get(String name, short defvalue) 106 throws IOException , IllegalArgumentException { 107 if (defaulted(name)) 108 return defvalue; 109 else return ((Short )fields.get(name)).shortValue(); 110 111 } 112 113 116 public int get(String name, int defvalue) 117 throws IOException , IllegalArgumentException { 118 if (defaulted(name)) 119 return defvalue; 120 else return ((Integer )fields.get(name)).intValue(); 121 122 } 123 124 127 public long get(String name, long defvalue) 128 throws IOException , IllegalArgumentException { 129 if (defaulted(name)) 130 return defvalue; 131 else return ((Long )fields.get(name)).longValue(); 132 133 } 134 135 138 public float get(String name, float defvalue) 139 throws IOException , IllegalArgumentException { 140 if (defaulted(name)) 141 return defvalue; 142 else return ((Float )fields.get(name)).floatValue(); 143 144 } 145 146 149 public double get(String name, double defvalue) 150 throws IOException , IllegalArgumentException { 151 if (defaulted(name)) 152 return defvalue; 153 else return ((Double )fields.get(name)).doubleValue(); 154 155 } 156 157 160 public Object get(String name, Object defvalue) 161 throws IOException , IllegalArgumentException { 162 if (defaulted(name)) 163 return defvalue; 164 else return fields.get(name); 165 166 } 167 168 public String toString(){ 169 return fields.toString(); 170 } 171 } 172 173 public InputStreamHook() 174 throws IOException { 175 super(); 176 } 177 178 public void defaultReadObject() 179 throws IOException , ClassNotFoundException , NotActiveException 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 fieldToValueMap) 191 throws java.io.InvalidClassException , java.io.StreamCorruptedException , 192 ClassNotFoundException , java.io.IOException ; 193 194 195 public ObjectInputStream.GetField readFields() 199 throws IOException , ClassNotFoundException , NotActiveException { 200 201 HashMap fieldValueMap = new HashMap(); 202 203 216 readFields(fieldValueMap); 217 218 readObjectState.endDefaultReadObject(this); 219 220 return new HookGetFields(fieldValueMap); 221 } 222 223 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 getOrbStream(); 239 240 protected static class ReadObjectState { 242 public void beginUnmarshalCustomValue(InputStreamHook stream, 243 boolean calledDefaultWriteObject, 244 boolean hasReadObject) throws IOException {} 245 246 public void endUnmarshalCustomValue(InputStreamHook stream) throws IOException {} 247 public void beginDefaultReadObject(InputStreamHook stream) throws IOException {} 248 public void endDefaultReadObject(InputStreamHook stream) throws IOException {} 249 public void readData(InputStreamHook stream) throws IOException {} 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 { 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 )stream.getOrbStream()).start_value(); 283 } catch( Exception e ) { 284 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 throw new StreamCorruptedException ("No default data sent"); 300 } 301 } 302 } 303 304 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 335 { 336 throw new StreamCorruptedException ("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 throw utilWrapper.badBeginUnmarshalCustomValue() ; 359 } 360 361 public void endUnmarshalCustomValue(InputStreamHook stream) { 362 363 if (stream.getStreamFormatVersion() == 2) { 368 ((ValueInputStream )stream.getOrbStream()).start_value(); 369 ((ValueInputStream )stream.getOrbStream()).end_value(); 370 } 371 372 stream.setState(DEFAULT_STATE); 373 } 374 375 public void endDefaultReadObject(InputStreamHook stream) throws IOException { 376 377 if (stream.getStreamFormatVersion() == 2) 379 ((ValueInputStream )stream.getOrbStream()).start_value(); 380 381 stream.setState(IN_READ_OBJECT_OPT_DATA); 382 } 383 384 public void readData(InputStreamHook stream) throws IOException { 385 org.omg.CORBA.ORB orb = stream.getOrbStream().orb(); 386 if ((orb == null) || 387 !(orb instanceof com.sun.corba.se.spi.orb.ORB)) { 388 throw new StreamCorruptedException ( 389 "Default data must be read first"); 390 } 391 ORBVersion clientOrbVersion = 392 ((com.sun.corba.se.spi.orb.ORB)orb).getORBVersion(); 393 394 if ((ORBVersionFactory.getPEORB().compareTo(clientOrbVersion) <= 0) || 399 (clientOrbVersion.equals(ORBVersionFactory.getFOREIGN()))) { 400 throw new StreamCorruptedException ("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 throw utilWrapper.badBeginUnmarshalCustomValue() ; 414 } 415 416 public void endUnmarshalCustomValue(InputStreamHook stream) throws IOException 417 { 418 if (stream.getStreamFormatVersion() == 2) { 419 ((ValueInputStream )stream.getOrbStream()).end_value(); 420 } 421 stream.setState(DEFAULT_STATE); 422 } 423 424 public void beginDefaultReadObject(InputStreamHook stream) throws IOException 425 { 426 throw new StreamCorruptedException ("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 { 437 stream.throwOptionalDataIncompatibleException(); 438 } 439 } 440 441 protected static class NoReadObjectDefaultsSentState extends ReadObjectState { 442 public void endUnmarshalCustomValue(InputStreamHook stream) throws IOException { 443 445 if (stream.getStreamFormatVersion() == 2) { 446 ((ValueInputStream )stream.getOrbStream()).start_value(); 447 ((ValueInputStream )stream.getOrbStream()).end_value(); 448 } 449 450 stream.setState(DEFAULT_STATE); 451 } 452 } 453 } 454 | Popular Tags |