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