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
.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
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.parser
.ITmfEventParser
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLocation
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
33 * <b><u>TmfTraceStub</u></b>
35 * Dummy test trace. Use in conjunction with TmfEventParserStub.
37 @SuppressWarnings("nls")
38 public class TmfTraceStub
extends TmfTrace
<TmfEvent
> {
40 // ------------------------------------------------------------------------
42 // ------------------------------------------------------------------------
45 private RandomAccessFile fTrace
;
47 // The associated event parser
48 private ITmfEventParser
<TmfEvent
> fParser
;
50 // The synchronization lock
51 private ReentrantLock fLock
= new ReentrantLock();
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
59 * @throws FileNotFoundException
61 public TmfTraceStub(String filename
) throws FileNotFoundException
{
62 this(filename
, DEFAULT_INDEX_PAGE_SIZE
, false);
68 * @throws FileNotFoundException
70 public TmfTraceStub(String filename
, int cacheSize
) throws FileNotFoundException
{
71 this(filename
, cacheSize
, false);
76 * @param waitForCompletion
77 * @throws FileNotFoundException
79 public TmfTraceStub(String filename
, boolean waitForCompletion
) throws FileNotFoundException
{
80 this(filename
, DEFAULT_INDEX_PAGE_SIZE
, waitForCompletion
);
86 * @param waitForCompletion
87 * @throws FileNotFoundException
89 public TmfTraceStub(String filename
, int cacheSize
, boolean waitForCompletion
) throws FileNotFoundException
{
90 super(null, TmfEvent
.class, filename
, cacheSize
, false);
91 fTrace
= new RandomAccessFile(filename
, "r");
92 fParser
= new TmfEventParserStub();
99 * @param waitForCompletion
101 * @throws FileNotFoundException
103 public TmfTraceStub(String filename
, int cacheSize
, boolean waitForCompletion
, ITmfEventParser
<TmfEvent
> parser
) throws FileNotFoundException
{
104 super(filename
, TmfEvent
.class, filename
, cacheSize
, false);
105 fTrace
= new RandomAccessFile(filename
, "r");
112 public TmfTraceStub
clone() {
113 TmfTraceStub clone
= null;
115 clone
= (TmfTraceStub
) super.clone();
116 clone
.fTrace
= new RandomAccessFile(getPath(), "r");
117 clone
.fParser
= new TmfEventParserStub();
118 } catch (CloneNotSupportedException e
) {
119 } catch (FileNotFoundException e
) {
125 public ITmfTrace
<TmfEvent
> copy() {
126 ITmfTrace
<TmfEvent
> returnedValue
= null;
127 returnedValue
= clone();
128 return returnedValue
;
131 // ------------------------------------------------------------------------
133 // ------------------------------------------------------------------------
135 public RandomAccessFile
getStream() {
139 // ------------------------------------------------------------------------
141 // ------------------------------------------------------------------------
144 @SuppressWarnings("unchecked")
145 public TmfContext
seekLocation(ITmfLocation
<?
> location
) {
148 if (fTrace
!= null) {
149 // Position the trace at the requested location and
150 // returns the corresponding context
153 if (location
!= null) {
154 loc
= ((TmfLocation
<Long
>) location
).getLocation();
155 rank
= ITmfContext
.UNKNOWN_RANK
;
157 if (loc
!= fTrace
.getFilePointer()) {
160 TmfContext context
= new TmfContext(getCurrentLocation(), rank
);
163 } catch (IOException e
) {
174 public TmfContext
seekLocation(double ratio
) {
177 if (fTrace
!= null) {
178 ITmfLocation
<?
> location
= new TmfLocation
<Long
>(Long
.valueOf((long) (ratio
* fTrace
.length())));
179 TmfContext context
= seekLocation(location
);
180 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
183 } catch (IOException e
) {
193 @SuppressWarnings("rawtypes")
194 public double getLocationRatio(ITmfLocation location
) {
197 if (fTrace
!= null) {
198 if (location
.getLocation() instanceof Long
) {
199 return (double) ((Long
) location
.getLocation()) / fTrace
.length();
202 } catch (IOException e
) {
211 public TmfLocation
<Long
> getCurrentLocation() {
214 if (fTrace
!= null) {
215 return new TmfLocation
<Long
>(fTrace
.getFilePointer());
217 } catch (IOException e
) {
226 public ITmfEvent
parseEvent(TmfContext context
) {
229 // parseNextEvent will update the context
230 if (fTrace
!= null) {
231 ITmfEvent event
= fParser
.parseNextEvent(this, context
.clone());
235 catch (IOException e
) {
244 public void setTimeRange(TmfTimeRange range
) {
245 super.setTimeRange(range
);
249 public void setStartTime(ITmfTimestamp startTime
) {
250 super.setStartTime(startTime
);
254 public void setEndTime(ITmfTimestamp endTime
) {
255 super.setEndTime(endTime
);
259 public void dispose() {
262 if (fTrace
!= null) {
266 } catch (IOException e
) {