Skip to content

Commit b2ec9aa

Browse files
committed
Cleanup ProcessConsoleManager implementation
- read extension registry only once, on startup, remove unneeded locks - get rid of shared `fDefaultColorProvider` which can't work per design Fixes #2638 Fixes #2637
1 parent 2743386 commit b2ec9aa

1 file changed

Lines changed: 53 additions & 67 deletions

File tree

debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/views/console/ProcessConsoleManager.java

Lines changed: 53 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
import java.util.HashMap;
2020
import java.util.List;
2121
import java.util.Map;
22+
import java.util.concurrent.ConcurrentHashMap;
2223

2324
import org.eclipse.core.runtime.CoreException;
2425
import org.eclipse.core.runtime.IConfigurationElement;
2526
import org.eclipse.core.runtime.IExtensionPoint;
27+
import org.eclipse.core.runtime.IExtensionRegistry;
2628
import org.eclipse.core.runtime.IProgressMonitor;
2729
import org.eclipse.core.runtime.IStatus;
2830
import org.eclipse.core.runtime.Platform;
@@ -107,28 +109,46 @@ public boolean shouldSchedule() {
107109
/**
108110
* Console document content provider extensions, keyed by extension id
109111
*/
110-
private Map<String, IConfigurationElement> fColorProviders;
111-
112-
/**
113-
* The default color provider. Used if no color provider is contributed
114-
* for the given process type.
115-
*/
116-
private IConsoleColorProvider fDefaultColorProvider;
112+
private final Map<String, IConfigurationElement> fColorProviders;
117113

118114
/**
119115
* Console line trackers; keyed by process type to list of trackers (1:N)
120116
*/
121-
private Map<String, List<IConfigurationElement>> fLineTrackers;
117+
private final Map<String, List<IConfigurationElement>> fLineTrackers;
122118

123119
/**
124120
* Map of processes for a launch to compute removed processes
125121
*/
126-
private Map<ILaunch, IProcess[]> fProcesses;
122+
private final Map<ILaunch, IProcess[]> fProcesses;
123+
124+
public ProcessConsoleManager() {
125+
fColorProviders = new HashMap<>();
126+
fLineTrackers = new HashMap<>();
127+
fProcesses = new ConcurrentHashMap<>();
128+
readExtensions();
129+
}
130+
131+
private void readExtensions() {
132+
IExtensionRegistry extensionRegistry = Platform.getExtensionRegistry();
133+
IExtensionPoint extensionPoint = extensionRegistry.getExtensionPoint(DebugUIPlugin.getUniqueIdentifier(),
134+
IDebugUIConstants.EXTENSION_POINT_CONSOLE_COLOR_PROVIDERS);
135+
for (IConfigurationElement extension : extensionPoint.getConfigurationElements()) {
136+
String processType = extension.getAttribute("processType"); //$NON-NLS-1$
137+
if (processType != null) {
138+
fColorProviders.put(processType, extension);
139+
}
140+
}
141+
extensionPoint = extensionRegistry.getExtensionPoint(DebugUIPlugin.getUniqueIdentifier(),
142+
IDebugUIConstants.EXTENSION_POINT_CONSOLE_LINE_TRACKERS);
143+
for (IConfigurationElement extension : extensionPoint.getConfigurationElements()) {
144+
String processType = extension.getAttribute("processType"); //$NON-NLS-1$
145+
if (processType != null) {
146+
List<IConfigurationElement> list = fLineTrackers.computeIfAbsent(processType, k -> new ArrayList<>());
147+
list.add(extension);
148+
}
149+
}
150+
}
127151

128-
/**
129-
* Lock for fLineTrackers
130-
*/
131-
private final Object fLineTrackersLock = new Object();
132152
/**
133153
* @see ILaunchListener#launchRemoved(ILaunch)
134154
*/
@@ -141,9 +161,7 @@ protected void removeLaunch(ILaunch launch) {
141161
for (IProcess process : launch.getProcesses()) {
142162
removeProcess(process);
143163
}
144-
if (fProcesses != null) {
145-
fProcesses.remove(launch);
146-
}
164+
fProcesses.remove(launch);
147165
}
148166

149167
/**
@@ -246,32 +264,24 @@ public void shutdown() {
246264
removeLaunch(launch);
247265
}
248266
launchManager.removeLaunchListener(this);
249-
if (fProcesses != null) {
250-
fProcesses.clear();
251-
}
267+
fProcesses.clear();
252268
}
253269

254270
/**
255-
* Returns a new console document color provider extension for the given
256-
* process type, or <code>null</code> if none.
271+
* Returns a new console document color provider extension for the given process
272+
* type, or new default {@link ConsoleColorProvider} implementation if no
273+
* providers registered for given type.
257274
*
258275
* @param type corresponds to <code>IProcess.ATTR_PROCESS_TYPE</code>
259276
* @return IConsoleColorProvider
260277
*/
261278
public IConsoleColorProvider getColorProvider(String type) {
262-
if (fColorProviders == null) {
263-
fColorProviders = new HashMap<>();
264-
IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(DebugUIPlugin.getUniqueIdentifier(), IDebugUIConstants.EXTENSION_POINT_CONSOLE_COLOR_PROVIDERS);
265-
for (IConfigurationElement extension : extensionPoint.getConfigurationElements()) {
266-
fColorProviders.put(extension.getAttribute("processType"), extension); //$NON-NLS-1$
267-
}
268-
}
269279
IConfigurationElement extension = fColorProviders.get(type);
270280
if (extension != null) {
271281
try {
272282
Object colorProvider = extension.createExecutableExtension("class"); //$NON-NLS-1$
273-
if (colorProvider instanceof IConsoleColorProvider) {
274-
return (IConsoleColorProvider)colorProvider;
283+
if (colorProvider instanceof IConsoleColorProvider consoleColorProvider) {
284+
return consoleColorProvider;
275285
}
276286
DebugUIPlugin.logErrorMessage(MessageFormat.format(
277287
"Extension {0} must specify an instanceof IConsoleColorProvider for class attribute.", //$NON-NLS-1$
@@ -280,11 +290,8 @@ public IConsoleColorProvider getColorProvider(String type) {
280290
DebugUIPlugin.log(e);
281291
}
282292
}
283-
//no color provider found of specified type, return default color provider.
284-
if (fDefaultColorProvider == null) {
285-
fDefaultColorProvider = new ConsoleColorProvider();
286-
}
287-
return fDefaultColorProvider;
293+
// no color provider found of specified type, return default color provider.
294+
return new ConsoleColorProvider();
288295
}
289296

290297
/**
@@ -294,40 +301,22 @@ public IConsoleColorProvider getColorProvider(String type) {
294301
*/
295302
public IConsoleLineTracker[] getLineTrackers(IProcess process) {
296303
String type = process.getAttribute(IProcess.ATTR_PROCESS_TYPE);
297-
298-
if (fLineTrackers == null) {
299-
synchronized (fLineTrackersLock) { // can't use fLineTrackers as lock as it is null here
300-
fLineTrackers = new HashMap<>();
301-
IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(DebugUIPlugin.getUniqueIdentifier(), IDebugUIConstants.EXTENSION_POINT_CONSOLE_LINE_TRACKERS);
302-
for (IConfigurationElement extension : extensionPoint.getConfigurationElements()) {
303-
String processType = extension.getAttribute("processType"); //$NON-NLS-1$
304-
List<IConfigurationElement> list = fLineTrackers.get(processType);
305-
if (list == null) {
306-
list = new ArrayList<>();
307-
fLineTrackers.put(processType, list);
308-
}
309-
list.add(extension);
310-
}
311-
}
304+
if (type == null) {
305+
return new IConsoleLineTracker[0];
312306
}
313307

314308
ArrayList<IConsoleLineTracker> trackers = new ArrayList<>();
315-
if (type != null) {
316-
List<IConfigurationElement> lineTrackerExtensions;
317-
synchronized (fLineTrackers) {// need to synchronize as the update to list might be still happening
318-
lineTrackerExtensions = fLineTrackers.get(type);
319-
}
320-
if(lineTrackerExtensions != null) {
321-
for (IConfigurationElement element : lineTrackerExtensions) {
322-
try {
323-
trackers.add((IConsoleLineTracker) element.createExecutableExtension("class")); //$NON-NLS-1$
324-
} catch (CoreException e) {
325-
DebugUIPlugin.log(e);
326-
}
309+
List<IConfigurationElement> lineTrackerExtensions = fLineTrackers.get(type);
310+
if (lineTrackerExtensions != null) {
311+
for (IConfigurationElement element : lineTrackerExtensions) {
312+
try {
313+
trackers.add((IConsoleLineTracker) element.createExecutableExtension("class")); //$NON-NLS-1$
314+
} catch (CoreException e) {
315+
DebugUIPlugin.log(e);
327316
}
328317
}
329318
}
330-
return trackers.toArray(new IConsoleLineTracker[0]);
319+
return trackers.toArray(IConsoleLineTracker[]::new);
331320
}
332321

333322
/**
@@ -339,9 +328,6 @@ public IConsoleLineTracker[] getLineTrackers(IProcess process) {
339328
*/
340329
private List<IProcess> getRemovedProcesses(ILaunch launch) {
341330
List<IProcess> removed = null;
342-
if (fProcesses == null) {
343-
fProcesses = new HashMap<>();
344-
}
345331
IProcess[] old = fProcesses.get(launch);
346332
IProcess[] curr = launch.getProcesses();
347333
if (old != null) {
@@ -366,7 +352,7 @@ private List<IProcess> getRemovedProcesses(ILaunch launch) {
366352
* @param object object to search for
367353
* @return whether the given object is contained in the list
368354
*/
369-
private boolean contains(Object[] list, Object object) {
355+
private static boolean contains(Object[] list, Object object) {
370356
for (Object object2 : list) {
371357
if (object2.equals(object)) {
372358
return true;

0 commit comments

Comments
 (0)