Contribution for Bug352466: [TMF] Implement UML2 Sequence Diagram
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.tests / stubs / org / eclipse / linuxtools / tmf / trace / TmfTraceStub.java
CommitLineData
d18dd09b 1/*******************************************************************************
e31e01e8 2 * Copyright (c) 2009, 2010 Ericsson
d18dd09b
ASL
3 *
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
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.tmf.trace;
14
15import java.io.FileNotFoundException;
16import java.io.IOException;
17import java.io.RandomAccessFile;
73005152 18import java.util.concurrent.locks.ReentrantLock;
d18dd09b
ASL
19
20import org.eclipse.linuxtools.tmf.event.TmfEvent;
ff4ed569
FC
21import org.eclipse.linuxtools.tmf.event.TmfTimeRange;
22import org.eclipse.linuxtools.tmf.event.TmfTimestamp;
e31e01e8 23import org.eclipse.linuxtools.tmf.parser.ITmfEventParser;
d18dd09b
ASL
24
25/**
26 * <b><u>TmfTraceStub</u></b>
27 * <p>
8d2e2848 28 * Dummy test trace. Use in conjunction with TmfEventParserStub.
d18dd09b 29 */
3b38ea61 30@SuppressWarnings("nls")
e31e01e8 31public class TmfTraceStub extends TmfTrace<TmfEvent> {
d18dd09b 32
e31e01e8 33 // ------------------------------------------------------------------------
d18dd09b 34 // Attributes
e31e01e8 35 // ------------------------------------------------------------------------
d18dd09b
ASL
36
37 // The actual stream
ff4ed569 38 private RandomAccessFile fTrace;
d18dd09b
ASL
39
40 // The associated event parser
ff4ed569 41 private ITmfEventParser fParser;
d18dd09b 42
73005152
BH
43 // The synchronization lock
44 private ReentrantLock fLock = new ReentrantLock();
45
e31e01e8 46 // ------------------------------------------------------------------------
d18dd09b 47 // Constructors
e31e01e8 48 // ------------------------------------------------------------------------
d18dd09b
ASL
49
50 /**
51 * @param filename
52 * @throws FileNotFoundException
53 */
54 public TmfTraceStub(String filename) throws FileNotFoundException {
664902f7 55 this(filename, DEFAULT_INDEX_PAGE_SIZE, false);
8d2e2848
FC
56 }
57
58 /**
59 * @param filename
e31e01e8 60 * @param cacheSize
8d2e2848
FC
61 * @throws FileNotFoundException
62 */
e31e01e8
FC
63 public TmfTraceStub(String filename, int cacheSize) throws FileNotFoundException {
64 this(filename, cacheSize, false);
d18dd09b
ASL
65 }
66
67 /**
68 * @param filename
ff4ed569 69 * @param waitForCompletion
d18dd09b
ASL
70 * @throws FileNotFoundException
71 */
e31e01e8 72 public TmfTraceStub(String filename, boolean waitForCompletion) throws FileNotFoundException {
664902f7 73 this(filename, DEFAULT_INDEX_PAGE_SIZE, waitForCompletion);
8d2e2848 74 }
fe79470e 75
8d2e2848
FC
76 /**
77 * @param filename
78 * @param cacheSize
ff4ed569 79 * @param waitForCompletion
8d2e2848
FC
80 * @throws FileNotFoundException
81 */
82 public TmfTraceStub(String filename, int cacheSize, boolean waitForCompletion) throws FileNotFoundException {
8016d660 83 super(filename, TmfEvent.class, filename, cacheSize, false);
d18dd09b
ASL
84 fTrace = new RandomAccessFile(filename, "r");
85 fParser = new TmfEventParserStub();
86 }
87
73005152
BH
88
89 /**
90 * @param filename
91 * @param cacheSize
92 * @param waitForCompletion
93 * @param parser
94 * @throws FileNotFoundException
95 */
96 public TmfTraceStub(String filename, int cacheSize, boolean waitForCompletion, ITmfEventParser parser) throws FileNotFoundException {
97 super(filename, TmfEvent.class, filename, cacheSize, false);
98 fTrace = new RandomAccessFile(filename, "r");
99 fParser = parser;
100 }
101
ff4ed569
FC
102 /**
103 */
104 @Override
105 public TmfTraceStub clone() {
106 TmfTraceStub clone = null;
107 try {
108 clone = (TmfTraceStub) super.clone();
cb866e08 109 clone.fTrace = new RandomAccessFile(getPath(), "r");
ff4ed569
FC
110 clone.fParser = new TmfEventParserStub();
111 } catch (CloneNotSupportedException e) {
112 } catch (FileNotFoundException e) {
113 }
114 return clone;
115 }
116
d4011df2
FC
117 @Override
118 public ITmfTrace createTraceCopy() {
ff4ed569 119 ITmfTrace returnedValue = null;
cb866e08 120 returnedValue = clone();
ff4ed569
FC
121 return returnedValue;
122 }
123
e31e01e8 124 // ------------------------------------------------------------------------
d18dd09b 125 // Accessors
e31e01e8 126 // ------------------------------------------------------------------------
d18dd09b
ASL
127
128 public RandomAccessFile getStream() {
129 return fTrace;
130 }
131
e31e01e8 132 // ------------------------------------------------------------------------
d18dd09b 133 // Operators
e31e01e8 134 // ------------------------------------------------------------------------
d18dd09b 135
452ad365 136 @Override
9f584e4c 137 @SuppressWarnings("unchecked")
452ad365 138 public TmfContext seekLocation(ITmfLocation<?> location) {
73005152 139 fLock.lock();
d18dd09b 140 try {
73005152
BH
141 if (fTrace != null) {
142 // Position the trace at the requested location and
143 // returns the corresponding context
144 long loc = 0;
145 long rank = 0;
146 if (location != null) {
147 loc = ((TmfLocation<Long>) location).getLocation();
148 rank = ITmfContext.UNKNOWN_RANK;
149 }
150 if (loc != fTrace.getFilePointer()) {
151 fTrace.seek(loc);
152 }
153 TmfContext context = new TmfContext(getCurrentLocation(), rank);
154 return context;
155 }
d18dd09b
ASL
156 } catch (IOException e) {
157 e.printStackTrace();
158 }
73005152
BH
159 finally{
160 fLock.unlock();
161 }
e31e01e8 162 return null;
d18dd09b
ASL
163 }
164
c76c54bb
FC
165
166 @Override
167 public TmfContext seekLocation(double ratio) {
73005152 168 fLock.lock();
c76c54bb 169 try {
73005152
BH
170 if (fTrace != null) {
171 ITmfLocation<?> location = new TmfLocation<Long>(new Long((long) (ratio * fTrace.length())));
172 TmfContext context = seekLocation(location);
173 context.setRank(ITmfContext.UNKNOWN_RANK);
174 return context;
175 }
c76c54bb
FC
176 } catch (IOException e) {
177 e.printStackTrace();
73005152
BH
178 } finally {
179 fLock.unlock();
c76c54bb 180 }
73005152 181
c76c54bb
FC
182 return null;
183 }
184
185 @Override
186 public double getLocationRatio(ITmfLocation<?> location) {
73005152 187 fLock.lock();
c76c54bb 188 try {
73005152
BH
189 if (fTrace != null) {
190 if (location.getLocation() instanceof Long) {
191 return (double) ((Long) location.getLocation()) / fTrace.length();
192 }
c76c54bb
FC
193 }
194 } catch (IOException e) {
195 e.printStackTrace();
73005152
BH
196 } finally {
197 fLock.unlock();
c76c54bb
FC
198 }
199 return 0;
200 }
201
4e3aa37d 202 @Override
9f584e4c 203 public TmfLocation<Long> getCurrentLocation() {
73005152 204 fLock.lock();
d18dd09b 205 try {
73005152
BH
206 if (fTrace != null) {
207 return new TmfLocation<Long>(fTrace.getFilePointer());
208 }
d18dd09b
ASL
209 } catch (IOException e) {
210 e.printStackTrace();
73005152
BH
211 } finally {
212 fLock.unlock();
d18dd09b
ASL
213 }
214 return null;
215 }
216
4e3aa37d 217 @Override
9f584e4c 218 public TmfEvent parseEvent(TmfContext context) {
73005152 219 fLock.lock();
cc6eec3e 220 try {
cb866e08 221 // parseNextEvent will update the context
73005152
BH
222 if (fTrace != null) {
223 TmfEvent event = fParser.parseNextEvent(this, context.clone());
224 return event;
225 }
cc6eec3e
FC
226 }
227 catch (IOException e) {
228 e.printStackTrace();
73005152
BH
229 } finally {
230 fLock.unlock();
cc6eec3e
FC
231 }
232 return null;
d18dd09b
ASL
233 }
234
ff4ed569
FC
235 @Override
236 public void setTimeRange(TmfTimeRange range) {
237 super.setTimeRange(range);
238 }
239
240 @Override
241 public void setStartTime(TmfTimestamp startTime) {
242 super.setStartTime(startTime);
243 }
244
245 @Override
246 public void setEndTime(TmfTimestamp endTime) {
247 super.setEndTime(endTime);
248 }
73005152
BH
249
250 @Override
251 public void dispose() {
252 fLock.lock();
253 try {
254 if (fTrace != null) {
255 fTrace.close();
256 fTrace = null;
257 }
258 } catch (IOException e) {
259 // Ignore
260 } finally {
261 fLock.unlock();
262 }
263 super.dispose();
264 }
ff4ed569 265
e31e01e8 266}
This page took 0.042376 seconds and 5 git commands to generate.