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
.TmfEvent
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfEventParser
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTraceIndexer
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLongLocation
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
37 * <b><u>TmfTraceStub</u></b>
39 * Dummy test trace. Use in conjunction with TmfEventParserStub.
41 @SuppressWarnings({"nls","javadoc"})
42 public class TmfTraceStub
extends TmfTrace
implements ITmfEventParser
{
44 // ------------------------------------------------------------------------
46 // ------------------------------------------------------------------------
49 private RandomAccessFile fTrace
;
51 // // The associated event parser
52 // private ITmfEventParser<TmfEvent> fParser;
54 // The synchronization lock
55 private final ReentrantLock fLock
= new ReentrantLock();
57 private ITmfTimestamp fInitialRangeOffset
= null;
59 // ------------------------------------------------------------------------
61 // ------------------------------------------------------------------------
63 public TmfTraceStub() {
65 setParser(new TmfEventParserStub(this));
70 * @throws FileNotFoundException
72 public TmfTraceStub(final String path
) throws TmfTraceException
{
73 this(path
, ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
, false);
79 * @throws FileNotFoundException
81 public TmfTraceStub(final String path
, final int cacheSize
) throws TmfTraceException
{
82 this(path
, cacheSize
, false);
88 * @throws FileNotFoundException
90 public TmfTraceStub(final String path
, final int cacheSize
, final long interval
) throws TmfTraceException
{
91 super(null, TmfEvent
.class, path
, cacheSize
, interval
);
93 fTrace
= new RandomAccessFile(path
, "r");
94 } catch (FileNotFoundException e
) {
95 throw new TmfTraceException(e
.getMessage());
97 setParser(new TmfEventParserStub(this));
103 * @throws FileNotFoundException
105 public TmfTraceStub(final String path
, final int cacheSize
, final ITmfTraceIndexer indexer
) throws TmfTraceException
{
106 this(path
, cacheSize
, false, null, indexer
);
111 * @param waitForCompletion
112 * @throws FileNotFoundException
114 public TmfTraceStub(final String path
, final boolean waitForCompletion
) throws TmfTraceException
{
115 this(path
, ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
, waitForCompletion
);
121 * @param waitForCompletion
122 * @throws FileNotFoundException
124 public TmfTraceStub(final String path
, final int cacheSize
, final boolean waitForCompletion
) throws TmfTraceException
{
125 super(null, TmfEvent
.class, path
, cacheSize
);
127 fTrace
= new RandomAccessFile(path
, "r");
128 } catch (FileNotFoundException e
) {
129 throw new TmfTraceException(e
.getMessage());
131 setParser(new TmfEventParserStub(this));
132 if (waitForCompletion
) {
140 * @param waitForCompletion
141 * @throws FileNotFoundException
143 public TmfTraceStub(final IResource resource
, final String path
, final int cacheSize
, final boolean waitForCompletion
) throws TmfTraceException
{
144 super(resource
, TmfEvent
.class, path
, cacheSize
);
146 fTrace
= new RandomAccessFile(path
, "r");
147 } catch (FileNotFoundException e
) {
148 throw new TmfTraceException(e
.getMessage());
150 setParser(new TmfEventParserStub(this));
156 * @param waitForCompletion
158 * @throws FileNotFoundException
160 public TmfTraceStub(final String path
, final int cacheSize
, final boolean waitForCompletion
,
161 final ITmfEventParser parser
, final ITmfTraceIndexer indexer
) throws TmfTraceException
{
162 super(null, TmfEvent
.class, path
, cacheSize
, 0, indexer
);
164 fTrace
= new RandomAccessFile(path
, "r");
165 } catch (FileNotFoundException e
) {
166 throw new TmfTraceException(e
.getMessage());
168 setParser((parser
!= null) ? parser
: new TmfEventParserStub(this));
174 public TmfTraceStub(final TmfTraceStub trace
) throws TmfTraceException
{
177 fTrace
= new RandomAccessFile(getPath(), "r");
178 } catch (FileNotFoundException e
) {
179 throw new TmfTraceException(e
.getMessage());
181 setParser(new TmfEventParserStub(this));
184 public void indexTrace() {
189 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
191 fTrace
= new RandomAccessFile(path
, "r");
192 } catch (FileNotFoundException e
) {
193 throw new TmfTraceException(e
.getMessage());
195 setParser(new TmfEventParserStub(this));
196 super.initTrace(resource
, path
, type
);
200 public void initialize(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
201 super.initialize(resource
, path
, type
);
204 // ------------------------------------------------------------------------
206 // ------------------------------------------------------------------------
208 public RandomAccessFile
getStream() {
212 public void setInitialRangeOffset(ITmfTimestamp initOffset
) {
213 fInitialRangeOffset
= initOffset
;
217 public ITmfTimestamp
getInitialRangeOffset() {
218 if (fInitialRangeOffset
!= null) {
219 return fInitialRangeOffset
;
221 return super.getInitialRangeOffset();
224 // ------------------------------------------------------------------------
226 // ------------------------------------------------------------------------
229 public TmfContext
seekEvent(final ITmfLocation location
) {
233 if (fTrace
!= null) {
234 // Position the trace at the requested location and
235 // returns the corresponding context
238 if (location
!= null) {
239 loc
= (Long
) location
.getLocationInfo();
240 rank
= ITmfContext
.UNKNOWN_RANK
;
242 if (loc
!= fTrace
.getFilePointer()) {
245 final TmfContext context
= new TmfContext(getCurrentLocation(), rank
);
248 } catch (final IOException e
) {
250 } catch (final NullPointerException e
) {
256 } catch (final NullPointerException e
) {
264 public TmfContext
seekEvent(final double ratio
) {
267 if (fTrace
!= null) {
268 final ITmfLocation location
= new TmfLongLocation(Long
.valueOf(Math
.round(ratio
* fTrace
.length())));
269 final TmfContext context
= seekEvent(location
);
270 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
273 } catch (final IOException e
) {
283 public double getLocationRatio(ITmfLocation location
) {
286 if (fTrace
!= null) {
287 if (location
.getLocationInfo() instanceof Long
) {
288 return (double) ((Long
) location
.getLocationInfo()) / fTrace
.length();
291 } catch (final IOException e
) {
300 public ITmfLocation
getCurrentLocation() {
303 if (fTrace
!= null) {
304 return new TmfLongLocation(fTrace
.getFilePointer());
306 } catch (final IOException e
) {
315 public ITmfEvent
parseEvent(final ITmfContext context
) {
318 // parseNextEvent will update the context
319 if (fTrace
!= null && getParser() != null && context
!= null) {
320 final ITmfEvent event
= getParser().parseEvent(context
.clone());
330 public synchronized void setNbEvents(final long nbEvents
) {
331 super.setNbEvents(nbEvents
);
335 public void setTimeRange(final TmfTimeRange range
) {
336 super.setTimeRange(range
);
340 public void setStartTime(final ITmfTimestamp startTime
) {
341 super.setStartTime(startTime
);
345 public void setEndTime(final ITmfTimestamp endTime
) {
346 super.setEndTime(endTime
);
350 public void setStreamingInterval(final long interval
) {
351 super.setStreamingInterval(interval
);
355 public synchronized void dispose() {
358 if (fTrace
!= null) {
362 } catch (final IOException e
) {
371 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
374 public boolean validate(IProject project
, String path
) {
375 return fileExists(path
);