Internalize lttng.jni
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.jni / src / org / eclipse / linuxtools / lttng / jni / JniParser.java
1 package org.eclipse.linuxtools.lttng.jni;
2 /*******************************************************************************
3 * Copyright (c) 2009 Ericsson
4 *
5 * All rights reserved. This program and the accompanying materials are
6 * made available under the terms of the Eclipse Public License v1.0 which
7 * accompanies this distribution, and is available at
8 * http://www.eclipse.org/legal/epl-v10.html
9 *
10 * Contributors:
11 * William Bourque (wbourque@gmail.com) - Initial API and implementation
12 *******************************************************************************/
13
14
15 import java.util.HashMap;
16 import java.util.Iterator;
17
18 import org.eclipse.linuxtools.internal.lttng.jni.common.Jni_C_Pointer;
19
20 /**
21 * <b><u>JniParser</u></b><p>
22 *
23 * JniParser is used to parse an event payload into something usable.<p>
24 *
25 * All methods are static, the parser shouldn't be instantiated.
26 *
27 * <b>NOTE</b><p>
28 * This class is ABSTRACT, you need to extends it to support your specific LTTng version.<p>
29 *
30 */
31 public abstract class JniParser extends Jni_C_Common
32 {
33 protected static native void ltt_getParsedData(int libId, ParsedObjectContent parseddata, long eventPtr, long markerFieldPtr);
34
35 // *** HACK ***
36 // We cannot use "Object" directly as java does not support swapping primitive value
37 // We either need to create a new object type or to use a "non-primitive" type that have "Setter()" functions
38 // Another (ugly) hack would be to pass an array to modify the reference's reference.
39 // ***
40 private static ParsedObjectContent parsedData = new ParsedObjectContent();
41
42 /*
43 * Default constructor is forbidden
44 */
45 protected JniParser() {
46 }
47
48
49 /**
50 * Method to parse a single field identified by its id.<p>
51 *
52 * All parsing will be done on C side as we need LTT functions.
53 *
54 * @param eventToParse The jni event we want to parse.
55 * @param fieldPosition The position (or id) of the field we want to parse
56 *
57 * @return An Object that contain the JniEvent payload parsed by the C, or null, if if was impossible to parse (i.e., wrong position)
58 *
59 * @see org.eclipse.linuxtools.lttng.jni.JniEvent
60 */
61 static public Object parseField(JniEvent eventToParse, int fieldPosition) {
62
63 // Sanity check
64 if ( (fieldPosition < 0) || ( fieldPosition >= eventToParse.requestEventMarker().getMarkerFieldsArrayList().size() ) ){
65 return null;
66 }
67
68 JniMarkerField tmpField = eventToParse.requestEventMarker().getMarkerFieldsArrayList().get(fieldPosition);
69
70 // Call the parsing function in C. The result will be put in parsedData object
71 ltt_getParsedData(eventToParse.getEventPtr().getLibraryId(), parsedData, eventToParse.getEventPtr().getPointer(), tmpField.getMarkerFieldPtr().getPointer());
72
73 return parsedData.getData();
74 }
75
76
77 /**
78 * Method to parse a single field identified by its name.<p>
79 *
80 * All parsing will be done on C side as we need LTT functions.
81 *
82 * @param eventToParse The jni event we want to parse.
83 * @param fieldName The name of the field we want to parse.
84 *
85 * @return An Object that contain the JniEvent payload parsed by the C, or null, if if was impossible to parse (i.e., wrong position)
86 *
87 * @see org.eclipse.linuxtools.lttng.jni.JniEvent
88 */
89 static public Object parseField(JniEvent eventToParse, String fieldName) {
90
91 JniMarkerField tmpField = eventToParse.requestEventMarker().getMarkerFieldsHashMap().get(fieldName);
92
93 // return immediately if there is no field by that name
94 if ( tmpField == null ) {
95 return null;
96 }
97
98 ltt_getParsedData(eventToParse.getEventPtr().getLibraryId(), parsedData, eventToParse.getEventPtr().getPointer(), tmpField.getMarkerFieldPtr().getPointer());
99
100 return parsedData.getData();
101 }
102
103
104
105 /**
106 * Method to parse all fields at once.<p>
107 *
108 * All parsing will be done on C side as we need LTT functions.
109 *
110 * @param eventToParse The jni event we want to parse.
111 * @return An HashMap of Object that contain the is the JniEvent's payload parsed by the C
112 *
113 * @see org.eclipse.linuxtools.lttng.jni.JniEvent
114 */
115 static public HashMap<String, Object> parseAllFields(JniEvent eventToParse) {
116 HashMap<String,JniMarkerField> markerFieldData = eventToParse.requestEventMarker().getMarkerFieldsHashMap();
117
118 // This hashmap will contain the parsed content.
119 // ParsedContent is a local class defined at the end of this file
120
121 // *** HACK ***
122 // We want (need?) the map that contain the parsed data to be in the same order as markerField map
123 // The "instinctive way" would be to use :
124 // HashMap<String, Object> parsedDataMap = new HashMap<String, Object>(nbMarkerField);
125 //
126 // However, we cannot ensure that the newly created hashmap will use the same order.
127 // The hard way would be to override the default hash function for both hashmap
128 // However, this is way easier to abuse the fact that both hashmap are of type <String, something...>
129 // Therefore we can abuse the java-cast with clone() :
130 // HashMap<String, Object> parsedDataMap = (HashMap<String, Object>)markerFieldData.clone();
131 // Or even safer, use HashMap constructor to do so :
132 HashMap<String, Object> parsedDataMap = new HashMap<String, Object>(markerFieldData);
133
134 JniMarkerField newMarkerField = null;
135 Iterator<String> iterator = markerFieldData.keySet().iterator();
136
137 while ( iterator.hasNext() ) {
138 newMarkerField = markerFieldData.get(iterator.next());
139 // Call the C to parse the data
140 ltt_getParsedData(eventToParse.getEventPtr().getLibraryId(), parsedData, eventToParse.getEventPtr().getPointer(), newMarkerField.getMarkerFieldPtr().getPointer());
141 // Save the result into the HashMap
142 parsedDataMap.put(newMarkerField.getField(), parsedData.getData() );
143 }
144
145 return parsedDataMap;
146 }
147
148
149 /*
150 * Add a parsed String value to the Array<br>
151 * <br>
152 * Note : this function will be called from the C side.
153 * Note2: contentHolder is of type "Object" instead of "ParsedObjectContent" to be able to use the most generic function signature on the C side.
154 * its goal is to give a generic interface to people that would like to use the JNI library
155 *
156 * @param parsedArray Array where to store the value
157 * @param fieldName The name of the parsed field
158 * @param stringToAdd The parsed data to add
159 * @param formatToAdd The format of the raw data
160 */
161 static private void addStringToParsingFromC(Object contentHolder, String stringToAdd) {
162 ((ParsedObjectContent)contentHolder).setData( stringToAdd);
163 }
164
165 /*
166 * Add a parsed 64 bits Pointer value to the Array<br>
167 * <br>
168 * Note : this function will be called from the C side.
169 * Note2: contentHolder is of type "Object" instead of "ParsedObjectContent" to be able to use the most generic function signature on the C side.
170 * its goal is to give a generic interface to people that would like to use the JNI library
171 *
172 * @param contentHolder Object where to store the parsed value
173 * @param fieldName The name of the parsed field
174 * @param pointerToAdd The parsed data to add (in 64 bits long!)
175 * @param formatToAdd The format of the raw data
176 */
177 static private void addLongPointerToParsingFromC(Object contentHolder, long pointerToAdd) {
178 ((ParsedObjectContent)contentHolder).setData( new Jni_C_Pointer((long) pointerToAdd));
179 }
180
181 /*
182 * Add a parsed 32 bits Pointer value to the Array<br>
183 * <br>
184 * Note : this function will be called from the C side.
185 * Note2: contentHolder is of type "Object" instead of "ParsedObjectContent" to be able to use the most generic function signature on the C side.
186 * its goal is to give a generic interface to people that would like to use the JNI library
187 *
188 * @param contentHolder Object where to store the parsed value
189 * @param fieldName The name of the parsed field
190 * @param pointerToAdd The parsed data to add (converted in 64 bits long!)
191 * @param formatToAdd The format of the raw data
192 */
193 static private void addIntPointerToParsingFromC(Object contentHolder, long pointerToAdd) {
194 ((ParsedObjectContent)contentHolder).setData( new Jni_C_Pointer((int) pointerToAdd));
195 }
196
197 /*
198 * Add a parsed short value to the Array<br>
199 * <br>
200 * Note : this function will be called from the C side.
201 * Note2: contentHolder is of type "Object" instead of "ParsedObjectContent" to be able to use the most generic function signature on the C side.
202 * its goal is to give a generic interface to people that would like to use the JNI library
203 *
204 * @param contentHolder Object where to store the parsed value
205 * @param fieldName The name of the parsed field
206 * @param shortToAdd The parsed data to add
207 * @param formatToAdd The format of the raw data
208 */
209 static private void addShortToParsingFromC(Object contentHolder, short shortToAdd) {
210 ((ParsedObjectContent)contentHolder).setData( Short.valueOf(shortToAdd));
211 }
212
213 /*
214 * Add a parsed integer value to the Array<br>
215 * <br>
216 * Note : this function will be called from the C side.
217 * Note2: contentHolder is of type "Object" instead of "ParsedObjectContent" to be able to use the most generic function signature on the C side.
218 * its goal is to give a generic interface to people that would like to use the JNI library
219 *
220 * @param contentHolder Object where to store the parsed value
221 * @param fieldName The name of the parsed field
222 * @param intToAdd The parsed data to add
223 * @param formatToAdd The format of the raw data
224 */
225 static private void addIntegerToParsingFromC(Object contentHolder, int intToAdd) {
226 ((ParsedObjectContent)contentHolder).setData( Integer.valueOf(intToAdd));
227 }
228
229 /*
230 * Add a parsed long value to the Array<br>
231 * <br>
232 * Note : this function will be called from the C side.
233 * Note2: contentHolder is of type "Object" instead of "ParsedObjectContent" to be able to use the most generic function signature on the C side.
234 * its goal is to give a generic interface to people that would like to use the JNI library
235 *
236 * @param contentHolder Object where to store the parsed value
237 * @param fieldName The name of the parsed field
238 * @param longToAdd The parsed data to add
239 * @param formatToAdd The format of the raw data
240 */
241 static private void addLongToParsingFromC(Object contentHolder, long longToAdd) {
242 ((ParsedObjectContent)contentHolder).setData( Long.valueOf(longToAdd));
243 }
244
245 /*
246 * Add a parsed float value to the Array<br>
247 * <br>
248 * Note : this function will be called from the C side.
249 * Note2: contentHolder is of type "Object" instead of "ParsedObjectContent" to be able to use the most generic function signature on the C side.
250 * its goal is to give a generic interface to people that would like to use the JNI library
251 *
252 * @param contentHolder Object where to store the parsed value
253 * @param fieldName The name of the parsed field
254 * @param floatToAdd The parsed data to add
255 * @param formatToAdd The format of the raw data
256 */
257 static private void addFloatToParsingFromC(Object contentHolder, float floatToAdd) {
258 ((ParsedObjectContent)contentHolder).setData( new Float(floatToAdd));
259 }
260
261 /*
262 * Add a parsed double value to the Array<br>
263 * <br>
264 * Note : this function will be called from the C side.
265 * Note2: contentHolder is of type "Object" instead of "ParsedObjectContent" to be able to use the most generic function signature on the C side.
266 * its goal is to give a generic interface to people that would like to use the JNI library
267 *
268 *
269 * @param contentHolder Object where to store the parsed value
270 * @param fieldName The name of the parsed field
271 * @param doubleToAdd The parsed data to add
272 * @param formatToAdd The format of the raw data
273 */
274 static private void addDoubleToParsingFromC(Object contentHolder, double doubleToAdd) {
275 ((ParsedObjectContent)contentHolder).setData( new Double(doubleToAdd));
276 }
277
278 }
279
280
281 /**
282 * <b><u>ParsedObjectContent</u></b><p>
283 *
284 * ParsedObjectContent class.
285 * Only be used locally in this object to parse event data more efficiently in the C.
286 */
287 class ParsedObjectContent {
288 private Object parsedData = null;
289
290 public Object getData() {
291 return parsedData;
292 }
293
294 public void setData(Object newData) {
295 parsedData = newData;
296 }
297 }
This page took 0.037517 seconds and 5 git commands to generate.