KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bsf > debug > meta > JsEngineStub


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.meta;
57
58 import java.rmi.RemoteException JavaDoc;
59 import java.util.*;
60 import java.io.*;
61 import org.apache.bsf.debug.jsdi.*;
62 import org.apache.bsf.debug.util.*;
63
64 public class JsEngineStub extends Stub implements JsEngine {
65
66     //-------------------------------------------------
67
boolean fSuspended;
68     JsCallbacks fCallbacks;
69     //-------------------------------------------------
70
public JsEngineStub(SocketConnection con,int tid, int uid) {
71         super(con,tid,uid);
72     }
73
74     ////////////////////////////////////////////////////////////////////
75
// JsEngine Interface
76
////////////////////////////////////////////////////////////////////
77

78     public boolean poll() throws RemoteException JavaDoc {
79         ResultCell cell;
80         try {
81             cell = m_con.prepareOutgoingInvoke(this,DebugConstants.JS_ENGINE_TID,DebugConstants.CB_POLL);
82             return cell.waitForBooleanValue();
83         } catch (IOException ex) {
84             throw new RemoteException JavaDoc("Marshalling error", ex);
85         } catch (Exception JavaDoc ex) {
86             throw new RemoteException JavaDoc("Error at server", ex);
87         }
88     }
89     /**
90      * Set the associated debugger.
91      * @param debugger the debugger to be used on callbacks from
92      * the engine.
93      */

94     public void setDebugger(JsCallbacks debugger) throws RemoteException JavaDoc {
95
96         fCallbacks = debugger;
97
98         ResultCell cell;
99         try {
100             cell = m_con.prepareOutgoingInvoke(this,DebugConstants.JS_ENGINE_TID,DebugConstants.JE_SET_DEBUGGER);
101             cell.writeObject(debugger);
102             cell.waitForCompletion();
103         } catch (IOException ex) {
104             throw new RemoteException JavaDoc("Marshalling error", ex);
105         } catch (Exception JavaDoc ex) {
106             throw new RemoteException JavaDoc("Error at server", ex);
107         }
108     }
109
110     /**
111      * Return the current debugger.
112      * @return the debugger, or null if none is attached.
113      */

114     public JsCallbacks getDebugger() throws RemoteException JavaDoc {
115         return fCallbacks;
116     }
117
118
119     /**
120      * Allow the debugger to evaluate an expression
121      * within the current context.
122      */

123     public Object JavaDoc eval(String JavaDoc docname, String JavaDoc fnOrScript, int lineno)
124         throws RemoteException JavaDoc {
125         throw new Error JavaDoc("NYI");
126     }
127
128     /**
129      * Returns the count of JsContext on the current stack.
130      * This is a valid call only if the engine is stopped
131      * in a callback to the debugger (breakpoint or stepping
132      * completed).
133      */

134     public int getContextCount()
135         throws RemoteException JavaDoc {
136         
137         ResultCell cell;
138         try {
139             cell = m_con.prepareOutgoingInvoke(this,DebugConstants.JS_ENGINE_TID,DebugConstants.JE_GET_CONTEXT_COUNT);
140             return cell.waitForIntValue();
141         } catch (IOException ex) {
142             throw new RemoteException JavaDoc("Marshalling error", ex);
143         } catch (Exception JavaDoc ex) {
144             throw new RemoteException JavaDoc("Error at server", ex);
145         }
146     }
147
148     public String JavaDoc getThread() throws RemoteException JavaDoc {
149
150         ResultCell cell;
151         try {
152             cell =
153                 m_con.prepareOutgoingInvoke(this,
154                                             DebugConstants.JS_ENGINE_TID,
155                                             DebugConstants.JE_GET_THREAD);
156             return (String JavaDoc)cell.waitForObject();
157         }
158         catch (IOException ex) {
159             throw new RemoteException JavaDoc("Marshalling error", ex);
160         }
161         catch (Exception JavaDoc ex) {
162             throw new RemoteException JavaDoc("Error at server", ex);
163         }
164     }
165
166     public String JavaDoc getThreadGroup() throws RemoteException JavaDoc {
167
168         ResultCell cell;
169         try {
170             cell =
171                 m_con.prepareOutgoingInvoke(this,
172                                             DebugConstants.JS_ENGINE_TID,
173                                             DebugConstants.JE_GET_THREADGROUP);
174             return (String JavaDoc)cell.waitForObject();
175         }
176         catch (IOException ex) {
177             throw new RemoteException JavaDoc("Marshalling error", ex);
178         }
179         catch (Exception JavaDoc ex) {
180             throw new RemoteException JavaDoc("Error at server", ex);
181         }
182     }
183
184     /**
185      * Returns the JsContext at a certain depth.
186      * Depth zero is the top of the stack, that is,
187      * the inner execution context.
188      *
189      * This is a valid call only if the engine is stopped
190      * in a callback to the debugger (breakpoint or stepping
191      * completed).
192      */

193     public JsContext getContext(int depth)
194         throws RemoteException JavaDoc {
195
196         ResultCell cell;
197         try {
198             cell = m_con.prepareOutgoingInvoke(this,DebugConstants.JS_ENGINE_TID,DebugConstants.JE_GET_CONTEXT_AT);
199             cell.writeInt(depth);
200             return (JsContext)cell.waitForObject();
201         } catch (IOException ex) {
202             throw new RemoteException JavaDoc("Marshalling error", ex);
203         } catch (Exception JavaDoc ex) {
204             throw new RemoteException JavaDoc("Error at server", ex);
205         }
206     }
207     /**
208      * Any execution in JavaScript happen with respect to a
209      * global object, sort of the top-level name space for
210      * properties. This is global object return by this call.
211      */

212     public JsObject getGlobalObject()
213         throws RemoteException JavaDoc {
214             
215         ResultCell cell;
216         try {
217             cell = m_con.prepareOutgoingInvoke(this,DebugConstants.JS_ENGINE_TID,DebugConstants.JE_GET_GLOBAL_OBJECT);
218             return (JsObject)cell.waitForObject();
219                 
220         } catch (IOException ex) {
221             throw new RemoteException JavaDoc("Marshalling error", ex);
222         } catch (Exception JavaDoc ex) {
223             throw new RemoteException JavaDoc("Error at server", ex);
224         }
225     }
226
227     /**
228      * As per ECMA specification, each JavaScript execution
229      * defines a unique object for the undefined value.
230      */

231     public JsObject getUndefinedValue()
232         throws RemoteException JavaDoc {
233             
234         ResultCell cell;
235         try {
236             cell = m_con.prepareOutgoingInvoke(this,DebugConstants.JS_ENGINE_TID,DebugConstants.JE_GET_UNDEFINED_VALUE);
237             return (JsObject)cell.waitForObject();
238                 
239         } catch (IOException ex) {
240             throw new RemoteException JavaDoc("Marshalling error", ex);
241         } catch (Exception JavaDoc ex) {
242             throw new RemoteException JavaDoc("Error at server", ex);
243         }
244     }
245     
246     /**
247      * Stepping commands:
248      * run: resume execution until it finishes or a breakpoint is hit.
249      * stepIn: steps to the next statement, considering callee's statement if any.
250      * stepOut: steps until the current JsContext exits.
251      * stepOver: steps to the next statement within the same JsContext.
252      */

253     public void run() throws RemoteException JavaDoc {
254         resume(DebugConstants.JE_RUN);
255     }
256     public void stepIn() throws RemoteException JavaDoc {
257         resume(DebugConstants.JE_STEP_IN);
258     }
259     public void stepOut() throws RemoteException JavaDoc {
260         resume(DebugConstants.JE_STEP_OUT);
261     }
262     public void stepOver() throws RemoteException JavaDoc {
263         resume(DebugConstants.JE_STEP_OVER);
264     }
265
266     private void resume(int cmd) throws RemoteException JavaDoc {
267         ResultCell cell;
268         try {
269             cell = m_con.prepareOutgoingInvoke(this,DebugConstants.JS_ENGINE_TID,cmd);
270             cell.waitForCompletion();
271         } catch (Exception JavaDoc ex) {
272             throw new RemoteException JavaDoc("Marshalling error",ex);
273         }
274         this.suspended(false);
275     }
276     
277     void suspended(boolean suspended) {
278         fSuspended = suspended;
279     }
280     
281     public boolean isSuspended() {
282         return fSuspended;
283     }
284 }
285
286
Popular Tags