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