forked from eclipse-pde/eclipse.pde
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApiBaselineManager.java
740 lines (691 loc) · 23.7 KB
/
ApiBaselineManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/*******************************************************************************
* Copyright (c) 2007, 2021 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.pde.api.tools.internal;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.eclipse.core.resources.ISaveContext;
import org.eclipse.core.resources.ISaveParticipant;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.IJobFunction;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IPreferencesService;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.pde.api.tools.internal.builder.ApiAnalysisBuilder;
import org.eclipse.pde.api.tools.internal.builder.ApiAnalysisBuilder.ApiAnalysisJob;
import org.eclipse.pde.api.tools.internal.builder.ApiAnalysisBuilder.ApiAnalysisJobRule;
import org.eclipse.pde.api.tools.internal.model.ApiBaseline;
import org.eclipse.pde.api.tools.internal.model.ApiModelCache;
import org.eclipse.pde.api.tools.internal.model.ApiModelFactory;
import org.eclipse.pde.api.tools.internal.model.StubApiComponent;
import org.eclipse.pde.api.tools.internal.model.WorkspaceBaseline;
import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin;
import org.eclipse.pde.api.tools.internal.provisional.IApiBaselineManager;
import org.eclipse.pde.api.tools.internal.provisional.model.IApiBaseline;
import org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent;
import org.eclipse.pde.api.tools.internal.util.Util;
import org.eclipse.pde.core.plugin.IPluginModelBase;
import org.eclipse.pde.core.plugin.ModelEntry;
import org.eclipse.pde.core.plugin.PluginRegistry;
import org.eclipse.pde.internal.core.DependencyManager;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* This manager is used to maintain (persist, restore, access, update) API
* baselines. This manager is lazy, in that caches are built and maintained when
* requests are made for information, nothing is pre-loaded when the manager is
* initialized.
*
* @since 1.0.0
* @noinstantiate This class is not intended to be instantiated by clients.
*/
public final class ApiBaselineManager implements IApiBaselineManager, ISaveParticipant {
/**
* Constant for the default API baseline. Value is:
* <code>default_api_profile</code>
*/
private static final String DEFAULT_BASELINE = "default_api_profile"; //$NON-NLS-1$
/**
* Constant representing the id of the workspace {@link IApiBaseline}. Value
* is: <code>workspace</code>
*/
public static final String WORKSPACE_API_BASELINE_ID = "workspace"; //$NON-NLS-1$
/**
* Constant representing the file extension for a baseline file. Value is:
* <code>.profile</code>
*/
private static final String BASELINE_FILE_EXTENSION = ".profile"; //$NON-NLS-1$
/**
* The main cache for the manager. The form of the cache is:
*
* <pre>
* Map<String(baselineid), {@link IApiBaseline}>
* </pre>
*/
private volatile ConcurrentHashMap<String, IApiBaseline> baselinecache;
/**
* Cache of baseline names to the location with their infos in it
*/
private volatile Map<String, String> handlecache;
private volatile Set<String> hasinfos;
/**
* The current default {@link IApiBaseline}
*/
private String defaultbaseline = null;
/**
* The current workspace baseline
*/
private volatile IApiBaseline workspacebaseline;
/**
* The default save location for persisting the cache from this manager.
*/
private IPath savelocation = null;
/**
* If the cache of baselines needs to be saved or not.
*/
private volatile boolean fNeedsSaving;
/**
* The singleton instance
*/
private static ApiBaselineManager fInstance = null;
/**
* Constructor
*/
private ApiBaselineManager(boolean framework) {
if (framework) {
ApiPlugin.getDefault().addSaveParticipant(this);
savelocation = ApiPlugin.getDefault().getStateLocation().append(".api_profiles").addTrailingSeparator(); //$NON-NLS-1$
}
hasinfos = Collections.emptySet();
}
/**
* Returns the singleton instance of the manager
*
* @return the singleton instance of the manager
*/
public static synchronized ApiBaselineManager getManager() {
if (fInstance == null) {
fInstance = new ApiBaselineManager(ApiPlugin.isRunningInFramework());
}
return fInstance;
}
@Override
public IApiBaseline getApiBaseline(String name) {
if (name == null) {
return null;
}
initializeStateCache();
return baselinecache.get(name);
}
@Override
public IApiBaseline[] getApiBaselines() {
initializeStateCache();
return baselinecache.values().toArray(new IApiBaseline[0]);
}
@Override
public void addApiBaseline(IApiBaseline newbaseline) {
if (newbaseline != null) {
initializeStateCache();
baselinecache.put(newbaseline.getName(), newbaseline);
if (((ApiBaseline) newbaseline).peekInfos()) {
hasinfos.add(newbaseline.getName());
}
fNeedsSaving = true;
}
}
@Override
public boolean removeApiBaseline(String name) {
if (name == null) {
return false;
}
initializeStateCache();
IApiBaseline baseline = baselinecache.remove(name);
if (baseline == null) {
return false;
}
synchronized (this) {
baseline.dispose();
boolean success = true;
if (savelocation == null) {
return success;
}
// remove from filesystem
File file = savelocation.append(name + BASELINE_FILE_EXTENSION).toFile();
if (file.exists()) {
try {
success &= Files.deleteIfExists(file.toPath());
} catch (IOException e) {
ApiPlugin.log(e);
}
}
fNeedsSaving = true;
// flush the model cache
ApiModelCache.getCache().removeElementInfo(baseline);
return success;
}
}
/**
* Loads the infos for the given baseline from persisted storage (the
* *.profile file)
*
* @param baseline the given baseline
* @throws CoreException if an exception occurs while loading baseline infos
*/
public void loadBaselineInfos(ApiBaseline baseline) throws CoreException {
initializeStateCache();
if (isBaselineLoaded(baseline)) {
return;
}
String filename = handlecache.get(baseline.getName());
if (filename != null) {
File file = new File(filename);
if (file.exists()) {
try (FileInputStream inputStream = new FileInputStream(file)) {
baseline.restoreFrom(inputStream);
} catch (IOException e) {
ApiPlugin.log(e);
}
hasinfos.add(baseline.getName());
}
}
}
public boolean isBaselineLoaded(IApiBaseline baseline) {
return hasinfos.contains(baseline.getName());
}
/**
* Initializes the baseline cache lazily. Only performs work if the current
* cache has not been created yet
*/
private void initializeStateCache() {
if (baselinecache != null) {
return;
}
if (!ApiPlugin.isRunningInFramework()) {
synchronized (this) {
if (baselinecache == null) {
handlecache = new ConcurrentHashMap<>(8);
hasinfos = ConcurrentHashMap.newKeySet(8);
baselinecache = new ConcurrentHashMap<>(8);
}
}
return;
}
long time = System.currentTimeMillis();
synchronized (this) {
if (baselinecache == null) {
handlecache = new ConcurrentHashMap<>(8);
hasinfos = ConcurrentHashMap.newKeySet(8);
ConcurrentHashMap<String, IApiBaseline> bcache = new ConcurrentHashMap<>(8);
File[] baselines = savelocation.toFile().listFiles((FileFilter) pathname -> pathname.getName().endsWith(BASELINE_FILE_EXTENSION));
if (baselines != null) {
IApiBaseline newbaseline = null;
for (File baseline : baselines) {
if (baseline.exists()) {
newbaseline = new ApiBaseline(IPath.fromOSString(baseline.getName()).removeFileExtension().toString());
handlecache.put(newbaseline.getName(), baseline.getAbsolutePath());
bcache.put(newbaseline.getName(), newbaseline);
}
}
}
String def = getDefaultProfilePref();
if (def != null && bcache.get(def) != null) {
defaultbaseline = def;
} else {
defaultbaseline = null;
}
baselinecache = bcache;
if (ApiPlugin.DEBUG_BASELINE_MANAGER) {
System.out.println("Time to initialize state cache: " + (System.currentTimeMillis() - time) + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
}
}
}
}
/**
* @return the default API baseline saved in the preferences, or
* <code>null</code> if there isn't one
*/
private String getDefaultProfilePref() {
IPreferencesService service = Platform.getPreferencesService();
return service.getString(ApiPlugin.PLUGIN_ID, DEFAULT_BASELINE, null, new IScopeContext[] { InstanceScope.INSTANCE });
}
/**
* Persists all of the cached elements to individual xml files named with
* the id of the API baseline
*/
private void persistStateCache() throws CoreException {
if (savelocation == null) {
return;
}
IEclipsePreferences node = InstanceScope.INSTANCE.getNode(ApiPlugin.PLUGIN_ID);
if (defaultbaseline != null) {
node.put(DEFAULT_BASELINE, defaultbaseline);
} else {
node.remove(DEFAULT_BASELINE);
}
if (baselinecache != null && !hasinfos.isEmpty()) {
Path dir = savelocation.toPath();
for (Entry<String, IApiBaseline> entry : baselinecache.entrySet()) {
String id = entry.getKey();
IApiBaseline baseline = entry.getValue();
if (!isBaselineLoaded(baseline)) {
continue;
}
Path file = dir.resolve(id + BASELINE_FILE_EXTENSION);
writeBaselineDescription(baseline, file);
// need to save the api baseline state in order to be able
// to reload it later
handlecache.put(baseline.getName(), file.toAbsolutePath().toString());
}
}
}
/**
* Writes the current state of the {@link IApiBaseline} as XML to the given file
*/
private void writeBaselineDescription(IApiBaseline baseline, Path file) throws CoreException {
Document xml = getProfileXML(baseline);
try {
Util.writeDocumentToFile(xml, file);
} catch (IOException e) {
throw new CoreException(Status.error("Error writing profile descrition", e)); //$NON-NLS-1$
}
}
/**
* Returns an XML description of the given baseline.
*
* @param baseline the given API baseline
* @return XML string representation of the given baseline
* @throws CoreException if an exception occurs while retrieving the xml
* string representation
*/
private Document getProfileXML(IApiBaseline baseline) throws CoreException {
Document document = Util.newDocument();
Element root = document.createElement(IApiXmlConstants.ELEMENT_APIPROFILE);
document.appendChild(root);
root.setAttribute(IApiXmlConstants.ATTR_NAME, baseline.getName());
root.setAttribute(IApiXmlConstants.ATTR_VERSION, IApiXmlConstants.API_PROFILE_CURRENT_VERSION);
String location = baseline.getLocation();
if (location != null) {
root.setAttribute(IApiXmlConstants.ATTR_LOCATION, location);
}
Element celement = null;
IApiComponent[] components = baseline.getApiComponents();
for (IApiComponent component : components) {
Set<IApiComponent> allComponentSet = new HashSet<>();
// if the baseline has multiple versions, persist all versions
Set<IApiComponent> multipleComponents = baseline.getAllApiComponents(component.getSymbolicName());
if (multipleComponents.isEmpty()) {
// no multiple version - add the current component
allComponentSet.add(component);
} else {
allComponentSet.addAll(multipleComponents);
}
for (IApiComponent iApiComponent : allComponentSet) {
if (!iApiComponent.isSystemComponent()) {
celement = document.createElement(IApiXmlConstants.ELEMENT_APICOMPONENT);
celement.setAttribute(IApiXmlConstants.ATTR_ID, iApiComponent.getSymbolicName());
celement.setAttribute(IApiXmlConstants.ATTR_VERSION, iApiComponent.getVersion());
celement.setAttribute(IApiXmlConstants.ATTR_LOCATION, IPath.fromOSString(iApiComponent.getLocation()).toPortableString());
root.appendChild(celement);
}
}
// clear the temporary hashset
allComponentSet.clear();
}
return document;
}
/**
* Restore a baseline from the given input stream (persisted baseline).
*
* @param baseline the given baseline to restore
* @param stream the given input stream
* @throws CoreException if unable to restore the baseline
* @return restored baseline components or null if restore didn't work
*/
public IApiComponent[] readBaselineComponents(ApiBaseline baseline, InputStream stream) throws CoreException {
long start = System.currentTimeMillis();
DocumentBuilder parser = getConfiguredParser();
IApiComponent[] restored = null;
try {
Document document = parser.parse(stream);
Element root = document.getDocumentElement();
if (root.getNodeName().equals(IApiXmlConstants.ELEMENT_APIPROFILE)) {
String baselineLocation = root.getAttribute(IApiXmlConstants.ATTR_LOCATION);
if (baselineLocation != null && !baselineLocation.equals(Util.EMPTY_STRING)) {
baseline.setLocation(IPath.fromPortableString(baselineLocation).toOSString());
}
// un-pooled components
NodeList children = root.getElementsByTagName(IApiXmlConstants.ELEMENT_APICOMPONENT);
List<IApiComponent> components = new ArrayList<>();
for (int j = 0; j < children.getLength(); j++) {
Element componentNode = (Element) children.item(j);
// this also contains components in pools, so don't process
// them
if (componentNode.getParentNode().equals(root)) {
String location = componentNode.getAttribute(IApiXmlConstants.ATTR_LOCATION);
IApiComponent component = ApiModelFactory.newApiComponent(baseline, IPath.fromPortableString(location).toOSString());
if (component != null) {
components.add(component);
}
}
}
// pooled components - only for xml file with version <= 1
// since version 2, pools have been removed
children = root.getElementsByTagName(IApiXmlConstants.ELEMENT_POOL);
IApiComponent component = null;
for (int j = 0; j < children.getLength(); j++) {
String location = ((Element) children.item(j)).getAttribute(IApiXmlConstants.ATTR_LOCATION);
IPath poolPath = IPath.fromPortableString(location);
NodeList componentNodes = root.getElementsByTagName(IApiXmlConstants.ELEMENT_APICOMPONENT);
for (int i = 0; i < componentNodes.getLength(); i++) {
Element compElement = (Element) componentNodes.item(i);
String id = compElement.getAttribute(IApiXmlConstants.ATTR_ID);
String ver = compElement.getAttribute(IApiXmlConstants.ATTR_VERSION);
StringBuilder name = new StringBuilder();
name.append(id);
name.append('_');
name.append(ver);
File file = poolPath.append(name.toString()).toFile();
if (!file.exists()) {
name.append(".jar"); //$NON-NLS-1$
file = poolPath.append(name.toString()).toFile();
}
component = ApiModelFactory.newApiComponent(baseline, file.getAbsolutePath());
if (component != null) {
components.add(component);
}
}
}
restored = components.toArray(new IApiComponent[components.size()]);
// Avoid unstable bundle traversal order to simplify our life
Arrays.sort(restored, (o1, o2) -> o1.getName().compareTo(o2.getName()));
}
} catch (IOException | SAXException e) {
throw new CoreException(Status.error("Error restoring API baseline", e)); //$NON-NLS-1$
}
if (ApiPlugin.DEBUG_BASELINE_MANAGER) {
System.out.println("Time to restore a persisted baseline : " + (System.currentTimeMillis() - start) + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
}
return restored;
}
private static DocumentBuilder getConfiguredParser() throws CoreException {
try {
@SuppressWarnings("restriction")
DocumentBuilder parser = org.eclipse.core.internal.runtime.XmlProcessorFactory
.createDocumentBuilderWithErrorOnDOCTYPE();
parser.setErrorHandler(new DefaultHandler());
return parser;
} catch (ParserConfigurationException | FactoryConfigurationError e) {
throw new CoreException(Status.error("Error restoring API baseline", e)); //$NON-NLS-1$
}
}
@Override
public void saving(ISaveContext context) throws CoreException {
if (!fNeedsSaving) {
return;
}
persistStateCache();
cleanStateCache();
fNeedsSaving = false;
}
/**
* Cleans out all but the default baseline from the in-memory cache of
* baselines
*/
private void cleanStateCache() {
if (baselinecache != null) {
IApiBaseline baseline = null;
for (Entry<String, IApiBaseline> entry : baselinecache.entrySet()) {
baseline = entry.getValue();
if (!baseline.getName().equals(defaultbaseline)) {
baseline.dispose();
hasinfos.remove(baseline.getName());
// iter.remove();
}
}
}
}
/**
* Returns if the given name is an existing baseline name
*
* @return true if the given name is an existing baseline name, false
* otherwise
*/
public boolean isExistingProfileName(String name) {
if (baselinecache == null || name == null) {
return false;
}
return baselinecache.containsKey(name);
}
/**
* Cleans up the manager
*/
public void stop() {
try {
Job.getJobManager().cancel(ApiAnalysisJob.class);
if (baselinecache != null) {
// we should first dispose all existing baselines
for (IApiBaseline iApiBaseline : baselinecache.values()) {
iApiBaseline.dispose();
}
baselinecache.clear();
}
synchronized (this) {
if (workspacebaseline != null) {
workspacebaseline.dispose();
}
}
if (handlecache != null) {
handlecache.clear();
}
if (!hasinfos.isEmpty()) {
hasinfos.clear();
}
StubApiComponent.disposeAllCaches();
} finally {
if (ApiPlugin.isRunningInFramework()) {
ApiPlugin.getDefault().removeSaveParticipant(this);
}
}
}
@Override
public void doneSaving(ISaveContext context) {
//
}
@Override
public void prepareToSave(ISaveContext context) throws CoreException {
//
}
@Override
public void rollback(ISaveContext context) {
//
}
@Override
public IApiBaseline getDefaultApiBaseline() {
initializeStateCache();
String defbaseline = defaultbaseline;
if (defbaseline == null) {
return null;
}
return baselinecache.get(defbaseline);
}
@Override
public void setDefaultApiBaseline(String name) {
fNeedsSaving = true;
defaultbaseline = name;
}
@Override
public IApiBaseline getWorkspaceBaseline() {
if (!ApiPlugin.isRunningInFramework()) {
return null;
}
if (this.workspacebaseline == null) {
try {
synchronized (this) {
if (this.workspacebaseline == null) {
this.workspacebaseline = createWorkspaceBaseline();
}
}
} catch (CoreException e) {
ApiPlugin.log(e);
}
}
return this.workspacebaseline;
}
/**
* Disposes the workspace baseline such that a new one will be created on
* the next request.
*/
public void disposeWorkspaceBaseline() {
final IApiBaseline originalBaseline = workspacebaseline;
if (originalBaseline == null) {
return;
}
IJobFunction runnable = m -> {
IApiBaseline oldBaseline = null;
synchronized (ApiBaselineManager.this) {
if (workspacebaseline != null && originalBaseline == workspacebaseline) {
if (ApiPlugin.DEBUG_BASELINE_MANAGER) {
System.out.println("disposing workspace baseline"); //$NON-NLS-1$
}
oldBaseline = workspacebaseline;
StubApiComponent.disposeAllCaches();
workspacebaseline = null;
}
}
if (oldBaseline != null) {
oldBaseline.dispose();
}
return Status.OK_STATUS;
};
if (ApiAnalysisBuilder.isRunningAsJob()) {
Job.getJobManager().cancel(ApiAnalysisJob.class);
Job.getJobManager().cancel(ApiBaselineManager.class);
Job job = new Job("Disposing Workspace API Baseline") { //$NON-NLS-1$
@Override
public IStatus run(IProgressMonitor monitor) {
return runnable.run(monitor);
}
@Override
public boolean belongsTo(Object family) {
return super.belongsTo(family) || family == ApiBaselineManager.class;
}
};
job.setRule(new ApiBaselineManagerRule());
job.setSystem(true);
job.schedule();
} else {
runnable.run(new NullProgressMonitor());
}
}
public static final class ApiBaselineManagerRule implements ISchedulingRule {
@Override
public boolean contains(ISchedulingRule rule) {
return isConflicting(rule);
}
@Override
public boolean isConflicting(ISchedulingRule rule) {
return rule instanceof ApiBaselineManagerRule || rule instanceof ApiAnalysisJobRule;
}
}
/**
* Creates a workspace {@link IApiBaseline}
*
* @return a new workspace {@link IApiBaseline} or <code>null</code>
*/
private IApiBaseline createWorkspaceBaseline() throws CoreException {
long time = System.currentTimeMillis();
IApiBaseline baseline = null;
try {
baseline = new WorkspaceBaseline();
// populate it with only projects that are API aware
List<IPluginModelBase> models = Arrays.asList(PluginRegistry.getWorkspaceModels());
Set<BundleDescription> bundles = DependencyManager.getSelfAndDependencies(models);
List<IApiComponent> componentsList = new ArrayList<>(bundles.size());
for (BundleDescription bundle : bundles) {
String id = bundle.getSymbolicName();
ModelEntry modelEntry = PluginRegistry.findEntry(id);
IPluginModelBase[] workspaceModels = modelEntry.getWorkspaceModels();
IApiComponent apiComponent = null;
if (workspaceModels.length == 0) {
// external model - reexported case
IPluginModelBase externalModel = PluginRegistry.findModel(id);
if (externalModel != null) {
try {
apiComponent = ApiModelFactory.newApiComponent(baseline, externalModel);
if (apiComponent != null) {
componentsList.add(apiComponent);
}
} catch (CoreException e) {
ApiPlugin.log(e);
}
}
continue;
}
for (IPluginModelBase iPluginModelBase : workspaceModels) {
try {
apiComponent = ApiModelFactory.newApiComponent(baseline, iPluginModelBase);
if (apiComponent != null) {
componentsList.add(apiComponent);
}
} catch (CoreException e) {
ApiPlugin.log(e);
}
}
}
baseline.addApiComponents(componentsList.toArray(new IApiComponent[componentsList.size()]));
} finally {
if (ApiPlugin.DEBUG_BASELINE_MANAGER) {
System.out.println("Time to create a workspace baseline : " + (System.currentTimeMillis() - time) + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
}
}
return baseline;
}
@Override
public IApiComponent getWorkspaceComponent(String symbolicName) {
IApiBaseline baseline = getWorkspaceBaseline();
if (baseline != null) {
return baseline.getApiComponent(symbolicName);
}
return null;
}
}