1 /*******************************************************************************
2 * Copyright (c) 2009, 2014 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 * Patrick Tasse - Updated for removal of context clone
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
;
16 import java
.io
.FileNotFoundException
;
17 import java
.io
.IOException
;
18 import java
.io
.RandomAccessFile
;
19 import java
.nio
.ByteBuffer
;
20 import java
.util
.concurrent
.locks
.ReentrantLock
;
22 import org
.eclipse
.core
.resources
.IProject
;
23 import org
.eclipse
.core
.resources
.IResource
;
24 import org
.eclipse
.core
.runtime
.IStatus
;
25 import org
.eclipse
.core
.runtime
.Status
;
26 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalManager
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceSelectedSignal
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfEventParser
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.ITmfPersistentlyIndexable
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpoint
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpoint
;
42 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.location
.ITmfLocation
;
43 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.location
.TmfLongLocation
;
46 * <b><u>TmfTraceStub</u></b>
48 * Dummy test trace. Use in conjunction with TmfEventParserStub.
50 public class TmfTraceStub
extends TmfTrace
implements ITmfEventParser
, ITmfPersistentlyIndexable
{
52 // ------------------------------------------------------------------------
54 // ------------------------------------------------------------------------
57 private RandomAccessFile fTrace
;
59 // // The associated event parser
60 // private ITmfEventParser<TmfEvent> fParser;
62 // The synchronization lock
63 private final ReentrantLock fLock
= new ReentrantLock();
65 private ITmfTimestamp fInitialRangeOffset
= null;
67 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
74 public TmfTraceStub() {
76 setParser(new TmfEventParserStub(this));
80 * Constructor with which you can specify a custom streaming interval. The
81 * parser and indexer won't be specified.
84 * The path to the trace file
88 * The trace streaming interval
89 * @throws TmfTraceException
90 * If an error occurred opening the trace
92 public TmfTraceStub(final String path
,
94 final long interval
) throws TmfTraceException
{
95 super(null, ITmfEvent
.class, path
, cacheSize
, interval
, null);
97 setParser(new TmfEventParserStub(this));
101 * Constructor to specify the parser and indexer. The streaming interval
105 * The path to the trace file
108 * @param waitForCompletion
109 * Do we block the caller until the trace is indexed, or not.
111 * The trace parser. If left 'null', it will use a
112 * {@link TmfEventParserStub}.
113 * @throws TmfTraceException
114 * If an error occurred opening the trace
116 public TmfTraceStub(final String path
,
118 final boolean waitForCompletion
,
119 final ITmfEventParser parser
) throws TmfTraceException
{
120 super(null, ITmfEvent
.class, path
, cacheSize
, 0, null);
122 setParser((parser
!= null) ? parser
: new TmfEventParserStub(this));
123 if (waitForCompletion
) {
133 * @throws TmfTraceException
134 * If an error occurred opening the trace
136 public TmfTraceStub(final TmfTraceStub trace
) throws TmfTraceException
{
138 setupTrace(getPath()); // fPath will be set by the super-constructor
139 setParser(new TmfEventParserStub(this));
143 private void setupTrace(String path
) throws TmfTraceException
{
145 fTrace
= new RandomAccessFile(path
, "r"); //$NON-NLS-1$
146 } catch (FileNotFoundException e
) {
147 throw new TmfTraceException(e
.getMessage());
151 // ------------------------------------------------------------------------
153 // ------------------------------------------------------------------------
156 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
158 fTrace
= new RandomAccessFile(path
, "r"); //$NON-NLS-1$
159 } catch (FileNotFoundException e
) {
160 throw new TmfTraceException(e
.getMessage());
162 setParser(new TmfEventParserStub(this));
163 super.initTrace(resource
, path
, type
);
167 public void initialize(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
168 super.initialize(resource
, path
, type
);
171 // ------------------------------------------------------------------------
173 // ------------------------------------------------------------------------
176 * @return The file stream to the trace
178 public RandomAccessFile
getStream() {
183 * Set the initial range offset.
186 * The new initial range offset
188 public void setInitialRangeOffset(ITmfTimestamp initOffset
) {
189 fInitialRangeOffset
= initOffset
;
193 public ITmfTimestamp
getInitialRangeOffset() {
194 if (fInitialRangeOffset
!= null) {
195 return fInitialRangeOffset
;
197 return super.getInitialRangeOffset();
200 // ------------------------------------------------------------------------
202 // ------------------------------------------------------------------------
205 public TmfContext
seekEvent(final ITmfLocation location
) {
209 if (fTrace
!= null) {
210 // Position the trace at the requested location and
211 // returns the corresponding context
214 if (location
!= null) {
215 loc
= (Long
) location
.getLocationInfo();
216 rank
= ITmfContext
.UNKNOWN_RANK
;
218 if (loc
!= fTrace
.getFilePointer()) {
221 final TmfContext context
= new TmfContext(getCurrentLocation(), rank
);
224 } catch (final IOException e
) {
226 } catch (final NullPointerException e
) {
232 } catch (final NullPointerException e
) {
240 public TmfContext
seekEvent(final double ratio
) {
243 if (fTrace
!= null) {
244 final ITmfLocation location
= new TmfLongLocation(Long
.valueOf(Math
.round(ratio
* fTrace
.length())));
245 final TmfContext context
= seekEvent(location
);
246 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
249 } catch (final IOException e
) {
259 public double getLocationRatio(ITmfLocation location
) {
262 if (fTrace
!= null) {
263 if (location
.getLocationInfo() instanceof Long
) {
264 return ((Long
) location
.getLocationInfo()).doubleValue() / fTrace
.length();
267 } catch (final IOException e
) {
276 public ITmfLocation
getCurrentLocation() {
279 if (fTrace
!= null) {
280 return new TmfLongLocation(fTrace
.getFilePointer());
282 } catch (final IOException e
) {
291 public ITmfEvent
parseEvent(final ITmfContext context
) {
294 // parseNextEvent will update the context
295 if (fTrace
!= null && getParser() != null && context
!= null) {
296 final ITmfEvent event
= getParser().parseEvent(context
);
306 public synchronized void setNbEvents(final long nbEvents
) {
307 super.setNbEvents(nbEvents
);
311 public void setTimeRange(final TmfTimeRange range
) {
312 super.setTimeRange(range
);
316 public void setStartTime(final ITmfTimestamp startTime
) {
317 super.setStartTime(startTime
);
321 public void setEndTime(final ITmfTimestamp endTime
) {
322 super.setEndTime(endTime
);
326 public void setStreamingInterval(final long interval
) {
327 super.setStreamingInterval(interval
);
331 public synchronized void dispose() {
334 if (fTrace
!= null) {
338 } catch (final IOException e
) {
347 public IStatus
validate(IProject project
, String path
) {
348 if (fileExists(path
)) {
349 return Status
.OK_STATUS
;
351 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, "File does not exist: " + path
);
354 private static int fCheckpointSize
= -1;
357 public synchronized int getCheckpointSize() {
358 if (fCheckpointSize
== -1) {
359 TmfCheckpoint c
= new TmfCheckpoint(new TmfTimestamp(0L), new TmfLongLocation(0L), 0);
360 ByteBuffer b
= ByteBuffer
.allocate(ITmfCheckpoint
.MAX_SERIALIZE_SIZE
);
363 fCheckpointSize
= b
.position();
366 return fCheckpointSize
;
370 public ITmfLocation
restoreLocation(ByteBuffer bufferIn
) {
371 return new TmfLongLocation(bufferIn
);
375 * Simulate trace opening, to be called by tests who need an actively opened
378 public void openTrace() {
379 TmfSignalManager
.dispatchSignal(new TmfTraceOpenedSignal(this, this, null));
384 * Simulate selecting the trace
386 public void selectTrace() {
387 TmfSignalManager
.dispatchSignal(new TmfTraceSelectedSignal(this, this));