c9d032d4e6780cce4848d9bf057646a08a00036d
[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.parser.ITmfEventParser;
26 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
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> {
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();
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();
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();
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();
135 }
136
137 /**
138 * @param path
139 * @param cacheSize
140 * @param waitForCompletion
141 * @param parser
142 * @throws FileNotFoundException
143 */
144 public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion,
145 final ITmfEventParser<TmfEvent> parser, final ITmfTraceIndexer<?> indexer) throws FileNotFoundException {
146 super(null, TmfEvent.class, path, cacheSize, 0, indexer);
147 fTrace = new RandomAccessFile(path, "r");
148 fParser = (parser != null) ? parser : new TmfEventParserStub();
149 }
150
151 /**
152 * Copy constructor
153 */
154 public TmfTraceStub(final TmfTraceStub trace) throws FileNotFoundException {
155 super(trace);
156 fTrace = new RandomAccessFile(getPath(), "r");
157 fParser = new TmfEventParserStub();
158 }
159
160 public void indexTrace() {
161 fIndexer.buildIndex(true);
162 }
163
164 @Override
165 public void initTrace(final IResource resource, final String path, final Class<TmfEvent> type) throws FileNotFoundException {
166 fTrace = new RandomAccessFile(path, "r");
167 fParser = new TmfEventParserStub();
168 super.initTrace(resource, path, type);
169 }
170
171 @Override
172 public void initialize(final IResource resource, final String path, final Class<TmfEvent> type) throws FileNotFoundException {
173 super.initialize(resource, path, type);
174 }
175
176 // ------------------------------------------------------------------------
177 // Accessors
178 // ------------------------------------------------------------------------
179
180 public RandomAccessFile getStream() {
181 return fTrace;
182 }
183
184 // ------------------------------------------------------------------------
185 // Operators
186 // ------------------------------------------------------------------------
187
188 @Override
189 @SuppressWarnings("unchecked")
190 public TmfContext seekLocation(final ITmfLocation<?> location) {
191 try {
192 fLock.lock();
193 try {
194 if (fTrace != null) {
195 // Position the trace at the requested location and
196 // returns the corresponding context
197 long loc = 0;
198 long rank = 0;
199 if (location != null) {
200 loc = ((TmfLocation<Long>) location).getLocation();
201 rank = ITmfContext.UNKNOWN_RANK;
202 }
203 if (loc != fTrace.getFilePointer()) {
204 fTrace.seek(loc);
205 }
206 final TmfContext context = new TmfContext(getCurrentLocation(), rank);
207 return context;
208 }
209 } catch (final IOException e) {
210 e.printStackTrace();
211 } catch (final NullPointerException e) {
212 e.printStackTrace();
213 }
214 } catch (final NullPointerException e) {
215 e.printStackTrace();
216 }
217 finally{
218 fLock.unlock();
219 }
220 return null;
221 }
222
223
224 @Override
225 public TmfContext seekLocation(final double ratio) {
226 fLock.lock();
227 try {
228 if (fTrace != null) {
229 final ITmfLocation<?> location = new TmfLocation<Long>(Long.valueOf((long) (ratio * fTrace.length())));
230 final TmfContext context = seekLocation(location);
231 context.setRank(ITmfContext.UNKNOWN_RANK);
232 return context;
233 }
234 } catch (final IOException e) {
235 e.printStackTrace();
236 } finally {
237 fLock.unlock();
238 }
239
240 return null;
241 }
242
243 @Override
244 @SuppressWarnings("rawtypes")
245 public double getLocationRatio(final ITmfLocation location) {
246 fLock.lock();
247 try {
248 if (fTrace != null)
249 if (location.getLocation() instanceof Long)
250 return (double) ((Long) location.getLocation()) / fTrace.length();
251 } catch (final IOException e) {
252 e.printStackTrace();
253 } finally {
254 fLock.unlock();
255 }
256 return 0;
257 }
258
259 @Override
260 public TmfLocation<Long> getCurrentLocation() {
261 fLock.lock();
262 try {
263 if (fTrace != null)
264 return new TmfLocation<Long>(fTrace.getFilePointer());
265 } catch (final IOException e) {
266 e.printStackTrace();
267 } finally {
268 fLock.unlock();
269 }
270 return null;
271 }
272
273 @Override
274 public ITmfEvent parseEvent(final ITmfContext context) {
275 fLock.lock();
276 try {
277 // parseNextEvent will update the context
278 if (fTrace != null) {
279 final ITmfEvent event = fParser.parseNextEvent(this, context.clone());
280 return event;
281 }
282 }
283 catch (final IOException e) {
284 e.printStackTrace();
285 } finally {
286 fLock.unlock();
287 }
288 return null;
289 }
290
291 @Override
292 public void setTimeRange(final TmfTimeRange range) {
293 super.setTimeRange(range);
294 }
295
296 @Override
297 public void setStartTime(final ITmfTimestamp startTime) {
298 super.setStartTime(startTime);
299 }
300
301 @Override
302 public void setEndTime(final ITmfTimestamp endTime) {
303 super.setEndTime(endTime);
304 }
305
306 @Override
307 public void setStreamingInterval(final long interval) {
308 super.setStreamingInterval(interval);
309 }
310
311 @Override
312 public void dispose() {
313 fLock.lock();
314 try {
315 if (fTrace != null) {
316 fTrace.close();
317 fTrace = null;
318 }
319 } catch (final IOException e) {
320 // Ignore
321 } finally {
322 fLock.unlock();
323 }
324 super.dispose();
325 }
326
327 }
This page took 0.037315 seconds and 4 git commands to generate.