analysis: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core.tests / stubs / org / eclipse / tracecompass / tmf / tests / stubs / trace / TmfTraceStub.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2014 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 * Francois Chouinard - Initial API and implementation
11 * Patrick Tasse - Updated for removal of context clone
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.tmf.tests.stubs.trace;
15
16 import java.io.FileNotFoundException;
17 import java.io.IOException;
18 import java.io.RandomAccessFile;
19 import java.lang.reflect.Method;
20 import java.nio.ByteBuffer;
21 import java.util.LinkedList;
22 import java.util.List;
23 import java.util.concurrent.locks.ReentrantLock;
24
25 import org.eclipse.core.resources.IProject;
26 import org.eclipse.core.resources.IResource;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.jdt.annotation.NonNull;
30 import org.eclipse.tracecompass.internal.tmf.core.Activator;
31 import org.eclipse.tracecompass.internal.tmf.core.request.TmfCoalescedEventRequest;
32 import org.eclipse.tracecompass.tmf.core.component.TmfEventProvider;
33 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
34 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
35 import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
36 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
37 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSelectedSignal;
38 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
39 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
40 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
41 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
42 import org.eclipse.tracecompass.tmf.core.trace.ITmfEventParser;
43 import org.eclipse.tracecompass.tmf.core.trace.TmfContext;
44 import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
45 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
46 import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.ITmfCheckpoint;
47 import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.TmfCheckpoint;
48 import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
49 import org.eclipse.tracecompass.tmf.core.trace.location.TmfLongLocation;
50
51 /**
52 * <b><u>TmfTraceStub</u></b>
53 * <p>
54 * Dummy test trace. Use in conjunction with TmfEventParserStub.
55 */
56 public class TmfTraceStub extends TmfTrace implements ITmfPersistentlyIndexable {
57
58 // ------------------------------------------------------------------------
59 // Attributes
60 // ------------------------------------------------------------------------
61
62 // The actual stream
63 private RandomAccessFile fTrace;
64
65 // The associated event parser
66 private final @NonNull ITmfEventParser fParser;
67
68 // The synchronization lock
69 private final ReentrantLock fLock = new ReentrantLock();
70
71 private ITmfTimestamp fInitialRangeOffset = null;
72
73 // ------------------------------------------------------------------------
74 // Constructors
75 // ------------------------------------------------------------------------
76
77 /**
78 * Default constructor
79 */
80 public TmfTraceStub() {
81 super();
82 fParser = new TmfEventParserStub(this);
83 }
84
85 /**
86 * Constructor with which you can specify a custom streaming interval. The
87 * parser and indexer won't be specified.
88 *
89 * @param path
90 * The path to the trace file
91 * @param cacheSize
92 * The cache size
93 * @param interval
94 * The trace streaming interval
95 * @throws TmfTraceException
96 * If an error occurred opening the trace
97 */
98 public TmfTraceStub(final String path,
99 final int cacheSize,
100 final long interval) throws TmfTraceException {
101 super(null, ITmfEvent.class, path, cacheSize, interval);
102 setupTrace(path);
103 fParser = new TmfEventParserStub(this);
104 }
105
106 /**
107 * Constructor to specify the parser and indexer. The streaming interval
108 * will be 0.
109 *
110 * @param path
111 * The path to the trace file
112 * @param cacheSize
113 * The cache size
114 * @param waitForCompletion
115 * Do we block the caller until the trace is indexed, or not.
116 * @param parser
117 * The trace parser. If left 'null', it will use a
118 * {@link TmfEventParserStub}.
119 * @throws TmfTraceException
120 * If an error occurred opening the trace
121 */
122 public TmfTraceStub(final String path,
123 final int cacheSize,
124 final boolean waitForCompletion,
125 final ITmfEventParser parser) throws TmfTraceException {
126 super(null, ITmfEvent.class, path, cacheSize, 0);
127 setupTrace(path);
128 fParser = ((parser != null) ? parser : new TmfEventParserStub(this));
129 if (waitForCompletion) {
130 indexTrace(true);
131 }
132 }
133
134 /**
135 * Constructor to specify the resource, parser and indexer. The streaming
136 * interval will be 0.
137 *
138 * @param resource
139 * The trace resource
140 * @param path
141 * The path to the trace file
142 * @param cacheSize
143 * The cache size
144 * @param waitForCompletion
145 * Do we block the caller until the trace is indexed, or not.
146 * @param parser
147 * The trace parser. If left 'null', it will use a
148 * {@link TmfEventParserStub}.
149 * @throws TmfTraceException
150 * If an error occurred opening the trace
151 */
152 public TmfTraceStub(final IResource resource,
153 final String path,
154 final int cacheSize,
155 final boolean waitForCompletion,
156 final ITmfEventParser parser) throws TmfTraceException {
157 super(resource, ITmfEvent.class, path, cacheSize, 0);
158 setupTrace(path);
159 fParser = ((parser != null) ? parser : new TmfEventParserStub(this));
160 if (waitForCompletion) {
161 indexTrace(true);
162 }
163 }
164
165 /**
166 * Copy constructor
167 *
168 * @param trace
169 * The trace to copy
170 * @throws TmfTraceException
171 * If an error occurred opening the trace
172 */
173 public TmfTraceStub(final TmfTraceStub trace) throws TmfTraceException {
174 super(trace);
175 setupTrace(getPath()); // fPath will be set by the super-constructor
176 fParser = new TmfEventParserStub(this);
177 }
178
179
180 private void setupTrace(String path) throws TmfTraceException {
181 try {
182 fTrace = new RandomAccessFile(path, "r"); //$NON-NLS-1$
183 } catch (FileNotFoundException e) {
184 throw new TmfTraceException(e.getMessage());
185 }
186 }
187
188 // ------------------------------------------------------------------------
189 // Initializers
190 // ------------------------------------------------------------------------
191
192 @Override
193 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException {
194 try {
195 fTrace = new RandomAccessFile(path, "r"); //$NON-NLS-1$
196 } catch (FileNotFoundException e) {
197 throw new TmfTraceException(e.getMessage());
198 }
199 super.initTrace(resource, path, type);
200 }
201
202 @Override
203 public void initialize(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException {
204 super.initialize(resource, path, type);
205 }
206
207 // ------------------------------------------------------------------------
208 // Accessors
209 // ------------------------------------------------------------------------
210
211 /**
212 * @return The file stream to the trace
213 */
214 public RandomAccessFile getStream() {
215 return fTrace;
216 }
217
218 /**
219 * Set the initial range offset.
220 *
221 * @param initOffset
222 * The new initial range offset
223 */
224 public void setInitialRangeOffset(ITmfTimestamp initOffset) {
225 fInitialRangeOffset = initOffset;
226 }
227
228 @Override
229 public ITmfTimestamp getInitialRangeOffset() {
230 if (fInitialRangeOffset != null) {
231 return fInitialRangeOffset;
232 }
233 return super.getInitialRangeOffset();
234 }
235
236 // ------------------------------------------------------------------------
237 // Operators
238 // ------------------------------------------------------------------------
239
240 @Override
241 public TmfContext seekEvent(final ITmfLocation location) {
242 try {
243 fLock.lock();
244 try {
245 if (fTrace != null) {
246 // Position the trace at the requested location and
247 // returns the corresponding context
248 long loc = 0;
249 long rank = 0;
250 if (location != null) {
251 loc = (Long) location.getLocationInfo();
252 rank = ITmfContext.UNKNOWN_RANK;
253 }
254 if (loc != fTrace.getFilePointer()) {
255 fTrace.seek(loc);
256 }
257 final TmfContext context = new TmfContext(getCurrentLocation(), rank);
258 return context;
259 }
260 } catch (final IOException e) {
261 e.printStackTrace();
262 } catch (final NullPointerException e) {
263 e.printStackTrace();
264 }
265 finally{
266 fLock.unlock();
267 }
268 } catch (final NullPointerException e) {
269 e.printStackTrace();
270 }
271 return null;
272 }
273
274
275 @Override
276 public TmfContext seekEvent(final double ratio) {
277 fLock.lock();
278 try {
279 if (fTrace != null) {
280 final ITmfLocation location = new TmfLongLocation(Long.valueOf(Math.round(ratio * fTrace.length())));
281 final TmfContext context = seekEvent(location);
282 context.setRank(ITmfContext.UNKNOWN_RANK);
283 return context;
284 }
285 } catch (final IOException e) {
286 e.printStackTrace();
287 } finally {
288 fLock.unlock();
289 }
290
291 return null;
292 }
293
294 @Override
295 public double getLocationRatio(ITmfLocation location) {
296 fLock.lock();
297 try {
298 if (fTrace != null) {
299 if (location.getLocationInfo() instanceof Long) {
300 return ((Long) location.getLocationInfo()).doubleValue() / fTrace.length();
301 }
302 }
303 } catch (final IOException e) {
304 e.printStackTrace();
305 } finally {
306 fLock.unlock();
307 }
308 return 0;
309 }
310
311 @Override
312 public ITmfLocation getCurrentLocation() {
313 fLock.lock();
314 try {
315 if (fTrace != null) {
316 return new TmfLongLocation(fTrace.getFilePointer());
317 }
318 } catch (final IOException e) {
319 e.printStackTrace();
320 } finally {
321 fLock.unlock();
322 }
323 return null;
324 }
325
326 @Override
327 public ITmfEvent parseEvent(final ITmfContext context) {
328 fLock.lock();
329 try {
330 // parseNextEvent will update the context
331 if (fTrace != null && context != null) {
332 final ITmfEvent event = fParser.parseEvent(context);
333 return event;
334 }
335 } finally {
336 fLock.unlock();
337 }
338 return null;
339 }
340
341 @Override
342 public ITmfTimestamp createTimestamp(long ts) {
343 return new TmfTimestamp(getTimestampTransform().transform(ts) / 1000000L, ITmfTimestamp.MILLISECOND_SCALE);
344 }
345
346 @Override
347 public synchronized void setNbEvents(final long nbEvents) {
348 super.setNbEvents(nbEvents);
349 }
350
351 @Override
352 public void setTimeRange(final TmfTimeRange range) {
353 super.setTimeRange(range);
354 }
355
356 @Override
357 public void setStartTime(final ITmfTimestamp startTime) {
358 super.setStartTime(startTime);
359 }
360
361 @Override
362 public void setEndTime(final ITmfTimestamp endTime) {
363 super.setEndTime(endTime);
364 }
365
366 @Override
367 public void setStreamingInterval(final long interval) {
368 super.setStreamingInterval(interval);
369 }
370
371 @Override
372 public synchronized void dispose() {
373 fLock.lock();
374 try {
375 if (fTrace != null) {
376 fTrace.close();
377 fTrace = null;
378 }
379 } catch (final IOException e) {
380 // Ignore
381 } finally {
382 fLock.unlock();
383 }
384 super.dispose();
385 }
386
387 @Override
388 public IStatus validate(IProject project, String path) {
389 if (fileExists(path)) {
390 return Status.OK_STATUS;
391 }
392 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "File does not exist: " + path);
393 }
394
395 private static int fCheckpointSize = -1;
396
397 @Override
398 public synchronized int getCheckpointSize() {
399 if (fCheckpointSize == -1) {
400 TmfCheckpoint c = new TmfCheckpoint(new TmfTimestamp(0L), new TmfLongLocation(0L), 0);
401 ByteBuffer b = ByteBuffer.allocate(ITmfCheckpoint.MAX_SERIALIZE_SIZE);
402 b.clear();
403 c.serialize(b);
404 fCheckpointSize = b.position();
405 }
406
407 return fCheckpointSize;
408 }
409
410 @Override
411 public ITmfLocation restoreLocation(ByteBuffer bufferIn) {
412 return new TmfLongLocation(bufferIn);
413 }
414
415 /**
416 * Simulate trace opening, to be called by tests who need an actively opened
417 * trace
418 */
419 public void openTrace() {
420 TmfSignalManager.dispatchSignal(new TmfTraceOpenedSignal(this, this, null));
421 selectTrace();
422 }
423
424 /**
425 * Simulate selecting the trace
426 */
427 public void selectTrace() {
428 TmfSignalManager.dispatchSignal(new TmfTraceSelectedSignal(this, this));
429 }
430
431 /**
432 * @return a copy of the pending request list
433 * @throws Exception if java reflection failed
434 */
435 public List<TmfCoalescedEventRequest> getAllPendingRequests() throws Exception {
436 Method m = TmfEventProvider.class.getDeclaredMethod("getPendingRequests");
437 m.setAccessible(true);
438 LinkedList<?> list= (LinkedList<?>) m.invoke(this);
439 LinkedList<TmfCoalescedEventRequest> retList = new LinkedList<>();
440 for (Object element : list) {
441 retList.add((TmfCoalescedEventRequest) element);
442 }
443 return retList;
444 }
445
446 /**
447 * Clears the pending request list
448 * @throws Exception if java reflection failed
449 */
450 public void clearAllPendingRequests() throws Exception {
451 Method m = TmfEventProvider.class.getDeclaredMethod("clearPendingRequests");
452 m.setAccessible(true);
453 m.invoke(this);
454 }
455
456 /**
457 * Sets the timer flag
458 * @param enabled
459 * flag to set
460 * @throws Exception if java reflection failed
461 */
462 public void setTimerEnabledFlag(boolean enabled) throws Exception {
463 Class<?>[] paramTypes = new Class[1];
464 paramTypes[0] = Boolean.class;
465 Method m = TmfEventProvider.class.getDeclaredMethod("setTimerEnabled", paramTypes);
466
467 Object[] params = new Object[1];
468 params[0] = Boolean.valueOf(enabled);
469 m.setAccessible(true);
470 m.invoke(this, params);
471 }
472
473 }
This page took 0.041276 seconds and 5 git commands to generate.