KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > ui > BreakpointOutput


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.debugger.jpda.ui;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.util.List JavaDoc;
24 import org.netbeans.api.debugger.*;
25 import org.netbeans.api.debugger.DebuggerManager;
26 import org.netbeans.api.debugger.Session;
27 import org.netbeans.api.debugger.jpda.*;
28 import org.netbeans.api.debugger.jpda.event.JPDABreakpointEvent;
29 import org.netbeans.api.debugger.jpda.event.JPDABreakpointListener;
30 import org.netbeans.modules.debugger.jpda.ui.models.BreakpointsNodeModel;
31 import org.netbeans.spi.debugger.ContextProvider;
32 import java.beans.PropertyChangeEvent JavaDoc;
33 import java.util.*;
34 import java.util.regex.Pattern JavaDoc;
35 import java.util.regex.Matcher JavaDoc;
36 import org.netbeans.spi.viewmodel.NodeModel;
37 import org.openide.util.NbBundle;
38
39 /**
40  * Listener on all breakpoints and prints text specified in the breakpoint when a it hits.
41  *
42  * @see JPDABreakpoint#setPrintText(java.lang.String)
43  * @author Maros Sandor
44  */

45 public class BreakpointOutput extends LazyActionsManagerListener
46 implements DebuggerManagerListener, JPDABreakpointListener,
47 PropertyChangeListener JavaDoc {
48
49     private static final Pattern JavaDoc dollarEscapePattern = Pattern.compile
50         ("\\$");
51     private static final Pattern JavaDoc backslashEscapePattern = Pattern.compile
52         ("\\\\");
53     private static final Pattern JavaDoc threadNamePattern = Pattern.compile
54         ("\\{threadName\\}");
55     private static final Pattern JavaDoc classNamePattern = Pattern.compile
56         ("\\{className\\}");
57     private static final Pattern JavaDoc methodNamePattern = Pattern.compile
58         ("\\{methodName\\}");
59     private static final Pattern JavaDoc lineNumberPattern = Pattern.compile
60         ("\\{lineNumber\\}");
61     private static final Pattern JavaDoc expressionPattern = Pattern.compile
62         ("\\{=(.*?)\\}");
63
64     private IOManager ioManager;
65     private JPDADebugger debugger;
66     private ContextProvider contextProvider;
67     private Object JavaDoc lock = new Object JavaDoc();
68
69     
70     public BreakpointOutput (ContextProvider contextProvider) {
71         this.contextProvider = contextProvider;
72         this.debugger = (JPDADebugger) contextProvider.lookupFirst
73             (null, JPDADebugger.class);
74         debugger.addPropertyChangeListener (
75             debugger.PROP_STATE,
76             this
77         );
78         hookBreakpoints ();
79         DebuggerManager.getDebuggerManager ().addDebuggerListener
80             (DebuggerManager.PROP_BREAKPOINTS, this);
81     }
82     
83     
84     // LazyActionsManagerListener ..............................................
85

86     protected void destroy () {
87         DebuggerManager.getDebuggerManager ().removeDebuggerListener
88             (DebuggerManager.PROP_BREAKPOINTS, this);
89         unhookBreakpoints ();
90         synchronized (lock) {
91             ioManager = null;
92             debugger = null;
93         }
94     }
95
96     public String JavaDoc[] getProperties () {
97         return new String JavaDoc[] { ActionsManagerListener.PROP_ACTION_PERFORMED };
98     }
99     
100     
101     // JPDABreakpointListener ..................................................
102

103     public void breakpointReached (JPDABreakpointEvent event) {
104         synchronized (lock) {
105             if (event.getDebugger () != debugger) return;
106         }
107         if (event.getConditionResult () == event.CONDITION_FALSE) return;
108         JPDABreakpoint breakpoint = (JPDABreakpoint) event.getSource ();
109         getBreakpointsNodeModel ().setCurrentBreakpoint (breakpoint);
110         synchronized (lock) {
111             if (ioManager == null) {
112                 lookupIOManager ();
113                 if (ioManager == null) return;
114             }
115         }
116         String JavaDoc printText = breakpoint.getPrintText ();
117         if (printText == null || printText.length () == 0) return;
118         printText = substitute(printText, event);
119         synchronized (lock) {
120             if (ioManager != null) {
121                 ioManager.println (printText, null);
122             }
123         }
124     }
125
126     
127     // DebuggerManagerListener .................................................
128

129     public void breakpointAdded (Breakpoint breakpoint) {
130         hookBreakpoint (breakpoint);
131     }
132
133     public void breakpointRemoved (Breakpoint breakpoint) {
134         unhookBreakpoint (breakpoint);
135     }
136     
137     public Breakpoint[] initBreakpoints () {return new Breakpoint[0];}
138     public void initWatches () {}
139     public void watchAdded (Watch watch) {}
140     public void watchRemoved (Watch watch) {}
141     public void sessionAdded (Session session) {}
142     public void sessionRemoved (Session session) {}
143     public void engineAdded (DebuggerEngine engine) {}
144     public void engineRemoved (DebuggerEngine engine) {}
145
146     
147     // PropertyChangeListener ..................................................
148

149     public void propertyChange (PropertyChangeEvent JavaDoc evt) {
150         if (JPDABreakpoint.PROP_VALIDITY.equals(evt.getPropertyName())) {
151             if (JPDABreakpoint.VALIDITY.INVALID.equals(evt.getNewValue())) {
152                 JPDABreakpoint bp = (JPDABreakpoint) evt.getSource();
153                 String JavaDoc msg = bp.getValidityMessage();
154                 synchronized (lock) {
155                     if (ioManager == null) {
156                         lookupIOManager ();
157                         if (ioManager == null) return;
158                     }
159                     String JavaDoc printText = (msg != null) ?
160                                        NbBundle.getMessage(BreakpointOutput.class, "MSG_InvalidBreakpointWithReason", bp.toString(), msg) :
161                                        NbBundle.getMessage(BreakpointOutput.class, "MSG_InvalidBreakpoint", bp.toString());
162                     IOManager.Line line = null;
163                     if (bp instanceof LineBreakpoint) {
164                         line = new IOManager.Line (
165                             ((LineBreakpoint) bp).getURL(),
166                             ((LineBreakpoint) bp).getLineNumber(),
167                             debugger
168                         );
169                     }
170                     ioManager.println (printText, null, true);
171                     if (line != null) {
172                         ioManager.println(
173                                 NbBundle.getMessage(BreakpointOutput.class, "Link_InvalidBreakpoint", bp.toString()),
174                                 line, true);
175                     }
176                 }
177             } else if (JPDABreakpoint.VALIDITY.VALID.equals(evt.getNewValue())) {
178                 JPDABreakpoint bp = (JPDABreakpoint) evt.getSource();
179                 synchronized (lock) {
180                     if (ioManager == null) {
181                         lookupIOManager ();
182                         if (ioManager == null) return;
183                     }
184                     String JavaDoc printText = NbBundle.getMessage(BreakpointOutput.class, "MSG_ValidBreakpoint", bp.toString());
185                     IOManager.Line line = null;
186                     if (bp instanceof LineBreakpoint) {
187                         line = new IOManager.Line (
188                             ((LineBreakpoint) bp).getURL(),
189                             ((LineBreakpoint) bp).getLineNumber(),
190                             debugger
191                         );
192                     }
193                     ioManager.println (printText, line, false);
194                 }
195             }
196             return ;
197         }
198         synchronized (lock) {
199             if (debugger == null ||
200                 evt.getPropertyName () != debugger.PROP_STATE ||
201                 debugger.getState () == debugger.STATE_STOPPED) {
202                 
203                 return ;
204             }
205         }
206         getBreakpointsNodeModel ().setCurrentBreakpoint (null);
207             
208     }
209
210     
211     // private methods .........................................................
212

213     /**
214      * threadName name of thread where breakpoint ocurres
215      * className name of class where breakpoint ocurres
216      * methodName name of method where breakpoint ocurres
217      * lineNumber number of line where breakpoint ocurres
218      *
219      * @param printText
220      * @return
221      */

222     private String JavaDoc substitute (String JavaDoc printText, JPDABreakpointEvent event) {
223         
224         // 1) replace {threadName} by the name of current thread
225
JPDAThread t = event.getThread ();
226         if (t != null) {
227             // replace \ by \\
228
String JavaDoc name = backslashEscapePattern.matcher (t.getName ()).
229                 replaceAll ("\\\\\\\\");
230             // replace $ by \$
231
name = dollarEscapePattern.matcher (name).replaceAll ("\\\\\\$");
232             printText = threadNamePattern.matcher (printText).replaceAll (name);
233         }
234         else
235             printText = threadNamePattern.matcher (printText).replaceAll ("?");
236         
237         // 2) replace {className} by the name of current class
238
if (event.getReferenceType () != null) {
239             // replace $ by \$
240
String JavaDoc name = dollarEscapePattern.matcher
241                 (event.getReferenceType ().name ()).replaceAll ("\\\\\\$");
242             printText = classNamePattern.matcher (printText).replaceAll (name);
243         } else
244             printText = classNamePattern.matcher (printText).replaceAll ("?");
245
246         // 3) replace {methodName} by the name of current method
247
Session session = null;
248         Session[] sessions = DebuggerManager.getDebuggerManager().getSessions();
249         for (int i = 0; i < sessions.length; i++) {
250             if (sessions[i].lookupFirst(null, JPDADebugger.class) == debugger) {
251                 session = sessions[i];
252                 break;
253             }
254         }
255         String JavaDoc language = (session != null) ? session.getCurrentLanguage() : null;
256         String JavaDoc methodName = t.getMethodName ();
257         if ("".equals (methodName)) methodName = "?";
258         // replace $ by \$
259
methodName = dollarEscapePattern.matcher (methodName).replaceAll
260             ("\\\\\\$");
261         printText = methodNamePattern.matcher (printText).replaceAll
262             (methodName);
263         
264         // 4) replace {lineNumber} by the current line number
265
int lineNumber = t.getLineNumber (language);
266         if (lineNumber < 0)
267             printText = lineNumberPattern.matcher (printText).replaceAll
268                 ("?");
269         else
270             printText = lineNumberPattern.matcher (printText).replaceAll
271                 (String.valueOf (lineNumber));
272              
273         // 5) resolve all expressions {=expression}
274
for (;;) {
275             Matcher JavaDoc m = expressionPattern.matcher (printText);
276             if (!m.find ()) break;
277             String JavaDoc expression = m.group (1);
278             String JavaDoc value = "";
279             try {
280                 JPDADebugger theDebugger;
281                 synchronized (lock) {
282                     if (debugger == null) {
283                         return value; // The debugger is gone
284
}
285                     theDebugger = debugger;
286                 }
287                 value = theDebugger.evaluate (expression).getValue ();
288                 value = backslashEscapePattern.matcher (value).
289                     replaceAll ("\\\\\\\\");
290                 value = dollarEscapePattern.matcher (value).
291                     replaceAll ("\\\\\\$");
292             } catch (InvalidExpressionException e) {
293                 // expression is invalid or cannot be evaluated
294
String JavaDoc msg = e.getCause () != null ?
295                     e.getCause ().getMessage () : e.getMessage ();
296                 synchronized (lock) {
297                     if (ioManager != null) {
298                         ioManager.println (
299                             "Cannot evaluate expression '" + expression + "' : " + msg,
300                             null
301                         );
302                     }
303                 }
304             }
305             printText = m.replaceFirst (value);
306         }
307         Throwable JavaDoc thr = event.getConditionException();
308         if (thr != null) {
309             printText = printText + "\n***\n"+ thr.getLocalizedMessage()+"\n***\n";
310         }
311         return printText;
312     }
313
314     private void lookupIOManager () {
315         List JavaDoc lamls = contextProvider.lookup
316             (null, LazyActionsManagerListener.class);
317         for (Iterator i = lamls.iterator (); i.hasNext ();) {
318             Object JavaDoc o = i.next();
319             if (o instanceof DebuggerOutput) {
320                 ioManager = ((DebuggerOutput) o).getIOManager ();
321                 break;
322             }
323         }
324     }
325     
326     private void hookBreakpoints () {
327         Breakpoint [] bpts = DebuggerManager.getDebuggerManager ().
328             getBreakpoints ();
329         for (int i = 0; i < bpts.length; i++) {
330             Breakpoint bpt = bpts [i];
331             hookBreakpoint (bpt);
332         }
333     }
334
335     private void unhookBreakpoints () {
336         Breakpoint [] bpts = DebuggerManager.getDebuggerManager ().
337             getBreakpoints ();
338         for (int i = 0; i < bpts.length; i++) {
339             Breakpoint bpt = bpts [i];
340             unhookBreakpoint (bpt);
341         }
342     }
343
344     private void hookBreakpoint (Breakpoint breakpoint) {
345         if (breakpoint instanceof JPDABreakpoint) {
346             JPDABreakpoint jpdaBreakpoint = (JPDABreakpoint) breakpoint;
347             jpdaBreakpoint.addJPDABreakpointListener (this);
348             jpdaBreakpoint.addPropertyChangeListener(JPDABreakpoint.PROP_VALIDITY, this);
349         }
350     }
351
352     private void unhookBreakpoint (Breakpoint breakpoint) {
353         if (breakpoint instanceof JPDABreakpoint) {
354             JPDABreakpoint jpdaBreakpoint = (JPDABreakpoint) breakpoint;
355             jpdaBreakpoint.removeJPDABreakpointListener (this);
356             jpdaBreakpoint.removePropertyChangeListener(JPDABreakpoint.PROP_VALIDITY, this);
357         }
358     }
359     
360     private BreakpointsNodeModel breakpointsNodeModel;
361     private BreakpointsNodeModel getBreakpointsNodeModel () {
362         if (breakpointsNodeModel == null) {
363             List JavaDoc l = DebuggerManager.getDebuggerManager ().lookup
364                 ("BreakpointsView", NodeModel.class);
365             Iterator it = l.iterator ();
366             while (it.hasNext ()) {
367                 NodeModel nm = (NodeModel) it.next ();
368                 if (nm instanceof BreakpointsNodeModel) {
369                     breakpointsNodeModel = (BreakpointsNodeModel) nm;
370                     break;
371                 }
372             }
373         }
374         return breakpointsNodeModel;
375     }
376 }
377
Popular Tags