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));
187 public void indexTrace() {
192 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
194 fTrace
= new RandomAccessFile(path
, "r"); //$NON-NLS-1$
195 } catch (FileNotFoundException e
) {
196 throw new TmfTraceException(e
.getMessage());
198 setParser(new TmfEventParserStub(this));
199 super.initTrace(resource
, path
, type
);
203 public void initialize(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
204 super.initialize(resource
, path
, type
);
207 // ------------------------------------------------------------------------
209 // ------------------------------------------------------------------------
211 public RandomAccessFile
getStream() {
215 public void setInitialRangeOffset(ITmfTimestamp initOffset
) {
216 fInitialRangeOffset
= initOffset
;
220 public ITmfTimestamp
getInitialRangeOffset() {
221 if (fInitialRangeOffset
!= null) {
222 return fInitialRangeOffset
;
224 return super.getInitialRangeOffset();
227 // ------------------------------------------------------------------------
229 // ------------------------------------------------------------------------
232 public TmfContext
seekEvent(final ITmfLocation location
) {
236 if (fTrace
!= null) {
237 // Position the trace at the requested location and
238 // returns the corresponding context
241 if (location
!= null) {
242 loc
= (Long
) location
.getLocationInfo();
243 rank
= ITmfContext
.UNKNOWN_RANK
;
245 if (loc
!= fTrace
.getFilePointer()) {
248 final TmfContext context
= new TmfContext(getCurrentLocation(), rank
);
251 } catch (final IOException e
) {
253 } catch (final NullPointerException e
) {
259 } catch (final NullPointerException e
) {
267 public TmfContext
seekEvent(final double ratio
) {
270 if (fTrace
!= null) {
271 final ITmfLocation location
= new TmfLongLocation(Long
.valueOf(Math
.round(ratio
* fTrace
.length())));
272 final TmfContext context
= seekEvent(location
);
273 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
276 } catch (final IOException e
) {
286 public double getLocationRatio(ITmfLocation location
) {
289 if (fTrace
!= null) {
290 if (location
.getLocationInfo() instanceof Long
) {
291 return (double) ((Long
) location
.getLocationInfo()) / fTrace
.length();
294 } catch (final IOException e
) {
303 public ITmfLocation
getCurrentLocation() {
306 if (fTrace
!= null) {
307 return new TmfLongLocation(fTrace
.getFilePointer());
309 } catch (final IOException e
) {
318 public ITmfEvent
parseEvent(final ITmfContext context
) {
321 // parseNextEvent will update the context
322 if (fTrace
!= null && getParser() != null && context
!= null) {
323 final ITmfEvent event
= getParser().parseEvent(context
);
333 public synchronized void setNbEvents(final long nbEvents
) {
334 super.setNbEvents(nbEvents
);
338 public void setTimeRange(final TmfTimeRange range
) {
339 super.setTimeRange(range
);
343 public void setStartTime(final ITmfTimestamp startTime
) {
344 super.setStartTime(startTime
);
348 public void setEndTime(final ITmfTimestamp endTime
) {
349 super.setEndTime(endTime
);
353 public void setStreamingInterval(final long interval
) {
354 super.setStreamingInterval(interval
);
358 public synchronized void dispose() {
361 if (fTrace
!= null) {
365 } catch (final IOException e
) {
374 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
377 public IStatus
validate(IProject project
, String path
) {
378 if (fileExists(path
)) {
379 return Status
.OK_STATUS
;
381 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.TmfTraceStub_FileNotFound
+ ": " + path
); //$NON-NLS-1$