1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 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
.util
.concurrent
.locks
.ReentrantLock
;
21 import org
.eclipse
.core
.resources
.IProject
;
22 import org
.eclipse
.core
.resources
.IResource
;
23 import org
.eclipse
.core
.runtime
.IStatus
;
24 import org
.eclipse
.core
.runtime
.Status
;
25 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfEventParser
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTraceIndexer
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLongLocation
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
40 * <b><u>TmfTraceStub</u></b>
42 * Dummy test trace. Use in conjunction with TmfEventParserStub.
44 @SuppressWarnings("javadoc")
45 public class TmfTraceStub
extends TmfTrace
implements ITmfEventParser
{
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
52 private RandomAccessFile fTrace
;
54 // // The associated event parser
55 // private ITmfEventParser<TmfEvent> fParser;
57 // The synchronization lock
58 private final ReentrantLock fLock
= new ReentrantLock();
60 private ITmfTimestamp fInitialRangeOffset
= null;
62 // ------------------------------------------------------------------------
64 // ------------------------------------------------------------------------
66 public TmfTraceStub() {
68 setParser(new TmfEventParserStub(this));
73 * @throws FileNotFoundException
75 public TmfTraceStub(final String path
) throws TmfTraceException
{
76 this(path
, ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
, false);
82 * @throws FileNotFoundException
84 public TmfTraceStub(final String path
, final int cacheSize
) throws TmfTraceException
{
85 this(path
, cacheSize
, false);
91 * @throws FileNotFoundException
93 public TmfTraceStub(final String path
, final int cacheSize
, final long interval
) throws TmfTraceException
{
94 super(null, ITmfEvent
.class, path
, cacheSize
, interval
, null, null);
96 fTrace
= new RandomAccessFile(path
, "r"); //$NON-NLS-1$
97 } catch (FileNotFoundException e
) {
98 throw new TmfTraceException(e
.getMessage());
100 setParser(new TmfEventParserStub(this));
106 * @throws FileNotFoundException
108 public TmfTraceStub(final String path
, final int cacheSize
, final ITmfTraceIndexer indexer
) throws TmfTraceException
{
109 this(path
, cacheSize
, false, null, indexer
);
114 * @param waitForCompletion
115 * @throws FileNotFoundException
117 public TmfTraceStub(final String path
, final boolean waitForCompletion
) throws TmfTraceException
{
118 this(path
, ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
, waitForCompletion
);
124 * @param waitForCompletion
125 * @throws FileNotFoundException
127 public TmfTraceStub(final String path
, final int cacheSize
, final boolean waitForCompletion
) throws TmfTraceException
{
128 super(null, ITmfEvent
.class, path
, cacheSize
, 0, null, null);
130 fTrace
= new RandomAccessFile(path
, "r"); //$NON-NLS-1$
131 } catch (FileNotFoundException e
) {
132 throw new TmfTraceException(e
.getMessage());
134 setParser(new TmfEventParserStub(this));
135 if (waitForCompletion
) {
143 * @param waitForCompletion
144 * @throws FileNotFoundException
146 public TmfTraceStub(final IResource resource
, final String path
, final int cacheSize
, final boolean waitForCompletion
) throws TmfTraceException
{
147 super(resource
, ITmfEvent
.class, path
, cacheSize
, 0, null, null);
149 fTrace
= new RandomAccessFile(path
, "r"); //$NON-NLS-1$
150 } catch (FileNotFoundException e
) {
151 throw new TmfTraceException(e
.getMessage());
153 setParser(new TmfEventParserStub(this));
159 * @param waitForCompletion
161 * @throws FileNotFoundException
163 public TmfTraceStub(final String path
, final int cacheSize
, final boolean waitForCompletion
,
164 final ITmfEventParser parser
, final ITmfTraceIndexer indexer
) throws TmfTraceException
{
165 super(null, ITmfEvent
.class, path
, cacheSize
, 0, indexer
, null);
167 fTrace
= new RandomAccessFile(path
, "r"); //$NON-NLS-1$
168 } catch (FileNotFoundException e
) {
169 throw new TmfTraceException(e
.getMessage());
171 setParser((parser
!= null) ? parser
: new TmfEventParserStub(this));
177 public TmfTraceStub(final TmfTraceStub trace
) throws TmfTraceException
{
180 fTrace
= new RandomAccessFile(getPath(), "r"); //$NON-NLS-1$
181 } catch (FileNotFoundException e
) {
182 throw new TmfTraceException(e
.getMessage());
184 setParser(new TmfEventParserStub(this));
188 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
190 fTrace
= new RandomAccessFile(path
, "r"); //$NON-NLS-1$
191 } catch (FileNotFoundException e
) {
192 throw new TmfTraceException(e
.getMessage());
194 setParser(new TmfEventParserStub(this));
195 super.initTrace(resource
, path
, type
);
199 public void initialize(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
200 super.initialize(resource
, path
, type
);
203 // ------------------------------------------------------------------------
205 // ------------------------------------------------------------------------
207 public RandomAccessFile
getStream() {
211 public void setInitialRangeOffset(ITmfTimestamp initOffset
) {
212 fInitialRangeOffset
= initOffset
;
216 public ITmfTimestamp
getInitialRangeOffset() {
217 if (fInitialRangeOffset
!= null) {
218 return fInitialRangeOffset
;
220 return super.getInitialRangeOffset();
223 // ------------------------------------------------------------------------
225 // ------------------------------------------------------------------------
228 public TmfContext
seekEvent(final ITmfLocation location
) {
232 if (fTrace
!= null) {
233 // Position the trace at the requested location and
234 // returns the corresponding context
237 if (location
!= null) {
238 loc
= (Long
) location
.getLocationInfo();
239 rank
= ITmfContext
.UNKNOWN_RANK
;
241 if (loc
!= fTrace
.getFilePointer()) {
244 final TmfContext context
= new TmfContext(getCurrentLocation(), rank
);
247 } catch (final IOException e
) {
249 } catch (final NullPointerException e
) {
255 } catch (final NullPointerException e
) {
263 public TmfContext
seekEvent(final double ratio
) {
266 if (fTrace
!= null) {
267 final ITmfLocation location
= new TmfLongLocation(Long
.valueOf(Math
.round(ratio
* fTrace
.length())));
268 final TmfContext context
= seekEvent(location
);
269 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
272 } catch (final IOException e
) {
282 public double getLocationRatio(ITmfLocation location
) {
285 if (fTrace
!= null) {
286 if (location
.getLocationInfo() instanceof Long
) {
287 return (double) ((Long
) location
.getLocationInfo()) / fTrace
.length();
290 } catch (final IOException e
) {
299 public ITmfLocation
getCurrentLocation() {
302 if (fTrace
!= null) {
303 return new TmfLongLocation(fTrace
.getFilePointer());
305 } catch (final IOException e
) {
314 public ITmfEvent
parseEvent(final ITmfContext context
) {
317 // parseNextEvent will update the context
318 if (fTrace
!= null && getParser() != null && context
!= null) {
319 final ITmfEvent event
= getParser().parseEvent(context
);
329 public synchronized void setNbEvents(final long nbEvents
) {
330 super.setNbEvents(nbEvents
);
334 public void setTimeRange(final TmfTimeRange range
) {
335 super.setTimeRange(range
);
339 public void setStartTime(final ITmfTimestamp startTime
) {
340 super.setStartTime(startTime
);
344 public void setEndTime(final ITmfTimestamp endTime
) {
345 super.setEndTime(endTime
);
349 public void setStreamingInterval(final long interval
) {
350 super.setStreamingInterval(interval
);
354 public synchronized void dispose() {
357 if (fTrace
!= null) {
361 } catch (final IOException e
) {
370 public IStatus
validate(IProject project
, String path
) {
371 if (fileExists(path
)) {
372 return Status
.OK_STATUS
;
374 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, "File does not exist: " + path
);