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 // ------------------------------------------------------------------------
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 public TmfContext
seekEvent(final ITmfLocation location
) {
219 if (fTrace
!= null) {
220 // Position the trace at the requested location and
221 // returns the corresponding context
224 if (location
!= null) {
225 loc
= (Long
) location
.getLocationInfo();
226 rank
= ITmfContext
.UNKNOWN_RANK
;
228 if (loc
!= fTrace
.getFilePointer()) {
231 final TmfContext context
= new TmfContext(getCurrentLocation(), rank
);
234 } catch (final IOException e
) {
236 } catch (final NullPointerException e
) {
242 } catch (final NullPointerException e
) {
250 public TmfContext
seekEvent(final double ratio
) {
253 if (fTrace
!= null) {
254 final ITmfLocation location
= new TmfLongLocation(Long
.valueOf((long) (ratio
* fTrace
.length())));
255 final TmfContext context
= seekEvent(location
);
256 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
259 } catch (final IOException e
) {
269 public double getLocationRatio(ITmfLocation location
) {
272 if (fTrace
!= null) {
273 if (location
.getLocationInfo() instanceof Long
) {
274 return (double) ((Long
) location
.getLocationInfo()) / fTrace
.length();
277 } catch (final IOException e
) {
286 public ITmfLocation
getCurrentLocation() {
289 if (fTrace
!= null) {
290 return new TmfLongLocation(fTrace
.getFilePointer());
292 } catch (final IOException e
) {
301 public ITmfEvent
parseEvent(final ITmfContext context
) {
304 // parseNextEvent will update the context
305 if (fTrace
!= null && getParser() != null && context
!= null) {
306 final ITmfEvent event
= getParser().parseEvent(context
.clone());
316 public synchronized void setNbEvents(final long nbEvents
) {
317 super.setNbEvents(nbEvents
);
321 public void setTimeRange(final TmfTimeRange range
) {
322 super.setTimeRange(range
);
326 public void setStartTime(final ITmfTimestamp startTime
) {
327 super.setStartTime(startTime
);
331 public void setEndTime(final ITmfTimestamp endTime
) {
332 super.setEndTime(endTime
);
336 public void setStreamingInterval(final long interval
) {
337 super.setStreamingInterval(interval
);
341 public synchronized void dispose() {
344 if (fTrace
!= null) {
348 } catch (final IOException e
) {
357 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
360 public boolean validate(IProject project
, String path
) {
361 return fileExists(path
);
This page took 0.039077 seconds and 6 git commands to generate.