Skip to content

Commit 3595d74

Browse files
committed
Improve RequiredPluginsInitializer.initialize
- Collect deferred projects to be processed by a single job. #1667
1 parent 2decd24 commit 3595d74

File tree

1 file changed

+66
-12
lines changed

1 file changed

+66
-12
lines changed

ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/RequiredPluginsInitializer.java

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@
1313
*******************************************************************************/
1414
package org.eclipse.pde.internal.core;
1515

16+
import java.util.LinkedHashMap;
17+
import java.util.LinkedHashSet;
18+
import java.util.Map;
19+
import java.util.Set;
20+
1621
import org.eclipse.core.resources.IProject;
1722
import org.eclipse.core.runtime.CoreException;
1823
import org.eclipse.core.runtime.IPath;
24+
import org.eclipse.core.runtime.IProgressMonitor;
1925
import org.eclipse.core.runtime.IStatus;
2026
import org.eclipse.core.runtime.Status;
2127
import org.eclipse.core.runtime.jobs.Job;
@@ -35,26 +41,64 @@ public class RequiredPluginsInitializer extends ClasspathContainerInitializer {
3541
}
3642
});
3743

44+
private static final DeferredClasspathContainerInitializerJob deferredClasspathContainerInitializerJob = new DeferredClasspathContainerInitializerJob();
45+
46+
private static class DeferredClasspathContainerInitializerJob extends Job {
47+
48+
private final Set<IJavaProject> projects = new LinkedHashSet<>();
49+
50+
public DeferredClasspathContainerInitializerJob() {
51+
// This name is not displayed to a user.
52+
super("DeferredClasspathContainerInitializerJob"); //$NON-NLS-1$
53+
setSystem(true);
54+
}
55+
56+
public synchronized void initialize(IJavaProject project) {
57+
if (projects.add(project)) {
58+
schedule();
59+
}
60+
}
61+
62+
private synchronized IJavaProject[] consumeProjects() {
63+
try {
64+
return projects.toArray(IJavaProject[]::new);
65+
} finally {
66+
projects.clear();
67+
}
68+
}
69+
70+
@Override
71+
protected IStatus run(IProgressMonitor monitor) {
72+
try {
73+
setupClasspath(consumeProjects());
74+
} catch (JavaModelException e) {
75+
PDECore.log(e);
76+
}
77+
return Status.OK_STATUS;
78+
}
79+
80+
@Override
81+
public boolean belongsTo(Object family) {
82+
return family == PluginModelManager.class;
83+
}
84+
}
85+
3886
@Override
3987
public void initialize(IPath containerPath, IJavaProject javaProject) throws CoreException {
4088
if (Job.getJobManager().isSuspended()) {
4189
// if the jobmanager is currently suspended we can't use the
4290
// schedule/join pattern here, instead we must retry the requested
4391
// action once jobs are enabled again, this will the possibly
4492
// trigger a rebuild if required or notify other listeners.
45-
Job job = Job.create(PDECoreMessages.PluginModelManager_InitializingPluginModels, m -> {
46-
setupClasspath(javaProject);
47-
});
48-
job.setSystem(true);
49-
job.schedule();
93+
deferredClasspathContainerInitializerJob.initialize(javaProject);
5094
} else {
5195
setupClasspath(javaProject);
5296
}
5397
}
5498

55-
protected void setupClasspath(IJavaProject javaProject) throws JavaModelException {
56-
IProject project = javaProject.getProject();
57-
// The first project to be built may initialize the PDE models, potentially long running, so allow cancellation
99+
protected static void setupClasspath(IJavaProject... javaProjects) throws JavaModelException {
100+
// The first project to be built may initialize the PDE models,
101+
// potentially long running, so allow cancellation
58102
PluginModelManager manager = PDECore.getDefault().getModelManager();
59103
if (!manager.isInitialized()) {
60104
initPDEJob.schedule();
@@ -63,11 +107,21 @@ protected void setupClasspath(IJavaProject javaProject) throws JavaModelExceptio
63107
} catch (InterruptedException e) {
64108
}
65109
}
66-
if (project.exists() && project.isOpen()) {
67-
IPluginModelBase model = manager.findModel(project);
68-
JavaCore.setClasspathContainer(PDECore.REQUIRED_PLUGINS_CONTAINER_PATH, new IJavaProject[] { javaProject },
69-
new IClasspathContainer[] { new RequiredPluginsClasspathContainer(model, project) }, null);
110+
111+
Map<IJavaProject, RequiredPluginsClasspathContainer> classPathContainers = new LinkedHashMap<>();
112+
for (IJavaProject javaProject : javaProjects) {
113+
IProject project = javaProject.getProject();
114+
if (project.exists() && project.isOpen()) {
115+
IPluginModelBase model = manager.findModel(project);
116+
RequiredPluginsClasspathContainer requiredPluginsClasspathContainer = new RequiredPluginsClasspathContainer(
117+
model, project);
118+
classPathContainers.put(javaProject, requiredPluginsClasspathContainer);
119+
}
70120
}
121+
122+
JavaCore.setClasspathContainer(PDECore.REQUIRED_PLUGINS_CONTAINER_PATH,
123+
classPathContainers.keySet().toArray(IJavaProject[]::new),
124+
classPathContainers.values().toArray(IClasspathContainer[]::new), null);
71125
}
72126

73127
@Override

0 commit comments

Comments
 (0)