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
.TmfLocation
;
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 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
61 public TmfTraceStub() {
63 setParser(new TmfEventParserStub(this));
68 * @throws FileNotFoundException
70 public TmfTraceStub(final String path
) throws TmfTraceException
{
71 this(path
, ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
, false);
77 * @throws FileNotFoundException
79 public TmfTraceStub(final String path
, final int cacheSize
) throws TmfTraceException
{
80 this(path
, cacheSize
, false);
86 * @throws FileNotFoundException
88 public TmfTraceStub(final String path
, final int cacheSize
, final long interval
) throws TmfTraceException
{
89 super(null, TmfEvent
.class, path
, cacheSize
, interval
);
91 fTrace
= new RandomAccessFile(path
, "r");
92 } catch (FileNotFoundException e
) {
93 throw new TmfTraceException(e
.getMessage());
95 setParser(new TmfEventParserStub(this));
101 * @throws FileNotFoundException
103 public TmfTraceStub(final String path
, final int cacheSize
, final ITmfTraceIndexer indexer
) throws TmfTraceException
{
104 this(path
, cacheSize
, false, null, indexer
);
109 * @param waitForCompletion
110 * @throws FileNotFoundException
112 public TmfTraceStub(final String path
, final boolean waitForCompletion
) throws TmfTraceException
{
113 this(path
, ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
, waitForCompletion
);
119 * @param waitForCompletion
120 * @throws FileNotFoundException
122 public TmfTraceStub(final String path
, final int cacheSize
, final boolean waitForCompletion
) throws TmfTraceException
{
123 super(null, TmfEvent
.class, path
, cacheSize
);
125 fTrace
= new RandomAccessFile(path
, "r");
126 } catch (FileNotFoundException e
) {
127 throw new TmfTraceException(e
.getMessage());
129 setParser(new TmfEventParserStub(this));
130 if (waitForCompletion
) {
138 * @param waitForCompletion
139 * @throws FileNotFoundException
141 public TmfTraceStub(final IResource resource
, final String path
, final int cacheSize
, final boolean waitForCompletion
) throws TmfTraceException
{
142 super(resource
, TmfEvent
.class, path
, cacheSize
);
144 fTrace
= new RandomAccessFile(path
, "r");
145 } catch (FileNotFoundException e
) {
146 throw new TmfTraceException(e
.getMessage());
148 setParser(new TmfEventParserStub(this));
154 * @param waitForCompletion
156 * @throws FileNotFoundException
158 public TmfTraceStub(final String path
, final int cacheSize
, final boolean waitForCompletion
,
159 final ITmfEventParser 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 setParser((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 setParser(new TmfEventParserStub(this));
182 public void indexTrace() {
187 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
189 fTrace
= new RandomAccessFile(path
, "r");
190 } catch (FileNotFoundException e
) {
191 throw new TmfTraceException(e
.getMessage());
193 setParser(new TmfEventParserStub(this));
194 super.initTrace(resource
, path
, type
);
198 public void initialize(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> 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
) {
243 } 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 public double getLocationRatio(ITmfLocation
<?
> location
) {
273 if (fTrace
!= null) {
274 if (location
.getLocation() instanceof Long
) {
275 return (double) ((Long
) location
.getLocation()) / fTrace
.length();
278 } catch (final IOException e
) {
287 public TmfLocation
<Long
> getCurrentLocation() {
290 if (fTrace
!= null) {
291 return new TmfLocation
<Long
>(fTrace
.getFilePointer());
293 } catch (final IOException e
) {
302 public ITmfEvent
parseEvent(final ITmfContext context
) {
305 // parseNextEvent will update the context
306 if (fTrace
!= null && getParser() != null && context
!= null) {
307 final ITmfEvent event
= getParser().parseEvent(context
.clone());
317 public synchronized void setNbEvents(final long nbEvents
) {
318 super.setNbEvents(nbEvents
);
322 public void setTimeRange(final TmfTimeRange range
) {
323 super.setTimeRange(range
);
327 public void setStartTime(final ITmfTimestamp startTime
) {
328 super.setStartTime(startTime
);
332 public void setEndTime(final ITmfTimestamp endTime
) {
333 super.setEndTime(endTime
);
337 public void setStreamingInterval(final long interval
) {
338 super.setStreamingInterval(interval
);
342 public synchronized void dispose() {
345 if (fTrace
!= null) {
349 } catch (final IOException e
) {
358 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
361 public boolean validate(IProject project
, String path
) {
362 return fileExists(path
);
This page took 0.038826 seconds and 6 git commands to generate.