13
13
*******************************************************************************/
14
14
package org .eclipse .pde .internal .core ;
15
15
16
+ import java .util .Map ;
17
+ import java .util .concurrent .ConcurrentHashMap ;
18
+ import java .util .concurrent .atomic .AtomicBoolean ;
19
+
16
20
import org .eclipse .core .resources .IProject ;
17
21
import org .eclipse .core .runtime .CoreException ;
22
+ import org .eclipse .core .runtime .ILog ;
18
23
import org .eclipse .core .runtime .IPath ;
19
24
import org .eclipse .core .runtime .IStatus ;
20
25
import org .eclipse .core .runtime .Status ;
26
+ import org .eclipse .core .runtime .jobs .IJobChangeEvent ;
21
27
import org .eclipse .core .runtime .jobs .Job ;
28
+ import org .eclipse .core .runtime .jobs .JobChangeAdapter ;
22
29
import org .eclipse .jdt .core .ClasspathContainerInitializer ;
23
30
import org .eclipse .jdt .core .IClasspathContainer ;
24
31
import org .eclipse .jdt .core .IJavaProject ;
25
32
import org .eclipse .jdt .core .JavaCore ;
33
+ import org .eclipse .jdt .core .JavaModelException ;
26
34
import org .eclipse .pde .core .plugin .IPluginModelBase ;
27
35
28
36
public class RequiredPluginsInitializer extends ClasspathContainerInitializer {
29
37
38
+ private static final AtomicBoolean WARNING_LOGGED = new AtomicBoolean ();
39
+
40
+ private static final Map <IJavaProject , Job > JOB_MAP = new ConcurrentHashMap <>();
41
+
30
42
private static final Job initPDEJob = Job .create (PDECoreMessages .PluginModelManager_InitializingPluginModels ,
31
43
monitor -> {
32
44
if (!PDECore .getDefault ().getModelManager ().isInitialized ()) {
@@ -36,6 +48,48 @@ public class RequiredPluginsInitializer extends ClasspathContainerInitializer {
36
48
37
49
@ Override
38
50
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
+ oldjob .cancel ();
61
+ }
62
+ Job job = Job .create (PDECoreMessages .PluginModelManager_InitializingPluginModels , m -> {
63
+ if (oldjob != null ) {
64
+ try {
65
+ oldjob .join ();
66
+ } catch (InterruptedException e ) {
67
+ }
68
+ }
69
+ setClasspath (jp );
70
+ });
71
+ job .addJobChangeListener (new JobChangeAdapter () {
72
+ @ Override
73
+ public void done (IJobChangeEvent event ) {
74
+ JOB_MAP .remove (jp );
75
+ }
76
+ });
77
+ job .schedule ();
78
+ return job ;
79
+ });
80
+ return ;
81
+ }
82
+ Job job = JOB_MAP .get (javaProject );
83
+ if (job != null ) {
84
+ try {
85
+ job .join ();
86
+ } catch (InterruptedException e ) {
87
+ }
88
+ }
89
+ setClasspath (javaProject );
90
+ }
91
+
92
+ protected void setClasspath (IJavaProject javaProject ) throws JavaModelException {
39
93
IProject project = javaProject .getProject ();
40
94
// The first project to be built may initialize the PDE models, potentially long running, so allow cancellation
41
95
PluginModelManager manager = PDECore .getDefault ().getModelManager ();
0 commit comments