Refactor 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 }
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<TmfEvent> 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 getIndexer().buildIndex(true);
184 }
185
186 @Override
187 public void initTrace(final IResource resource, final String path, final Class<TmfEvent> 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<TmfEvent> 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 } catch (final NullPointerException e) {
241 e.printStackTrace();
242 }
243 finally{
244 fLock.unlock();
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 } catch (final IOException e) {
277 e.printStackTrace();
278 } finally {
279 fLock.unlock();
280 }
281 return 0;
282 }
283
284 @Override
285 public TmfLocation<Long> getCurrentLocation() {
286 fLock.lock();
287 try {
288 if (fTrace != null)
289 return new TmfLocation<Long>(fTrace.getFilePointer());
290 } catch (final IOException e) {
291 e.printStackTrace();
292 } finally {
293 fLock.unlock();
294 }
295 return null;
296 }
297
298 @Override
299 public TmfEvent parseEvent(final ITmfContext context) {
300 fLock.lock();
301 try {
302 // parseNextEvent will update the context
303 if (fTrace != null && getParser() != null && context != null) {
304 final TmfEvent event = getParser().parseEvent(context.clone());
305 return event;
306 }
307 } finally {
308 fLock.unlock();
309 }
310 return null;
311 }
312
313 @Override
314 public void setTimeRange(final TmfTimeRange range) {
315 super.setTimeRange(range);
316 }
317
318 @Override
319 public void setStartTime(final ITmfTimestamp startTime) {
320 super.setStartTime(startTime);
321 }
322
323 @Override
324 public void setEndTime(final ITmfTimestamp endTime) {
325 super.setEndTime(endTime);
326 }
327
328 @Override
329 public void setStreamingInterval(final long interval) {
330 super.setStreamingInterval(interval);
331 }
332
333 @Override
334 public void dispose() {
335 fLock.lock();
336 try {
337 if (fTrace != null) {
338 fTrace.close();
339 fTrace = null;
340 }
341 } catch (final IOException e) {
342 // Ignore
343 } finally {
344 fLock.unlock();
345 }
346 super.dispose();
347 }
348
349 /* (non-Javadoc)
350 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
351 */
352 @Override
353 public boolean validate(IProject project, String path) {
354 return fileExists(path);
355 }
356
357 }
This page took 0.040231 seconds and 6 git commands to generate.