Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2014 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 2014 Sun Microsystems, Inc. |
41 |
*/ |
42 |
|
43 |
package org.netbeans.api.debugger; |
44 |
|
45 |
import java.beans.Customizer; |
46 |
import java.beans.PropertyChangeEvent; |
47 |
import java.beans.PropertyChangeListener; |
48 |
import java.util.ArrayList; |
49 |
import java.util.Collections; |
50 |
import java.util.HashMap; |
51 |
import java.util.List; |
52 |
import java.util.Map; |
53 |
import java.util.Set; |
54 |
import java.util.concurrent.CopyOnWriteArraySet; |
55 |
import org.netbeans.spi.debugger.DebuggerServiceRegistration; |
56 |
|
57 |
/** |
58 |
* Bridge between sessions. |
59 |
* Use this for mixed languages debugging. Any debug session can suggest to change |
60 |
* the debugging session for a debug action. A registered implementation of |
61 |
* {@link SessionChanger} can decide to change the session in order to perform |
62 |
* the given action. |
63 |
* <p/> |
64 |
* In the current implementation, step into action of JPDA debugger is suggested |
65 |
* for a session change only. The support can be extended according to the future |
66 |
* requirements. |
67 |
* |
68 |
* @author Martin Entlicher |
69 |
* @since 1.48 |
70 |
*/ |
71 |
public final class SessionBridge { |
72 |
|
73 |
private static SessionBridge instance; |
74 |
|
75 |
private final Map<String, Set<SessionChanger>> sessionChangers = new HashMap<String, Set<SessionChanger>>(); |
76 |
private final List<SessionChanger> lookupSessionChangers; |
77 |
|
78 |
private SessionBridge() { |
79 |
Lookup lookup = new Lookup.MetaInf(null); |
80 |
final List<? extends SessionChanger> scList = lookup.lookup(null, SessionChanger.class); |
81 |
((Customizer) scList).addPropertyChangeListener(new PropertyChangeListener() { |
82 |
@Override |
83 |
public void propertyChange(PropertyChangeEvent evt) { |
84 |
for (SessionChanger sc : lookupSessionChangers) { |
85 |
removeSessionChangerListener(sc); |
86 |
} |
87 |
lookupSessionChangers.clear(); |
88 |
for (SessionChanger sc : scList) { |
89 |
lookupSessionChangers.add(sc); |
90 |
addSessionChangerListener(sc); |
91 |
} |
92 |
} |
93 |
}); |
94 |
lookupSessionChangers = new ArrayList<SessionChanger>(); |
95 |
for (SessionChanger sc : scList) { |
96 |
lookupSessionChangers.add(sc); |
97 |
addSessionChangerListener(sc); |
98 |
} |
99 |
} |
100 |
|
101 |
/** |
102 |
* Get the default instance of SessionBridge. |
103 |
* @return the default instance |
104 |
*/ |
105 |
public static synchronized SessionBridge getDefault() { |
106 |
if (instance == null) { |
107 |
instance = new SessionBridge(); |
108 |
} |
109 |
return instance; |
110 |
} |
111 |
|
112 |
/** |
113 |
* Suggest a session change to perform a particular action. |
114 |
* @param origin The original session suggesting the session change |
115 |
* @param action An action - a constant from ActionsManager.Action_* |
116 |
* @param properties Properties describing the current state of the current session before the given action. |
117 |
* The actual properties are specific for the particular session type. |
118 |
* @return <code>true</code> when the session is changed and another session |
119 |
* decided to perform the given action.<br/> |
120 |
* <code>false</code> when no other session would like to perform this action. |
121 |
*/ |
122 |
public boolean suggestChange(Session origin, String action, Map<Object, Object> properties) { |
123 |
Set<SessionChanger> scs; |
124 |
synchronized (sessionChangers) { |
125 |
scs = sessionChangers.get(action); |
126 |
} |
127 |
if (scs != null) { |
128 |
for (SessionChanger sc : scs) { |
129 |
Session newSession = sc.changeSuggested(origin, action, properties); |
130 |
if (newSession != null) { |
131 |
if (DebuggerManager.getDebuggerManager().getCurrentSession() == origin) { |
132 |
DebuggerManager.getDebuggerManager().setCurrentSession(newSession); |
133 |
} |
134 |
return true; |
135 |
} |
136 |
} |
137 |
} |
138 |
return false; |
139 |
} |
140 |
|
141 |
/** |
142 |
* Test whether there is some session changer registered for the given action. |
143 |
* @param action An action - a constant from ActionsManager.Action_* |
144 |
* @return <code>true</code> when there is some session changer registered |
145 |
* for this action, <code>false</code> otherwise. |
146 |
*/ |
147 |
public boolean isChangerFor(String action) { |
148 |
synchronized (sessionChangers) { |
149 |
Set<SessionChanger> scs = sessionChangers.get(action); |
150 |
return scs != null; |
151 |
} |
152 |
} |
153 |
|
154 |
private void addSessionChangerListener(SessionChanger sc) { |
155 |
Set<String> actions = sc.getActions(); |
156 |
synchronized (sessionChangers) { |
157 |
for (String action : actions) { |
158 |
Set<SessionChanger> scs = sessionChangers.get(action); |
159 |
if (scs == null) { |
160 |
sessionChangers.put(action, Collections.singleton(sc)); |
161 |
} else { |
162 |
if (scs.size() == 1) { |
163 |
SessionChanger old = scs.iterator().next(); |
164 |
scs = new CopyOnWriteArraySet<SessionChanger>(); |
165 |
scs.add(old); |
166 |
} |
167 |
scs.add(sc); |
168 |
} |
169 |
} |
170 |
} |
171 |
} |
172 |
|
173 |
private void removeSessionChangerListener(SessionChanger sc) { |
174 |
Set<String> actions = sc.getActions(); |
175 |
synchronized (sessionChangers) { |
176 |
for (String action : actions) { |
177 |
Set<SessionChanger> scs = sessionChangers.get(action); |
178 |
if (scs == null) { |
179 |
continue; |
180 |
} |
181 |
if (scs.size() == 1) { |
182 |
SessionChanger old = scs.iterator().next(); |
183 |
if (sc.equals(old)) { |
184 |
sessionChangers.remove(action); |
185 |
} |
186 |
} else { |
187 |
scs.remove(sc); |
188 |
} |
189 |
} |
190 |
} |
191 |
} |
192 |
|
193 |
/** |
194 |
* Implement this interface to handle a debug session change. |
195 |
* Register the implementation via {@link DebuggerServiceRegistration} annotation. |
196 |
*/ |
197 |
public static interface SessionChanger { |
198 |
|
199 |
/** |
200 |
* Provide the set of actions that are handled by this implementation. |
201 |
* @return A set of constants from ActionsManager.Action_* |
202 |
*/ |
203 |
Set<String> getActions(); |
204 |
|
205 |
/** |
206 |
* Called when a session suggests a session change for an action. |
207 |
* @param origin The session suggesting the session change |
208 |
* @param action The action, a constant from ActionsManager.Action_* |
209 |
* @param properties Session-specific properties describing the state |
210 |
* right before the given action. These are used by a new session |
211 |
* to complete the given action. |
212 |
* @return A new session, or <code>null<code> when this handler decides |
213 |
* not to change the debug session for this action. |
214 |
*/ |
215 |
Session changeSuggested(Session origin, String action, Map<Object, Object> properties); |
216 |
} |
217 |
|
218 |
} |