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