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