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
.TmfEvent
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.parser
.ITmfEventParser
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLocation
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
32 * <b><u>TmfTraceStub</u></b>
34 * Dummy test trace. Use in conjunction with TmfEventParserStub.
36 @SuppressWarnings("nls")
37 public class TmfTraceStub
extends TmfTrace
<TmfEvent
> {
39 // ------------------------------------------------------------------------
41 // ------------------------------------------------------------------------
44 private RandomAccessFile fTrace
;
46 // The associated event parser
47 private ITmfEventParser fParser
;
49 // The synchronization lock
50 private ReentrantLock fLock
= new ReentrantLock();
52 // ------------------------------------------------------------------------
54 // ------------------------------------------------------------------------
58 * @throws FileNotFoundException
60 public TmfTraceStub(String filename
) throws FileNotFoundException
{
61 this(filename
, DEFAULT_INDEX_PAGE_SIZE
, false);
67 * @throws FileNotFoundException
69 public TmfTraceStub(String filename
, int cacheSize
) throws FileNotFoundException
{
70 this(filename
, cacheSize
, false);
75 * @param waitForCompletion
76 * @throws FileNotFoundException
78 public TmfTraceStub(String filename
, boolean waitForCompletion
) throws FileNotFoundException
{
79 this(filename
, DEFAULT_INDEX_PAGE_SIZE
, waitForCompletion
);
85 * @param waitForCompletion
86 * @throws FileNotFoundException
88 public TmfTraceStub(String filename
, int cacheSize
, boolean waitForCompletion
) throws FileNotFoundException
{
89 super(null, TmfEvent
.class, filename
, cacheSize
, false);
90 fTrace
= new RandomAccessFile(filename
, "r");
91 fParser
= new TmfEventParserStub();
98 * @param waitForCompletion
100 * @throws FileNotFoundException
102 public TmfTraceStub(String filename
, int cacheSize
, boolean waitForCompletion
, ITmfEventParser parser
) throws FileNotFoundException
{
103 super(filename
, TmfEvent
.class, filename
, cacheSize
, false);
104 fTrace
= new RandomAccessFile(filename
, "r");
111 public TmfTraceStub
clone() {
112 TmfTraceStub clone
= null;
114 clone
= (TmfTraceStub
) super.clone();
115 clone
.fTrace
= new RandomAccessFile(getPath(), "r");
116 clone
.fParser
= new TmfEventParserStub();
117 } catch (CloneNotSupportedException e
) {
118 } catch (FileNotFoundException e
) {
124 public ITmfTrace
copy() {
125 ITmfTrace returnedValue
= null;
126 returnedValue
= clone();
127 return returnedValue
;
130 // ------------------------------------------------------------------------
132 // ------------------------------------------------------------------------
134 public RandomAccessFile
getStream() {
138 // ------------------------------------------------------------------------
140 // ------------------------------------------------------------------------
143 @SuppressWarnings("unchecked")
144 public TmfContext
seekLocation(ITmfLocation location
) {
147 if (fTrace
!= null) {
148 // Position the trace at the requested location and
149 // returns the corresponding context
152 if (location
!= null) {
153 loc
= ((TmfLocation
<Long
>) location
).getLocation();
154 rank
= ITmfContext
.UNKNOWN_RANK
;
156 if (loc
!= fTrace
.getFilePointer()) {
159 TmfContext context
= new TmfContext(getCurrentLocation(), rank
);
162 } catch (IOException e
) {
173 public TmfContext
seekLocation(double ratio
) {
176 if (fTrace
!= null) {
177 ITmfLocation
<?
> location
= new TmfLocation
<Long
>(new Long((long) (ratio
* fTrace
.length())));
178 TmfContext context
= seekLocation(location
);
179 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
182 } catch (IOException e
) {
192 @SuppressWarnings("rawtypes")
193 public double getLocationRatio(ITmfLocation location
) {
196 if (fTrace
!= null) {
197 if (location
.getLocation() instanceof Long
) {
198 return (double) ((Long
) location
.getLocation()) / fTrace
.length();
201 } catch (IOException e
) {
210 public TmfLocation
<Long
> getCurrentLocation() {
213 if (fTrace
!= null) {
214 return new TmfLocation
<Long
>(fTrace
.getFilePointer());
216 } catch (IOException e
) {
225 public TmfEvent
parseEvent(TmfContext context
) {
228 // parseNextEvent will update the context
229 if (fTrace
!= null) {
230 TmfEvent event
= fParser
.parseNextEvent(this, context
.clone());
234 catch (IOException e
) {
243 public void setTimeRange(TmfTimeRange range
) {
244 super.setTimeRange(range
);
248 public void setStartTime(TmfTimestamp startTime
) {
249 super.setStartTime(startTime
);
253 public void setEndTime(TmfTimestamp endTime
) {
254 super.setEndTime(endTime
);
258 public void dispose() {
261 if (fTrace
!= null) {
265 } catch (IOException e
) {