1 /*******************************************************************************
2 * Copyright (c) 2016 EfficiOS Inc., Alexandre Montplaisir
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.tmf
.core
.analysis
.ondemand
;
12 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
14 import java
.util
.ArrayList
;
15 import java
.util
.List
;
17 import java
.util
.stream
.Collectors
;
19 import org
.eclipse
.core
.runtime
.CoreException
;
20 import org
.eclipse
.core
.runtime
.IConfigurationElement
;
21 import org
.eclipse
.core
.runtime
.Platform
;
22 import org
.eclipse
.jdt
.annotation
.Nullable
;
23 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
24 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
26 import com
.google
.common
.cache
.CacheBuilder
;
27 import com
.google
.common
.cache
.CacheLoader
;
28 import com
.google
.common
.cache
.LoadingCache
;
29 import com
.google
.common
.collect
.ImmutableSet
;
32 * Manager for {@link IOnDemandAnalysis}.
34 * Takes care of reading the extension point information, and providing analyses
35 * for traces via the {@link #getOndemandAnalyses(ITmfTrace)} method.
37 * @author Alexandre Montplaisir
40 public final class OnDemandAnalysisManager
{
42 /** The singleton instance */
43 private static @Nullable OnDemandAnalysisManager INSTANCE
;
45 private static final String EXTENSION_POINT_ID
= "org.eclipse.tracecompass.tmf.core.analysis.ondemand"; //$NON-NLS-1$
46 private static final String ELEM_NAME_ANALYSIS
= "analysis"; //$NON-NLS-1$
48 private static final String ATTR_CLASS
= "class"; //$NON-NLS-1$
50 private final LoadingCache
<ITmfTrace
, Set
<IOnDemandAnalysis
>> analysisCache
= checkNotNull(CacheBuilder
.newBuilder()
53 .build(new CacheLoader
<ITmfTrace
, Set
<IOnDemandAnalysis
>>() {
55 public Set
<IOnDemandAnalysis
> load(ITmfTrace trace
) {
56 return fAnalysisWrappers
.stream()
57 .map(wrapper
-> wrapper
.analysis
)
58 .filter(analysis
-> analysis
.appliesTo(trace
))
59 .collect(Collectors
.collectingAndThen(Collectors
.toSet(), ImmutableSet
::copyOf
));
63 private final List
<OndemandAnalysisWrapper
> fAnalysisWrappers
;
66 * Internal class used to store extension point information
68 private static class OndemandAnalysisWrapper
{
70 public final IOnDemandAnalysis analysis
;
72 public OndemandAnalysisWrapper(IOnDemandAnalysis analysis
) {
73 this.analysis
= analysis
;
78 * Get the instance of this manager.
80 * @return The singleton instance of this class
82 public static synchronized OnDemandAnalysisManager
getInstance() {
83 OnDemandAnalysisManager inst
= INSTANCE
;
85 inst
= new OnDemandAnalysisManager();
92 * Private constructor, should only be called via {@link #getInstance()}.
94 private OnDemandAnalysisManager() {
95 fAnalysisWrappers
= new ArrayList
<>();
96 IConfigurationElement
[] configElements
= Platform
.getExtensionRegistry().getConfigurationElementsFor(EXTENSION_POINT_ID
);
98 for (IConfigurationElement element
: configElements
) {
99 if (ELEM_NAME_ANALYSIS
.equals(element
.getName())) {
101 Object extension
= element
.createExecutableExtension(ATTR_CLASS
);
102 if (extension
!= null) {
103 fAnalysisWrappers
.add(new OndemandAnalysisWrapper((IOnDemandAnalysis
) extension
));
105 } catch (CoreException
| ClassCastException e
) {
106 Activator
.logError("Exception while loading extension point", e
); //$NON-NLS-1$
113 * Get all the registered on-demand analyses that apply to the given trace.
116 * The trace to get the analyses for
117 * @return The set of on-demand analyses that apply to this trace. It can be
118 * empty, but not null
120 public Set
<IOnDemandAnalysis
> getOndemandAnalyses(ITmfTrace trace
) {
121 return checkNotNull(analysisCache
.getUnchecked(trace
));