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
);
238 } catch (final IOException e
) {
240 } catch (final NullPointerException e
) {
243 } catch (final NullPointerException e
) {
254 public TmfContext
seekEvent(final double ratio
) {
257 if (fTrace
!= null) {
258 final ITmfLocation
<?
> location
= new TmfLocation
<Long
>(Long
.valueOf((long) (ratio
* fTrace
.length())));
259 final TmfContext context
= seekEvent(location
);
260 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
263 } catch (final IOException e
) {
273 public double getLocationRatio(ITmfLocation
<?
> location
) {
277 if (location
.getLocation() instanceof Long
)
278 return (double) ((Long
) location
.getLocation()) / fTrace
.length();
279 } catch (final IOException e
) {
288 public TmfLocation
<Long
> getCurrentLocation() {
292 return new TmfLocation
<Long
>(fTrace
.getFilePointer());
293 } catch (final IOException e
) {
302 public TmfEvent
parseEvent(final ITmfContext context
) {
305 // parseNextEvent will update the context
306 if (fTrace
!= null && getParser() != null && context
!= null) {
307 final TmfEvent event
= getParser().parseEvent(context
.clone());
317 public void setTimeRange(final TmfTimeRange range
) {
318 super.setTimeRange(range
);
322 public void setStartTime(final ITmfTimestamp startTime
) {
323 super.setStartTime(startTime
);
327 public void setEndTime(final ITmfTimestamp endTime
) {
328 super.setEndTime(endTime
);
332 public void setStreamingInterval(final long interval
) {
333 super.setStreamingInterval(interval
);
337 public void dispose() {
340 if (fTrace
!= null) {
344 } catch (final IOException e
) {
353 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
356 public boolean validate(IProject project
, String path
) {
357 return fileExists(path
);
This page took 0.03826 seconds and 5 git commands to generate.