1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 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 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
;
15 import java
.io
.FileNotFoundException
;
16 import java
.io
.IOException
;
17 import java
.io
.RandomAccessFile
;
18 import java
.util
.concurrent
.locks
.ReentrantLock
;
20 import org
.eclipse
.core
.resources
.IProject
;
21 import org
.eclipse
.core
.resources
.IResource
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfEventParser
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTraceIndexer
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLongLocation
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
36 * <b><u>TmfTraceStub</u></b>
38 * Dummy test trace. Use in conjunction with TmfEventParserStub.
40 @SuppressWarnings({"nls","javadoc"})
41 public class TmfTraceStub
extends TmfTrace
implements ITmfEventParser
{
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
48 private RandomAccessFile fTrace
;
50 // // The associated event parser
51 // private ITmfEventParser<TmfEvent> fParser;
53 // The synchronization lock
54 private final ReentrantLock fLock
= new ReentrantLock();
56 private ITmfTimestamp fInitialRangeOffset
= null;
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
62 public TmfTraceStub() {
64 setParser(new TmfEventParserStub(this));
69 * @throws FileNotFoundException
71 public TmfTraceStub(final String path
) throws TmfTraceException
{
72 this(path
, ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
, false);
78 * @throws FileNotFoundException
80 public TmfTraceStub(final String path
, final int cacheSize
) throws TmfTraceException
{
81 this(path
, cacheSize
, false);
87 * @throws FileNotFoundException
89 public TmfTraceStub(final String path
, final int cacheSize
, final long interval
) throws TmfTraceException
{
90 super(null, ITmfEvent
.class, path
, cacheSize
, interval
);
92 fTrace
= new RandomAccessFile(path
, "r");
93 } catch (FileNotFoundException e
) {
94 throw new TmfTraceException(e
.getMessage());
96 setParser(new TmfEventParserStub(this));
102 * @throws FileNotFoundException
104 public TmfTraceStub(final String path
, final int cacheSize
, final ITmfTraceIndexer indexer
) throws TmfTraceException
{
105 this(path
, cacheSize
, false, null, indexer
);
110 * @param waitForCompletion
111 * @throws FileNotFoundException
113 public TmfTraceStub(final String path
, final boolean waitForCompletion
) throws TmfTraceException
{
114 this(path
, ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
, waitForCompletion
);
120 * @param waitForCompletion
121 * @throws FileNotFoundException
123 public TmfTraceStub(final String path
, final int cacheSize
, final boolean waitForCompletion
) throws TmfTraceException
{
124 super(null, ITmfEvent
.class, path
, cacheSize
);
126 fTrace
= new RandomAccessFile(path
, "r");
127 } catch (FileNotFoundException e
) {
128 throw new TmfTraceException(e
.getMessage());
130 setParser(new TmfEventParserStub(this));
131 if (waitForCompletion
) {
139 * @param waitForCompletion
140 * @throws FileNotFoundException
142 public TmfTraceStub(final IResource resource
, final String path
, final int cacheSize
, final boolean waitForCompletion
) throws TmfTraceException
{
143 super(resource
, ITmfEvent
.class, path
, cacheSize
);
145 fTrace
= new RandomAccessFile(path
, "r");
146 } catch (FileNotFoundException e
) {
147 throw new TmfTraceException(e
.getMessage());
149 setParser(new TmfEventParserStub(this));
155 * @param waitForCompletion
157 * @throws FileNotFoundException
159 public TmfTraceStub(final String path
, final int cacheSize
, final boolean waitForCompletion
,
160 final ITmfEventParser parser
, final ITmfTraceIndexer indexer
) throws TmfTraceException
{
161 super(null, ITmfEvent
.class, path
, cacheSize
, 0, indexer
);
163 fTrace
= new RandomAccessFile(path
, "r");
164 } catch (FileNotFoundException e
) {
165 throw new TmfTraceException(e
.getMessage());
167 setParser((parser
!= null) ? parser
: new TmfEventParserStub(this));
173 public TmfTraceStub(final TmfTraceStub trace
) throws TmfTraceException
{
176 fTrace
= new RandomAccessFile(getPath(), "r");
177 } catch (FileNotFoundException e
) {
178 throw new TmfTraceException(e
.getMessage());
180 setParser(new TmfEventParserStub(this));
183 public void indexTrace() {
188 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
190 fTrace
= new RandomAccessFile(path
, "r");
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
.clone());
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 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
373 public boolean validate(IProject project
, String path
) {
374 return fileExists(path
);