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