KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bsf > debug > util > ResultCell


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2002 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowlegement may appear in the software itself,
24  * if and wherever such third-party acknowlegements normally appear.
25  *
26  * 4. The names "Apache BSF", "Apache", and "Apache Software Foundation"
27  * must not be used to endorse or promote products derived from
28  * this software without prior written permission. For written
29  * permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache"
32  * nor may "Apache" appear in their names without prior written
33  * permission of the Apache Group.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many individuals
50  * on behalf of the Apache Software Foundation and was originally created by
51  * Sanjiva Weerawarana and others at International Business Machines
52  * Corporation. For more information on the Apache Software Foundation,
53  * please see <http://www.apache.org/>.
54  */

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 JavaDoc oval;
68     public boolean bool;
69     
70     private Exception JavaDoc exception; // when an exception occurs at the server.
71
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     //------------------------------------------------------------
88
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 JavaDoc(stackTraceBytes),
113                                        DebugLog.BSF_LOG_L3);
114     }
115
116     public Exception JavaDoc getException() {
117         return exception;
118     }
119     
120     public void setException(Exception JavaDoc 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 JavaDoc)readObject();
136         stackTraceBytes = (byte[])readObject();
137     }
138     //------------------------------------------------------------
139
public String JavaDoc toString() {
140         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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     //------------------------------------------------------------
157
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         // allocate the outgoing packet...
171
this.fOutPacket = new ByteArrayOutputStream();
172         this.fDataOutputStream = new DataOutputStream(fOutPacket);
173         // no in packet, it will be provided upon
174
// completion...
175
this.fInPacket = null;
176         this.fDataInputStream = null;
177         
178         // format the header of the packet.
179
// Pre-packet header is composed of the size,
180
// the thid, and the cmdId.
181
// They are written when the packet is emitted.
182
writeId(classId);
183         writeId(methodId);
184         writeObject(self);
185     }
186     //------------------------------------------------------------
187
void incomingInvocation(int cmdId, byte bytes[])
188     throws IOException {
189
190         this.done = false;
191         this.cmdId = cmdId;
192
193         // set the incoming packet...
194
this.fInPacket = new ByteArrayInputStream(bytes);
195         this.fDataInputStream = new DataInputStream(fInPacket);
196         
197         // pre-allocate the outgoing packet.
198
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     //------------------------------------------------------------
215
/**
216      * Once a packet has been read from the socket,
217      * it is passed to the ResultCell and further processed
218      * to parse the remaining data item.
219      */

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     //------------------------------------------------------------
227
public void completionNotify() {
228         if (thread!=null) {
229             thread.completionNotify(this);
230         }
231     }
232     //------------------------------------------------------------
233
public void parseResult() {
234         try {
235             _parseResult();
236         } catch (Exception JavaDoc ex) {
237             m_con.wireExceptionNotify(ex);
238         }
239     }
240     //------------------------------------------------------------
241
public void sendResult() {
242         try {
243             _sendResult();
244         } catch (Exception JavaDoc ex) {
245             m_con.wireExceptionNotify(ex);
246         }
247     }
248     //------------------------------------------------------------
249
private void _parseResult()
250         throws Exception JavaDoc {
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 JavaDoc("Error in the request/answer protocol");
276         }
277     }
278
279     private void _sendResult()
280         throws Exception JavaDoc {
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 JavaDoc("Error in the request/answer protocol");
326         }
327         bytes = fOutPacket.toByteArray();
328         m_con.sendPacket(thread.getThId(),cmdId,true,bytes,false);
329     }
330     //------------------------------------------------------------
331
public void sendInvocation()
332         throws Exception JavaDoc {
333                     
334         byte bytes[];
335         bytes = fOutPacket.toByteArray();
336         m_con.sendPacket(thread.getThId(),cmdId,false,bytes,false);
337     }
338     //--------------------------------------------
339
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 JavaDoc obj) {
363         oval = obj;
364         waitingForCode = DebugConstants.WAIT_FOR_OBJECT;
365     }
366
367     //////////////////////////////////////////////////////////
368
/**
369      * Default writing methods for marshalling out parameters
370      * in remote method calls.
371      */

372     //////////////////////////////////////////////////////////
373

374     ////////////////////////////////////////////////////////////////////
375

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 JavaDoc 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                     // no need to send the tid, the uid identifies
438
// the skeleton on the other side that already exists.
439
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     //////////////////////////////////////////////////////////
455
/**
456      * Default reading methods for unmarshalling in parameters
457      * from remote method calls.
458      */

459     //////////////////////////////////////////////////////////
460

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 JavaDoc readObject() throws IOException {
505             ObjectInputStream ois;
506             
507             Object JavaDoc 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 JavaDoc ex) {
528                     object = null;
529                 }
530                 break;
531             case DebugConstants.STUB_OBJECT :
532                 // it was a stub on the sender side,
533
// so it maps to a local skeleton
534
// that was remoted...
535
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                 // it was a skeleton on the sender's side
543
// so it has to swizzled into a stub on this
544
// side...
545
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 JavaDoc("Wire Protocol Error: unknown object format.");
554             }
555             return object;
556     }
557
558     ////////////////////////////////////////////////////////////////////
559
////////////////////////////////////////////////////////////////////
560
/**
561      * The following methods are for waiting for the result of an
562      * outgoing method invocation.
563      */

564     ////////////////////////////////////////////////////////////////////
565
////////////////////////////////////////////////////////////////////
566
public Object JavaDoc waitForValueObject() throws Exception JavaDoc {
567         
568         waitingForCode = DebugConstants.WAIT_FOR_OBJECT;
569         thread.waitOnCompletion(this);
570         return oval;
571     }
572     //-----------------------------------------------------------
573
public boolean waitForBooleanValue() throws Exception JavaDoc {
574         waitingForCode = DebugConstants.WAIT_FOR_BOOLEAN;
575         thread.waitOnCompletion(this);
576         return bool;
577     }
578     //-----------------------------------------------------------
579
public int waitForIntValue() throws Exception JavaDoc {
580         waitingForCode = DebugConstants.WAIT_FOR_INT;
581         thread.waitOnCompletion(this);
582         return val32;
583     }
584     //-----------------------------------------------------------
585
public long waitForLongValue() throws Exception JavaDoc {
586         waitingForCode = DebugConstants.WAIT_FOR_LONG;
587         thread.waitOnCompletion(this);
588         return val64;
589     }
590     //-----------------------------------------------------------
591
public float waitForFloatValue() throws Exception JavaDoc {
592         waitingForCode = DebugConstants.WAIT_FOR_FLOAT;
593         thread.waitOnCompletion(this);
594         return fval;
595     }
596     //-----------------------------------------------------------
597
public double waitForDoubleValue() throws Exception JavaDoc {
598         waitingForCode = DebugConstants.WAIT_FOR_DOUBLE;
599         thread.waitOnCompletion(this);
600         return dval;
601     }
602     //-----------------------------------------------------------
603

604     public void waitForCompletion() throws Exception JavaDoc {
605         waitingForCode = DebugConstants.WAIT_FOR_VOID;
606         thread.waitOnCompletion(this);
607     }
608     //-----------------------------------------------------------
609
public Object JavaDoc waitForObject() throws Exception JavaDoc {
610         waitingForCode = DebugConstants.WAIT_FOR_OBJECT;
611         thread.waitOnCompletion(this);
612         return oval;
613     }
614 }
615
Popular Tags