Implement TmfTrace changes - introduce TmfTraceException
[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.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.ITmfTraceIndexer;
30 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
31 import org.eclipse.linuxtools.tmf.core.trace.TmfLocation;
32 import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
33
34 /**
35 * <b><u>TmfTraceStub</u></b>
36 * <p>
37 * Dummy test trace. Use in conjunction with TmfEventParserStub.
38 */
39 @SuppressWarnings("nls")
40 public class TmfTraceStub extends TmfTrace<TmfEvent> implements ITmfEventParser<TmfEvent> {
41
42 // ------------------------------------------------------------------------
43 // Attributes
44 // ------------------------------------------------------------------------
45
46 // The actual stream
47 private RandomAccessFile fTrace;
48
49 // // The associated event parser
50 // private ITmfEventParser<TmfEvent> fParser;
51
52 // The synchronization lock
53 private final ReentrantLock fLock = new ReentrantLock();
54
55 // ------------------------------------------------------------------------
56 // Constructors
57 // ------------------------------------------------------------------------
58
59 /**
60 * @param path
61 * @throws FileNotFoundException
62 */
63 public TmfTraceStub() {
64 super();
65 fParser = new TmfEventParserStub(this);
66 }
67
68 /**
69 * @param path
70 * @throws FileNotFoundException
71 */
72 public TmfTraceStub(final String path) throws TmfTraceException {
73 this(path, DEFAULT_TRACE_CACHE_SIZE, false);
74 }
75
76 /**
77 * @param path
78 * @param cacheSize
79 * @throws FileNotFoundException
80 */
81 public TmfTraceStub(final String path, final int cacheSize) throws TmfTraceException {
82 this(path, cacheSize, false);
83 }
84
85 /**
86 * @param path
87 * @param cacheSize
88 * @throws FileNotFoundException
89 */
90 public TmfTraceStub(final String path, final int cacheSize, final long interval) throws TmfTraceException {
91 super(null, TmfEvent.class, path, cacheSize, interval);
92 try {
93 fTrace = new RandomAccessFile(path, "r");
94 } catch (FileNotFoundException e) {
95 throw new TmfTraceException(e.getMessage());
96 }
97 fParser = new TmfEventParserStub(this);
98 }
99
100 /**
101 * @param path
102 * @param cacheSize
103 * @throws FileNotFoundException
104 */
105 public TmfTraceStub(final String path, final int cacheSize, final ITmfTraceIndexer<?> indexer) throws TmfTraceException {
106 this(path, cacheSize, false, null, indexer);
107 }
108
109 /**
110 * @param path
111 * @param waitForCompletion
112 * @throws FileNotFoundException
113 */
114 public TmfTraceStub(final String path, final boolean waitForCompletion) throws TmfTraceException {
115 this(path, DEFAULT_TRACE_CACHE_SIZE, waitForCompletion);
116 }
117
118 /**
119 * @param path
120 * @param cacheSize
121 * @param waitForCompletion
122 * @throws FileNotFoundException
123 */
124 public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion) throws TmfTraceException {
125 super(null, TmfEvent.class, path, cacheSize);
126 try {
127 fTrace = new RandomAccessFile(path, "r");
128 } catch (FileNotFoundException e) {
129 throw new TmfTraceException(e.getMessage());
130 }
131 fParser = new TmfEventParserStub(this);
132 }
133
134 /**
135 * @param path
136 * @param cacheSize
137 * @param waitForCompletion
138 * @throws FileNotFoundException
139 */
140 public TmfTraceStub(final IResource resource, final String path, final int cacheSize, final boolean waitForCompletion) throws TmfTraceException {
141 super(resource, TmfEvent.class, path, cacheSize);
142 try {
143 fTrace = new RandomAccessFile(path, "r");
144 } catch (FileNotFoundException e) {
145 throw new TmfTraceException(e.getMessage());
146 }
147 fParser = new TmfEventParserStub(this);
148 }
149
150 /**
151 * @param path
152 * @param cacheSize
153 * @param waitForCompletion
154 * @param parser
155 * @throws FileNotFoundException
156 */
157 @SuppressWarnings("unchecked")
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 fParser = (ITmfEventParser<ITmfEvent>) ((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 fParser = new TmfEventParserStub(this);
180 }
181
182 public void indexTrace() {
183 fIndexer.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 fParser = 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 @SuppressWarnings("rawtypes")
271 public double getLocationRatio(final ITmfLocation location) {
272 fLock.lock();
273 try {
274 if (fTrace != null)
275 if (location.getLocation() instanceof Long)
276 return (double) ((Long) location.getLocation()) / fTrace.length();
277 } catch (final IOException e) {
278 e.printStackTrace();
279 } finally {
280 fLock.unlock();
281 }
282 return 0;
283 }
284
285 @Override
286 public TmfLocation<Long> getCurrentLocation() {
287 fLock.lock();
288 try {
289 if (fTrace != null)
290 return new TmfLocation<Long>(fTrace.getFilePointer());
291 } catch (final IOException e) {
292 e.printStackTrace();
293 } finally {
294 fLock.unlock();
295 }
296 return null;
297 }
298
299 @Override
300 public ITmfEvent parseEvent(final ITmfContext context) {
301 fLock.lock();
302 try {
303 // parseNextEvent will update the context
304 if (fTrace != null) {
305 final ITmfEvent event = fParser.parseEvent(context.clone());
306 return event;
307 }
308 // }
309 // catch (final IOException e) {
310 // e.printStackTrace();
311 } finally {
312 fLock.unlock();
313 }
314 return null;
315 }
316
317 @Override
318 public void setTimeRange(final TmfTimeRange range) {
319 super.setTimeRange(range);
320 }
321
322 @Override
323 public void setStartTime(final ITmfTimestamp startTime) {
324 super.setStartTime(startTime);
325 }
326
327 @Override
328 public void setEndTime(final ITmfTimestamp endTime) {
329 super.setEndTime(endTime);
330 }
331
332 @Override
333 public void setStreamingInterval(final long interval) {
334 super.setStreamingInterval(interval);
335 }
336
337 @Override
338 public void dispose() {
339 fLock.lock();
340 try {
341 if (fTrace != null) {
342 fTrace.close();
343 fTrace = null;
344 }
345 } catch (final IOException e) {
346 // Ignore
347 } finally {
348 fLock.unlock();
349 }
350 super.dispose();
351 }
352
353 }
This page took 0.039255 seconds and 6 git commands to generate.