1313 *******************************************************************************/
1414package org .eclipse .pde .internal .core ;
1515
16+ import java .util .Map ;
17+ import java .util .concurrent .ConcurrentHashMap ;
18+ import java .util .concurrent .atomic .AtomicBoolean ;
19+
1620import org .eclipse .core .resources .IProject ;
1721import org .eclipse .core .runtime .CoreException ;
22+ import org .eclipse .core .runtime .ILog ;
1823import org .eclipse .core .runtime .IPath ;
1924import org .eclipse .core .runtime .IStatus ;
2025import org .eclipse .core .runtime .Status ;
26+ import org .eclipse .core .runtime .jobs .IJobChangeEvent ;
2127import org .eclipse .core .runtime .jobs .Job ;
28+ import org .eclipse .core .runtime .jobs .JobChangeAdapter ;
2229import org .eclipse .jdt .core .ClasspathContainerInitializer ;
2330import org .eclipse .jdt .core .IClasspathContainer ;
2431import org .eclipse .jdt .core .IJavaProject ;
2532import org .eclipse .jdt .core .JavaCore ;
33+ import org .eclipse .jdt .core .JavaModelException ;
2634import org .eclipse .pde .core .plugin .IPluginModelBase ;
2735
2836public class RequiredPluginsInitializer extends ClasspathContainerInitializer {
2937
38+ private static final AtomicBoolean WARNING_LOGGED = new AtomicBoolean ();
39+
40+ private static final Map <IJavaProject , Job > JOB_MAP = new ConcurrentHashMap <>();
41+
3042 private static final Job initPDEJob = Job .create (PDECoreMessages .PluginModelManager_InitializingPluginModels ,
3143 monitor -> {
3244 if (!PDECore .getDefault ().getModelManager ().isInitialized ()) {
@@ -36,6 +48,46 @@ public class RequiredPluginsInitializer extends ClasspathContainerInitializer {
3648
3749 @ Override
3850 public void initialize (IPath containerPath , IJavaProject javaProject ) throws CoreException {
51+ if ("main" .equals (Thread .currentThread ().getName ())) { //$NON-NLS-1$
52+ // See https://github.com/eclipse-pde/eclipse.pde/issues/1481
53+ if (WARNING_LOGGED .compareAndSet (false , true )) {
54+ ILog .get ().warn (
55+ "RequiredPluginsInitializer called from within the UI thread this will badly impact your IDE performance!" , //$NON-NLS-1$
56+ new RuntimeException ("Called from main thread here" )); //$NON-NLS-1$
57+ }
58+ JOB_MAP .compute (javaProject , (jp , oldjob ) -> {
59+ if (oldjob != null ) {
60+ try {
61+ oldjob .cancel ();
62+ oldjob .join ();
63+ } catch (InterruptedException e ) {
64+ }
65+ }
66+ Job job = Job .create (PDECoreMessages .PluginModelManager_InitializingPluginModels , m -> {
67+ setClasspath (jp );
68+ });
69+ job .addJobChangeListener (new JobChangeAdapter () {
70+ @ Override
71+ public void done (IJobChangeEvent event ) {
72+ JOB_MAP .remove (jp );
73+ }
74+ });
75+ job .schedule ();
76+ return job ;
77+ });
78+ return ;
79+ }
80+ Job job = JOB_MAP .get (javaProject );
81+ if (job != null ) {
82+ try {
83+ job .join ();
84+ } catch (InterruptedException e ) {
85+ }
86+ }
87+ setClasspath (javaProject );
88+ }
89+
90+ protected void setClasspath (IJavaProject javaProject ) throws JavaModelException {
3991 IProject project = javaProject .getProject ();
4092 // The first project to be built may initialize the PDE models, potentially long running, so allow cancellation
4193 PluginModelManager manager = PDECore .getDefault ().getModelManager ();
0 commit comments