KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.*;
59 import java.util.*;
60 import java.io.*;
61 import java.beans.*;
62
63 import org.apache.bsf.*;
64 import org.apache.bsf.debug.*;
65 import org.apache.bsf.debug.serverImpl.*;
66 import org.apache.bsf.debug.util.DebugLog;
67
68 import java.rmi.RemoteException JavaDoc;
69
70 import org.apache.bsf.debug.jsdi.*;
71 import org.apache.bsf.debug.util.*;
72
73 public class BSFDebugManagerImpl
74     extends Skeleton
75     implements BSFDebugManager, RemoteServiceListener {
76         
77     static BSFDebugManagerImpl self;
78
79     private long m_fnOrScriptIdGenerator = 0x1;
80     private long m_documentIdGenerator = 0x100000000l;
81
82     private Hashtable m_langcells;
83     private Hashtable m_documents;
84
85     private ObjectServer m_server;
86         
87     public BSFDebugManagerImpl() throws RemoteException JavaDoc {
88
89         super(DebugConstants.BSF_DEBUG_MANAGER_TID,
90               DebugConstants.BSF_DEBUG_MANAGER_UID);
91         self = this;
92         m_langcells = new Hashtable();
93         m_documents = new Hashtable();
94
95         Integer JavaDoc port = Integer.getInteger("org.apache.bsf.serverPort", -1);
96         m_server = new ObjectServer(this, port.intValue());
97     }
98
99     /**
100      * Callback from the socket/stub layer.
101      * A stub for a remote debugger is being revoked due
102      * to a lost connection.
103      * Since we support only one debuggers from only one
104      * JVM, there is only one connection and therefore we
105      * can drop all debuggers...
106      * and then drop all breakpoints.
107      *
108      * ATTENTION:
109      * Breakpoints shall not be dropped if we extend this
110      * implementation to multiple debuggers through multiple
111      * socket connections.
112      */

113     public void revokedNotify(RemoteService service) {
114         Enumeration e;
115         LangCell cell;
116         
117         e = m_langcells.elements();
118         while (e.hasMoreElements()) {
119             cell = (LangCell)e.nextElement();
120             DebugLog.stderrPrintln("Disconnecting the debugger",
121                                    DebugLog.BSF_LOG_L1);
122             cell.disconnectDebugger();
123             DebugLog.stderrPrintln("Debugger disconnected.",
124                                    DebugLog.BSF_LOG_L1);
125         }
126         DebugLog.stderrPrintln("Dropping all breakpoints...",
127                                DebugLog.BSF_LOG_L1);
128         removeAllBreakpoints();
129         DebugLog.stderrPrintln("All breakpoints dropped.",
130                                DebugLog.BSF_LOG_L1);
131     }
132     ////////////////////////////////////////////////////////////////////////
133
public void finalize() {
134         terminate();
135     }
136     long generateDocumentId() {
137         long docid = m_documentIdGenerator;
138         m_documentIdGenerator += 0x100000000l;
139         return docid;
140     }
141     ////////////////////////////////////////////////////////////////////////
142

143     /**
144      * Determine the language of a script file by looking at the file
145      * extension.
146      *
147      * @param filename the name of the file
148      *
149      * @return the scripting language the file is in if the file extension
150      * is known to me (must have been registered via
151      * registerScriptingEngine).
152      *
153      * @exception RemoteException if file's extension is unknown.
154      */

155     public String JavaDoc getLangFromFilename(String JavaDoc fileName) throws RemoteException JavaDoc {
156         try {
157             return BSFManager.getLangFromFilename(fileName);
158         } catch (BSFException ex) {
159             return null;
160         }
161     }
162     ////////////////////////////////////////////////////////////////////////
163
////////////////////////////////////////////////////////////////////////
164

165     /**
166      * Determine whether a language is registered.
167      *
168      * @param lang string identifying a language
169      *
170      * @return true iff it is
171      */

172     public boolean isLanguageRegistered(String JavaDoc lang) {
173         return BSFManager.isLanguageRegistered(lang);
174     }
175     /**
176      * Allows a debugger to ask if the engine for a given language
177      * will support either line or offset breakpoints.
178      * Note: this will most likely provoke the loading of the engine.
179      */

180     public boolean supportBreakpointAtOffset(String JavaDoc lang)
181         throws RemoteException JavaDoc {
182         return false;
183     }
184
185     public boolean supportBreakpointAtLine(String JavaDoc lang)
186         throws RemoteException JavaDoc {
187         return true;
188     }
189         
190     ////////////////////////////////////////////////////////////////////////
191
public synchronized void placeBreakpointAtLine(
192                                                    int brkptid,
193                                                    String JavaDoc docname,
194                                                    int lineno)
195         throws RemoteException JavaDoc {
196
197         DocumentCell cell;
198         DebugLog.stdoutPrintln("Placing breakpoint in "+docname+" at "+lineno, DebugLog.BSF_LOG_L1);
199         cell = getDocumentCell(docname);
200         cell.addBreakpointAtLine(brkptid, lineno);
201     }
202     public synchronized void placeBreakpointAtOffset(
203                                                      int brkptid,
204                                                      String JavaDoc docname,
205                                                      int offset)
206         throws RemoteException JavaDoc {
207
208         DocumentCell cell;
209         cell = getDocumentCell(docname);
210         cell.addBreakpointAtOffset(brkptid, offset);
211     }
212
213     public synchronized void removeBreakpoint(String JavaDoc docname, int brkptid)
214         throws RemoteException JavaDoc {
215
216         DocumentCell cell;
217         cell = (DocumentCell) m_documents.get(docname);
218         if (cell != null)
219             cell.removeBreakpoint(brkptid);
220     }
221
222     public synchronized void setEntryExit(String JavaDoc docname, boolean on)
223         throws RemoteException JavaDoc {
224
225         DocumentCell cell;
226         cell = getDocumentCell(docname);
227         if (cell != null)
228             cell.setEntryExit(on);
229     }
230
231     public synchronized void removeAllBreakpoints() {
232         String JavaDoc docname;
233         int bpid;
234         DocumentCell cell;
235         Enumeration e;
236         e = m_documents.elements();
237         while (e.hasMoreElements()) {
238             cell = (DocumentCell) e.nextElement();
239             cell.removeAllBreakpoints();
240         }
241     }
242     ////////////////////////////////////////////////////////////////////////
243

244     /**
245      * Register a debugger for a scripting engine.
246      *
247      * @param lang string identifying language
248      * @exception RemoteException if the language is unknown (i.e., if it
249      * has not been registered) with a reason of
250      * REASON_UNKNOWN_LANGUAGE. If the language is known but
251      * if the interface can't be created for some reason, then
252      * the reason is set to REASON_OTHER_ERROR and the actual
253      * exception is passed on as well.
254      */

255     
256     public synchronized void registerDebugger(String JavaDoc lang, BSFDebugger debugger)
257         throws RemoteException JavaDoc {
258
259         BSFDebugger dbg;
260         DebugLog.stdoutPrintln("Registering debugger for "+lang,
261                                DebugLog.BSF_LOG_L1);
262         try {
263             LangCell cell = (LangCell)m_langcells.get(lang);
264             if (cell == null) {
265                 cell = new LangCell(lang);
266                 m_langcells.put(lang,cell);
267             } else {
268                 cell.disconnectDebugger();
269             }
270             // Now add the debugger...
271
// This will notify the new registered debugger about
272
// the existing engines for that language.
273
cell.setDebugger(debugger);
274             debugger.addListener(this);
275             
276         } catch (Exception JavaDoc ex) {
277             DebugLog.stdoutPrintln("Error:", DebugLog.BSF_LOG_L0);
278             DebugLog.stdoutPrintln(ex.getMessage(), DebugLog.BSF_LOG_L0);
279             ex.printStackTrace(DebugLog.getDebugStream());
280         }
281     }
282     
283     ////////////////////////////////////////////////////////////////////////
284
// Called upon creation of a BSFManager.
285

286     public synchronized DocumentCell loadDocumentNotify(BSFEngine eng,
287                                                         String JavaDoc name) {
288         DocumentCell cell;
289         cell = (DocumentCell) m_documents.get(name);
290         if (cell == null) {
291             cell = new DocumentCell(this, name);
292             m_documents.put(name, cell);
293         }
294         cell.loadNotify(eng);
295         return cell;
296     }
297     ////////////////////////////////////////////////////////////////////////
298
// Get the document cell for the provided name or create it if
299
// it does not exist.
300

301     synchronized DocumentCell getDocumentCell(String JavaDoc name) {
302         DocumentCell cell;
303         cell = (DocumentCell) m_documents.get(name);
304         if (cell == null) {
305             cell = new DocumentCell(this, name);
306             m_documents.put(name, cell);
307         }
308         return cell;
309     }
310     ////////////////////////////////////////////////////////////////////////
311
// Called upon the loading of an engine for the given BSFManager and
312
// language.
313

314     public synchronized void registerEngine(
315                                             BSFManager mger,
316                                             BSFEngine eng,
317                                             String JavaDoc lang) {
318
319         LangCell cell = (LangCell)m_langcells.get(lang);
320         if (cell == null) {
321             cell = new LangCell(lang);
322             m_langcells.put(lang,cell);
323         }
324         // Add the new engine, this will notify
325
// the debugger for that language of the creation.
326
cell.addEngine(eng);
327     }
328     ////////////////////////////////////////////////////////////////////////
329
// Called upon creation of a BSFManager.
330

331     public synchronized void registerManager(BSFManager mger) {
332     }
333     ////////////////////////////////////////////////////////////////////////
334
// Called upon termination of a BSFManager.
335

336     public synchronized void terminateManagerNotify(BSFManager mger) {
337     }
338     
339     public void terminate() {
340         Enumeration e;
341         LangCell cell;
342         e = m_langcells.elements();
343         while (e.hasMoreElements()) {
344             cell = (LangCell) e.nextElement();
345             cell.terminateNotify();
346         }
347         m_langcells = new Hashtable();
348     }
349     public synchronized void unregisterDebugger(String JavaDoc lang)
350         throws RemoteException JavaDoc {
351
352         LangCell cell;
353         cell = (LangCell)m_langcells.get(lang);
354         if (cell==null)
355             return; // silently ignore the error...
356

357         cell.disconnectDebugger();
358     }
359     ////////////////////////////////////////////////////////////////////////
360
// Called upon termination of an engine for the given manager.
361

362     public synchronized void terminateEngineNotify(
363                                                    BSFManager mger,
364                                                    BSFEngine eng,
365                                                    String JavaDoc lang) {
366
367         LangCell cell;
368         cell = (LangCell)m_langcells.get(lang);
369         if (cell==null)
370             return; // silently ignore the error...
371

372         cell.removeEngine(eng);
373
374         // Remove the engine from all document cell for
375
// an unregistered engine
376
Enumeration e;
377         DocumentCell doccell;
378         e = m_documents.elements();
379         while (e.hasMoreElements()) {
380             doccell = (DocumentCell) e.nextElement();
381             doccell.terminateEngineNotify(eng);
382         }
383     }
384 }
385
Popular Tags