[Tmf][Ctf] Add descriptive fail to import messages.
[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();
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 public void indexTrace() {
188 indexTrace(true);
189 }
190
191 @Override
192 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException {
193 try {
194 fTrace = new RandomAccessFile(path, "r"); //$NON-NLS-1$
195 } catch (FileNotFoundException e) {
196 throw new TmfTraceException(e.getMessage());
197 }
198 setParser(new TmfEventParserStub(this));
199 super.initTrace(resource, path, type);
200 }
201
202 @Override
203 public void initialize(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException {
204 super.initialize(resource, path, type);
205 }
206
207 // ------------------------------------------------------------------------
208 // Accessors
209 // ------------------------------------------------------------------------
210
211 public RandomAccessFile getStream() {
212 return fTrace;
213 }
214
215 public void setInitialRangeOffset(ITmfTimestamp initOffset) {
216 fInitialRangeOffset = initOffset;
217 }
218
219 @Override
220 public ITmfTimestamp getInitialRangeOffset() {
221 if (fInitialRangeOffset != null) {
222 return fInitialRangeOffset;
223 }
224 return super.getInitialRangeOffset();
225 }
226
227 // ------------------------------------------------------------------------
228 // Operators
229 // ------------------------------------------------------------------------
230
231 @Override
232 public TmfContext seekEvent(final ITmfLocation location) {
233 try {
234 fLock.lock();
235 try {
236 if (fTrace != null) {
237 // Position the trace at the requested location and
238 // returns the corresponding context
239 long loc = 0;
240 long rank = 0;
241 if (location != null) {
242 loc = (Long) location.getLocationInfo();
243 rank = ITmfContext.UNKNOWN_RANK;
244 }
245 if (loc != fTrace.getFilePointer()) {
246 fTrace.seek(loc);
247 }
248 final TmfContext context = new TmfContext(getCurrentLocation(), rank);
249 return context;
250 }
251 } catch (final IOException e) {
252 e.printStackTrace();
253 } catch (final NullPointerException e) {
254 e.printStackTrace();
255 }
256 finally{
257 fLock.unlock();
258 }
259 } catch (final NullPointerException e) {
260 e.printStackTrace();
261 }
262 return null;
263 }
264
265
266 @Override
267 public TmfContext seekEvent(final double ratio) {
268 fLock.lock();
269 try {
270 if (fTrace != null) {
271 final ITmfLocation location = new TmfLongLocation(Long.valueOf(Math.round(ratio * fTrace.length())));
272 final TmfContext context = seekEvent(location);
273 context.setRank(ITmfContext.UNKNOWN_RANK);
274 return context;
275 }
276 } catch (final IOException e) {
277 e.printStackTrace();
278 } finally {
279 fLock.unlock();
280 }
281
282 return null;
283 }
284
285 @Override
286 public double getLocationRatio(ITmfLocation location) {
287 fLock.lock();
288 try {
289 if (fTrace != null) {
290 if (location.getLocationInfo() instanceof Long) {
291 return (double) ((Long) location.getLocationInfo()) / fTrace.length();
292 }
293 }
294 } catch (final IOException e) {
295 e.printStackTrace();
296 } finally {
297 fLock.unlock();
298 }
299 return 0;
300 }
301
302 @Override
303 public ITmfLocation getCurrentLocation() {
304 fLock.lock();
305 try {
306 if (fTrace != null) {
307 return new TmfLongLocation(fTrace.getFilePointer());
308 }
309 } catch (final IOException e) {
310 e.printStackTrace();
311 } finally {
312 fLock.unlock();
313 }
314 return null;
315 }
316
317 @Override
318 public ITmfEvent parseEvent(final ITmfContext context) {
319 fLock.lock();
320 try {
321 // parseNextEvent will update the context
322 if (fTrace != null && getParser() != null && context != null) {
323 final ITmfEvent event = getParser().parseEvent(context);
324 return event;
325 }
326 } finally {
327 fLock.unlock();
328 }
329 return null;
330 }
331
332 @Override
333 public synchronized void setNbEvents(final long nbEvents) {
334 super.setNbEvents(nbEvents);
335 }
336
337 @Override
338 public void setTimeRange(final TmfTimeRange range) {
339 super.setTimeRange(range);
340 }
341
342 @Override
343 public void setStartTime(final ITmfTimestamp startTime) {
344 super.setStartTime(startTime);
345 }
346
347 @Override
348 public void setEndTime(final ITmfTimestamp endTime) {
349 super.setEndTime(endTime);
350 }
351
352 @Override
353 public void setStreamingInterval(final long interval) {
354 super.setStreamingInterval(interval);
355 }
356
357 @Override
358 public synchronized void dispose() {
359 fLock.lock();
360 try {
361 if (fTrace != null) {
362 fTrace.close();
363 fTrace = null;
364 }
365 } catch (final IOException e) {
366 // Ignore
367 } finally {
368 fLock.unlock();
369 }
370 super.dispose();
371 }
372
373 /* (non-Javadoc)
374 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
375 */
376 @Override
377 public IStatus validate(IProject project, String path) {
378 if (fileExists(path)) {
379 return Status.OK_STATUS;
380 }
381 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TmfTraceStub_FileNotFound + ": " + path); //$NON-NLS-1$
382 }
383
384 }
This page took 0.040071 seconds and 5 git commands to generate.