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
.IResource
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
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
.ITmfTraceIndexer
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLocation
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
35 * <b><u>TmfTraceStub</u></b>
37 * Dummy test trace. Use in conjunction with TmfEventParserStub.
39 @SuppressWarnings("nls")
40 public class TmfTraceStub
extends TmfTrace
<TmfEvent
> implements ITmfEventParser
<TmfEvent
> {
42 // ------------------------------------------------------------------------
44 // ------------------------------------------------------------------------
47 private RandomAccessFile fTrace
;
49 // // The associated event parser
50 // private ITmfEventParser<TmfEvent> fParser;
52 // The synchronization lock
53 private final ReentrantLock fLock
= new ReentrantLock();
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
61 * @throws FileNotFoundException
63 public TmfTraceStub() {
65 fParser
= new TmfEventParserStub(this);
70 * @throws FileNotFoundException
72 public TmfTraceStub(final String path
) throws TmfTraceException
{
73 this(path
, 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 fParser
= 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
, 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 fParser
= new TmfEventParserStub(this);
137 * @param waitForCompletion
138 * @throws FileNotFoundException
140 public TmfTraceStub(final IResource resource
, final String path
, final int cacheSize
, final boolean waitForCompletion
) throws TmfTraceException
{
141 super(resource
, TmfEvent
.class, path
, cacheSize
);
143 fTrace
= new RandomAccessFile(path
, "r");
144 } catch (FileNotFoundException e
) {
145 throw new TmfTraceException(e
.getMessage());
147 fParser
= new TmfEventParserStub(this);
153 * @param waitForCompletion
155 * @throws FileNotFoundException
157 @SuppressWarnings("unchecked")
158 public TmfTraceStub(final String path
, final int cacheSize
, final boolean waitForCompletion
,
159 final ITmfEventParser
<TmfEvent
> parser
, final ITmfTraceIndexer
<?
> indexer
) throws TmfTraceException
{
160 super(null, TmfEvent
.class, path
, cacheSize
, 0, indexer
);
162 fTrace
= new RandomAccessFile(path
, "r");
163 } catch (FileNotFoundException e
) {
164 throw new TmfTraceException(e
.getMessage());
166 fParser
= (ITmfEventParser
<ITmfEvent
>) ((parser
!= null) ? parser
: new TmfEventParserStub(this));
172 public TmfTraceStub(final TmfTraceStub trace
) throws TmfTraceException
{
175 fTrace
= new RandomAccessFile(getPath(), "r");
176 } catch (FileNotFoundException e
) {
177 throw new TmfTraceException(e
.getMessage());
179 fParser
= new TmfEventParserStub(this);
182 public void indexTrace() {
183 fIndexer
.buildIndex(true);
187 public void initTrace(final IResource resource
, final String path
, final Class
<TmfEvent
> type
) throws TmfTraceException
{
189 fTrace
= new RandomAccessFile(path
, "r");
190 } catch (FileNotFoundException e
) {
191 throw new TmfTraceException(e
.getMessage());
193 fParser
= new TmfEventParserStub(this);
194 super.initTrace(resource
, path
, type
);
198 public void initialize(final IResource resource
, final String path
, final Class
<TmfEvent
> type
) throws TmfTraceException
{
199 super.initialize(resource
, path
, type
);
202 // ------------------------------------------------------------------------
204 // ------------------------------------------------------------------------
206 public RandomAccessFile
getStream() {
210 // ------------------------------------------------------------------------
212 // ------------------------------------------------------------------------
215 @SuppressWarnings("unchecked")
216 public TmfContext
seekEvent(final ITmfLocation
<?
> location
) {
220 if (fTrace
!= null) {
221 // Position the trace at the requested location and
222 // returns the corresponding context
225 if (location
!= null) {
226 loc
= ((TmfLocation
<Long
>) location
).getLocation();
227 rank
= ITmfContext
.UNKNOWN_RANK
;
229 if (loc
!= fTrace
.getFilePointer()) {
232 final TmfContext context
= new TmfContext(getCurrentLocation(), rank
);
235 } catch (final IOException e
) {
237 } catch (final NullPointerException e
) {
240 } catch (final NullPointerException e
) {
251 public TmfContext
seekEvent(final double ratio
) {
254 if (fTrace
!= null) {
255 final ITmfLocation
<?
> location
= new TmfLocation
<Long
>(Long
.valueOf((long) (ratio
* fTrace
.length())));
256 final TmfContext context
= seekEvent(location
);
257 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
260 } catch (final IOException e
) {
270 @SuppressWarnings("rawtypes")
271 public double getLocationRatio(final ITmfLocation location
) {
275 if (location
.getLocation() instanceof Long
)
276 return (double) ((Long
) location
.getLocation()) / fTrace
.length();
277 } catch (final IOException e
) {
286 public TmfLocation
<Long
> getCurrentLocation() {
290 return new TmfLocation
<Long
>(fTrace
.getFilePointer());
291 } catch (final IOException e
) {
300 public ITmfEvent
parseEvent(final ITmfContext context
) {
303 // parseNextEvent will update the context
304 if (fTrace
!= null) {
305 final ITmfEvent event
= fParser
.parseEvent(context
.clone());
309 // catch (final IOException e) {
310 // e.printStackTrace();
318 public void setTimeRange(final TmfTimeRange range
) {
319 super.setTimeRange(range
);
323 public void setStartTime(final ITmfTimestamp startTime
) {
324 super.setStartTime(startTime
);
328 public void setEndTime(final ITmfTimestamp endTime
) {
329 super.setEndTime(endTime
);
333 public void setStreamingInterval(final long interval
) {
334 super.setStreamingInterval(interval
);
338 public void dispose() {
341 if (fTrace
!= null) {
345 } catch (final IOException e
) {
This page took 0.041665 seconds and 5 git commands to generate.