KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > breakpoints > PersistenceManager


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.breakpoints;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import org.netbeans.api.debugger.Breakpoint;
29 import org.netbeans.api.debugger.DebuggerEngine;
30 import org.netbeans.api.debugger.DebuggerManager;
31 import org.netbeans.api.debugger.DebuggerManagerListener;
32 import org.netbeans.api.debugger.LazyDebuggerManagerListener;
33 import org.netbeans.api.debugger.Properties;
34 import org.netbeans.api.debugger.Properties.Reader;
35 import org.netbeans.api.debugger.Session;
36 import org.netbeans.api.debugger.Watch;
37 import org.netbeans.api.debugger.jpda.JPDABreakpoint;
38 import org.netbeans.api.debugger.jpda.LineBreakpoint;
39
40 import org.openide.ErrorManager;
41 import org.openide.filesystems.FileObject;
42 import org.openide.filesystems.URLMapper;
43
44 /**
45  * Listens on DebuggerManager and:
46  * - loads all breakpoints. Watches are loaded by debuggercore's PersistentManager.
47  * - listens on all changes of breakpoints and saves new values
48  *
49  * @author Jan Jancura
50  */

51 public class PersistenceManager implements LazyDebuggerManagerListener {
52     
53     public synchronized Breakpoint[] initBreakpoints () {
54         Properties p = Properties.getDefault ().getProperties ("debugger").
55             getProperties (DebuggerManager.PROP_BREAKPOINTS);
56         Breakpoint[] breakpoints = (Breakpoint[]) p.getArray (
57             "jpda",
58             new Breakpoint [0]
59         );
60         for (int i = 0; i < breakpoints.length; i++) {
61             if (breakpoints[i] instanceof LineBreakpoint) {
62                 LineBreakpoint lb = (LineBreakpoint) breakpoints[i];
63                 try {
64                     FileObject fo = URLMapper.findFileObject(new URL JavaDoc(lb.getURL()));
65                     if (fo == null) {
66                         // The file is gone - we should remove the breakpoint as well.
67
Breakpoint[] breakpoints2 = new Breakpoint[breakpoints.length - 1];
68                         if (i > 0) {
69                             System.arraycopy(breakpoints, 0, breakpoints2, 0, i);
70                         }
71                         if (i < breakpoints2.length) {
72                             System.arraycopy(breakpoints, i + 1, breakpoints2, i, breakpoints2.length - i);
73                         }
74                         breakpoints = breakpoints2;
75                         i--;
76                         continue;
77                     }
78                 } catch (MalformedURLException JavaDoc ex) {
79                     ErrorManager.getDefault().notify(ex);
80                 }
81             }
82             breakpoints[i].addPropertyChangeListener(this);
83         }
84         return breakpoints;
85     }
86     
87     public void initWatches () {
88     }
89     
90     public String JavaDoc[] getProperties () {
91         return new String JavaDoc [] {
92             DebuggerManager.PROP_BREAKPOINTS_INIT,
93             DebuggerManager.PROP_BREAKPOINTS,
94         };
95     }
96     
97     public void breakpointAdded (Breakpoint breakpoint) {
98         if (breakpoint instanceof JPDABreakpoint &&
99                 !((JPDABreakpoint) breakpoint).isHidden ()) {
100             
101             storeBreakpoints();
102             breakpoint.addPropertyChangeListener(this);
103         }
104     }
105
106     public void breakpointRemoved (Breakpoint breakpoint) {
107         if (breakpoint instanceof JPDABreakpoint &&
108                 !((JPDABreakpoint) breakpoint).isHidden ()) {
109             
110             storeBreakpoints();
111             breakpoint.removePropertyChangeListener(this);
112         }
113     }
114     public void watchAdded (Watch watch) {
115     }
116     
117     public void watchRemoved (Watch watch) {
118     }
119     
120     public void propertyChange (PropertyChangeEvent JavaDoc evt) {
121         if (evt.getSource() instanceof JPDABreakpoint) {
122             if (LineBreakpoint.PROP_LINE_NUMBER.equals(evt.getPropertyName())) {
123                 BreakpointsReader r = findBreakpointsReader();
124                 if (r != null) {
125                     // Reset the class name, which might change
126
r.storeCachedClassName((JPDABreakpoint) evt.getSource(), null);
127                 }
128             }
129             storeBreakpoints();
130         }
131     }
132     
133     static BreakpointsReader findBreakpointsReader() {
134         BreakpointsReader breakpointsReader = null;
135         Iterator JavaDoc i = DebuggerManager.getDebuggerManager().lookup (null, Reader.class).iterator ();
136         while (i.hasNext ()) {
137             Reader r = (Reader) i.next ();
138             String JavaDoc[] ns = r.getSupportedClassNames ();
139             if (ns.length == 1 && JPDABreakpoint.class.getName().equals(ns[0])) {
140                 breakpointsReader = (BreakpointsReader) r;
141                 break;
142             }
143         }
144         return breakpointsReader;
145     }
146
147     static void storeBreakpoints() {
148         Properties.getDefault ().getProperties ("debugger").
149             getProperties (DebuggerManager.PROP_BREAKPOINTS).setArray (
150                 "jpda",
151                 getBreakpoints ()
152             );
153     }
154     
155     public void sessionAdded (Session session) {}
156     public void sessionRemoved (Session session) {}
157     public void engineAdded (DebuggerEngine engine) {}
158     public void engineRemoved (DebuggerEngine engine) {}
159     
160     
161     private static Breakpoint[] getBreakpoints () {
162         Breakpoint[] bs = DebuggerManager.getDebuggerManager ().
163             getBreakpoints ();
164         int i, k = bs.length;
165         ArrayList JavaDoc<Breakpoint> bb = new ArrayList JavaDoc<Breakpoint>();
166         for (i = 0; i < k; i++)
167             // Don't store hidden breakpoints
168
if ( bs[i] instanceof JPDABreakpoint &&
169                  !((JPDABreakpoint) bs [i]).isHidden ()
170             )
171                 bb.add (bs [i]);
172         bs = new Breakpoint [bb.size ()];
173         return bb.toArray (bs);
174     }
175
176 }
177
Popular Tags