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