Remove the generic location (replace by Comparable)
[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.ITmfEvent;
23 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
24 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
25 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
26 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
27 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
28 import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser;
29 import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
30 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
31 import org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer;
32 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
33 import org.eclipse.linuxtools.tmf.core.trace.TmfLongLocation;
34 import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
35
36 /**
37 * <b><u>TmfTraceStub</u></b>
38 * <p>
39 * Dummy test trace. Use in conjunction with TmfEventParserStub.
40 */
41 @SuppressWarnings({"nls","javadoc"})
42 public class TmfTraceStub extends TmfTrace implements ITmfEventParser {
43
44 // ------------------------------------------------------------------------
45 // Attributes
46 // ------------------------------------------------------------------------
47
48 // The actual stream
49 private RandomAccessFile fTrace;
50
51 // // The associated event parser
52 // private ITmfEventParser<TmfEvent> fParser;
53
54 // The synchronization lock
55 private final ReentrantLock fLock = new ReentrantLock();
56
57 // ------------------------------------------------------------------------
58 // Constructors
59 // ------------------------------------------------------------------------
60
61 public TmfTraceStub() {
62 super();
63 setParser(new TmfEventParserStub(this));
64 }
65
66 /**
67 * @param path
68 * @throws FileNotFoundException
69 */
70 public TmfTraceStub(final String path) throws TmfTraceException {
71 this(path, ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, false);
72 }
73
74 /**
75 * @param path
76 * @param cacheSize
77 * @throws FileNotFoundException
78 */
79 public TmfTraceStub(final String path, final int cacheSize) throws TmfTraceException {
80 this(path, cacheSize, false);
81 }
82
83 /**
84 * @param path
85 * @param cacheSize
86 * @throws FileNotFoundException
87 */
88 public TmfTraceStub(final String path, final int cacheSize, final long interval) throws TmfTraceException {
89 super(null, TmfEvent.class, path, cacheSize, interval);
90 try {
91 fTrace = new RandomAccessFile(path, "r");
92 } catch (FileNotFoundException e) {
93 throw new TmfTraceException(e.getMessage());
94 }
95 setParser(new TmfEventParserStub(this));
96 }
97
98 /**
99 * @param path
100 * @param cacheSize
101 * @throws FileNotFoundException
102 */
103 public TmfTraceStub(final String path, final int cacheSize, final ITmfTraceIndexer indexer) throws TmfTraceException {
104 this(path, cacheSize, false, null, indexer);
105 }
106
107 /**
108 * @param path
109 * @param waitForCompletion
110 * @throws FileNotFoundException
111 */
112 public TmfTraceStub(final String path, final boolean waitForCompletion) throws TmfTraceException {
113 this(path, ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, waitForCompletion);
114 }
115
116 /**
117 * @param path
118 * @param cacheSize
119 * @param waitForCompletion
120 * @throws FileNotFoundException
121 */
122 public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion) throws TmfTraceException {
123 super(null, TmfEvent.class, path, cacheSize);
124 try {
125 fTrace = new RandomAccessFile(path, "r");
126 } catch (FileNotFoundException e) {
127 throw new TmfTraceException(e.getMessage());
128 }
129 setParser(new TmfEventParserStub(this));
130 if (waitForCompletion) {
131 indexTrace();
132 }
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 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 indexTrace(true);
184 }
185
186 @Override
187 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> 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<? extends ITmfEvent> 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 public TmfContext seekEvent(final ITmfLocation location) {
216 try {
217 fLock.lock();
218 try {
219 if (fTrace != null) {
220 // Position the trace at the requested location and
221 // returns the corresponding context
222 long loc = 0;
223 long rank = 0;
224 if (location != null) {
225 loc = (Long) location.getLocationData();
226 rank = ITmfContext.UNKNOWN_RANK;
227 }
228 if (loc != fTrace.getFilePointer()) {
229 fTrace.seek(loc);
230 }
231 final TmfContext context = new TmfContext(getCurrentLocation(), rank);
232 return context;
233 }
234 } catch (final IOException e) {
235 e.printStackTrace();
236 } catch (final NullPointerException e) {
237 e.printStackTrace();
238 }
239 finally{
240 fLock.unlock();
241 }
242 } catch (final NullPointerException e) {
243 e.printStackTrace();
244 }
245 return null;
246 }
247
248
249 @Override
250 public TmfContext seekEvent(final double ratio) {
251 fLock.lock();
252 try {
253 if (fTrace != null) {
254 final ITmfLocation location = new TmfLongLocation(Long.valueOf((long) (ratio * fTrace.length())));
255 final TmfContext context = seekEvent(location);
256 context.setRank(ITmfContext.UNKNOWN_RANK);
257 return context;
258 }
259 } catch (final IOException e) {
260 e.printStackTrace();
261 } finally {
262 fLock.unlock();
263 }
264
265 return null;
266 }
267
268 @Override
269 public double getLocationRatio(ITmfLocation location) {
270 fLock.lock();
271 try {
272 if (fTrace != null) {
273 if (location.getLocationData() instanceof Long) {
274 return (double) ((Long) location.getLocationData()) / fTrace.length();
275 }
276 }
277 } catch (final IOException e) {
278 e.printStackTrace();
279 } finally {
280 fLock.unlock();
281 }
282 return 0;
283 }
284
285 @Override
286 public ITmfLocation getCurrentLocation() {
287 fLock.lock();
288 try {
289 if (fTrace != null) {
290 return new TmfLongLocation(fTrace.getFilePointer());
291 }
292 } catch (final IOException e) {
293 e.printStackTrace();
294 } finally {
295 fLock.unlock();
296 }
297 return null;
298 }
299
300 @Override
301 public ITmfEvent parseEvent(final ITmfContext context) {
302 fLock.lock();
303 try {
304 // parseNextEvent will update the context
305 if (fTrace != null && getParser() != null && context != null) {
306 final ITmfEvent event = getParser().parseEvent(context.clone());
307 return event;
308 }
309 } finally {
310 fLock.unlock();
311 }
312 return null;
313 }
314
315 @Override
316 public synchronized void setNbEvents(final long nbEvents) {
317 super.setNbEvents(nbEvents);
318 }
319
320 @Override
321 public void setTimeRange(final TmfTimeRange range) {
322 super.setTimeRange(range);
323 }
324
325 @Override
326 public void setStartTime(final ITmfTimestamp startTime) {
327 super.setStartTime(startTime);
328 }
329
330 @Override
331 public void setEndTime(final ITmfTimestamp endTime) {
332 super.setEndTime(endTime);
333 }
334
335 @Override
336 public void setStreamingInterval(final long interval) {
337 super.setStreamingInterval(interval);
338 }
339
340 @Override
341 public synchronized void dispose() {
342 fLock.lock();
343 try {
344 if (fTrace != null) {
345 fTrace.close();
346 fTrace = null;
347 }
348 } catch (final IOException e) {
349 // Ignore
350 } finally {
351 fLock.unlock();
352 }
353 super.dispose();
354 }
355
356 /* (non-Javadoc)
357 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
358 */
359 @Override
360 public boolean validate(IProject project, String path) {
361 return fileExists(path);
362 }
363
364 }
This page took 0.039744 seconds and 6 git commands to generate.