0bdf08a9dcc54df0c29738e8d05e2c3d70054bef
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / analysis / ondemand / OnDemandAnalysisManager.java
1 /*******************************************************************************
2 * Copyright (c) 2016 EfficiOS Inc., Alexandre Montplaisir
3 *
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 *******************************************************************************/
9
10 package org.eclipse.tracecompass.tmf.core.analysis.ondemand;
11
12 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
13
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Set;
17 import java.util.stream.Collectors;
18
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;
25
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;
30
31 /**
32 * Manager for {@link IOnDemandAnalysis}.
33 *
34 * Takes care of reading the extension point information, and providing analyses
35 * for traces via the {@link #getOndemandAnalyses(ITmfTrace)} method.
36 *
37 * @author Alexandre Montplaisir
38 * @since 2.0
39 */
40 public final class OnDemandAnalysisManager {
41
42 /** The singleton instance */
43 private static @Nullable OnDemandAnalysisManager INSTANCE;
44
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$
47
48 private static final String ATTR_CLASS = "class"; //$NON-NLS-1$
49
50 private final LoadingCache<ITmfTrace, Set<IOnDemandAnalysis>> analysisCache = checkNotNull(CacheBuilder.newBuilder()
51 .weakKeys()
52 .softValues()
53 .build(new CacheLoader<ITmfTrace, Set<IOnDemandAnalysis>>() {
54 @Override
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));
60 }
61 }));
62
63 private final List<OndemandAnalysisWrapper> fAnalysisWrappers;
64
65 /**
66 * Internal class used to store extension point information
67 */
68 private static class OndemandAnalysisWrapper {
69
70 public final IOnDemandAnalysis analysis;
71
72 public OndemandAnalysisWrapper(IOnDemandAnalysis analysis) {
73 this.analysis = analysis;
74 }
75 }
76
77 /**
78 * Get the instance of this manager.
79 *
80 * @return The singleton instance of this class
81 */
82 public static synchronized OnDemandAnalysisManager getInstance() {
83 OnDemandAnalysisManager inst = INSTANCE;
84 if (inst == null) {
85 inst = new OnDemandAnalysisManager();
86 INSTANCE = inst;
87 }
88 return inst;
89 }
90
91 /**
92 * Private constructor, should only be called via {@link #getInstance()}.
93 */
94 private OnDemandAnalysisManager() {
95 fAnalysisWrappers = new ArrayList<>();
96 IConfigurationElement[] configElements = Platform.getExtensionRegistry().getConfigurationElementsFor(EXTENSION_POINT_ID);
97
98 for (IConfigurationElement element : configElements) {
99 if (ELEM_NAME_ANALYSIS.equals(element.getName())) {
100 try {
101 Object extension = element.createExecutableExtension(ATTR_CLASS);
102 if (extension != null) {
103 fAnalysisWrappers.add(new OndemandAnalysisWrapper((IOnDemandAnalysis) extension));
104 }
105 } catch (CoreException | ClassCastException e) {
106 Activator.logError("Exception while loading extension point", e); //$NON-NLS-1$
107 }
108 }
109 }
110 }
111
112 /**
113 * Get all the registered on-demand analyses that apply to the given trace.
114 *
115 * @param 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
119 */
120 public Set<IOnDemandAnalysis> getOndemandAnalyses(ITmfTrace trace) {
121 return checkNotNull(analysisCache.getUnchecked(trace));
122 }
123 }
This page took 0.032636 seconds and 4 git commands to generate.