Line 0
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.HashSet; |
52 |
import java.util.List; |
53 |
import java.util.Map; |
54 |
import java.util.Set; |
55 |
import java.util.concurrent.CopyOnWriteArraySet; |
56 |
|
57 |
/** |
58 |
* Bridge between sessions. |
59 |
* |
60 |
* @author Martin Entlicher |
61 |
*/ |
62 |
public final class SessionBridge { |
63 |
|
64 |
private static SessionBridge instance; |
65 |
|
66 |
private final Map<String, Set<SessionChanger>> sessionChangers = new HashMap<String, Set<SessionChanger>>(); |
67 |
private final List<SessionChanger> lookupSessionChangers; |
68 |
|
69 |
private SessionBridge() { |
70 |
Lookup lookup = new Lookup.MetaInf(null); |
71 |
final List<? extends SessionChanger> scList = lookup.lookup(null, SessionChanger.class); |
72 |
((Customizer) scList).addPropertyChangeListener(new PropertyChangeListener() { |
73 |
@Override |
74 |
public void propertyChange(PropertyChangeEvent evt) { |
75 |
for (SessionChanger sc : lookupSessionChangers) { |
76 |
removeSessionChangerListener(sc); |
77 |
} |
78 |
lookupSessionChangers.clear(); |
79 |
for (SessionChanger sc : scList) { |
80 |
lookupSessionChangers.add(sc); |
81 |
addSessionChangerListener(sc); |
82 |
} |
83 |
} |
84 |
}); |
85 |
lookupSessionChangers = new ArrayList<SessionChanger>(); |
86 |
for (SessionChanger sc : scList) { |
87 |
lookupSessionChangers.add(sc); |
88 |
addSessionChangerListener(sc); |
89 |
} |
90 |
} |
91 |
|
92 |
/** |
93 |
* Get the default instance of SessionBridge. |
94 |
* @return the default instance |
95 |
*/ |
96 |
public static synchronized SessionBridge getDefault() { |
97 |
if (instance == null) { |
98 |
instance = new SessionBridge(); |
99 |
} |
100 |
return instance; |
101 |
} |
102 |
|
103 |
/** |
104 |
* Suggest a session change to perform a particular action. |
105 |
* @param origin The original session suggesting the session change |
106 |
* @param action An action - a constant from ActionsManager.Action_* |
107 |
* @param properties Properties describing the current state of the current session before the given action. |
108 |
* The actual properties are specific for the particular session type. |
109 |
* @return <code>true</code> when the session is changed and another session |
110 |
* decided to perform the given action. The call-back will be called later on.<br/> |
111 |
* <code>false</code> when no other session would like to perform this action. |
112 |
*/ |
113 |
public boolean suggestChange(Session origin, String action, Map<Object, Object> properties) { |
114 |
Set<SessionChanger> scs; |
115 |
synchronized (sessionChangers) { |
116 |
scs = sessionChangers.get(action); |
117 |
} |
118 |
if (scs != null) { |
119 |
for (SessionChanger sc : scs) { |
120 |
Session newSession = sc.changeSuggested(origin, action, properties); |
121 |
if (newSession != null) { |
122 |
if (DebuggerManager.getDebuggerManager().getCurrentSession() == origin) { |
123 |
DebuggerManager.getDebuggerManager().setCurrentSession(newSession); |
124 |
} |
125 |
return true; |
126 |
} |
127 |
} |
128 |
} |
129 |
return false; |
130 |
} |
131 |
|
132 |
private void addSessionChangerListener(SessionChanger sc) { |
133 |
Set<String> actions = sc.getActions(); |
134 |
synchronized (sessionChangers) { |
135 |
for (String action : actions) { |
136 |
Set<SessionChanger> scs = sessionChangers.get(action); |
137 |
if (scs == null) { |
138 |
sessionChangers.put(action, Collections.singleton(sc)); |
139 |
} else { |
140 |
if (scs.size() == 1) { |
141 |
SessionChanger old = scs.iterator().next(); |
142 |
scs = new CopyOnWriteArraySet<SessionChanger>(); |
143 |
scs.add(old); |
144 |
} |
145 |
scs.add(sc); |
146 |
} |
147 |
} |
148 |
} |
149 |
} |
150 |
|
151 |
private void removeSessionChangerListener(SessionChanger sc) { |
152 |
Set<String> actions = sc.getActions(); |
153 |
synchronized (sessionChangers) { |
154 |
for (String action : actions) { |
155 |
Set<SessionChanger> scs = sessionChangers.get(action); |
156 |
if (scs == null) { |
157 |
continue; |
158 |
} |
159 |
if (scs.size() == 1) { |
160 |
SessionChanger old = scs.iterator().next(); |
161 |
if (sc.equals(old)) { |
162 |
sessionChangers.remove(action); |
163 |
} |
164 |
} else { |
165 |
scs.remove(sc); |
166 |
} |
167 |
} |
168 |
} |
169 |
} |
170 |
|
171 |
public static interface SessionChanger { |
172 |
|
173 |
Set<String> getActions(); |
174 |
|
175 |
Session changeSuggested(Session origin, String action, Map<Object, Object> properties); |
176 |
} |
177 |
|
178 |
} |