|
13 | 13 | *******************************************************************************/
|
14 | 14 | package org.eclipse.pde.internal.core;
|
15 | 15 |
|
| 16 | +import java.lang.StackWalker.Option; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.concurrent.ConcurrentHashMap; |
| 19 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 20 | + |
16 | 21 | import org.eclipse.core.resources.IProject;
|
17 | 22 | import org.eclipse.core.runtime.CoreException;
|
| 23 | +import org.eclipse.core.runtime.ILog; |
18 | 24 | import org.eclipse.core.runtime.IPath;
|
19 | 25 | import org.eclipse.core.runtime.IStatus;
|
20 | 26 | import org.eclipse.core.runtime.Status;
|
| 27 | +import org.eclipse.core.runtime.jobs.IJobChangeEvent; |
21 | 28 | import org.eclipse.core.runtime.jobs.Job;
|
| 29 | +import org.eclipse.core.runtime.jobs.JobChangeAdapter; |
22 | 30 | import org.eclipse.jdt.core.ClasspathContainerInitializer;
|
23 | 31 | import org.eclipse.jdt.core.IClasspathContainer;
|
24 | 32 | import org.eclipse.jdt.core.IJavaProject;
|
25 | 33 | import org.eclipse.jdt.core.JavaCore;
|
| 34 | +import org.eclipse.jdt.core.JavaModelException; |
26 | 35 | import org.eclipse.pde.core.plugin.IPluginModelBase;
|
27 | 36 |
|
28 | 37 | public class RequiredPluginsInitializer extends ClasspathContainerInitializer {
|
29 | 38 |
|
| 39 | + private static final AtomicBoolean WARNING_LOGGED = new AtomicBoolean(); |
| 40 | + |
| 41 | + private static final Map<IJavaProject, Job> JOB_MAP = new ConcurrentHashMap<>(); |
| 42 | + |
30 | 43 | private static final Job initPDEJob = Job.create(PDECoreMessages.PluginModelManager_InitializingPluginModels,
|
31 | 44 | monitor -> {
|
32 | 45 | if (!PDECore.getDefault().getModelManager().isInitialized()) {
|
33 | 46 | PDECore.getDefault().getModelManager().targetReloaded(monitor);
|
34 | 47 | }
|
35 | 48 | });
|
36 | 49 |
|
| 50 | + private static StackWalker walker = StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE); |
| 51 | + |
37 | 52 | @Override
|
38 | 53 | public void initialize(IPath containerPath, IJavaProject javaProject) throws CoreException {
|
| 54 | + if (isCalledWhileWorkbenchStartup()) { |
| 55 | + // See https://github.com/eclipse-pde/eclipse.pde/issues/1481 |
| 56 | + if (WARNING_LOGGED.compareAndSet(false, true)) { |
| 57 | + ILog.get().warn( |
| 58 | + "RequiredPluginsInitializer called from within the Application startup thread this will badly impact your IDE performance!", //$NON-NLS-1$ |
| 59 | + new RuntimeException("Called from Application startup thread thread here")); //$NON-NLS-1$ |
| 60 | + } |
| 61 | + JOB_MAP.compute(javaProject, (jp, oldjob) -> { |
| 62 | + if (oldjob != null) { |
| 63 | + oldjob.cancel(); |
| 64 | + } |
| 65 | + Job job = Job.create(PDECoreMessages.PluginModelManager_InitializingPluginModels, m -> { |
| 66 | + if (oldjob != null) { |
| 67 | + try { |
| 68 | + oldjob.join(); |
| 69 | + } catch (InterruptedException e) { |
| 70 | + } |
| 71 | + } |
| 72 | + setClasspath(jp); |
| 73 | + }); |
| 74 | + job.addJobChangeListener(new JobChangeAdapter() { |
| 75 | + @Override |
| 76 | + public void done(IJobChangeEvent event) { |
| 77 | + JOB_MAP.remove(jp); |
| 78 | + } |
| 79 | + }); |
| 80 | + job.schedule(); |
| 81 | + return job; |
| 82 | + }); |
| 83 | + return; |
| 84 | + } |
| 85 | + Job job = JOB_MAP.get(javaProject); |
| 86 | + if (job != null) { |
| 87 | + try { |
| 88 | + job.join(); |
| 89 | + } catch (InterruptedException e) { |
| 90 | + } |
| 91 | + } |
| 92 | + setClasspath(javaProject); |
| 93 | + } |
| 94 | + |
| 95 | + protected void setClasspath(IJavaProject javaProject) throws JavaModelException { |
39 | 96 | IProject project = javaProject.getProject();
|
40 | 97 | // The first project to be built may initialize the PDE models, potentially long running, so allow cancellation
|
41 | 98 | PluginModelManager manager = PDECore.getDefault().getModelManager();
|
@@ -85,4 +142,14 @@ public void requestClasspathContainerUpdate(IPath containerPath, IJavaProject pr
|
85 | 142 | JavaCore.setClasspathContainer(containerPath, new IJavaProject[] {project}, new IClasspathContainer[] {containerSuggestion}, null);
|
86 | 143 | }
|
87 | 144 |
|
| 145 | + private static boolean isCalledWhileWorkbenchStartup() { |
| 146 | + |
| 147 | + return walker.walk(frames -> { |
| 148 | + return frames.anyMatch(sf -> { |
| 149 | + return "org.eclipse.e4.ui.internal.workbench.E4Workbench".equals(sf.getClassName()) //$NON-NLS-1$ |
| 150 | + && "createAndRunUI".equals(sf.getMethodName()); //$NON-NLS-1$ |
| 151 | + }); |
| 152 | + }); |
| 153 | + } |
| 154 | + |
88 | 155 | }
|
0 commit comments