Re-structure LTTng sub-project as per the Linux Tools guidelines
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / stubs / org / eclipse / linuxtools / tmf / 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.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.linuxtools.tmf.core.event.TmfEvent;
21 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
22 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
23 import org.eclipse.linuxtools.tmf.core.parser.ITmfEventParser;
24 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
25 import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
26 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
27 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
28 import org.eclipse.linuxtools.tmf.core.trace.TmfLocation;
29 import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
30
31 /**
32 * <b><u>TmfTraceStub</u></b>
33 * <p>
34 * Dummy test trace. Use in conjunction with TmfEventParserStub.
35 */
36 @SuppressWarnings("nls")
37 public class TmfTraceStub extends TmfTrace<TmfEvent> {
38
39 // ------------------------------------------------------------------------
40 // Attributes
41 // ------------------------------------------------------------------------
42
43 // The actual stream
44 private RandomAccessFile fTrace;
45
46 // The associated event parser
47 private ITmfEventParser fParser;
48
49 // The synchronization lock
50 private ReentrantLock fLock = new ReentrantLock();
51
52 // ------------------------------------------------------------------------
53 // Constructors
54 // ------------------------------------------------------------------------
55
56 /**
57 * @param filename
58 * @throws FileNotFoundException
59 */
60 public TmfTraceStub(String filename) throws FileNotFoundException {
61 this(filename, DEFAULT_INDEX_PAGE_SIZE, false);
62 }
63
64 /**
65 * @param filename
66 * @param cacheSize
67 * @throws FileNotFoundException
68 */
69 public TmfTraceStub(String filename, int cacheSize) throws FileNotFoundException {
70 this(filename, cacheSize, false);
71 }
72
73 /**
74 * @param filename
75 * @param waitForCompletion
76 * @throws FileNotFoundException
77 */
78 public TmfTraceStub(String filename, boolean waitForCompletion) throws FileNotFoundException {
79 this(filename, DEFAULT_INDEX_PAGE_SIZE, waitForCompletion);
80 }
81
82 /**
83 * @param filename
84 * @param cacheSize
85 * @param waitForCompletion
86 * @throws FileNotFoundException
87 */
88 public TmfTraceStub(String filename, int cacheSize, boolean waitForCompletion) throws FileNotFoundException {
89 super(null, TmfEvent.class, filename, cacheSize, false);
90 fTrace = new RandomAccessFile(filename, "r");
91 fParser = new TmfEventParserStub();
92 }
93
94
95 /**
96 * @param filename
97 * @param cacheSize
98 * @param waitForCompletion
99 * @param parser
100 * @throws FileNotFoundException
101 */
102 public TmfTraceStub(String filename, int cacheSize, boolean waitForCompletion, ITmfEventParser parser) throws FileNotFoundException {
103 super(filename, TmfEvent.class, filename, cacheSize, false);
104 fTrace = new RandomAccessFile(filename, "r");
105 fParser = parser;
106 }
107
108 /**
109 */
110 @Override
111 public TmfTraceStub clone() {
112 TmfTraceStub clone = null;
113 try {
114 clone = (TmfTraceStub) super.clone();
115 clone.fTrace = new RandomAccessFile(getPath(), "r");
116 clone.fParser = new TmfEventParserStub();
117 } catch (CloneNotSupportedException e) {
118 } catch (FileNotFoundException e) {
119 }
120 return clone;
121 }
122
123 @Override
124 public ITmfTrace copy() {
125 ITmfTrace returnedValue = null;
126 returnedValue = clone();
127 return returnedValue;
128 }
129
130 // ------------------------------------------------------------------------
131 // Accessors
132 // ------------------------------------------------------------------------
133
134 public RandomAccessFile getStream() {
135 return fTrace;
136 }
137
138 // ------------------------------------------------------------------------
139 // Operators
140 // ------------------------------------------------------------------------
141
142 @Override
143 @SuppressWarnings("unchecked")
144 public TmfContext seekLocation(ITmfLocation location) {
145 fLock.lock();
146 try {
147 if (fTrace != null) {
148 // Position the trace at the requested location and
149 // returns the corresponding context
150 long loc = 0;
151 long rank = 0;
152 if (location != null) {
153 loc = ((TmfLocation<Long>) location).getLocation();
154 rank = ITmfContext.UNKNOWN_RANK;
155 }
156 if (loc != fTrace.getFilePointer()) {
157 fTrace.seek(loc);
158 }
159 TmfContext context = new TmfContext(getCurrentLocation(), rank);
160 return context;
161 }
162 } catch (IOException e) {
163 e.printStackTrace();
164 }
165 finally{
166 fLock.unlock();
167 }
168 return null;
169 }
170
171
172 @Override
173 public TmfContext seekLocation(double ratio) {
174 fLock.lock();
175 try {
176 if (fTrace != null) {
177 ITmfLocation<?> location = new TmfLocation<Long>(new Long((long) (ratio * fTrace.length())));
178 TmfContext context = seekLocation(location);
179 context.setRank(ITmfContext.UNKNOWN_RANK);
180 return context;
181 }
182 } catch (IOException e) {
183 e.printStackTrace();
184 } finally {
185 fLock.unlock();
186 }
187
188 return null;
189 }
190
191 @Override
192 @SuppressWarnings("rawtypes")
193 public double getLocationRatio(ITmfLocation location) {
194 fLock.lock();
195 try {
196 if (fTrace != null) {
197 if (location.getLocation() instanceof Long) {
198 return (double) ((Long) location.getLocation()) / fTrace.length();
199 }
200 }
201 } catch (IOException e) {
202 e.printStackTrace();
203 } finally {
204 fLock.unlock();
205 }
206 return 0;
207 }
208
209 @Override
210 public TmfLocation<Long> getCurrentLocation() {
211 fLock.lock();
212 try {
213 if (fTrace != null) {
214 return new TmfLocation<Long>(fTrace.getFilePointer());
215 }
216 } catch (IOException e) {
217 e.printStackTrace();
218 } finally {
219 fLock.unlock();
220 }
221 return null;
222 }
223
224 @Override
225 public TmfEvent parseEvent(TmfContext context) {
226 fLock.lock();
227 try {
228 // parseNextEvent will update the context
229 if (fTrace != null) {
230 TmfEvent event = fParser.parseNextEvent(this, context.clone());
231 return event;
232 }
233 }
234 catch (IOException e) {
235 e.printStackTrace();
236 } finally {
237 fLock.unlock();
238 }
239 return null;
240 }
241
242 @Override
243 public void setTimeRange(TmfTimeRange range) {
244 super.setTimeRange(range);
245 }
246
247 @Override
248 public void setStartTime(TmfTimestamp startTime) {
249 super.setStartTime(startTime);
250 }
251
252 @Override
253 public void setEndTime(TmfTimestamp endTime) {
254 super.setEndTime(endTime);
255 }
256
257 @Override
258 public void dispose() {
259 fLock.lock();
260 try {
261 if (fTrace != null) {
262 fTrace.close();
263 fTrace = null;
264 }
265 } catch (IOException e) {
266 // Ignore
267 } finally {
268 fLock.unlock();
269 }
270 super.dispose();
271 }
272
273 }
This page took 0.08389 seconds and 5 git commands to generate.