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