001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.debugger.gdb.breakpoints;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.net.MalformedURLException;
046: import java.net.URL;
047: import java.util.ArrayList;
048: import java.util.Iterator;
049: import org.netbeans.api.debugger.Breakpoint;
050: import org.netbeans.api.debugger.DebuggerEngine;
051:
052: import org.netbeans.api.debugger.DebuggerManager;
053: import org.netbeans.api.debugger.LazyDebuggerManagerListener;
054: import org.netbeans.api.debugger.Properties;
055: import org.netbeans.api.debugger.Properties.Reader;
056: import org.netbeans.api.debugger.Session;
057: import org.netbeans.api.debugger.Watch;
058: import org.openide.ErrorManager;
059: import org.openide.filesystems.FileObject;
060: import org.openide.filesystems.URLMapper;
061:
062: /**
063: * Listens on DebuggerManager and:
064: * - loads all breakpoints. Watches are loaded by debuggercore's PersistentManager.
065: * - listens on all changes of breakpoints and saves new values
066: *
067: * @author Jan Jancura
068: */
069: public class PersistenceManager implements LazyDebuggerManagerListener {
070:
071: public synchronized Breakpoint[] initBreakpoints() {
072: Properties p = Properties.getDefault()
073: .getProperties("debugger").getProperties(
074: DebuggerManager.PROP_BREAKPOINTS); // NOI18N
075: Breakpoint[] breakpoints = (Breakpoint[]) p.getArray("gdb",
076: new Breakpoint[0]); // NOI18N
077:
078: for (int i = 0; i < breakpoints.length; i++) {
079: if (breakpoints[i] instanceof LineBreakpoint) {
080: LineBreakpoint lb = (LineBreakpoint) breakpoints[i];
081: try {
082: FileObject fo = URLMapper.findFileObject(new URL(lb
083: .getURL()));
084: if (fo == null) {
085: // The file is gone - we should remove the breakpoint as well.
086: Breakpoint[] breakpoints2 = new Breakpoint[breakpoints.length - 1];
087: if (i > 0) {
088: System.arraycopy(breakpoints, 0,
089: breakpoints2, 0, i);
090: }
091: if (i < breakpoints2.length) {
092: System.arraycopy(breakpoints, i + 1,
093: breakpoints2, i,
094: breakpoints2.length - i);
095: }
096: breakpoints = breakpoints2;
097: i--;
098: continue;
099: }
100: } catch (MalformedURLException ex) {
101: ErrorManager.getDefault().notify(ex);
102: }
103: }
104: breakpoints[i].addPropertyChangeListener(this );
105: }
106: return breakpoints;
107: }
108:
109: public synchronized Breakpoint[] unloadBreakpoints() {
110: Breakpoint[] bpts = DebuggerManager.getDebuggerManager()
111: .getBreakpoints();
112: ArrayList<Breakpoint> unloaded = new ArrayList<Breakpoint>();
113: for (Breakpoint b : bpts) {
114: if (b instanceof GdbBreakpoint) {
115: unloaded.add(b);
116: b.removePropertyChangeListener(this );
117: }
118: }
119: return unloaded.toArray(new Breakpoint[0]);
120: }
121:
122: public void initWatches() {
123: }
124:
125: public String[] getProperties() {
126: return new String[] { DebuggerManager.PROP_BREAKPOINTS_INIT,
127: DebuggerManager.PROP_BREAKPOINTS, };
128: }
129:
130: public void breakpointAdded(Breakpoint breakpoint) {
131: if (breakpoint instanceof GdbBreakpoint) {
132: storeBreakpoints();
133: breakpoint.addPropertyChangeListener(this );
134: }
135: }
136:
137: public void breakpointRemoved(Breakpoint breakpoint) {
138: if (breakpoint instanceof GdbBreakpoint) {
139: storeBreakpoints();
140: breakpoint.removePropertyChangeListener(this );
141: }
142: }
143:
144: public void watchAdded(Watch watch) {
145: }
146:
147: public void watchRemoved(Watch watch) {
148: }
149:
150: public void propertyChange(PropertyChangeEvent evt) {
151: if (evt.getSource() instanceof GdbBreakpoint) {
152: if (!Breakpoint.PROP_VALIDITY.equals(evt.getPropertyName())) {
153: storeBreakpoints();
154: }
155: }
156: }
157:
158: static BreakpointsReader findBreakpointsReader() {
159: BreakpointsReader breakpointsReader = null;
160: Iterator i = DebuggerManager.getDebuggerManager().lookup(null,
161: Reader.class).iterator();
162: while (i.hasNext()) {
163: Reader r = (Reader) i.next();
164: String[] ns = r.getSupportedClassNames();
165: if (ns.length == 1
166: && GdbBreakpoint.class.getName().equals(ns[0])) {
167: breakpointsReader = (BreakpointsReader) r;
168: break;
169: }
170: }
171: return breakpointsReader;
172: }
173:
174: static void storeBreakpoints() {
175: Properties.getDefault().getProperties("debugger")
176: . // NOI18N
177: getProperties(DebuggerManager.PROP_BREAKPOINTS)
178: .setArray("gdb", getBreakpoints()); //NOI18N
179: }
180:
181: public void sessionAdded(Session session) {
182: }
183:
184: public void sessionRemoved(Session session) {
185: }
186:
187: public void engineAdded(DebuggerEngine engine) {
188: }
189:
190: public void engineRemoved(DebuggerEngine engine) {
191: }
192:
193: private static Breakpoint[] getBreakpoints() {
194: Breakpoint[] bs = DebuggerManager.getDebuggerManager()
195: .getBreakpoints();
196: int i, k = bs.length;
197: ArrayList bb = new ArrayList();
198: for (i = 0; i < k; i++) {
199: // Don't store hidden breakpoints
200: if (bs[i] instanceof GdbBreakpoint
201: && !((GdbBreakpoint) bs[i]).isHidden()) {
202: bb.add(bs[i]);
203: }
204: }
205: bs = new Breakpoint[bb.size()];
206: return (Breakpoint[]) bb.toArray(bs);
207: }
208: }
|