Remove unneeded checkNotNull() calls
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / trace / TmfTraceManager.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2015 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Alexandre Montplaisir - Initial API and implementation
11 * Patrick Tasse - Support selection range
12 * Xavier Raynaud - Support filters tracking
13 *******************************************************************************/
14
15 package org.eclipse.tracecompass.tmf.core.trace;
16
17 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
18
19 import java.io.File;
20 import java.net.URISyntaxException;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.LinkedHashMap;
24 import java.util.LinkedHashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
29 import org.eclipse.core.resources.IFile;
30 import org.eclipse.core.resources.IFolder;
31 import org.eclipse.core.resources.IProject;
32 import org.eclipse.core.resources.IResource;
33 import org.eclipse.core.runtime.CoreException;
34 import org.eclipse.core.runtime.URIUtil;
35 import org.eclipse.jdt.annotation.NonNull;
36 import org.eclipse.jdt.annotation.Nullable;
37 import org.eclipse.tracecompass.internal.tmf.core.Activator;
38 import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
39 import org.eclipse.tracecompass.tmf.core.signal.TmfEventFilterAppliedSignal;
40 import org.eclipse.tracecompass.tmf.core.signal.TmfSelectionRangeUpdatedSignal;
41 import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
42 import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
43 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceClosedSignal;
44 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
45 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSelectedSignal;
46 import org.eclipse.tracecompass.tmf.core.signal.TmfWindowRangeUpdatedSignal;
47 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
48 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
49 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
50 import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment;
51
52 import com.google.common.collect.ImmutableSet;
53
54 /**
55 * Central trace manager for TMF. It tracks the currently opened traces and
56 * experiment, as well as the currently-selected time or time range and the
57 * current window time range for each one of those. It also tracks filters
58 * applied for each trace.
59 *
60 * It's a singleton class, so only one instance should exist (available via
61 * {@link #getInstance()}).
62 *
63 * @author Alexandre Montplaisir
64 */
65 public final class TmfTraceManager {
66
67 // ------------------------------------------------------------------------
68 // Attributes
69 // ------------------------------------------------------------------------
70
71 private final Map<ITmfTrace, TmfTraceContext> fTraces;
72
73 /** The currently-selected trace. Should always be part of the trace map */
74 private ITmfTrace fCurrentTrace = null;
75
76 private static final String TEMP_DIR_NAME = ".temp"; //$NON-NLS-1$
77
78 // ------------------------------------------------------------------------
79 // Constructor
80 // ------------------------------------------------------------------------
81
82 private TmfTraceManager() {
83 fTraces = new LinkedHashMap<>();
84 TmfSignalManager.registerVIP(this);
85 }
86
87 /** Singleton instance */
88 private static TmfTraceManager tm = null;
89
90 /**
91 * Get an instance of the trace manager.
92 *
93 * @return The trace manager
94 */
95 public static synchronized TmfTraceManager getInstance() {
96 if (tm == null) {
97 tm = new TmfTraceManager();
98 }
99 return tm;
100 }
101
102 // ------------------------------------------------------------------------
103 // Accessors
104 // ------------------------------------------------------------------------
105
106 /**
107 * Get the currently selected trace (normally, the focused editor).
108 *
109 * @return The active trace
110 */
111 public synchronized ITmfTrace getActiveTrace() {
112 return fCurrentTrace;
113 }
114
115 /**
116 * Get the trace set of the currently active trace.
117 *
118 * @return The active trace set
119 * @see #getTraceSet(ITmfTrace)
120 */
121 public synchronized @NonNull Collection<ITmfTrace> getActiveTraceSet() {
122 final ITmfTrace trace = fCurrentTrace;
123 return getTraceSet(trace);
124 }
125
126 /**
127 * Get the currently-opened traces, as an unmodifiable set.
128 *
129 * @return A set containing the opened traces
130 */
131 public synchronized Set<ITmfTrace> getOpenedTraces() {
132 return Collections.unmodifiableSet(fTraces.keySet());
133 }
134
135 /**
136 * Get the editor file for an opened trace.
137 *
138 * @param trace
139 * the trace
140 * @return the editor file or null if the trace is not opened
141 */
142 public synchronized IFile getTraceEditorFile(ITmfTrace trace) {
143 TmfTraceContext ctx = fTraces.get(trace);
144 if (ctx != null) {
145 return ctx.getEditorFile();
146 }
147 return null;
148 }
149
150 /**
151 * Get the {@link TmfTraceContext} of the current active trace. This can be
152 * used to retrieve the current active/selected time ranges and such.
153 *
154 * @return The trace's context.
155 * @since 1.0
156 */
157 public synchronized TmfTraceContext getCurrentTraceContext() {
158 TmfTraceContext curCtx = fTraces.get(fCurrentTrace);
159 if (curCtx == null) {
160 /* There are no traces opened at the moment. */
161 return TmfTraceContext.NULL_CONTEXT;
162 }
163 return curCtx;
164 }
165
166 // ------------------------------------------------------------------------
167 // Public utility methods
168 // ------------------------------------------------------------------------
169
170 /**
171 * Get the trace set of a given trace. For a standard trace, this is simply
172 * an array with only that trace in it. For experiments, this is an array of
173 * all the traces contained in this experiment.
174 *
175 * @param trace
176 * The trace or experiment
177 * @return The corresponding trace set.
178 */
179 public static @NonNull Collection<@NonNull ITmfTrace> getTraceSet(ITmfTrace trace) {
180 if (trace == null) {
181 return ImmutableSet.of();
182 }
183 List<@NonNull ITmfTrace> traces = trace.getChildren(ITmfTrace.class);
184 if (traces.size() > 0) {
185 return ImmutableSet.copyOf(traces);
186 }
187 return ImmutableSet.of(trace);
188 }
189
190 /**
191 * Get the trace set of a given trace or experiment, including the
192 * experiment. For a standard trace, this is simply a set containing only
193 * that trace. For experiments, it is the set of all the traces contained in
194 * this experiment, along with the experiment.
195 *
196 * @param trace
197 * The trace or experiment
198 * @return The corresponding trace set, including the experiment.
199 */
200 public static @NonNull Collection<ITmfTrace> getTraceSetWithExperiment(ITmfTrace trace) {
201 if (trace == null) {
202 return ImmutableSet.of();
203 }
204 if (trace instanceof TmfExperiment) {
205 TmfExperiment exp = (TmfExperiment) trace;
206 List<ITmfTrace> traces = exp.getTraces();
207 Set<ITmfTrace> alltraces = new LinkedHashSet<>(traces);
208 alltraces.add(exp);
209 return ImmutableSet.copyOf(alltraces);
210 }
211 return Collections.singleton(trace);
212 }
213
214 /**
215 * Return the path (as a string) to the directory for supplementary files to
216 * use with a given trace. If no supplementary file directory has been
217 * configured, a temporary directory based on the trace's name will be
218 * provided.
219 *
220 * @param trace
221 * The trace
222 * @return The path to the supplementary file directory (trailing slash is
223 * INCLUDED!)
224 */
225 public static String getSupplementaryFileDir(ITmfTrace trace) {
226 IResource resource = trace.getResource();
227 if (resource == null) {
228 return getTemporaryDir(trace);
229 }
230
231 String supplDir = null;
232 try {
233 supplDir = resource.getPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER);
234 } catch (CoreException e) {
235 return getTemporaryDir(trace);
236 }
237 return supplDir + File.separator;
238 }
239
240 /**
241 * Refresh the supplementary files resources for a trace, so it can pick up
242 * new files that got created.
243 *
244 * @param trace
245 * The trace for which to refresh the supplementary files
246 */
247 public static void refreshSupplementaryFiles(ITmfTrace trace) {
248 IResource resource = trace.getResource();
249 if (resource != null && resource.exists()) {
250 String supplFolderPath = getSupplementaryFileDir(trace);
251 IProject project = resource.getProject();
252 /* Remove the project's path from the supplementary path dir */
253 if (!supplFolderPath.startsWith(project.getLocation().toOSString())) {
254 Activator.logWarning(String.format("Supplementary files folder for trace %s is not within the project.", trace.getName())); //$NON-NLS-1$
255 return;
256 }
257 IFolder supplFolder = project.getFolder(supplFolderPath.substring(project.getLocationURI().getPath().length()));
258 if (supplFolder.exists()) {
259 try {
260 supplFolder.refreshLocal(IResource.DEPTH_INFINITE, null);
261 } catch (CoreException e) {
262 Activator.logError("Error refreshing resources", e); //$NON-NLS-1$
263 }
264 }
265 }
266 }
267
268 // ------------------------------------------------------------------------
269 // Signal handlers
270 // ------------------------------------------------------------------------
271
272 /**
273 * Signal handler for the traceOpened signal.
274 *
275 * @param signal
276 * The incoming signal
277 */
278 @TmfSignalHandler
279 public synchronized void traceOpened(final TmfTraceOpenedSignal signal) {
280 final ITmfTrace trace = signal.getTrace();
281 final IFile editorFile = signal.getEditorFile();
282 final ITmfTimestamp startTs = trace.getStartTime();
283
284 long offset = trace.getInitialRangeOffset().toNanos();
285 long endTime = startTs.toNanos() + offset;
286 final TmfTimeRange selectionRange = new TmfTimeRange(startTs, startTs);
287 final TmfTimeRange windowRange = new TmfTimeRange(startTs, new TmfTimestamp(endTime, ITmfTimestamp.NANOSECOND_SCALE));
288
289 final TmfTraceContext startCtx = new TmfTraceContext(selectionRange, windowRange, editorFile, null);
290
291 fTraces.put(trace, startCtx);
292
293 /* We also want to set the newly-opened trace as the active trace */
294 fCurrentTrace = trace;
295 }
296
297 /**
298 * Handler for the TmfTraceSelectedSignal.
299 *
300 * @param signal
301 * The incoming signal
302 */
303 @TmfSignalHandler
304 public synchronized void traceSelected(final TmfTraceSelectedSignal signal) {
305 final ITmfTrace newTrace = signal.getTrace();
306 if (!fTraces.containsKey(newTrace)) {
307 throw new RuntimeException();
308 }
309 fCurrentTrace = newTrace;
310 }
311
312 /**
313 * Signal handler for the filterApplied signal.
314 *
315 * @param signal
316 * The incoming signal
317 */
318 @TmfSignalHandler
319 public synchronized void filterApplied(TmfEventFilterAppliedSignal signal) {
320 final ITmfTrace newTrace = signal.getTrace();
321 TmfTraceContext context = fTraces.get(newTrace);
322 if (context == null) {
323 throw new RuntimeException();
324 }
325 fTraces.put(newTrace, new TmfTraceContext(context.getSelectionRange(),
326 context.getWindowRange(),
327 context.getEditorFile(),
328 signal.getEventFilter()));
329 }
330
331 /**
332 * Signal handler for the traceClosed signal.
333 *
334 * @param signal
335 * The incoming signal
336 */
337 @TmfSignalHandler
338 public synchronized void traceClosed(final TmfTraceClosedSignal signal) {
339 fTraces.remove(signal.getTrace());
340 if (fTraces.size() == 0) {
341 fCurrentTrace = null;
342 /*
343 * In other cases, we should receive a traceSelected signal that
344 * will indicate which trace is the new one.
345 */
346 }
347 }
348
349 /**
350 * Signal handler for the selection range signal.
351 *
352 * The current time of *all* traces whose range contains the requested new
353 * selection time range will be updated.
354 *
355 * @param signal
356 * The incoming signal
357 * @since 1.0
358 */
359 @TmfSignalHandler
360 public synchronized void selectionRangeUpdated(final TmfSelectionRangeUpdatedSignal signal) {
361 final ITmfTimestamp beginTs = signal.getBeginTime();
362 final ITmfTimestamp endTs = signal.getEndTime();
363
364 for (Map.Entry<ITmfTrace, TmfTraceContext> entry : fTraces.entrySet()) {
365 final ITmfTrace trace = entry.getKey();
366 if (beginTs.intersects(getValidTimeRange(trace)) || endTs.intersects(getValidTimeRange(trace))) {
367 TmfTraceContext prevCtx = checkNotNull(entry.getValue());
368
369 /*
370 * We want to update the selection range, but keep everything
371 * else the same as the previous trace context.
372 */
373 TmfTimeRange newSelectionRange = new TmfTimeRange(beginTs, endTs);
374 TmfTraceContext newCtx = new TmfTraceContext(newSelectionRange,
375 prevCtx.getWindowRange(),
376 prevCtx.getEditorFile(),
377 prevCtx.getFilter());
378 entry.setValue(newCtx);
379 }
380 }
381 }
382
383 /**
384 * Signal handler for the window range signal.
385 *
386 * The current window time range of *all* valid traces will be updated to
387 * the new requested times.
388 *
389 * @param signal
390 * The incoming signal
391 * @since 1.0
392 */
393 @TmfSignalHandler
394 public synchronized void windowRangeUpdated(final TmfWindowRangeUpdatedSignal signal) {
395 for (Map.Entry<ITmfTrace, TmfTraceContext> entry : fTraces.entrySet()) {
396 final ITmfTrace trace = entry.getKey();
397 final TmfTraceContext prevCtx = checkNotNull(entry.getValue());
398
399 final TmfTimeRange validTr = getValidTimeRange(trace);
400 if (validTr == null) {
401 return;
402 }
403
404 /* Determine the new time range */
405 TmfTimeRange targetTr = signal.getCurrentRange().getIntersection(validTr);
406 TmfTimeRange newWindowTr = (targetTr == null ? prevCtx.getWindowRange() : targetTr);
407
408 /* Keep the values from the old context, except for the window range */
409 TmfTraceContext newCtx = new TmfTraceContext(prevCtx.getSelectionRange(),
410 newWindowTr, prevCtx.getEditorFile(), prevCtx.getFilter());
411 entry.setValue(newCtx);
412 }
413 }
414
415 // ------------------------------------------------------------------------
416 // Private utility methods
417 // ------------------------------------------------------------------------
418
419 /**
420 * Return the valid time range of a trace (not the current window time
421 * range, but the range of all possible valid timestamps).
422 *
423 * For a real trace this is the whole range of the trace. For an experiment,
424 * it goes from the start time of the earliest trace to the end time of the
425 * latest one.
426 *
427 * @param trace
428 * The trace to check for
429 * @return The valid time span, or 'null' if the trace is not valid
430 */
431 private @Nullable TmfTimeRange getValidTimeRange(ITmfTrace trace) {
432 if (!fTraces.containsKey(trace)) {
433 /* Trace is not part of the currently opened traces */
434 return null;
435 }
436
437 List<ITmfTrace> traces = trace.getChildren(ITmfTrace.class);
438
439 if (traces.isEmpty()) {
440 /* "trace" is a single trace, return its time range directly */
441 return trace.getTimeRange();
442 }
443
444 if (traces.size() == 1) {
445 /* Trace is an experiment with only 1 trace */
446 return traces.get(0).getTimeRange();
447 }
448
449 /*
450 * Trace is an trace set with 2+ traces, so get the earliest start and
451 * the latest end.
452 */
453 ITmfTimestamp start = traces.get(0).getStartTime();
454 ITmfTimestamp end = traces.get(0).getEndTime();
455
456 for (int i = 1; i < traces.size(); i++) {
457 ITmfTrace curTrace = traces.get(i);
458 if (curTrace.getStartTime().compareTo(start) < 0) {
459 start = curTrace.getStartTime();
460 }
461 if (curTrace.getEndTime().compareTo(end) > 0) {
462 end = curTrace.getEndTime();
463 }
464 }
465 return new TmfTimeRange(start, end);
466 }
467
468 /**
469 * Get the temporary directory path. If there is an instance of Eclipse
470 * running, the temporary directory will reside under the workspace.
471 *
472 * @return the temporary directory path suitable to be passed to the
473 * java.io.File constructor without a trailing separator
474 */
475 public static String getTemporaryDirPath() {
476 // Get the workspace path from the properties
477 String property = System.getProperty("osgi.instance.area"); //$NON-NLS-1$
478 if (property != null) {
479 try {
480 File dir = URIUtil.toFile(URIUtil.fromString(property));
481 dir = new File(dir.getAbsolutePath() + File.separator + TEMP_DIR_NAME);
482 if (!dir.exists()) {
483 dir.mkdirs();
484 }
485 return dir.getAbsolutePath();
486 } catch (URISyntaxException e) {
487 Activator.logError(e.getLocalizedMessage(), e);
488 }
489 }
490 return System.getProperty("java.io.tmpdir"); //$NON-NLS-1$
491 }
492
493 /**
494 * Get a temporary directory based on a trace's name. We will create the
495 * directory if it doesn't exist, so that it's ready to be used.
496 */
497 private static String getTemporaryDir(ITmfTrace trace) {
498 String pathName = getTemporaryDirPath() +
499 File.separator +
500 trace.getName() +
501 File.separator;
502 File dir = new File(pathName);
503 if (!dir.exists()) {
504 dir.mkdirs();
505 }
506 return pathName;
507 }
508 }
This page took 0.042357 seconds and 6 git commands to generate.