1 7 15 16 package com.sun.corba.se.impl.io; 17 18 import org.omg.CORBA.INTERNAL ; 19 import org.omg.CORBA.portable.OutputStream ; 20 21 import java.security.AccessController ; 22 import java.security.PrivilegedAction ; 23 24 import java.io.IOException ; 25 import java.io.DataOutputStream ; 26 import java.io.Serializable ; 27 import java.io.InvalidClassException ; 28 import java.io.StreamCorruptedException ; 29 import java.io.Externalizable ; 30 import java.io.ObjectStreamException ; 31 import java.io.NotSerializableException ; 32 import java.io.NotActiveException ; 33 34 import java.lang.reflect.InvocationTargetException ; 35 import java.lang.reflect.Field ; 36 37 import java.util.Stack ; 38 39 import javax.rmi.CORBA.Util ; 40 import javax.rmi.CORBA.ValueHandlerMultiFormat ; 41 42 import sun.corba.Bridge ; 43 44 import com.sun.corba.se.impl.io.ObjectStreamClass; 45 import com.sun.corba.se.impl.util.Utility; 46 import com.sun.corba.se.impl.util.RepositoryId; 47 48 import com.sun.corba.se.spi.logging.CORBALogDomains ; 49 import com.sun.corba.se.impl.logging.UtilSystemException ; 50 51 58 59 public class IIOPOutputStream 60 extends com.sun.corba.se.impl.io.OutputStreamHook 61 { 62 private UtilSystemException wrapper = UtilSystemException.get( 63 CORBALogDomains.RPC_ENCODING ) ; 64 65 private static Bridge bridge = 66 (Bridge)AccessController.doPrivileged( 67 new PrivilegedAction () { 68 public Object run() { 69 return Bridge.get() ; 70 } 71 } 72 ) ; 73 74 private org.omg.CORBA_2_3.portable.OutputStream orbStream; 75 76 private Object currentObject = null; 77 78 private ObjectStreamClass currentClassDesc = null; 79 80 private int recursionDepth = 0; 81 82 private int simpleWriteDepth = 0; 83 84 private IOException abortIOException = null; 85 86 private java.util.Stack classDescStack = new java.util.Stack (); 87 88 private Object [] writeObjectArgList = {this}; 90 91 public IIOPOutputStream() 92 throws java.io.IOException 93 { 94 super(); 95 } 96 97 protected void beginOptionalCustomData() { 102 103 if (streamFormatVersion == 2) { 104 105 org.omg.CORBA.portable.ValueOutputStream vout 106 = (org.omg.CORBA.portable.ValueOutputStream )orbStream; 107 108 vout.start_value(currentClassDesc.getRMIIIOPOptionalDataRepId()); 109 } 110 } 111 112 public final void setOrbStream(org.omg.CORBA_2_3.portable.OutputStream os) { 113 orbStream = os; 114 } 115 116 public final org.omg.CORBA_2_3.portable.OutputStream getOrbStream() { 117 return orbStream; 118 } 119 120 public final void increaseRecursionDepth(){ 121 recursionDepth++; 122 } 123 124 public final int decreaseRecursionDepth(){ 125 return --recursionDepth; 126 } 127 128 133 public final void writeObjectOverride(Object obj) 134 throws IOException 135 { 136 writeObjectState.writeData(this); 137 138 Util.writeAbstractObject((OutputStream )orbStream, obj); 139 } 140 141 146 public final void simpleWriteObject(Object obj, byte formatVersion) 147 148 { 149 byte oldStreamFormatVersion = streamFormatVersion; 150 151 streamFormatVersion = formatVersion; 152 153 Object prevObject = currentObject; 154 ObjectStreamClass prevClassDesc = currentClassDesc; 155 simpleWriteDepth++; 156 157 try { 158 outputObject(obj); 160 161 } catch (IOException ee) { 162 if (abortIOException == null) 163 abortIOException = ee; 164 } finally { 165 166 streamFormatVersion = oldStreamFormatVersion; 167 simpleWriteDepth--; 168 currentObject = prevObject; 169 currentClassDesc = prevClassDesc; 170 } 171 172 175 IOException pending = abortIOException; 176 if (simpleWriteDepth == 0) 177 abortIOException = null; 178 if (pending != null) { 179 bridge.throwException( pending ) ; 180 } 181 } 182 183 ObjectStreamField[] getFieldsNoCopy() { 185 return currentClassDesc.getFieldsNoCopy(); 186 } 187 188 193 public final void defaultWriteObjectDelegate() 194 195 { 196 try { 197 if (currentObject == null || currentClassDesc == null) 198 throw new NotActiveException ("defaultWriteObjectDelegate"); 200 201 ObjectStreamField[] fields = 202 currentClassDesc.getFieldsNoCopy(); 203 if (fields.length > 0) { 204 outputClassFields(currentObject, currentClassDesc.forClass(), 205 fields); 206 } 207 } catch(IOException ioe) { 208 bridge.throwException(ioe); 209 } 210 } 211 212 217 public final boolean enableReplaceObjectDelegate(boolean enable) 218 219 { 220 return false; 221 222 } 223 224 225 protected final void annotateClass(Class <?> cl) throws IOException { 226 throw new IOException ("Method annotateClass not supported"); 228 } 229 230 public final void close() throws IOException { 231 } 233 234 protected final void drain() throws IOException { 235 } 237 238 public final void flush() throws IOException { 239 try{ 240 orbStream.flush(); 241 } catch(Error e) { 242 IOException ioexc = new IOException (e.getMessage()); 243 ioexc.initCause(e) ; 244 throw ioexc ; 245 } 246 } 247 248 protected final Object replaceObject(Object obj) throws IOException { 249 throw new IOException ("Method replaceObject not supported"); 251 } 252 253 263 public final void reset() throws IOException { 264 try{ 265 267 if (currentObject != null || currentClassDesc != null) 268 throw new IOException ("Illegal call to reset"); 270 271 abortIOException = null; 272 273 if (classDescStack == null) 274 classDescStack = new java.util.Stack (); 275 else 276 classDescStack.setSize(0); 277 278 } catch(Error e) { 279 IOException ioexc = new IOException (e.getMessage()); 280 ioexc.initCause(e) ; 281 throw ioexc ; 282 } 283 } 284 285 public final void write(byte b[]) throws IOException { 286 try{ 287 writeObjectState.writeData(this); 288 289 orbStream.write_octet_array(b, 0, b.length); 290 } catch(Error e) { 291 IOException ioexc = new IOException (e.getMessage()); 292 ioexc.initCause(e) ; 293 throw ioexc ; 294 } 295 } 296 297 public final void write(byte b[], int off, int len) throws IOException { 298 try{ 299 writeObjectState.writeData(this); 300 301 orbStream.write_octet_array(b, off, len); 302 } catch(Error e) { 303 IOException ioexc = new IOException (e.getMessage()); 304 ioexc.initCause(e) ; 305 throw ioexc ; 306 } 307 } 308 309 public final void write(int data) throws IOException { 310 try{ 311 writeObjectState.writeData(this); 312 313 orbStream.write_octet((byte)(data & 0xFF)); 314 } catch(Error e) { 315 IOException ioexc = new IOException (e.getMessage()); 316 ioexc.initCause(e) ; 317 throw ioexc ; 318 } 319 } 320 321 public final void writeBoolean(boolean data) throws IOException { 322 try{ 323 writeObjectState.writeData(this); 324 325 orbStream.write_boolean(data); 326 } catch(Error e) { 327 IOException ioexc = new IOException (e.getMessage()); 328 ioexc.initCause(e) ; 329 throw ioexc ; 330 } 331 } 332 333 public final void writeByte(int data) throws IOException { 334 try{ 335 writeObjectState.writeData(this); 336 337 orbStream.write_octet((byte)data); 338 } catch(Error e) { 339 IOException ioexc = new IOException (e.getMessage()); 340 ioexc.initCause(e) ; 341 throw ioexc ; 342 } 343 } 344 345 public final void writeBytes(String data) throws IOException { 346 try{ 347 writeObjectState.writeData(this); 348 349 byte buf[] = data.getBytes(); 350 orbStream.write_octet_array(buf, 0, buf.length); 351 } catch(Error e) { 352 IOException ioexc = new IOException (e.getMessage()); 353 ioexc.initCause(e) ; 354 throw ioexc ; 355 } 356 } 357 358 public final void writeChar(int data) throws IOException { 359 try{ 360 writeObjectState.writeData(this); 361 362 orbStream.write_wchar((char)data); 363 } catch(Error e) { 364 IOException ioexc = new IOException (e.getMessage()); 365 ioexc.initCause(e) ; 366 throw ioexc ; 367 } 368 } 369 370 public final void writeChars(String data) throws IOException { 371 try{ 372 writeObjectState.writeData(this); 373 374 char buf[] = data.toCharArray(); 375 orbStream.write_wchar_array(buf, 0, buf.length); 376 } catch(Error e) { 377 IOException ioexc = new IOException (e.getMessage()); 378 ioexc.initCause(e) ; 379 throw ioexc ; 380 } 381 } 382 383 public final void writeDouble(double data) throws IOException { 384 try{ 385 writeObjectState.writeData(this); 386 387 orbStream.write_double(data); 388 } catch(Error e) { 389 IOException ioexc = new IOException (e.getMessage()); 390 ioexc.initCause(e) ; 391 throw ioexc ; 392 } 393 } 394 395 public final void writeFloat(float data) throws IOException { 396 try{ 397 writeObjectState.writeData(this); 398 399 orbStream.write_float(data); 400 } catch(Error e) { 401 IOException ioexc = new IOException (e.getMessage()); 402 ioexc.initCause(e) ; 403 throw ioexc ; 404 } 405 } 406 407 public final void writeInt(int data) throws IOException { 408 try{ 409 writeObjectState.writeData(this); 410 411 orbStream.write_long(data); 412 } catch(Error e) { 413 IOException ioexc = new IOException (e.getMessage()); 414 ioexc.initCause(e) ; 415 throw ioexc ; 416 } 417 } 418 419 public final void writeLong(long data) throws IOException { 420 try{ 421 writeObjectState.writeData(this); 422 423 orbStream.write_longlong(data); 424 } catch(Error e) { 425 IOException ioexc = new IOException (e.getMessage()); 426 ioexc.initCause(e) ; 427 throw ioexc ; 428 } 429 } 430 431 public final void writeShort(int data) throws IOException { 432 try{ 433 writeObjectState.writeData(this); 434 435 orbStream.write_short((short)data); 436 } catch(Error e) { 437 IOException ioexc = new IOException (e.getMessage()); 438 ioexc.initCause(e) ; 439 throw ioexc ; 440 } 441 } 442 443 protected final void writeStreamHeader() throws IOException { 444 } 446 447 453 protected void internalWriteUTF(org.omg.CORBA.portable.OutputStream stream, 454 String data) 455 { 456 stream.write_wstring(data); 457 } 458 459 public final void writeUTF(String data) throws IOException { 460 try{ 461 writeObjectState.writeData(this); 462 463 internalWriteUTF(orbStream, data); 464 } catch(Error e) { 465 IOException ioexc = new IOException (e.getMessage()); 466 ioexc.initCause(e) ; 467 throw ioexc ; 468 } 469 } 470 471 476 private boolean checkSpecialClasses(Object obj) throws IOException { 477 478 481 485 if (obj instanceof ObjectStreamClass) { 486 throw new IOException ("Serialization of ObjectStreamClass not supported"); 488 } 489 490 return false; 491 } 492 493 497 private boolean checkSubstitutableSpecialClasses(Object obj) 498 throws IOException 499 { 500 if (obj instanceof String ) { 501 orbStream.write_value((java.io.Serializable )obj); 502 return true; 503 } 504 505 510 return false; 511 } 512 513 516 private void outputObject(final Object obj) throws IOException { 517 518 currentObject = obj; 519 Class currclass = obj.getClass(); 520 521 524 currentClassDesc = ObjectStreamClass.lookup(currclass); 525 if (currentClassDesc == null) { 526 throw new NotSerializableException (currclass.getName()); 528 } 529 530 534 if (currentClassDesc.isExternalizable()) { 535 orbStream.write_octet(streamFormatVersion); 537 538 Externalizable ext = (Externalizable )obj; 539 ext.writeExternal(this); 540 541 } else { 542 543 547 int stackMark = classDescStack.size(); 548 try { 549 ObjectStreamClass next; 550 while ((next = currentClassDesc.getSuperclass()) != null) { 551 classDescStack.push(currentClassDesc); 552 currentClassDesc = next; 553 } 554 555 562 do { 563 564 WriteObjectState oldState = writeObjectState; 565 566 try { 567 568 setState(NOT_IN_WRITE_OBJECT); 569 570 if (currentClassDesc.hasWriteObject()) { 571 invokeObjectWriter(currentClassDesc, obj ); 572 } else { 573 defaultWriteObjectDelegate(); 574 } 575 } finally { 576 setState(oldState); 577 } 578 579 } while (classDescStack.size() > stackMark && 580 (currentClassDesc = (ObjectStreamClass)classDescStack.pop()) != null); 581 } finally { 582 classDescStack.setSize(stackMark); 583 } 584 } 585 } 586 587 592 private void invokeObjectWriter(ObjectStreamClass osc, Object obj) 593 throws IOException 594 { 595 Class c = osc.forClass() ; 596 597 try { 598 599 orbStream.write_octet(streamFormatVersion); 601 602 writeObjectState.enterWriteObject(this); 603 604 osc.writeObjectMethod.invoke( obj, writeObjectArgList ) ; 606 607 writeObjectState.exitWriteObject(this); 608 609 } catch (InvocationTargetException e) { 610 Throwable t = e.getTargetException(); 611 if (t instanceof IOException ) 612 throw (IOException )t; 613 else if (t instanceof RuntimeException ) 614 throw (RuntimeException ) t; 615 else if (t instanceof Error ) 616 throw (Error ) t; 617 else 618 throw new Error ("invokeObjectWriter internal error",e); 620 } catch (IllegalAccessException e) { 621 } 623 } 624 625 void writeField(ObjectStreamField field, Object value) throws IOException { 626 switch (field.getTypeCode()) { 627 case 'B': 628 if (value == null) 629 orbStream.write_octet((byte)0); 630 else 631 orbStream.write_octet(((Byte )value).byteValue()); 632 break; 633 case 'C': 634 if (value == null) 635 orbStream.write_wchar((char)0); 636 else 637 orbStream.write_wchar(((Character )value).charValue()); 638 break; 639 case 'F': 640 if (value == null) 641 orbStream.write_float((float)0); 642 else 643 orbStream.write_float(((Float )value).floatValue()); 644 break; 645 case 'D': 646 if (value == null) 647 orbStream.write_double((double)0); 648 else 649 orbStream.write_double(((Double )value).doubleValue()); 650 break; 651 case 'I': 652 if (value == null) 653 orbStream.write_long((int)0); 654 else 655 orbStream.write_long(((Integer )value).intValue()); 656 break; 657 case 'J': 658 if (value == null) 659 orbStream.write_longlong((long)0); 660 else 661 orbStream.write_longlong(((Long )value).longValue()); 662 break; 663 case 'S': 664 if (value == null) 665 orbStream.write_short((short)0); 666 else 667 orbStream.write_short(((Short )value).shortValue()); 668 break; 669 case 'Z': 670 if (value == null) 671 orbStream.write_boolean(false); 672 else 673 orbStream.write_boolean(((Boolean )value).booleanValue()); 674 break; 675 case '[': 676 case 'L': 677 writeObjectField(field, value); 679 break; 680 default: 681 throw new InvalidClassException (currentClassDesc.getName()); 683 } 684 } 685 686 private void writeObjectField(ObjectStreamField field, 687 Object objectValue) throws IOException { 688 689 if (ObjectStreamClassCorbaExt.isAny(field.getTypeString())) { 690 javax.rmi.CORBA.Util.writeAny(orbStream, objectValue); 691 } 692 else { 693 Class type = field.getType(); 694 int callType = ValueHandlerImpl.kValueType; 695 696 if (type.isInterface()) { 697 String className = type.getName(); 698 699 if (java.rmi.Remote .class.isAssignableFrom(type)) { 700 701 703 callType = ValueHandlerImpl.kRemoteType; 704 705 706 } else if (org.omg.CORBA.Object .class.isAssignableFrom(type)){ 707 708 callType = ValueHandlerImpl.kRemoteType; 710 711 } else if (RepositoryId.isAbstractBase(type)) { 712 callType = ValueHandlerImpl.kAbstractType; 714 } else if (ObjectStreamClassCorbaExt.isAbstractInterface(type)) { 715 callType = ValueHandlerImpl.kAbstractType; 716 } 717 } 718 719 switch (callType) { 720 case ValueHandlerImpl.kRemoteType: 721 Util.writeRemoteObject(orbStream, objectValue); 722 break; 723 case ValueHandlerImpl.kAbstractType: 724 Util.writeAbstractObject(orbStream, objectValue); 725 break; 726 case ValueHandlerImpl.kValueType: 727 try{ 728 orbStream.write_value((java.io.Serializable )objectValue, type); 729 } 730 catch(ClassCastException cce){ 731 if (objectValue instanceof java.io.Serializable ) 732 throw cce; 733 else 734 Utility.throwNotSerializableForCorba(objectValue.getClass().getName()); 735 } 736 } 737 } 738 } 739 740 743 private void outputClassFields(Object o, Class cl, 744 ObjectStreamField[] fields) 745 throws IOException , InvalidClassException { 746 747 for (int i = 0; i < fields.length; i++) { 748 if (fields[i].getField() == null) 749 throw new InvalidClassException (cl.getName(), 751 "Nonexistent field " + fields[i].getName()); 752 753 try { 754 switch (fields[i].getTypeCode()) { 755 case 'B': 756 byte byteValue = fields[i].getField().getByte( o ) ; 757 orbStream.write_octet(byteValue); 758 break; 759 case 'C': 760 char charValue = fields[i].getField().getChar( o ) ; 761 orbStream.write_wchar(charValue); 762 break; 763 case 'F': 764 float floatValue = fields[i].getField().getFloat( o ) ; 765 orbStream.write_float(floatValue); 766 break; 767 case 'D' : 768 double doubleValue = fields[i].getField().getDouble( o ) ; 769 orbStream.write_double(doubleValue); 770 break; 771 case 'I': 772 int intValue = fields[i].getField().getInt( o ) ; 773 orbStream.write_long(intValue); 774 break; 775 case 'J': 776 long longValue = fields[i].getField().getLong( o ) ; 777 orbStream.write_longlong(longValue); 778 break; 779 case 'S': 780 short shortValue = fields[i].getField().getShort( o ) ; 781 orbStream.write_short(shortValue); 782 break; 783 case 'Z': 784 boolean booleanValue = fields[i].getField().getBoolean( o ) ; 785 orbStream.write_boolean(booleanValue); 786 break; 787 case '[': 788 case 'L': 789 Object objectValue = fields[i].getField().get( o ) ; 790 writeObjectField(fields[i], objectValue); 791 break; 792 default: 793 throw new InvalidClassException (cl.getName()); 795 } 796 } catch (IllegalAccessException exc) { 797 throw wrapper.illegalFieldAccess( exc, fields[i].getName() ) ; 798 } 799 } 800 } 801 } 802 803 | Popular Tags |