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
.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
.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
.TmfLocation
;
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")
41 public class TmfTraceStub
extends TmfTrace
<TmfEvent
> implements ITmfEventParser
<TmfEvent
> {
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 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
62 * @throws FileNotFoundException
64 public TmfTraceStub() {
66 setParser(new TmfEventParserStub(this));
71 * @throws FileNotFoundException
73 public TmfTraceStub(final String path
) throws TmfTraceException
{
74 this(path
, ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
, false);
80 * @throws FileNotFoundException
82 public TmfTraceStub(final String path
, final int cacheSize
) throws TmfTraceException
{
83 this(path
, cacheSize
, false);
89 * @throws FileNotFoundException
91 public TmfTraceStub(final String path
, final int cacheSize
, final long interval
) throws TmfTraceException
{
92 super(null, TmfEvent
.class, path
, cacheSize
, interval
);
94 fTrace
= new RandomAccessFile(path
, "r");
95 } catch (FileNotFoundException e
) {
96 throw new TmfTraceException(e
.getMessage());
98 setParser(new TmfEventParserStub(this));
104 * @throws FileNotFoundException
106 public TmfTraceStub(final String path
, final int cacheSize
, final ITmfTraceIndexer
<?
> indexer
) throws TmfTraceException
{
107 this(path
, cacheSize
, false, null, indexer
);
112 * @param waitForCompletion
113 * @throws FileNotFoundException
115 public TmfTraceStub(final String path
, final boolean waitForCompletion
) throws TmfTraceException
{
116 this(path
, ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
, waitForCompletion
);
122 * @param waitForCompletion
123 * @throws FileNotFoundException
125 public TmfTraceStub(final String path
, final int cacheSize
, final boolean waitForCompletion
) throws TmfTraceException
{
126 super(null, TmfEvent
.class, path
, cacheSize
);
128 fTrace
= new RandomAccessFile(path
, "r");
129 } catch (FileNotFoundException e
) {
130 throw new TmfTraceException(e
.getMessage());
132 setParser(new TmfEventParserStub(this));
133 if (waitForCompletion
) {
141 * @param waitForCompletion
142 * @throws FileNotFoundException
144 public TmfTraceStub(final IResource resource
, final String path
, final int cacheSize
, final boolean waitForCompletion
) throws TmfTraceException
{
145 super(resource
, TmfEvent
.class, path
, cacheSize
);
147 fTrace
= new RandomAccessFile(path
, "r");
148 } catch (FileNotFoundException e
) {
149 throw new TmfTraceException(e
.getMessage());
151 setParser(new TmfEventParserStub(this));
157 * @param waitForCompletion
159 * @throws FileNotFoundException
161 public TmfTraceStub(final String path
, final int cacheSize
, final boolean waitForCompletion
,
162 final ITmfEventParser
<TmfEvent
> parser
, final ITmfTraceIndexer
<?
> indexer
) throws TmfTraceException
{
163 super(null, TmfEvent
.class, path
, cacheSize
, 0, indexer
);
165 fTrace
= new RandomAccessFile(path
, "r");
166 } catch (FileNotFoundException e
) {
167 throw new TmfTraceException(e
.getMessage());
169 setParser((parser
!= null) ? parser
: new TmfEventParserStub(this));
175 public TmfTraceStub(final TmfTraceStub trace
) throws TmfTraceException
{
178 fTrace
= new RandomAccessFile(getPath(), "r");
179 } catch (FileNotFoundException e
) {
180 throw new TmfTraceException(e
.getMessage());
182 setParser(new TmfEventParserStub(this));
185 public void indexTrace() {
190 public void initTrace(final IResource resource
, final String path
, final Class
<TmfEvent
> type
) throws TmfTraceException
{
192 fTrace
= new RandomAccessFile(path
, "r");
193 } catch (FileNotFoundException e
) {
194 throw new TmfTraceException(e
.getMessage());
196 setParser(new TmfEventParserStub(this));
197 super.initTrace(resource
, path
, type
);
201 public void initialize(final IResource resource
, final String path
, final Class
<TmfEvent
> type
) throws TmfTraceException
{
202 super.initialize(resource
, path
, type
);
205 // ------------------------------------------------------------------------
207 // ------------------------------------------------------------------------
209 public RandomAccessFile
getStream() {
213 // ------------------------------------------------------------------------
215 // ------------------------------------------------------------------------
218 @SuppressWarnings("unchecked")
219 public TmfContext
seekEvent(final ITmfLocation
<?
> location
) {
223 if (fTrace
!= null) {
224 // Position the trace at the requested location and
225 // returns the corresponding context
228 if (location
!= null) {
229 loc
= ((TmfLocation
<Long
>) location
).getLocation();
230 rank
= ITmfContext
.UNKNOWN_RANK
;
232 if (loc
!= fTrace
.getFilePointer()) {
235 final TmfContext context
= new TmfContext(getCurrentLocation(), rank
);
240 } catch (final IOException e
) {
242 } catch (final NullPointerException e
) {
248 } catch (final NullPointerException e
) {
256 public TmfContext
seekEvent(final double ratio
) {
259 if (fTrace
!= null) {
260 final ITmfLocation
<?
> location
= new TmfLocation
<Long
>(Long
.valueOf((long) (ratio
* fTrace
.length())));
261 final TmfContext context
= seekEvent(location
);
262 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
265 } catch (final IOException e
) {
275 public double getLocationRatio(ITmfLocation
<?
> location
) {
279 if (location
.getLocation() instanceof Long
)
280 return (double) ((Long
) location
.getLocation()) / fTrace
.length();
281 } catch (final IOException e
) {
290 public TmfLocation
<Long
> getCurrentLocation() {
294 return new TmfLocation
<Long
>(fTrace
.getFilePointer());
295 } catch (final IOException e
) {
304 public TmfEvent
parseEvent(final ITmfContext context
) {
307 // parseNextEvent will update the context
308 if (fTrace
!= null && getParser() != null && context
!= null) {
309 final TmfEvent event
= getParser().parseEvent(context
.clone());
319 public void setNbEvents(final long nbEvents
) {
320 super.setNbEvents(nbEvents
);
324 public void setTimeRange(final TmfTimeRange range
) {
325 super.setTimeRange(range
);
329 public void setStartTime(final ITmfTimestamp startTime
) {
330 super.setStartTime(startTime
);
334 public void setEndTime(final ITmfTimestamp endTime
) {
335 super.setEndTime(endTime
);
339 public void setStreamingInterval(final long interval
) {
340 super.setStreamingInterval(interval
);
344 public void dispose() {
347 if (fTrace
!= null) {
351 } catch (final IOException e
) {
360 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
363 public boolean validate(IProject project
, String path
) {
364 return fileExists(path
);
This page took 0.048474 seconds and 6 git commands to generate.