1 17 package org.apache.activemq.openwire.v3; 18 19 import org.apache.activemq.openwire.DataStreamMarshaller; 20 import org.apache.activemq.openwire.OpenWireFormat; 21 import org.apache.activemq.openwire.BooleanStream; 22 import org.apache.activemq.command.DataStructure; 23 import org.apache.activemq.util.ClassLoading; 24 import org.apache.activemq.util.ByteSequence; 25 26 import java.lang.reflect.Constructor ; 27 import java.io.IOException ; 28 import java.io.DataOutput ; 29 import java.io.DataInput ; 30 31 abstract public class BaseDataStreamMarshaller implements DataStreamMarshaller { 32 33 static final public Constructor STACK_TRACE_ELEMENT_CONSTRUCTOR; 34 35 static { 36 Constructor constructor=null; 37 try { 38 constructor = StackTraceElement .class.getConstructor(new Class []{String .class, String .class, String .class, int.class}); 39 } catch (Throwable e) { 40 } 41 STACK_TRACE_ELEMENT_CONSTRUCTOR = constructor; 42 } 43 44 45 abstract public byte getDataStructureType(); 46 abstract public DataStructure createObject(); 47 48 public int tightMarshal1(OpenWireFormat wireFormat, Object o, BooleanStream bs) throws IOException { 49 return 0; 50 } 51 public void tightMarshal2(OpenWireFormat wireFormat, Object o, DataOutput dataOut, BooleanStream bs) throws IOException { 52 } 53 public void tightUnmarshal(OpenWireFormat wireFormat, Object o, DataInput dataIn, BooleanStream bs) throws IOException { 54 } 55 56 public int tightMarshalLong1(OpenWireFormat wireFormat, long o, BooleanStream bs) throws IOException { 57 if( o == 0 ) { 58 bs.writeBoolean(false); 59 bs.writeBoolean(false); 60 return 0; 61 } else if ( (o & 0xFFFFFFFFFFFF0000l ) == 0 ) { 62 bs.writeBoolean(false); 63 bs.writeBoolean(true); 64 return 2; 65 } else if ( (o & 0xFFFFFFFF00000000l ) == 0) { 66 bs.writeBoolean(true); 67 bs.writeBoolean(false); 68 return 4; 69 } else { 70 bs.writeBoolean(true); 71 bs.writeBoolean(true); 72 return 8; 73 } 74 } 75 public void tightMarshalLong2(OpenWireFormat wireFormat, long o, DataOutput dataOut, BooleanStream bs) throws IOException { 76 if( bs.readBoolean() ) { 77 if( bs.readBoolean() ) { 78 dataOut.writeLong(o); 79 } else { 80 dataOut.writeInt((int) o); 81 } 82 } else { 83 if( bs.readBoolean() ) { 84 dataOut.writeShort((int) o); 85 } 86 } 87 } 88 public long tightUnmarshalLong(OpenWireFormat wireFormat, DataInput dataIn, BooleanStream bs) throws IOException { 89 if( bs.readBoolean() ) { 90 if( bs.readBoolean() ) { 91 return dataIn.readLong(); 92 } else { 93 return toLong(dataIn.readInt()); 94 } 95 } else { 96 if( bs.readBoolean() ) { 97 return toLong(dataIn.readShort()); 98 } else { 99 return 0; 100 } 101 } 102 } 103 104 protected long toLong(short value) { 105 long answer = value; 107 return answer & 0xffffL; 108 } 109 110 protected long toLong(int value) { 111 long answer = value; 113 return answer & 0xffffffffL; 114 } 115 116 protected DataStructure tightUnmarsalNestedObject(OpenWireFormat wireFormat, DataInput dataIn, BooleanStream bs) throws IOException { 117 return wireFormat.tightUnmarshalNestedObject(dataIn, bs); 118 } 119 protected int tightMarshalNestedObject1(OpenWireFormat wireFormat, DataStructure o, BooleanStream bs) throws IOException { 120 return wireFormat.tightMarshalNestedObject1(o, bs); 121 } 122 123 protected void tightMarshalNestedObject2(OpenWireFormat wireFormat, DataStructure o, DataOutput dataOut, BooleanStream bs) throws IOException { 124 wireFormat.tightMarshalNestedObject2(o, dataOut, bs); 125 } 126 127 protected DataStructure tightUnmarsalCachedObject(OpenWireFormat wireFormat, DataInput dataIn, BooleanStream bs) throws IOException { 128 if( wireFormat.isCacheEnabled() ) { 129 if( bs.readBoolean() ) { 130 short index = dataIn.readShort(); 131 DataStructure object = wireFormat.tightUnmarshalNestedObject(dataIn, bs); 132 wireFormat.setInUnmarshallCache(index, object); 133 return object; 134 } else { 135 short index = dataIn.readShort(); 136 return wireFormat.getFromUnmarshallCache(index); 137 } 138 } else { 139 return wireFormat.tightUnmarshalNestedObject(dataIn, bs); 140 } 141 } 142 protected int tightMarshalCachedObject1(OpenWireFormat wireFormat, DataStructure o, BooleanStream bs) throws IOException { 143 if( wireFormat.isCacheEnabled() ) { 144 Short index = wireFormat.getMarshallCacheIndex(o); 145 bs.writeBoolean(index == null); 146 if( index == null ) { 147 int rc = wireFormat.tightMarshalNestedObject1(o, bs); 148 wireFormat.addToMarshallCache(o); 149 return 2+rc; 150 } else { 151 return 2; 152 } 153 } else { 154 return wireFormat.tightMarshalNestedObject1(o, bs); 155 } 156 } 157 protected void tightMarshalCachedObject2(OpenWireFormat wireFormat, DataStructure o, DataOutput dataOut, BooleanStream bs) throws IOException { 158 if( wireFormat.isCacheEnabled() ) { 159 Short index = wireFormat.getMarshallCacheIndex(o); 160 if( bs.readBoolean() ) { 161 dataOut.writeShort(index.shortValue()); 162 wireFormat.tightMarshalNestedObject2(o, dataOut, bs); 163 } else { 164 dataOut.writeShort(index.shortValue()); 165 } 166 } else { 167 wireFormat.tightMarshalNestedObject2(o, dataOut, bs); 168 } 169 } 170 171 protected Throwable tightUnmarsalThrowable(OpenWireFormat wireFormat, DataInput dataIn, BooleanStream bs) throws IOException { 172 if( bs.readBoolean() ) { 173 String clazz = tightUnmarshalString(dataIn, bs); 174 String message = tightUnmarshalString(dataIn, bs); 175 Throwable o = createThrowable(clazz, message); 176 if( wireFormat.isStackTraceEnabled() ) { 177 if( STACK_TRACE_ELEMENT_CONSTRUCTOR !=null) { 178 StackTraceElement ss[] = new StackTraceElement [dataIn.readShort()]; 179 for (int i = 0; i < ss.length; i++) { 180 try { 181 ss[i] = (StackTraceElement ) STACK_TRACE_ELEMENT_CONSTRUCTOR.newInstance(new Object []{ 182 tightUnmarshalString(dataIn, bs), 183 tightUnmarshalString(dataIn, bs), 184 tightUnmarshalString(dataIn, bs), 185 new Integer (dataIn.readInt()) 186 }); 187 } catch (IOException e) { 188 throw e; 189 } catch (Throwable e) { 190 } 191 } 192 o.setStackTrace(ss); 193 } else { 194 short size = dataIn.readShort(); 195 for (int i = 0; i < size; i++) { 196 tightUnmarshalString(dataIn, bs); 197 tightUnmarshalString(dataIn, bs); 198 tightUnmarshalString(dataIn, bs); 199 dataIn.readInt(); 200 } 201 } 202 o.initCause(tightUnmarsalThrowable(wireFormat, dataIn, bs)); 203 204 } 205 return o; 206 } else { 207 return null; 208 } 209 } 210 211 private Throwable createThrowable(String className, String message) { 212 try { 213 Class clazz = ClassLoading.loadClass(className, BaseDataStreamMarshaller.class.getClassLoader()); 214 Constructor constructor = clazz.getConstructor(new Class []{String .class}); 215 return (Throwable ) constructor.newInstance(new Object []{message}); 216 } catch (Throwable e) { 217 return new Throwable (className+": "+message); 218 } 219 } 220 221 protected int tightMarshalThrowable1(OpenWireFormat wireFormat, Throwable o, BooleanStream bs) throws IOException { 222 if( o==null ) { 223 bs.writeBoolean(false); 224 return 0; 225 } else { 226 int rc=0; 227 bs.writeBoolean(true); 228 rc += tightMarshalString1(o.getClass().getName(), bs); 229 rc += tightMarshalString1(o.getMessage(), bs); 230 if( wireFormat.isStackTraceEnabled() ) { 231 rc += 2; 232 StackTraceElement [] stackTrace = o.getStackTrace(); 233 for (int i = 0; i < stackTrace.length; i++) { 234 StackTraceElement element = stackTrace[i]; 235 rc += tightMarshalString1(element.getClassName(), bs); 236 rc += tightMarshalString1(element.getMethodName(), bs); 237 rc += tightMarshalString1(element.getFileName(), bs); 238 rc += 4; 239 } 240 rc += tightMarshalThrowable1(wireFormat, o.getCause(), bs); 241 } 242 return rc; 243 } 244 } 245 246 protected void tightMarshalThrowable2(OpenWireFormat wireFormat, Throwable o, DataOutput dataOut, BooleanStream bs) throws IOException { 247 if( bs.readBoolean() ) { 248 tightMarshalString2(o.getClass().getName(), dataOut, bs); 249 tightMarshalString2(o.getMessage(), dataOut, bs); 250 if( wireFormat.isStackTraceEnabled() ) { 251 StackTraceElement [] stackTrace = o.getStackTrace(); 252 dataOut.writeShort(stackTrace.length); 253 for (int i = 0; i < stackTrace.length; i++) { 254 StackTraceElement element = stackTrace[i]; 255 tightMarshalString2(element.getClassName(), dataOut, bs); 256 tightMarshalString2(element.getMethodName(), dataOut, bs); 257 tightMarshalString2(element.getFileName(), dataOut, bs); 258 dataOut.writeInt(element.getLineNumber()); 259 } 260 tightMarshalThrowable2(wireFormat, o.getCause(), dataOut, bs); 261 } 262 } 263 } 264 265 protected String tightUnmarshalString(DataInput dataIn, BooleanStream bs) throws IOException { 266 if( bs.readBoolean() ) { 267 if( bs.readBoolean() ) { 268 int size = dataIn.readShort(); 269 byte data[] = new byte[size]; 270 dataIn.readFully(data); 271 return new String (data,0); } else { 273 return dataIn.readUTF(); 274 } 275 } else { 276 return null; 277 } 278 } 279 280 protected int tightMarshalString1(String value, BooleanStream bs) throws IOException { 281 bs.writeBoolean(value!=null); 282 if( value!=null ) { 283 284 int strlen = value.length(); 285 int utflen = 0; 286 char[] charr = new char[strlen]; 287 int c, count = 0; 288 boolean isOnlyAscii=true; 289 290 value.getChars(0, strlen, charr, 0); 291 292 for (int i = 0; i < strlen; i++) { 293 c = charr[i]; 294 if ((c >= 0x0001) && (c <= 0x007F)) { 295 utflen++; 296 } else if (c > 0x07FF) { 297 utflen += 3; 298 isOnlyAscii=false; 299 } else { 300 isOnlyAscii=false; 301 utflen += 2; 302 } 303 } 304 305 if( utflen >= Short.MAX_VALUE ) 306 throw new IOException ("Encountered a String value that is too long to encode."); 307 308 bs.writeBoolean(isOnlyAscii); 309 return utflen+2; 310 311 } else { 312 return 0; 313 } 314 } 315 316 protected void tightMarshalString2(String value, DataOutput dataOut, BooleanStream bs) throws IOException { 317 if( bs.readBoolean() ) { 318 if( bs.readBoolean() ) { 320 dataOut.writeShort(value.length()); 321 dataOut.writeBytes(value); 322 } else { 323 dataOut.writeUTF(value); 324 } 325 } 326 } 327 328 protected int tightMarshalObjectArray1(OpenWireFormat wireFormat, DataStructure[] objects, BooleanStream bs) throws IOException { 329 if( objects != null ) { 330 int rc=0; 331 bs.writeBoolean(true); 332 rc += 2; 333 for( int i=0; i < objects.length; i++ ) { 334 rc += tightMarshalNestedObject1(wireFormat,objects[i], bs); 335 } 336 return rc; 337 } else { 338 bs.writeBoolean(false); 339 return 0; 340 } 341 } 342 343 protected void tightMarshalObjectArray2(OpenWireFormat wireFormat, DataStructure[] objects, DataOutput dataOut, BooleanStream bs) throws IOException { 344 if( bs.readBoolean() ) { 345 dataOut.writeShort(objects.length); 346 for( int i=0; i < objects.length; i++ ) { 347 tightMarshalNestedObject2(wireFormat,objects[i], dataOut, bs); 348 } 349 } 350 } 351 352 protected int tightMarshalConstByteArray1(byte[] data, BooleanStream bs, int i) throws IOException { 353 return i; 354 } 355 protected void tightMarshalConstByteArray2(byte[] data, DataOutput dataOut, BooleanStream bs, int i) throws IOException { 356 dataOut.write(data, 0, i); 357 } 358 359 protected byte[] tightUnmarshalConstByteArray(DataInput dataIn, BooleanStream bs, int i) throws IOException { 360 byte data[] = new byte[i]; 361 dataIn.readFully(data); 362 return data; 363 } 364 365 protected int tightMarshalByteArray1(byte[] data, BooleanStream bs) throws IOException { 366 bs.writeBoolean(data!=null); 367 if( data!=null ){ 368 return data.length+4; 369 } else { 370 return 0; 371 } 372 } 373 374 protected void tightMarshalByteArray2(byte[] data, DataOutput dataOut, BooleanStream bs) throws IOException { 375 if( bs.readBoolean() ){ 376 dataOut.writeInt(data.length); 377 dataOut.write(data); 378 } 379 } 380 381 protected byte[] tightUnmarshalByteArray(DataInput dataIn, BooleanStream bs) throws IOException { 382 byte rc[]=null; 383 if( bs.readBoolean() ) { 384 int size = dataIn.readInt(); 385 rc = new byte[size]; 386 dataIn.readFully(rc); 387 } 388 return rc; 389 } 390 391 protected int tightMarshalByteSequence1(ByteSequence data, BooleanStream bs) throws IOException { 392 bs.writeBoolean(data!=null); 393 if( data!=null ){ 394 return data.getLength()+4; 395 } else { 396 return 0; 397 } 398 } 399 400 protected void tightMarshalByteSequence2(ByteSequence data, DataOutput dataOut, BooleanStream bs) throws IOException { 401 if( bs.readBoolean() ){ 402 dataOut.writeInt(data.getLength()); 403 dataOut.write(data.getData(), data.getOffset(), data.getLength()); 404 } 405 } 406 407 protected ByteSequence tightUnmarshalByteSequence(DataInput dataIn, BooleanStream bs) throws IOException { 408 ByteSequence rc=null; 409 if( bs.readBoolean() ) { 410 int size = dataIn.readInt(); 411 byte[] t = new byte[size]; 412 dataIn.readFully(t); 413 return new ByteSequence(t, 0, size); 414 } 415 return rc; 416 } 417 418 422 public void looseMarshal(OpenWireFormat wireFormat, Object o, DataOutput dataOut) throws IOException { 423 } 424 public void looseUnmarshal(OpenWireFormat wireFormat, Object o, DataInput dataIn) throws IOException { 425 } 426 427 public void looseMarshalLong(OpenWireFormat wireFormat, long o, DataOutput dataOut) throws IOException { 428 dataOut.writeLong(o); 429 } 430 public long looseUnmarshalLong(OpenWireFormat wireFormat, DataInput dataIn) throws IOException { 431 return dataIn.readLong(); 432 } 433 434 protected DataStructure looseUnmarsalNestedObject(OpenWireFormat wireFormat, DataInput dataIn) throws IOException { 435 return wireFormat.looseUnmarshalNestedObject(dataIn); 436 } 437 protected void looseMarshalNestedObject(OpenWireFormat wireFormat, DataStructure o, DataOutput dataOut) throws IOException { 438 wireFormat.looseMarshalNestedObject(o, dataOut); 439 } 440 441 protected DataStructure looseUnmarsalCachedObject(OpenWireFormat wireFormat, DataInput dataIn) throws IOException { 442 if( wireFormat.isCacheEnabled() ) { 443 if( dataIn.readBoolean() ) { 444 short index = dataIn.readShort(); 445 DataStructure object = wireFormat.looseUnmarshalNestedObject(dataIn); 446 wireFormat.setInUnmarshallCache(index, object); 447 return object; 448 } else { 449 short index = dataIn.readShort(); 450 return wireFormat.getFromUnmarshallCache(index); 451 } 452 } else { 453 return wireFormat.looseUnmarshalNestedObject(dataIn); 454 } 455 } 456 protected void looseMarshalCachedObject(OpenWireFormat wireFormat, DataStructure o, DataOutput dataOut) throws IOException { 457 if( wireFormat.isCacheEnabled() ) { 458 Short index = wireFormat.getMarshallCacheIndex(o); 459 dataOut.writeBoolean(index == null); 460 if( index == null ) { 461 index = wireFormat.addToMarshallCache(o); 462 dataOut.writeShort(index.shortValue()); 463 wireFormat.looseMarshalNestedObject(o, dataOut); 464 } else { 465 dataOut.writeShort(index.shortValue()); 466 } 467 } else { 468 wireFormat.looseMarshalNestedObject(o, dataOut); 469 } 470 } 471 472 protected Throwable looseUnmarsalThrowable(OpenWireFormat wireFormat, DataInput dataIn) throws IOException { 473 if( dataIn.readBoolean() ) { 474 String clazz = looseUnmarshalString(dataIn); 475 String message = looseUnmarshalString(dataIn); 476 Throwable o = createThrowable(clazz, message); 477 if( wireFormat.isStackTraceEnabled() ) { 478 if( STACK_TRACE_ELEMENT_CONSTRUCTOR !=null) { 479 StackTraceElement ss[] = new StackTraceElement [dataIn.readShort()]; 480 for (int i = 0; i < ss.length; i++) { 481 try { 482 ss[i] = (StackTraceElement ) STACK_TRACE_ELEMENT_CONSTRUCTOR.newInstance(new Object []{ 483 looseUnmarshalString(dataIn), 484 looseUnmarshalString(dataIn), 485 looseUnmarshalString(dataIn), 486 new Integer (dataIn.readInt()) 487 }); 488 } catch (IOException e) { 489 throw e; 490 } catch (Throwable e) { 491 } 492 } 493 o.setStackTrace(ss); 494 } else { 495 short size = dataIn.readShort(); 496 for (int i = 0; i < size; i++) { 497 looseUnmarshalString(dataIn); 498 looseUnmarshalString(dataIn); 499 looseUnmarshalString(dataIn); 500 dataIn.readInt(); 501 } 502 } 503 o.initCause(looseUnmarsalThrowable(wireFormat, dataIn)); 504 505 } 506 return o; 507 } else { 508 return null; 509 } 510 } 511 512 513 protected void looseMarshalThrowable(OpenWireFormat wireFormat, Throwable o, DataOutput dataOut) throws IOException { 514 dataOut.writeBoolean(o!=null); 515 if( o!=null ) { 516 looseMarshalString(o.getClass().getName(), dataOut); 517 looseMarshalString(o.getMessage(), dataOut); 518 if( wireFormat.isStackTraceEnabled() ) { 519 StackTraceElement [] stackTrace = o.getStackTrace(); 520 dataOut.writeShort(stackTrace.length); 521 for (int i = 0; i < stackTrace.length; i++) { 522 StackTraceElement element = stackTrace[i]; 523 looseMarshalString(element.getClassName(), dataOut); 524 looseMarshalString(element.getMethodName(), dataOut); 525 looseMarshalString(element.getFileName(), dataOut); 526 dataOut.writeInt(element.getLineNumber()); 527 } 528 looseMarshalThrowable(wireFormat, o.getCause(), dataOut); 529 } 530 } 531 } 532 533 protected String looseUnmarshalString(DataInput dataIn) throws IOException { 534 if( dataIn.readBoolean() ) { 535 return dataIn.readUTF(); 536 } else { 537 return null; 538 } 539 } 540 541 protected void looseMarshalString(String value, DataOutput dataOut) throws IOException { 542 dataOut.writeBoolean(value!=null); 543 if( value!=null ) { 544 dataOut.writeUTF(value); 545 } 546 } 547 548 protected void looseMarshalObjectArray(OpenWireFormat wireFormat, DataStructure[] objects, DataOutput dataOut) throws IOException { 549 dataOut.writeBoolean(objects!=null); 550 if( objects!=null ) { 551 dataOut.writeShort(objects.length); 552 for( int i=0; i < objects.length; i++ ) { 553 looseMarshalNestedObject(wireFormat,objects[i], dataOut); 554 } 555 } 556 } 557 558 protected void looseMarshalConstByteArray(OpenWireFormat wireFormat, byte[] data, DataOutput dataOut, int i) throws IOException { 559 dataOut.write(data, 0, i); 560 } 561 562 protected byte[] looseUnmarshalConstByteArray(DataInput dataIn, int i) throws IOException { 563 byte data[] = new byte[i]; 564 dataIn.readFully(data); 565 return data; 566 } 567 568 protected void looseMarshalByteArray(OpenWireFormat wireFormat, byte[] data, DataOutput dataOut) throws IOException { 569 dataOut.writeBoolean(data!=null); 570 if( data!=null ){ 571 dataOut.writeInt(data.length); 572 dataOut.write(data); 573 } 574 } 575 576 protected byte[] looseUnmarshalByteArray(DataInput dataIn) throws IOException { 577 byte rc[]=null; 578 if( dataIn.readBoolean() ) { 579 int size = dataIn.readInt(); 580 rc = new byte[size]; 581 dataIn.readFully(rc); 582 } 583 return rc; 584 } 585 586 protected void looseMarshalByteSequence(OpenWireFormat wireFormat, ByteSequence data, DataOutput dataOut) throws IOException { 587 dataOut.writeBoolean(data!=null); 588 if( data!=null ){ 589 dataOut.writeInt(data.getLength()); 590 dataOut.write(data.getData(), data.getOffset(), data.getLength()); 591 } 592 } 593 594 protected ByteSequence looseUnmarshalByteSequence(DataInput dataIn) throws IOException { 595 ByteSequence rc=null; 596 if( dataIn.readBoolean() ) { 597 int size = dataIn.readInt(); 598 byte[] t = new byte[size]; 599 dataIn.readFully(t); 600 rc = new ByteSequence(t, 0, size); 601 } 602 return rc; 603 } 604 } 605 | Popular Tags |