1 55 56 package org.apache.bsf.debug.util; 57 58 import java.io.*; 59 import org.apache.bsf.debug.jsdi.JsEngine; 60 61 public class ResultCell { 62 63 public int val32; 64 public long val64; 65 public float fval; 66 public double dval; 67 public Object oval; 68 public boolean bool; 69 70 private Exception exception; private byte stackTraceBytes[]; 72 73 public int tid, uid, classId, methodId, cmdId, waitingForCode; 74 public ThreadCell thread; 75 public ResultCell parent; 76 public JsEngine engine; 77 public Stub selfStub; 78 public Skeleton selfSkel; 79 public boolean done, disconnected; 80 81 private SocketConnection m_con; 82 private DataOutputStream fDataOutputStream; 83 private DataInputStream fDataInputStream; 84 private ByteArrayInputStream fInPacket; 85 private ByteArrayOutputStream fOutPacket; 86 87 public void print() { 89 DebugLog.stdoutPrintln("ResultCell...", DebugLog.BSF_LOG_L3); 90 DebugLog.stdoutPrintln(" tid=" + 91 DebugConstants.getConstantName(tid), 92 DebugLog.BSF_LOG_L3); 93 DebugLog.stdoutPrintln(" classId [" + classId + 94 "] =" + 95 DebugConstants.getConstantName(classId), 96 DebugLog.BSF_LOG_L3); 97 DebugLog.stdoutPrintln(" methodId [" + methodId + 98 "] =" + DebugConstants.getConstantName(methodId), DebugLog.BSF_LOG_L3); 99 100 DebugLog.stdoutPrintln(" bool="+bool, DebugLog.BSF_LOG_L3); 101 DebugLog.stdoutPrintln(" val32="+val32, DebugLog.BSF_LOG_L3); 102 DebugLog.stdoutPrintln(" val64="+val64, DebugLog.BSF_LOG_L3); 103 DebugLog.stdoutPrintln(" fval="+fval, DebugLog.BSF_LOG_L3); 104 DebugLog.stdoutPrintln(" dval="+dval, DebugLog.BSF_LOG_L3); 105 DebugLog.stdoutPrintln(" oval="+oval, DebugLog.BSF_LOG_L3); 106 DebugLog.stdoutPrintln(" exception=" + exception, 107 DebugLog.BSF_LOG_L3); 108 DebugLog.stdoutPrintln(" message=" + 109 exception.getMessage(), DebugLog.BSF_LOG_L3); 110 DebugLog.stdoutPrintln(" stack trace:", 111 DebugLog.BSF_LOG_L3); 112 DebugLog.stdoutPrintln(new String (stackTraceBytes), 113 DebugLog.BSF_LOG_L3); 114 } 115 116 public Exception getException() { 117 return exception; 118 } 119 120 public void setException(Exception ex) { 121 exception = ex; 122 ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 123 PrintStream stream = new PrintStream(bytes); 124 125 stream.println(ex.getMessage()); 126 ex.printStackTrace(stream); 127 stackTraceBytes = bytes.toByteArray(); 128 } 129 public void writeException() throws IOException { 130 writeObject(exception); 131 writeObject(stackTraceBytes); 132 } 133 public void readException() throws IOException { 134 135 exception = (Exception )readObject(); 136 stackTraceBytes = (byte[])readObject(); 137 } 138 public String toString() { 140 StringBuffer buf = new StringBuffer (); 141 buf.append("ResultCell...\n"); 142 buf.append(" tid="+DebugConstants.getConstantName(tid)+"\n"); 143 buf.append(" classId ["+classId+"] ="+DebugConstants.getConstantName(classId)+"\n"); 144 buf.append(" methodId ["+methodId+"] ="+DebugConstants.getConstantName(methodId)+"\n"); 145 146 buf.append(" bool="+bool+"\n"); 147 buf.append(" val32="+val32+"\n"); 148 buf.append(" val64="+val64+"\n"); 149 buf.append(" fval="+fval+"\n"); 150 buf.append(" dval="+dval+"\n"); 151 buf.append(" oval="+oval+"\n"); 152 buf.append(" exception="+exception+"\n"); 153 buf.append(" message="+exception.getMessage()+"\n"); 154 return buf.toString(); 155 } 156 ResultCell(SocketConnection con) 158 throws IOException { 159 m_con = con; 160 } 161 162 void outgoingInvocation(int cmdId, int classId, int methodId, Stub self) 163 throws IOException { 164 this.done = false; 165 this.cmdId = cmdId; 166 this.classId = classId; 167 this.methodId = methodId; 168 this.selfStub = self; 169 170 this.fOutPacket = new ByteArrayOutputStream(); 172 this.fDataOutputStream = new DataOutputStream(fOutPacket); 173 this.fInPacket = null; 176 this.fDataInputStream = null; 177 178 writeId(classId); 183 writeId(methodId); 184 writeObject(self); 185 } 186 void incomingInvocation(int cmdId, byte bytes[]) 188 throws IOException { 189 190 this.done = false; 191 this.cmdId = cmdId; 192 193 this.fInPacket = new ByteArrayInputStream(bytes); 195 this.fDataInputStream = new DataInputStream(fInPacket); 196 197 this.fOutPacket = new ByteArrayOutputStream(); 199 this.fDataOutputStream = new DataOutputStream(fOutPacket); 200 201 this.classId = readId(); 202 this.methodId = readId(); 203 this.selfSkel = (Skeleton)readObject(); 204 205 DebugLog.stdoutPrintln(" " + 206 DebugConstants.getConstantName(classId) + 207 "::" + 208 DebugConstants.getConstantName(methodId), 209 DebugLog.BSF_LOG_L3); 210 DebugLog.stdoutPrintln(" on " + this.selfSkel, 211 DebugLog.BSF_LOG_L3); 212 } 213 214 220 public void setPacketBytes(byte bytes[]) { 221 fInPacket = new ByteArrayInputStream(bytes); 222 this.fDataInputStream = new DataInputStream(fInPacket); 223 fOutPacket = new ByteArrayOutputStream(); 224 this.fDataOutputStream = new DataOutputStream(fOutPacket); 225 } 226 public void completionNotify() { 228 if (thread!=null) { 229 thread.completionNotify(this); 230 } 231 } 232 public void parseResult() { 234 try { 235 _parseResult(); 236 } catch (Exception ex) { 237 m_con.wireExceptionNotify(ex); 238 } 239 } 240 public void sendResult() { 242 try { 243 _sendResult(); 244 } catch (Exception ex) { 245 m_con.wireExceptionNotify(ex); 246 } 247 } 248 private void _parseResult() 250 throws Exception { 251 double dval; 252 253 switch(waitingForCode) { 254 case DebugConstants.WAIT_FOR_VOID: 255 break; 256 case DebugConstants.WAIT_FOR_BOOLEAN: 257 bool = readBoolean(); 258 break; 259 case DebugConstants.WAIT_FOR_INT: 260 val32 = readInt(); 261 break; 262 case DebugConstants.WAIT_FOR_LONG: 263 val64 = readLong(); 264 break; 265 case DebugConstants.WAIT_FOR_FLOAT: 266 fval = readFloat(); 267 break; 268 case DebugConstants.WAIT_FOR_DOUBLE: 269 dval = readDouble(); 270 break; 271 case DebugConstants.WAIT_FOR_OBJECT: 272 oval = readObject(); 273 break; 274 default : 275 throw new Error ("Error in the request/answer protocol"); 276 } 277 } 278 279 private void _sendResult() 280 throws Exception { 281 282 byte bytes[]; 283 284 if (exception != null) { 285 DebugLog.stdoutPrintln("\n**** Exception occurred while invoking...", DebugLog.BSF_LOG_L0); 286 DebugLog.stdoutPrintln(" message is " + 287 exception.getMessage(), 288 DebugLog.BSF_LOG_L0); 289 exception.printStackTrace(); 290 291 fOutPacket = new ByteArrayOutputStream(); 292 this.fDataOutputStream = new DataOutputStream(fOutPacket); 293 294 writeException(); 295 296 bytes = fOutPacket.toByteArray(); 297 m_con.sendPacket(thread.getThId(),cmdId,true,bytes,true); 298 299 DebugLog.stdoutPrintln(" Exception has been sent back...", DebugLog.BSF_LOG_L0); 300 return; 301 } 302 303 switch(waitingForCode) { 304 case DebugConstants.WAIT_FOR_VOID: 305 break; 306 case DebugConstants.WAIT_FOR_BOOLEAN: 307 writeBoolean(bool); 308 break; 309 case DebugConstants.WAIT_FOR_INT: 310 writeInt(val32); 311 break; 312 case DebugConstants.WAIT_FOR_LONG: 313 writeLong(val64); 314 break; 315 case DebugConstants.WAIT_FOR_FLOAT: 316 writeFloat(fval); 317 break; 318 case DebugConstants.WAIT_FOR_DOUBLE: 319 writeDouble(dval); 320 break; 321 case DebugConstants.WAIT_FOR_OBJECT: 322 writeObject(oval); 323 break; 324 default : 325 throw new Error ("Error in the request/answer protocol"); 326 } 327 bytes = fOutPacket.toByteArray(); 328 m_con.sendPacket(thread.getThId(),cmdId,true,bytes,false); 329 } 330 public void sendInvocation() 332 throws Exception { 333 334 byte bytes[]; 335 bytes = fOutPacket.toByteArray(); 336 m_con.sendPacket(thread.getThId(),cmdId,false,bytes,false); 337 } 338 public void voidResult() { 340 waitingForCode = DebugConstants.WAIT_FOR_VOID; 341 } 342 public void booleanResult(boolean val) { 343 bool = val; 344 waitingForCode = DebugConstants.WAIT_FOR_BOOLEAN; 345 } 346 public void intResult(int val) { 347 val32 = val; 348 waitingForCode = DebugConstants.WAIT_FOR_INT; 349 } 350 public void longResult(long val) { 351 val64 = val; 352 waitingForCode = DebugConstants.WAIT_FOR_LONG; 353 } 354 public void floatResult(float val) { 355 fval =val; 356 waitingForCode = DebugConstants.WAIT_FOR_FLOAT; 357 } 358 public void doubleResult(double val) { 359 dval = val; 360 waitingForCode = DebugConstants.WAIT_FOR_DOUBLE; 361 } 362 public void objectResult(Object obj) { 363 oval = obj; 364 waitingForCode = DebugConstants.WAIT_FOR_OBJECT; 365 } 366 367 372 374 376 public void writeBoolean(boolean bool) throws IOException { 377 DebugLog.stdoutPrintln(" marshalling bool" + bool, DebugLog.BSF_LOG_L3); 378 fDataOutputStream.writeBoolean(bool); 379 } 380 381 public void writeId(int id) throws IOException { 382 DebugLog.stdoutPrintln(" marshalling id=" + 383 DebugConstants.getConstantName(id), 384 DebugLog.BSF_LOG_L3); 385 fDataOutputStream.writeInt(id); 386 } 387 388 public void writeInt(int val32) throws IOException { 389 DebugLog.stdoutPrintln(" marshalling int " + val32, DebugLog.BSF_LOG_L3); 390 fDataOutputStream.writeInt(val32); 391 } 392 393 public void writeLong(long val64) throws IOException { 394 DebugLog.stdoutPrintln(" marshalling long " + val64, 395 DebugLog.BSF_LOG_L3); 396 fDataOutputStream.writeLong(val64); 397 } 398 399 public void writeFloat(float fval) throws IOException { 400 DebugLog.stdoutPrintln(" marshalling float " + fval, 401 DebugLog.BSF_LOG_L3); 402 fDataOutputStream.writeFloat(fval); 403 } 404 405 public void writeDouble(double dval) throws IOException { 406 DebugLog.stdoutPrintln(" marshalling double " + dval, 407 DebugLog.BSF_LOG_L3); 408 fDataOutputStream.writeDouble(dval); 409 } 410 411 public void writeObject(Object object) throws IOException { 412 if (object == null) { 413 DebugLog.stdoutPrintln(" marshalling null object ", 414 DebugLog.BSF_LOG_L3); 415 fDataOutputStream.writeInt(DebugConstants.NULL_OBJECT); 416 } 417 else { 418 if (object instanceof Skeleton) { 419 Skeleton skel = (Skeleton)object; 420 m_con.exportSkeleton(skel); 421 422 DebugLog.stdoutPrintln(" marshalling (iid=" + 423 skel.getTid() + ";uid=" + 424 skel.getUid() + " skeleton= " + 425 skel, DebugLog.BSF_LOG_L3); 426 fDataOutputStream.writeInt(DebugConstants.SKEL_OBJECT); 427 fDataOutputStream.writeInt(skel.getTid()); 428 fDataOutputStream.writeInt(skel.getUid()); 429 } 430 else if (object instanceof Stub) { 431 Stub stub = (Stub)object; 432 433 DebugLog.stdoutPrintln(" marshalling (tid=" + 434 tid + ";uid=" + stub.getUid() + 435 " stub= " + stub, DebugLog.BSF_LOG_L3); 436 fDataOutputStream.writeInt(DebugConstants.STUB_OBJECT); 437 fDataOutputStream.writeInt(stub.getUid()); 440 } 441 else { 442 ObjectOutputStream oos; 443 444 DebugLog.stdoutPrintln("Connection marshalling " + 445 "value object " + object, 446 DebugLog.BSF_LOG_L3); 447 fDataOutputStream.writeInt(DebugConstants.VALUE_OBJECT); 448 oos = new ObjectOutputStream(fOutPacket); 449 oos.writeObject(object); 450 } 451 } 452 } 453 454 459 461 public boolean readBoolean() throws IOException { 462 boolean bool = fDataInputStream.readBoolean(); 463 DebugLog.stdoutPrintln("Connection marshalling boolean " + 464 bool, DebugLog.BSF_LOG_L3); 465 return bool; 466 } 467 468 public int readId() throws IOException { 469 int val32 = fDataInputStream.readInt(); 470 DebugLog.stdoutPrintln(" Unmarshalling id=[" + 471 DebugConstants.getConstantName(val32) + 472 " (" + val32 + ")", DebugLog.BSF_LOG_L3); 473 return val32; 474 } 475 476 public int readInt() throws IOException { 477 int val32 = fDataInputStream.readInt(); 478 DebugLog.stdoutPrintln(" Unmarshalling int " + val32, 479 DebugLog.BSF_LOG_L3); 480 return val32; 481 } 482 483 public long readLong() throws IOException { 484 long val64 = fDataInputStream.readLong(); 485 DebugLog.stdoutPrintln(" Unmarshalling long " + val64, 486 DebugLog.BSF_LOG_L3); 487 return val64; 488 } 489 490 public float readFloat() throws IOException { 491 float fval = fDataInputStream.readFloat(); 492 DebugLog.stdoutPrintln(" Unmarshalling float " + fval, 493 DebugLog.BSF_LOG_L3); 494 return fval; 495 } 496 497 public double readDouble() throws IOException { 498 double dval = fDataInputStream.readDouble(); 499 DebugLog.stdoutPrintln(" Unmarshalling double " + dval, 500 DebugLog.BSF_LOG_L3); 501 return dval; 502 } 503 504 public Object readObject() throws IOException { 505 ObjectInputStream ois; 506 507 Object object = null; 508 int tag; 509 int tid, uid, enguid; 510 511 DebugLog.stdoutPrintln(" Unmarshalling an object...", 512 DebugLog.BSF_LOG_L3); 513 514 tag = fDataInputStream.readInt(); 515 switch (tag) { 516 case DebugConstants.NULL_OBJECT : 517 object = null; 518 DebugLog.stdoutPrintln(" null object", 519 DebugLog.BSF_LOG_L3); 520 break; 521 case DebugConstants.VALUE_OBJECT : 522 try { 523 ois = new ObjectInputStream(fInPacket); 524 object = ois.readObject(); 525 DebugLog.stdoutPrintln(" value object= " + 526 object, DebugLog.BSF_LOG_L3); 527 } catch (ClassNotFoundException ex) { 528 object = null; 529 } 530 break; 531 case DebugConstants.STUB_OBJECT : 532 uid = fDataInputStream.readInt(); 536 object = m_con.getSkeleton(uid); 537 DebugLog.stdoutPrintln(" Local skel object= " + 538 object + "(uid=" + uid + ")", 539 DebugLog.BSF_LOG_L3); 540 break; 541 case DebugConstants.SKEL_OBJECT : 542 tid = fDataInputStream.readInt(); 546 uid = fDataInputStream.readInt(); 547 object = m_con.getStub(tid,uid); 548 DebugLog.stdoutPrintln(" Local stub object= " + 549 object + "(tid=" + tid + "; uid=" + 550 uid + ")", DebugLog.BSF_LOG_L3); 551 break; 552 default: 553 throw new Error ("Wire Protocol Error: unknown object format."); 554 } 555 return object; 556 } 557 558 564 public Object waitForValueObject() throws Exception { 567 568 waitingForCode = DebugConstants.WAIT_FOR_OBJECT; 569 thread.waitOnCompletion(this); 570 return oval; 571 } 572 public boolean waitForBooleanValue() throws Exception { 574 waitingForCode = DebugConstants.WAIT_FOR_BOOLEAN; 575 thread.waitOnCompletion(this); 576 return bool; 577 } 578 public int waitForIntValue() throws Exception { 580 waitingForCode = DebugConstants.WAIT_FOR_INT; 581 thread.waitOnCompletion(this); 582 return val32; 583 } 584 public long waitForLongValue() throws Exception { 586 waitingForCode = DebugConstants.WAIT_FOR_LONG; 587 thread.waitOnCompletion(this); 588 return val64; 589 } 590 public float waitForFloatValue() throws Exception { 592 waitingForCode = DebugConstants.WAIT_FOR_FLOAT; 593 thread.waitOnCompletion(this); 594 return fval; 595 } 596 public double waitForDoubleValue() throws Exception { 598 waitingForCode = DebugConstants.WAIT_FOR_DOUBLE; 599 thread.waitOnCompletion(this); 600 return dval; 601 } 602 604 public void waitForCompletion() throws Exception { 605 waitingForCode = DebugConstants.WAIT_FOR_VOID; 606 thread.waitOnCompletion(this); 607 } 608 public Object waitForObject() throws Exception { 610 waitingForCode = DebugConstants.WAIT_FOR_OBJECT; 611 thread.waitOnCompletion(this); 612 return oval; 613 } 614 } 615 | Popular Tags |