Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2015 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2015 Sun Microsystems, Inc. |
41 |
*/ |
42 |
|
43 |
package org.netbeans.api.debugger; |
44 |
|
45 |
import java.beans.PropertyChangeListener; |
46 |
import java.lang.ref.Reference; |
47 |
import java.lang.ref.WeakReference; |
48 |
import java.util.LinkedList; |
49 |
import java.util.List; |
50 |
import org.netbeans.spi.debugger.BreakpointsActivationProvider; |
51 |
|
52 |
/** |
53 |
* This class handles an engine-related breakpoints activation/deactivation, |
54 |
* which is independent on their enabled/disabled state. |
55 |
* |
56 |
* @author Martin Entlicher |
57 |
* @since 1.51 |
58 |
*/ |
59 |
public final class ActiveBreakpoints { |
60 |
|
61 |
/** |
62 |
* Property change fired when breakpoints activation/deactivation changes. |
63 |
*/ |
64 |
public static final String PROP_BREAKPOINTS_ACTIVE = "breakpointsActive"; // NOI18N |
65 |
/** |
66 |
* Property change fired when a set of engine-related breakpoints is changed. |
67 |
* Make public when {@link #getEngineBreakpoints()} is public and assure it's fired. |
68 |
* |
69 |
private static final String PROP_BREAKPOINTS_CHANGED = "breakpointsChanged"; // NOI18N |
70 |
*/ |
71 |
|
72 |
// These two lists are used as a trivial weak map from DebuggerEngine to EngineBreakpoints, |
73 |
// where both keys and values are held weakly. |
74 |
private static final List<Reference<DebuggerEngine>> dEngines = new LinkedList<>(); |
75 |
private static final List<Reference<ActiveBreakpoints>> aBreakpoints = new LinkedList<>(); |
76 |
|
77 |
private static final ActiveBreakpoints UNSUPPORTED = new ActiveBreakpoints(null); |
78 |
|
79 |
private final BreakpointsActivationProvider bap; |
80 |
|
81 |
/** |
82 |
* Get an ActiveBreakpoints instance for a debugger engine. |
83 |
* @param debuggerEngine the debugger engine. |
84 |
* @return The ActiveBreakpoints instance, that delegates to an |
85 |
* {@link BreakpointsActivationProvider} provided in it's lookup. |
86 |
*/ |
87 |
public static ActiveBreakpoints get(DebuggerEngine debuggerEngine) { |
88 |
BreakpointsActivationProvider bap = debuggerEngine.lookupFirst(null, BreakpointsActivationProvider.class); |
89 |
if (bap == null) { |
90 |
return UNSUPPORTED; |
91 |
} |
92 |
synchronized (dEngines) { |
93 |
for (int i = 0; i < dEngines.size(); i++) { |
94 |
DebuggerEngine de = dEngines.get(i).get(); |
95 |
if (de == null) { |
96 |
dEngines.remove(i); |
97 |
aBreakpoints.remove(i); |
98 |
i--; |
99 |
continue; |
100 |
} |
101 |
if (de == debuggerEngine) { |
102 |
ActiveBreakpoints ab = aBreakpoints.get(i).get(); |
103 |
if (ab != null) { |
104 |
return ab; |
105 |
} |
106 |
} |
107 |
} |
108 |
// Not cached, create a new one: |
109 |
ActiveBreakpoints ab = new ActiveBreakpoints(bap); |
110 |
dEngines.add(new WeakReference<>(debuggerEngine)); |
111 |
aBreakpoints.add(new WeakReference<>(ab)); |
112 |
return ab; |
113 |
} |
114 |
} |
115 |
|
116 |
private ActiveBreakpoints(BreakpointsActivationProvider bap) { |
117 |
this.bap = bap; |
118 |
} |
119 |
|
120 |
/** |
121 |
* Test if breakpoint deactivation is supported. |
122 |
* When <code>false</code> is returned, {@link #setBreakpointsActive(boolean)} |
123 |
* throws UnsupportedOperationException when called with false argument. |
124 |
* @return <code>true</code> when engine-related breakpoints can be deactivated, |
125 |
* <code>false</code> otherwise. |
126 |
*/ |
127 |
public boolean canDeactivateBreakpoints() { |
128 |
return bap != null; |
129 |
} |
130 |
|
131 |
/** |
132 |
* Test if the engine's breakpoints are currently active. |
133 |
* @return <code>true</code> when breakpoints are active, |
134 |
* <code>false</code> otherwise. |
135 |
*/ |
136 |
public boolean areBreakpointsActive() { |
137 |
return bap == null || bap.areBreakpointsActive(); |
138 |
} |
139 |
|
140 |
/** |
141 |
* Activate or deactivate breakpoints handled by this debugger engine. |
142 |
* The breakpoints activation/deactivation is independent on breakpoints enabled/disabled state. |
143 |
* |
144 |
* @param active <code>true</code> to activate breakpoints, |
145 |
* <code>false</code> to deactivate them. |
146 |
* @throws UnsupportedOperationException when there is an attempt to deactivate |
147 |
* breakpoints even though {@link #canDeactivateBreakpoints()} return false |
148 |
*/ |
149 |
public void setBreakpointsActive(boolean active) throws UnsupportedOperationException { |
150 |
if (bap == null) { |
151 |
throw new UnsupportedOperationException("No implementation of BreakpointsActivationProvider provided by the engine."); |
152 |
} |
153 |
bap.setBreakpointsActive(active); |
154 |
} |
155 |
|
156 |
/** |
157 |
* Get the set of breakpoints that are managed by this engine. |
158 |
* These are the breakpoints that are subject of activation/deactivation |
159 |
* if it's supported. |
160 |
* @return A set of breakpoints managed by this engine. |
161 |
* |
162 |
// A possible extension |
163 |
private Set<Breakpoint> getEngineBreakpoints() { |
164 |
// Possible to iterate through breakpoints and check GroupProperties.getEngines() |
165 |
// Update the set on breakpoint add/remove |
166 |
return null; |
167 |
} |
168 |
*/ |
169 |
|
170 |
/** |
171 |
* Add a property change listener to be notified about properties |
172 |
* defined as PROP_* constants. |
173 |
* @param l a property change listener |
174 |
*/ |
175 |
public final void addPropertyChangeListener(PropertyChangeListener l) { |
176 |
if (bap != null) { |
177 |
bap.addPropertyChangeListener(l); |
178 |
} |
179 |
} |
180 |
|
181 |
/** |
182 |
* Remove a property change listener. |
183 |
* @param l a property change listener |
184 |
*/ |
185 |
public final void removePropertyChangeListener(PropertyChangeListener l) { |
186 |
if (bap != null) { |
187 |
bap.removePropertyChangeListener(l); |
188 |
} |
189 |
} |
190 |
} |