1 package org
.eclipse
.linuxtools
.lttng
.jni
;
2 /*******************************************************************************
3 * Copyright (c) 2009 Ericsson
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
11 * William Bourque (wbourque@gmail.com) - Initial API and implementation
12 *******************************************************************************/
15 import java
.util
.HashMap
;
16 import java
.util
.Iterator
;
18 import org
.eclipse
.linuxtools
.internal
.lttng
.jni
.common
.Jni_C_Pointer
;
21 * <b><u>JniParser</u></b><p>
23 * JniParser is used to parse an event payload into something usable.<p>
25 * All methods are static, the parser shouldn't be instantiated.
28 * This class is ABSTRACT, you need to extends it to support your specific LTTng version.<p>
31 public abstract class JniParser
extends Jni_C_Common
33 protected static native void ltt_getParsedData(int libId
, ParsedObjectContent parseddata
, long eventPtr
, long markerFieldPtr
);
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.
40 private static ParsedObjectContent parsedData
= new ParsedObjectContent();
43 * Default constructor is forbidden
45 protected JniParser() {
50 * Method to parse a single field identified by its id.<p>
52 * All parsing will be done on C side as we need LTT functions.
54 * @param eventToParse The jni event we want to parse.
55 * @param fieldPosition The position (or id) of the field we want to parse
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)
59 * @see org.eclipse.linuxtools.lttng.jni.JniEvent
61 static public Object
parseField(JniEvent eventToParse
, int fieldPosition
) {
64 if ( (fieldPosition
< 0) || ( fieldPosition
>= eventToParse
.requestEventMarker().getMarkerFieldsArrayList().size() ) ){
68 JniMarkerField tmpField
= eventToParse
.requestEventMarker().getMarkerFieldsArrayList().get(fieldPosition
);
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());
73 return parsedData
.getData();
78 * Method to parse a single field identified by its name.<p>
80 * All parsing will be done on C side as we need LTT functions.
82 * @param eventToParse The jni event we want to parse.
83 * @param fieldName The name of the field we want to parse.
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)
87 * @see org.eclipse.linuxtools.lttng.jni.JniEvent
89 static public Object
parseField(JniEvent eventToParse
, String fieldName
) {
91 JniMarkerField tmpField
= eventToParse
.requestEventMarker().getMarkerFieldsHashMap().get(fieldName
);
93 // return immediately if there is no field by that name
94 if ( tmpField
== null ) {
98 ltt_getParsedData(eventToParse
.getEventPtr().getLibraryId(), parsedData
, eventToParse
.getEventPtr().getPointer(), tmpField
.getMarkerFieldPtr().getPointer());
100 return parsedData
.getData();
106 * Method to parse all fields at once.<p>
108 * All parsing will be done on C side as we need LTT functions.
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
113 * @see org.eclipse.linuxtools.lttng.jni.JniEvent
115 static public HashMap
<String
, Object
> parseAllFields(JniEvent eventToParse
) {
116 HashMap
<String
,JniMarkerField
> markerFieldData
= eventToParse
.requestEventMarker().getMarkerFieldsHashMap();
118 // This hashmap will contain the parsed content.
119 // ParsedContent is a local class defined at the end of this file
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);
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
);
134 JniMarkerField newMarkerField
= null;
135 Iterator
<String
> iterator
= markerFieldData
.keySet().iterator();
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() );
145 return parsedDataMap
;
150 * Add a parsed String value to the Array<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
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
161 static private void addStringToParsingFromC(Object contentHolder
, String stringToAdd
) {
162 ((ParsedObjectContent
)contentHolder
).setData( stringToAdd
);
166 * Add a parsed 64 bits Pointer value to the Array<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
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
177 static private void addLongPointerToParsingFromC(Object contentHolder
, long pointerToAdd
) {
178 ((ParsedObjectContent
)contentHolder
).setData( new Jni_C_Pointer((long) pointerToAdd
));
182 * Add a parsed 32 bits Pointer value to the Array<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
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
193 static private void addIntPointerToParsingFromC(Object contentHolder
, long pointerToAdd
) {
194 ((ParsedObjectContent
)contentHolder
).setData( new Jni_C_Pointer((int) pointerToAdd
));
198 * Add a parsed short value to the Array<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
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
209 static private void addShortToParsingFromC(Object contentHolder
, short shortToAdd
) {
210 ((ParsedObjectContent
)contentHolder
).setData( Short
.valueOf(shortToAdd
));
214 * Add a parsed integer value to the Array<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
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
225 static private void addIntegerToParsingFromC(Object contentHolder
, int intToAdd
) {
226 ((ParsedObjectContent
)contentHolder
).setData( Integer
.valueOf(intToAdd
));
230 * Add a parsed long value to the Array<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
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
241 static private void addLongToParsingFromC(Object contentHolder
, long longToAdd
) {
242 ((ParsedObjectContent
)contentHolder
).setData( Long
.valueOf(longToAdd
));
246 * Add a parsed float value to the Array<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
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
257 static private void addFloatToParsingFromC(Object contentHolder
, float floatToAdd
) {
258 ((ParsedObjectContent
)contentHolder
).setData( new Float(floatToAdd
));
262 * Add a parsed double value to the Array<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
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
274 static private void addDoubleToParsingFromC(Object contentHolder
, double doubleToAdd
) {
275 ((ParsedObjectContent
)contentHolder
).setData( new Double(doubleToAdd
));
282 * <b><u>ParsedObjectContent</u></b><p>
284 * ParsedObjectContent class.
285 * Only be used locally in this object to parse event data more efficiently in the C.
287 class ParsedObjectContent
{
288 private Object parsedData
= null;
290 public Object
getData() {
294 public void setData(Object newData
) {
295 parsedData
= newData
;