Fix for Linux tree item height bug.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / stubs / org / eclipse / linuxtools / tmf / tests / stubs / trace / TmfTraceStub.java
CommitLineData
d18dd09b 1/*******************************************************************************
e31e01e8 2 * Copyright (c) 2009, 2010 Ericsson
d18dd09b
ASL
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
4918b8f2 13package org.eclipse.linuxtools.tmf.tests.stubs.trace;
d18dd09b
ASL
14
15import java.io.FileNotFoundException;
16import java.io.IOException;
17import java.io.RandomAccessFile;
73005152 18import java.util.concurrent.locks.ReentrantLock;
d18dd09b 19
2352aed9 20import org.eclipse.core.resources.IProject;
20658947 21import org.eclipse.core.resources.IResource;
dfee01ae 22import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
6c13869b
FC
23import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
24import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
b4f71e4a 25import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
6c13869b 26import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
7e6347b0 27import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser;
6c13869b 28import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
20658947 29import org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer;
6c13869b
FC
30import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
31import org.eclipse.linuxtools.tmf.core.trace.TmfLocation;
32import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
d18dd09b
ASL
33
34/**
35 * <b><u>TmfTraceStub</u></b>
36 * <p>
8d2e2848 37 * Dummy test trace. Use in conjunction with TmfEventParserStub.
d18dd09b 38 */
3b38ea61 39@SuppressWarnings("nls")
7e6347b0 40public class TmfTraceStub extends TmfTrace<TmfEvent> implements ITmfEventParser<TmfEvent> {
d18dd09b 41
e31e01e8 42 // ------------------------------------------------------------------------
d18dd09b 43 // Attributes
e31e01e8 44 // ------------------------------------------------------------------------
d18dd09b
ASL
45
46 // The actual stream
ff4ed569 47 private RandomAccessFile fTrace;
d18dd09b 48
7e6347b0
FC
49// // The associated event parser
50// private ITmfEventParser<TmfEvent> fParser;
d18dd09b 51
73005152 52 // The synchronization lock
085d898f
FC
53 private final ReentrantLock fLock = new ReentrantLock();
54
e31e01e8 55 // ------------------------------------------------------------------------
d18dd09b 56 // Constructors
e31e01e8 57 // ------------------------------------------------------------------------
d18dd09b 58
20658947
FC
59 /**
60 * @param path
61 * @throws FileNotFoundException
62 */
63 public TmfTraceStub() {
64 super();
7e6347b0 65 fParser = new TmfEventParserStub(this);
20658947
FC
66 }
67
d18dd09b 68 /**
3ef62bac 69 * @param path
d18dd09b
ASL
70 * @throws FileNotFoundException
71 */
b4f71e4a 72 public TmfTraceStub(final String path) throws TmfTraceException {
20658947 73 this(path, DEFAULT_TRACE_CACHE_SIZE, false);
8d2e2848
FC
74 }
75
76 /**
3ef62bac 77 * @param path
e31e01e8 78 * @param cacheSize
8d2e2848
FC
79 * @throws FileNotFoundException
80 */
b4f71e4a 81 public TmfTraceStub(final String path, final int cacheSize) throws TmfTraceException {
3ef62bac 82 this(path, cacheSize, false);
d18dd09b
ASL
83 }
84
1703b536
FC
85 /**
86 * @param path
87 * @param cacheSize
88 * @throws FileNotFoundException
89 */
b4f71e4a 90 public TmfTraceStub(final String path, final int cacheSize, final long interval) throws TmfTraceException {
1703b536 91 super(null, TmfEvent.class, path, cacheSize, interval);
b4f71e4a
FC
92 try {
93 fTrace = new RandomAccessFile(path, "r");
94 } catch (FileNotFoundException e) {
95 throw new TmfTraceException(e.getMessage());
96 }
7e6347b0 97 fParser = new TmfEventParserStub(this);
1703b536
FC
98 }
99
20658947
FC
100 /**
101 * @param path
102 * @param cacheSize
103 * @throws FileNotFoundException
104 */
b4f71e4a 105 public TmfTraceStub(final String path, final int cacheSize, final ITmfTraceIndexer<?> indexer) throws TmfTraceException {
20658947
FC
106 this(path, cacheSize, false, null, indexer);
107 }
108
d18dd09b 109 /**
3ef62bac 110 * @param path
ff4ed569 111 * @param waitForCompletion
d18dd09b
ASL
112 * @throws FileNotFoundException
113 */
b4f71e4a 114 public TmfTraceStub(final String path, final boolean waitForCompletion) throws TmfTraceException {
20658947 115 this(path, DEFAULT_TRACE_CACHE_SIZE, waitForCompletion);
8d2e2848 116 }
085d898f 117
8d2e2848 118 /**
3ef62bac 119 * @param path
8d2e2848 120 * @param cacheSize
ff4ed569 121 * @param waitForCompletion
8d2e2848
FC
122 * @throws FileNotFoundException
123 */
b4f71e4a 124 public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion) throws TmfTraceException {
09e86496 125 super(null, TmfEvent.class, path, cacheSize);
b4f71e4a
FC
126 try {
127 fTrace = new RandomAccessFile(path, "r");
128 } catch (FileNotFoundException e) {
129 throw new TmfTraceException(e.getMessage());
130 }
7e6347b0 131 fParser = new TmfEventParserStub(this);
d18dd09b
ASL
132 }
133
20658947
FC
134 /**
135 * @param path
136 * @param cacheSize
137 * @param waitForCompletion
138 * @throws FileNotFoundException
139 */
b4f71e4a 140 public TmfTraceStub(final IResource resource, final String path, final int cacheSize, final boolean waitForCompletion) throws TmfTraceException {
20658947 141 super(resource, TmfEvent.class, path, cacheSize);
b4f71e4a
FC
142 try {
143 fTrace = new RandomAccessFile(path, "r");
144 } catch (FileNotFoundException e) {
145 throw new TmfTraceException(e.getMessage());
146 }
7e6347b0 147 fParser = new TmfEventParserStub(this);
20658947 148 }
085d898f 149
73005152 150 /**
3ef62bac 151 * @param path
73005152
BH
152 * @param cacheSize
153 * @param waitForCompletion
154 * @param parser
155 * @throws FileNotFoundException
156 */
20658947 157 public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion,
b4f71e4a 158 final ITmfEventParser<TmfEvent> parser, final ITmfTraceIndexer<?> indexer) throws TmfTraceException {
20658947 159 super(null, TmfEvent.class, path, cacheSize, 0, indexer);
b4f71e4a
FC
160 try {
161 fTrace = new RandomAccessFile(path, "r");
162 } catch (FileNotFoundException e) {
163 throw new TmfTraceException(e.getMessage());
164 }
2352aed9 165 fParser = (parser != null) ? parser : new TmfEventParserStub(this);
73005152 166 }
085d898f 167
ff4ed569 168 /**
f17b2f70 169 * Copy constructor
ff4ed569 170 */
b4f71e4a 171 public TmfTraceStub(final TmfTraceStub trace) throws TmfTraceException {
20658947 172 super(trace);
b4f71e4a
FC
173 try {
174 fTrace = new RandomAccessFile(getPath(), "r");
175 } catch (FileNotFoundException e) {
176 throw new TmfTraceException(e.getMessage());
177 }
7e6347b0 178 fParser = new TmfEventParserStub(this);
ff4ed569 179 }
085d898f 180
20658947
FC
181 public void indexTrace() {
182 fIndexer.buildIndex(true);
183 }
184
185 @Override
b4f71e4a
FC
186 public void initTrace(final IResource resource, final String path, final Class<TmfEvent> type) throws TmfTraceException {
187 try {
188 fTrace = new RandomAccessFile(path, "r");
189 } catch (FileNotFoundException e) {
190 throw new TmfTraceException(e.getMessage());
191 }
7e6347b0 192 fParser = new TmfEventParserStub(this);
20658947
FC
193 super.initTrace(resource, path, type);
194 }
f17b2f70 195
1703b536 196 @Override
b4f71e4a 197 public void initialize(final IResource resource, final String path, final Class<TmfEvent> type) throws TmfTraceException {
1703b536
FC
198 super.initialize(resource, path, type);
199 }
200
e31e01e8 201 // ------------------------------------------------------------------------
d18dd09b 202 // Accessors
e31e01e8 203 // ------------------------------------------------------------------------
d18dd09b
ASL
204
205 public RandomAccessFile getStream() {
206 return fTrace;
207 }
208
e31e01e8 209 // ------------------------------------------------------------------------
d18dd09b 210 // Operators
e31e01e8 211 // ------------------------------------------------------------------------
d18dd09b 212
085d898f
FC
213 @Override
214 @SuppressWarnings("unchecked")
7e6347b0 215 public TmfContext seekEvent(final ITmfLocation<?> location) {
d18dd09b 216 try {
09e86496
FC
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 = ((TmfLocation<Long>) location).getLocation();
226 rank = ITmfContext.UNKNOWN_RANK;
227 }
20658947 228 if (loc != fTrace.getFilePointer()) {
09e86496 229 fTrace.seek(loc);
20658947 230 }
09e86496
FC
231 final TmfContext context = new TmfContext(getCurrentLocation(), rank);
232 return context;
73005152 233 }
09e86496
FC
234 } catch (final IOException e) {
235 e.printStackTrace();
236 } catch (final NullPointerException e) {
237 e.printStackTrace();
73005152 238 }
09e86496 239 } catch (final NullPointerException e) {
085d898f
FC
240 e.printStackTrace();
241 }
73005152
BH
242 finally{
243 fLock.unlock();
244 }
085d898f 245 return null;
d18dd09b
ASL
246 }
247
c76c54bb 248
085d898f 249 @Override
7e6347b0 250 public TmfContext seekEvent(final double ratio) {
085d898f 251 fLock.lock();
c76c54bb 252 try {
73005152 253 if (fTrace != null) {
085d898f 254 final ITmfLocation<?> location = new TmfLocation<Long>(Long.valueOf((long) (ratio * fTrace.length())));
7e6347b0 255 final TmfContext context = seekEvent(location);
73005152
BH
256 context.setRank(ITmfContext.UNKNOWN_RANK);
257 return context;
258 }
085d898f 259 } catch (final IOException e) {
c76c54bb 260 e.printStackTrace();
73005152
BH
261 } finally {
262 fLock.unlock();
c76c54bb 263 }
085d898f 264
c76c54bb
FC
265 return null;
266 }
267
268 @Override
2352aed9 269 public double getLocationRatio(ITmfLocation<?> location) {
73005152 270 fLock.lock();
c76c54bb 271 try {
085d898f
FC
272 if (fTrace != null)
273 if (location.getLocation() instanceof Long)
73005152 274 return (double) ((Long) location.getLocation()) / fTrace.length();
085d898f 275 } catch (final IOException e) {
c76c54bb 276 e.printStackTrace();
73005152
BH
277 } finally {
278 fLock.unlock();
c76c54bb
FC
279 }
280 return 0;
281 }
282
4e3aa37d 283 @Override
085d898f 284 public TmfLocation<Long> getCurrentLocation() {
73005152 285 fLock.lock();
d18dd09b 286 try {
085d898f 287 if (fTrace != null)
73005152 288 return new TmfLocation<Long>(fTrace.getFilePointer());
085d898f
FC
289 } catch (final IOException e) {
290 e.printStackTrace();
291 } finally {
292 fLock.unlock();
293 }
294 return null;
295 }
296
297 @Override
2352aed9 298 public TmfEvent parseEvent(final ITmfContext context) {
085d898f
FC
299 fLock.lock();
300 try {
301 // parseNextEvent will update the context
302 if (fTrace != null) {
2352aed9 303 final TmfEvent event = fParser.parseEvent(context.clone());
085d898f 304 return event;
73005152 305 }
7e6347b0
FC
306// }
307// catch (final IOException e) {
308// e.printStackTrace();
73005152
BH
309 } finally {
310 fLock.unlock();
d18dd09b
ASL
311 }
312 return null;
313 }
314
085d898f
FC
315 @Override
316 public void setTimeRange(final TmfTimeRange range) {
317 super.setTimeRange(range);
318 }
d18dd09b 319
085d898f
FC
320 @Override
321 public void setStartTime(final ITmfTimestamp startTime) {
322 super.setStartTime(startTime);
ff4ed569
FC
323 }
324
085d898f
FC
325 @Override
326 public void setEndTime(final ITmfTimestamp endTime) {
327 super.setEndTime(endTime);
ff4ed569
FC
328 }
329
1703b536
FC
330 @Override
331 public void setStreamingInterval(final long interval) {
332 super.setStreamingInterval(interval);
333 }
334
085d898f
FC
335 @Override
336 public void dispose() {
337 fLock.lock();
338 try {
339 if (fTrace != null) {
340 fTrace.close();
341 fTrace = null;
342 }
343 } catch (final IOException e) {
344 // Ignore
345 } finally {
346 fLock.unlock();
347 }
348 super.dispose();
ff4ed569
FC
349 }
350
2352aed9
FC
351 /* (non-Javadoc)
352 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
353 */
354 @Override
355 public boolean validate(IProject project, String path) {
356 return fileExists(path);
357 }
358
e31e01e8 359}
This page took 0.054299 seconds and 5 git commands to generate.