Commit | Line | Data |
---|---|---|
d18dd09b | 1 | /******************************************************************************* |
e31e01e8 | 2 | * Copyright (c) 2009, 2010 Ericsson |
0283f7ff | 3 | * |
d18dd09b ASL |
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 | |
0283f7ff | 8 | * |
d18dd09b ASL |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
4918b8f2 | 13 | package org.eclipse.linuxtools.tmf.tests.stubs.trace; |
d18dd09b ASL |
14 | |
15 | import java.io.FileNotFoundException; | |
16 | import java.io.IOException; | |
17 | import java.io.RandomAccessFile; | |
73005152 | 18 | import java.util.concurrent.locks.ReentrantLock; |
d18dd09b | 19 | |
2352aed9 | 20 | import org.eclipse.core.resources.IProject; |
20658947 | 21 | import org.eclipse.core.resources.IResource; |
6256d8ad | 22 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
dfee01ae | 23 | import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; |
6c13869b FC |
24 | import org.eclipse.linuxtools.tmf.core.event.TmfEvent; |
25 | import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange; | |
b4f71e4a | 26 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; |
6c13869b | 27 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
7e6347b0 | 28 | import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser; |
6c13869b | 29 | import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; |
0316808c | 30 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
20658947 | 31 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer; |
6c13869b | 32 | import org.eclipse.linuxtools.tmf.core.trace.TmfContext; |
cb8c854e | 33 | import org.eclipse.linuxtools.tmf.core.trace.TmfLongLocation; |
6c13869b | 34 | import org.eclipse.linuxtools.tmf.core.trace.TmfTrace; |
d18dd09b ASL |
35 | |
36 | /** | |
37 | * <b><u>TmfTraceStub</u></b> | |
38 | * <p> | |
8d2e2848 | 39 | * Dummy test trace. Use in conjunction with TmfEventParserStub. |
d18dd09b | 40 | */ |
54a7a54c | 41 | @SuppressWarnings({"nls","javadoc"}) |
6256d8ad | 42 | public class TmfTraceStub extends TmfTrace implements ITmfEventParser { |
d18dd09b | 43 | |
e31e01e8 | 44 | // ------------------------------------------------------------------------ |
d18dd09b | 45 | // Attributes |
e31e01e8 | 46 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
47 | |
48 | // The actual stream | |
ff4ed569 | 49 | private RandomAccessFile fTrace; |
d18dd09b | 50 | |
7e6347b0 FC |
51 | // // The associated event parser |
52 | // private ITmfEventParser<TmfEvent> fParser; | |
d18dd09b | 53 | |
73005152 | 54 | // The synchronization lock |
085d898f FC |
55 | private final ReentrantLock fLock = new ReentrantLock(); |
56 | ||
e31e01e8 | 57 | // ------------------------------------------------------------------------ |
d18dd09b | 58 | // Constructors |
e31e01e8 | 59 | // ------------------------------------------------------------------------ |
d18dd09b | 60 | |
20658947 FC |
61 | public TmfTraceStub() { |
62 | super(); | |
0316808c | 63 | setParser(new TmfEventParserStub(this)); |
20658947 FC |
64 | } |
65 | ||
d18dd09b | 66 | /** |
3ef62bac | 67 | * @param path |
d18dd09b ASL |
68 | * @throws FileNotFoundException |
69 | */ | |
b4f71e4a | 70 | public TmfTraceStub(final String path) throws TmfTraceException { |
0316808c | 71 | this(path, ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, false); |
8d2e2848 FC |
72 | } |
73 | ||
74 | /** | |
3ef62bac | 75 | * @param path |
e31e01e8 | 76 | * @param cacheSize |
8d2e2848 FC |
77 | * @throws FileNotFoundException |
78 | */ | |
b4f71e4a | 79 | public TmfTraceStub(final String path, final int cacheSize) throws TmfTraceException { |
3ef62bac | 80 | this(path, cacheSize, false); |
d18dd09b ASL |
81 | } |
82 | ||
1703b536 FC |
83 | /** |
84 | * @param path | |
85 | * @param cacheSize | |
86 | * @throws FileNotFoundException | |
87 | */ | |
b4f71e4a | 88 | public TmfTraceStub(final String path, final int cacheSize, final long interval) throws TmfTraceException { |
1703b536 | 89 | super(null, TmfEvent.class, path, cacheSize, interval); |
b4f71e4a FC |
90 | try { |
91 | fTrace = new RandomAccessFile(path, "r"); | |
92 | } catch (FileNotFoundException e) { | |
93 | throw new TmfTraceException(e.getMessage()); | |
94 | } | |
0316808c | 95 | setParser(new TmfEventParserStub(this)); |
1703b536 FC |
96 | } |
97 | ||
20658947 FC |
98 | /** |
99 | * @param path | |
100 | * @param cacheSize | |
101 | * @throws FileNotFoundException | |
102 | */ | |
6256d8ad | 103 | public TmfTraceStub(final String path, final int cacheSize, final ITmfTraceIndexer indexer) throws TmfTraceException { |
20658947 FC |
104 | this(path, cacheSize, false, null, indexer); |
105 | } | |
106 | ||
d18dd09b | 107 | /** |
3ef62bac | 108 | * @param path |
ff4ed569 | 109 | * @param waitForCompletion |
d18dd09b ASL |
110 | * @throws FileNotFoundException |
111 | */ | |
b4f71e4a | 112 | public TmfTraceStub(final String path, final boolean waitForCompletion) throws TmfTraceException { |
0316808c | 113 | this(path, ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, waitForCompletion); |
8d2e2848 | 114 | } |
085d898f | 115 | |
8d2e2848 | 116 | /** |
3ef62bac | 117 | * @param path |
8d2e2848 | 118 | * @param cacheSize |
ff4ed569 | 119 | * @param waitForCompletion |
8d2e2848 FC |
120 | * @throws FileNotFoundException |
121 | */ | |
b4f71e4a | 122 | public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion) throws TmfTraceException { |
09e86496 | 123 | super(null, TmfEvent.class, path, cacheSize); |
b4f71e4a FC |
124 | try { |
125 | fTrace = new RandomAccessFile(path, "r"); | |
126 | } catch (FileNotFoundException e) { | |
127 | throw new TmfTraceException(e.getMessage()); | |
128 | } | |
0316808c | 129 | setParser(new TmfEventParserStub(this)); |
07671572 FC |
130 | if (waitForCompletion) { |
131 | indexTrace(); | |
132 | } | |
d18dd09b ASL |
133 | } |
134 | ||
20658947 FC |
135 | /** |
136 | * @param path | |
137 | * @param cacheSize | |
138 | * @param waitForCompletion | |
139 | * @throws FileNotFoundException | |
140 | */ | |
b4f71e4a | 141 | public TmfTraceStub(final IResource resource, final String path, final int cacheSize, final boolean waitForCompletion) throws TmfTraceException { |
20658947 | 142 | super(resource, TmfEvent.class, path, cacheSize); |
b4f71e4a FC |
143 | try { |
144 | fTrace = new RandomAccessFile(path, "r"); | |
145 | } catch (FileNotFoundException e) { | |
146 | throw new TmfTraceException(e.getMessage()); | |
147 | } | |
0316808c | 148 | setParser(new TmfEventParserStub(this)); |
20658947 | 149 | } |
085d898f | 150 | |
73005152 | 151 | /** |
3ef62bac | 152 | * @param path |
73005152 BH |
153 | * @param cacheSize |
154 | * @param waitForCompletion | |
155 | * @param parser | |
156 | * @throws FileNotFoundException | |
157 | */ | |
20658947 | 158 | public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion, |
6256d8ad | 159 | final ITmfEventParser parser, final ITmfTraceIndexer indexer) throws TmfTraceException { |
20658947 | 160 | super(null, TmfEvent.class, path, cacheSize, 0, indexer); |
b4f71e4a FC |
161 | try { |
162 | fTrace = new RandomAccessFile(path, "r"); | |
163 | } catch (FileNotFoundException e) { | |
164 | throw new TmfTraceException(e.getMessage()); | |
165 | } | |
0316808c | 166 | setParser((parser != null) ? parser : new TmfEventParserStub(this)); |
73005152 | 167 | } |
085d898f | 168 | |
ff4ed569 | 169 | /** |
f17b2f70 | 170 | * Copy constructor |
ff4ed569 | 171 | */ |
b4f71e4a | 172 | public TmfTraceStub(final TmfTraceStub trace) throws TmfTraceException { |
20658947 | 173 | super(trace); |
b4f71e4a FC |
174 | try { |
175 | fTrace = new RandomAccessFile(getPath(), "r"); | |
176 | } catch (FileNotFoundException e) { | |
177 | throw new TmfTraceException(e.getMessage()); | |
178 | } | |
0316808c | 179 | setParser(new TmfEventParserStub(this)); |
ff4ed569 | 180 | } |
085d898f | 181 | |
20658947 | 182 | public void indexTrace() { |
c7e1020d | 183 | indexTrace(true); |
20658947 FC |
184 | } |
185 | ||
186 | @Override | |
6256d8ad | 187 | public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException { |
b4f71e4a FC |
188 | try { |
189 | fTrace = new RandomAccessFile(path, "r"); | |
190 | } catch (FileNotFoundException e) { | |
191 | throw new TmfTraceException(e.getMessage()); | |
192 | } | |
0316808c | 193 | setParser(new TmfEventParserStub(this)); |
20658947 FC |
194 | super.initTrace(resource, path, type); |
195 | } | |
f17b2f70 | 196 | |
1703b536 | 197 | @Override |
6256d8ad | 198 | public void initialize(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException { |
1703b536 FC |
199 | super.initialize(resource, path, type); |
200 | } | |
201 | ||
e31e01e8 | 202 | // ------------------------------------------------------------------------ |
d18dd09b | 203 | // Accessors |
e31e01e8 | 204 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
205 | |
206 | public RandomAccessFile getStream() { | |
207 | return fTrace; | |
208 | } | |
209 | ||
e31e01e8 | 210 | // ------------------------------------------------------------------------ |
d18dd09b | 211 | // Operators |
e31e01e8 | 212 | // ------------------------------------------------------------------------ |
d18dd09b | 213 | |
085d898f FC |
214 | @Override |
215 | @SuppressWarnings("unchecked") | |
7e6347b0 | 216 | public TmfContext seekEvent(final ITmfLocation<?> location) { |
d18dd09b | 217 | try { |
09e86496 FC |
218 | fLock.lock(); |
219 | try { | |
220 | if (fTrace != null) { | |
221 | // Position the trace at the requested location and | |
222 | // returns the corresponding context | |
223 | long loc = 0; | |
224 | long rank = 0; | |
225 | if (location != null) { | |
cb8c854e | 226 | loc = ((ITmfLocation<Long>) location).getLocationData(); |
09e86496 FC |
227 | rank = ITmfContext.UNKNOWN_RANK; |
228 | } | |
20658947 | 229 | if (loc != fTrace.getFilePointer()) { |
09e86496 | 230 | fTrace.seek(loc); |
20658947 | 231 | } |
09e86496 FC |
232 | final TmfContext context = new TmfContext(getCurrentLocation(), rank); |
233 | return context; | |
73005152 | 234 | } |
09e86496 FC |
235 | } catch (final IOException e) { |
236 | e.printStackTrace(); | |
237 | } catch (final NullPointerException e) { | |
238 | e.printStackTrace(); | |
73005152 | 239 | } |
3427112b FC |
240 | finally{ |
241 | fLock.unlock(); | |
242 | } | |
09e86496 | 243 | } catch (final NullPointerException e) { |
085d898f FC |
244 | e.printStackTrace(); |
245 | } | |
085d898f | 246 | return null; |
d18dd09b ASL |
247 | } |
248 | ||
c76c54bb | 249 | |
085d898f | 250 | @Override |
7e6347b0 | 251 | public TmfContext seekEvent(final double ratio) { |
085d898f | 252 | fLock.lock(); |
c76c54bb | 253 | try { |
73005152 | 254 | if (fTrace != null) { |
cb8c854e | 255 | final ITmfLocation<?> location = new TmfLongLocation(Long.valueOf((long) (ratio * fTrace.length()))); |
7e6347b0 | 256 | final TmfContext context = seekEvent(location); |
73005152 BH |
257 | context.setRank(ITmfContext.UNKNOWN_RANK); |
258 | return context; | |
259 | } | |
085d898f | 260 | } catch (final IOException e) { |
c76c54bb | 261 | e.printStackTrace(); |
73005152 BH |
262 | } finally { |
263 | fLock.unlock(); | |
c76c54bb | 264 | } |
085d898f | 265 | |
c76c54bb FC |
266 | return null; |
267 | } | |
268 | ||
269 | @Override | |
2352aed9 | 270 | public double getLocationRatio(ITmfLocation<?> location) { |
73005152 | 271 | fLock.lock(); |
c76c54bb | 272 | try { |
0283f7ff | 273 | if (fTrace != null) { |
6bab4511 AM |
274 | if (location.getLocationData() instanceof Long) { |
275 | return (double) ((Long) location.getLocationData()) / fTrace.length(); | |
0283f7ff FC |
276 | } |
277 | } | |
085d898f | 278 | } catch (final IOException e) { |
c76c54bb | 279 | e.printStackTrace(); |
73005152 BH |
280 | } finally { |
281 | fLock.unlock(); | |
c76c54bb FC |
282 | } |
283 | return 0; | |
284 | } | |
285 | ||
4e3aa37d | 286 | @Override |
cb8c854e | 287 | public ITmfLocation<Long> getCurrentLocation() { |
73005152 | 288 | fLock.lock(); |
d18dd09b | 289 | try { |
0283f7ff | 290 | if (fTrace != null) { |
cb8c854e | 291 | return new TmfLongLocation(fTrace.getFilePointer()); |
0283f7ff | 292 | } |
085d898f FC |
293 | } catch (final IOException e) { |
294 | e.printStackTrace(); | |
295 | } finally { | |
296 | fLock.unlock(); | |
297 | } | |
298 | return null; | |
299 | } | |
300 | ||
301 | @Override | |
6256d8ad | 302 | public ITmfEvent parseEvent(final ITmfContext context) { |
085d898f FC |
303 | fLock.lock(); |
304 | try { | |
305 | // parseNextEvent will update the context | |
0316808c | 306 | if (fTrace != null && getParser() != null && context != null) { |
6256d8ad | 307 | final ITmfEvent event = getParser().parseEvent(context.clone()); |
085d898f | 308 | return event; |
73005152 | 309 | } |
73005152 BH |
310 | } finally { |
311 | fLock.unlock(); | |
d18dd09b ASL |
312 | } |
313 | return null; | |
314 | } | |
315 | ||
b75d6b65 | 316 | @Override |
9b749023 | 317 | public synchronized void setNbEvents(final long nbEvents) { |
b75d6b65 FC |
318 | super.setNbEvents(nbEvents); |
319 | } | |
320 | ||
085d898f FC |
321 | @Override |
322 | public void setTimeRange(final TmfTimeRange range) { | |
323 | super.setTimeRange(range); | |
324 | } | |
d18dd09b | 325 | |
085d898f FC |
326 | @Override |
327 | public void setStartTime(final ITmfTimestamp startTime) { | |
328 | super.setStartTime(startTime); | |
ff4ed569 FC |
329 | } |
330 | ||
085d898f FC |
331 | @Override |
332 | public void setEndTime(final ITmfTimestamp endTime) { | |
333 | super.setEndTime(endTime); | |
ff4ed569 FC |
334 | } |
335 | ||
1703b536 FC |
336 | @Override |
337 | public void setStreamingInterval(final long interval) { | |
338 | super.setStreamingInterval(interval); | |
339 | } | |
340 | ||
085d898f | 341 | @Override |
9b749023 | 342 | public synchronized void dispose() { |
085d898f FC |
343 | fLock.lock(); |
344 | try { | |
345 | if (fTrace != null) { | |
346 | fTrace.close(); | |
347 | fTrace = null; | |
348 | } | |
349 | } catch (final IOException e) { | |
350 | // Ignore | |
351 | } finally { | |
352 | fLock.unlock(); | |
353 | } | |
354 | super.dispose(); | |
ff4ed569 FC |
355 | } |
356 | ||
2352aed9 FC |
357 | /* (non-Javadoc) |
358 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String) | |
359 | */ | |
360 | @Override | |
361 | public boolean validate(IProject project, String path) { | |
362 | return fileExists(path); | |
363 | } | |
364 | ||
9b749023 | 365 | } |