KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bsf > util > DocumentCell


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.util;
57
58 import java.io.StringReader JavaDoc;
59 import java.io.InputStream JavaDoc;
60 import java.io.IOException JavaDoc;
61 import java.lang.reflect.InvocationTargetException JavaDoc;
62 import java.util.Vector JavaDoc;
63 import java.util.Enumeration JavaDoc;
64
65 import org.apache.bsf.*;
66 import org.apache.bsf.util.BSFEngineImpl;
67 import org.apache.bsf.util.BSFFunctions;
68 import org.apache.bsf.debug.util.DebugLog;
69
70 import java.util.Hashtable JavaDoc;
71 import org.apache.bsf.debug.jsdi.*;
72
73 import java.io.Reader JavaDoc;
74
75 /**
76  * A DocumentCell allows the debug manager to keep track
77  * of known documents for which it has breakpoints.
78  * When a debugger sets a breakpoint, it specifies a document.
79  * This is when a DocumentCell is created for that document
80  * (identified through its URI).
81  * The debug manager will keep the document cells at least
82  * as long as there is breakpoints known and as long as there
83  * are engines known to currently execute the document.
84  *
85  * @author Olivier Gruber
86  */

87
88 public class DocumentCell {
89
90     BSFDebugManagerImpl m_mger;
91
92     String JavaDoc m_docName;
93     Vector JavaDoc m_fnOrScripts;
94     Vector JavaDoc m_breakpoints;
95     Vector JavaDoc m_engines;
96
97     Hashtable JavaDoc m_functionMap;
98     private boolean m_entryexit;
99
100     //-----------------------------------------
101
public DocumentCell(BSFDebugManagerImpl mger, String JavaDoc name) {
102         m_mger = mger;
103         m_docName = name;
104         m_breakpoints = new Vector JavaDoc();
105         m_functionMap = new Hashtable JavaDoc();
106         m_fnOrScripts = new Vector JavaDoc();
107         m_engines = new Vector JavaDoc();
108         m_entryexit = false;
109     }
110     //-----------------------------------------
111
public String JavaDoc getName() {
112         return m_docName;
113     }
114     //-----------------------------------------
115
// Register an engine that has loaded this
116
// document. Notice there is no unload notify.
117
// This is a problem with the current BSF approach
118
// where the use of a document is not bracketed.
119
// Only when an engine terminates that we will
120
// known it does not execute a document
121
// anymore.
122
public void loadNotify(BSFEngine eng) {
123         m_engines.addElement(eng);
124         propagateBreakpoints(eng);
125         propagateEntryExit(eng);
126     }
127     //-----------------------------------------
128
// Upon termination of an engine, remove it
129
// from the list of engines known as having
130
// loaded this document.
131
//-----------------------------------------
132
public void terminateEngineNotify(BSFEngine eng) {
133         m_engines.removeElement(eng);
134     }
135     //-----------------------------------------
136
// Propagate the known breakpoints for this
137
// document in the provided engine.
138
//-----------------------------------------
139
private void propagateBreakpoints(BSFEngine eng) {
140         Enumeration JavaDoc e;
141         BreakPoint bp;
142         int id;
143         int lineno, offset;
144
145         e = m_breakpoints.elements();
146         while (e.hasMoreElements()) {
147             bp = (BreakPoint) e.nextElement();
148             id = bp.getId();
149             try {
150                 if (bp.isLineDefined()) {
151                     lineno = bp.getLineNo();
152                     eng.placeBreakpointAtLine(id, m_docName, lineno);
153                 } else {
154                     offset = bp.getOffset();
155                     eng.placeBreakpointAtOffset(id, m_docName, offset);
156                 }
157             } catch (BSFException ex) {
158             }
159         }
160     }
161     //-----------------------------------------
162
// Place the provided breakpoint in all the
163
// engines known to have loaded this document.
164
//-----------------------------------------
165
private void propagateBreakpoint(BreakPoint bp) {
166         Enumeration JavaDoc e;
167         BSFEngine eng;
168         int id;
169         int lineno, offset;
170
171         e = m_engines.elements();
172         while (e.hasMoreElements()) {
173             eng = (BSFEngine) e.nextElement();
174             id = bp.getId();
175             try {
176                 if (bp.isLineDefined()) {
177                     lineno = bp.getLineNo();
178                     eng.placeBreakpointAtLine(id, m_docName, lineno);
179                 } else {
180                     offset = bp.getOffset();
181                     eng.placeBreakpointAtOffset(id, m_docName, offset);
182                 }
183             } catch (BSFException ex) {
184             }
185         }
186     }
187     //-----------------------------------------
188
// Propagate the removal to all the
189
// engines known to have loaded this document.
190
//-----------------------------------------
191
private void propagateBreakPointRemove(BreakPoint bp) {
192         Enumeration JavaDoc e;
193         BSFEngine eng;
194         int id;
195         int lineno, offset;
196
197         e = m_engines.elements();
198         while (e.hasMoreElements()) {
199             eng = (BSFEngine) e.nextElement();
200             id = bp.getId();
201             try {
202                 eng.removeBreakpoint(m_docName,id);
203             } catch (BSFException ex) {
204             }
205         }
206     }
207     //-----------------------------------------
208
public void addBreakpointAtLine(int brkptId, int lineno) {
209         BreakPoint bp = new BreakPoint(this, brkptId);
210         bp.setLineNo(lineno);
211         m_breakpoints.addElement(bp);
212         propagateBreakpoint(bp);
213     }
214     //-----------------------------------------
215
public void addBreakpointAtOffset(int brkptId, int offset) {
216         BreakPoint bp = new BreakPoint(this, brkptId);
217         bp.setOffset(offset);
218         m_breakpoints.addElement(bp);
219         propagateBreakpoint(bp);
220     }
221     //-----------------------------------------
222
public void removeBreakpoint(int brkptId) {
223         Enumeration JavaDoc e;
224         BreakPoint bp;
225
226         e = m_breakpoints.elements();
227         while (e.hasMoreElements()) {
228             bp = (BreakPoint) e.nextElement();
229             if (brkptId == bp.getId()) {
230                 m_breakpoints.removeElement(bp);
231                 propagateBreakPointRemove(bp);
232                 break;
233             }
234         }
235     }
236
237     public void setEntryExit(boolean on_value) {
238         Enumeration JavaDoc e;
239
240         m_entryexit = on_value;
241
242         e = m_engines.elements();
243         while (e.hasMoreElements()) {
244             propagateEntryExit((BSFEngine) e.nextElement());
245         }
246     }
247
248     private void propagateEntryExit(BSFEngine eng) {
249         try {
250             eng.setEntryExit(m_docName, m_entryexit);
251         }
252         catch (BSFException ex) {
253         }
254     }
255     
256     public boolean getEntryExit() {
257         return m_entryexit;
258     }
259
260     //-----------------------------------------
261
public void removeAllBreakpoints() {
262
263         Enumeration JavaDoc e;
264         BreakPoint bp;
265
266         DebugLog.stdoutPrintln("Drop breakpoints for "+m_docName,
267                                DebugLog.BSF_LOG_L3);
268         e = m_breakpoints.elements();
269         while (e.hasMoreElements()) {
270             bp = (BreakPoint) e.nextElement();
271             DebugLog.stdoutPrintln(" Breakpoints "+bp,
272                                    DebugLog.BSF_LOG_L3);
273             propagateBreakPointRemove(bp);
274         }
275         m_breakpoints = new Vector JavaDoc();
276     }
277     
278     //-----------------------------------------
279
public BreakPoint findBreakpointAtLine(int lineno) {
280         Enumeration JavaDoc e;
281         BreakPoint bp;
282
283         e = m_breakpoints.elements();
284         while (e.hasMoreElements()) {
285             bp = (BreakPoint) e.nextElement();
286             if (lineno == bp.getLineNo())
287                 return bp;
288         }
289         return null;
290     }
291     //-----------------------------------------
292
public BreakPoint findBreakpointAtOffset(int offset) {
293         Enumeration JavaDoc e;
294         BreakPoint bp;
295
296         e = m_breakpoints.elements();
297         while (e.hasMoreElements()) {
298             bp = (BreakPoint) e.nextElement();
299             if (offset == bp.getOffset())
300                 return bp;
301         }
302         return null;
303     }
304 }
305
Popular Tags