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