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