1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
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
10 * William Bourque (wbourque@gmail.com) - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.lttng
.jni
;
15 import java
.util
.HashMap
;
18 * <b><u>JniParser</u></b>
21 * All methods are static, the parser shouldn't be instantiated.
23 public class JniParser
extends Jni_C_Common
25 private static native void ltt_getParsedData(ParsedObjectContent parseddata
, long eventPtr
, long markerFieldPtr
);
28 System
.loadLibrary("lttvtraceread");
32 * Default constructor is forbidden
40 * Method to parse a single field identified by its id<br>
41 * All parsing will be done on C side as we need Ltt function
43 * @param eventToParse The jni event we want to parse.
44 * @param fieldPosition The position (or id) of the field we want to parse
46 * @return An Object that contain the JniEvent payload parsed by the C, or null, if if was impossible to parse (i.e., wrong position)
48 * @see org.eclipse.linuxtools.lttng.jni.JniEvent
50 static public Object
parseField(JniEvent eventToParse
, int fieldPosition
) {
53 if ( (fieldPosition
< 0) || ( fieldPosition
>= eventToParse
.requestEventMarker().getMarkerFieldsArrayList().size() ) ){
58 // We cannot use "Object" directly as java does not support swapping primitive value
59 // We either need to create a new object type or to use a "non-primitive" type that have "Setter()" functions
61 ParsedObjectContent parsedData
= new ParsedObjectContent();
63 // Call the parsing function in C. The result will be put in parsedData object
64 ltt_getParsedData(parsedData
, eventToParse
.getEventPtr().getPointer(), eventToParse
.requestEventMarker().getMarkerFieldsArrayList().get(fieldPosition
).getMarkerFieldPtr().getPointer() );
66 return parsedData
.getData();
71 * Method to parse a single field identified by its name<br>
72 * All parsing will be done on C side as we need Ltt function
74 * @param eventToParse The jni event we want to parse.
75 * @param fieldPosition The position (or id) of the field we want to parse
77 * @return An Object that contain the JniEvent payload parsed by the C, or null, if if was impossible to parse (i.e., wrong position)
79 * @see org.eclipse.linuxtools.lttng.jni.JniEvent
81 static public Object
parseField(JniEvent eventToParse
, String fieldName
) {
83 JniMarkerField tmpField
= eventToParse
.requestEventMarker().getMarkerFieldsHashMap().get(fieldName
);
85 // return immediately if there is no field by that name
86 if ( tmpField
== null ) {
91 // We cannot use "Object" directly as java does not support swapping on primitive value
92 // We either need to create a new object type or to use a "non-primitive" type that have "Setter()" functions
94 ParsedObjectContent parsedData
= new ParsedObjectContent();
96 ltt_getParsedData(parsedData
, eventToParse
.getEventPtr().getPointer(), tmpField
.getMarkerFieldPtr().getPointer() );
98 return parsedData
.getData();
104 * Method to parse all field at once<br>
105 * All parsing will be done on C side as we need Ltt function
107 * @param eventToParse The jni event we want to parse.
108 * @return An HashMap of Object that contain the is the JniEvent's payload parsed by the C
110 * @see org.eclipse.linuxtools.lttng.jni.JniEvent
112 static public HashMap
<String
, Object
> parseAllFields(JniEvent eventToParse
) {
113 JniMarker markerData
= eventToParse
.requestEventMarker();
114 int nbMarkerField
= markerData
.getMarkerFieldsArrayList().size();
116 // This hashmap will contain the parsed content.
117 // ParsedContent is defined at the end of this file
118 HashMap
<String
, Object
> parsedDataArray
= new HashMap
<String
, Object
>(nbMarkerField
);
121 // We cannot use "Object" directly as java does not support swapping on primitive value
122 // We either need to create a new object type or to use a "non-primitive" type that have "Setter()" functions
124 ParsedObjectContent parsedData
= new ParsedObjectContent();
126 // Loop on markerfield, as we need to parse each field in the event data
127 for (int pos
= 0; pos
< nbMarkerField
; pos
++) {
128 // Call the C to parse the data
129 ltt_getParsedData(parsedData
, eventToParse
.getEventPtr().getPointer(), markerData
.getMarkerFieldsArrayList().get(pos
).getMarkerFieldPtr().getPointer() );
130 // Save the result into the HashMap
131 parsedDataArray
.put(markerData
.getMarkerFieldsArrayList().get(pos
).getField(), parsedData
.getData() );
134 return parsedDataArray
;
138 * Add a parsed String value to the Array<br>
140 * Note : this function will be called from the C side.
141 * Note2: contentHolder is of type "Object" instead of "ParsedObjectContent" to be able to use the most generic function signature on the C side.
142 * its goal is to give a generic interface to people that would like to use the JNI library
144 * @param parsedArray Array where to store the value
145 * @param fieldName The name of the parsed field
146 * @param stringToAdd The parsed data to add
147 * @param formatToAdd The format of the raw data
149 @SuppressWarnings("unused")
150 static private void addStringToParsingFromC(Object contentHolder
, String fieldName
, String stringToAdd
) {
151 ((ParsedObjectContent
)contentHolder
).setData( stringToAdd
);
155 * Add a parsed 64 bits Pointer value to the Array<br>
157 * Note : this function will be called from the C side.
158 * Note2: contentHolder is of type "Object" instead of "ParsedObjectContent" to be able to use the most generic function signature on the C side.
159 * its goal is to give a generic interface to people that would like to use the JNI library
161 * @param contentHolder Object where to store the parsed value
162 * @param fieldName The name of the parsed field
163 * @param pointerToAdd The parsed data to add (in 64 bits long!)
164 * @param formatToAdd The format of the raw data
166 @SuppressWarnings("unused")
167 static private void addLongPointerToParsingFromC(Object contentHolder
, String fieldName
, long pointerToAdd
) {
168 ((ParsedObjectContent
)contentHolder
).setData( new C_Pointer((long) pointerToAdd
));
172 * Add a parsed 32 bits Pointer value to the Array<br>
174 * Note : this function will be called from the C side.
175 * Note2: contentHolder is of type "Object" instead of "ParsedObjectContent" to be able to use the most generic function signature on the C side.
176 * its goal is to give a generic interface to people that would like to use the JNI library
178 * @param contentHolder Object where to store the parsed value
179 * @param fieldName The name of the parsed field
180 * @param pointerToAdd The parsed data to add (converted in 64 bits long!)
181 * @param formatToAdd The format of the raw data
183 @SuppressWarnings("unused")
184 static private void addIntPointerToParsingFromC(Object contentHolder
, String fieldName
, long pointerToAdd
) {
185 ((ParsedObjectContent
)contentHolder
).setData( new C_Pointer((int) pointerToAdd
));
189 * Add a parsed short value to the Array<br>
191 * Note : this function will be called from the C side.
192 * Note2: contentHolder is of type "Object" instead of "ParsedObjectContent" to be able to use the most generic function signature on the C side.
193 * its goal is to give a generic interface to people that would like to use the JNI library
195 * @param contentHolder Object where to store the parsed value
196 * @param fieldName The name of the parsed field
197 * @param shortToAdd The parsed data to add
198 * @param formatToAdd The format of the raw data
200 @SuppressWarnings("unused")
201 static private void addShortToParsingFromC(Object contentHolder
, String fieldName
, short shortToAdd
) {
202 ((ParsedObjectContent
)contentHolder
).setData( new Short(shortToAdd
));
206 * Add a parsed integer value to the Array<br>
208 * Note : this function will be called from the C side.
209 * Note2: contentHolder is of type "Object" instead of "ParsedObjectContent" to be able to use the most generic function signature on the C side.
210 * its goal is to give a generic interface to people that would like to use the JNI library
212 * @param contentHolder Object where to store the parsed value
213 * @param fieldName The name of the parsed field
214 * @param intToAdd The parsed data to add
215 * @param formatToAdd The format of the raw data
217 @SuppressWarnings("unused")
218 static private void addIntegerToParsingFromC(Object contentHolder
, String fieldName
, int intToAdd
) {
219 ((ParsedObjectContent
)contentHolder
).setData( new Integer(intToAdd
));
223 * Add a parsed long value to the Array<br>
225 * Note : this function will be called from the C side.
226 * Note2: contentHolder is of type "Object" instead of "ParsedObjectContent" to be able to use the most generic function signature on the C side.
227 * its goal is to give a generic interface to people that would like to use the JNI library
229 * @param contentHolder Object where to store the parsed value
230 * @param fieldName The name of the parsed field
231 * @param longToAdd The parsed data to add
232 * @param formatToAdd The format of the raw data
234 @SuppressWarnings("unused")
235 static private void addLongToParsingFromC(Object contentHolder
, String fieldName
, long longToAdd
) {
236 ((ParsedObjectContent
)contentHolder
).setData( new Long(longToAdd
));
240 * Add a parsed float value to the Array<br>
242 * Note : this function will be called from the C side.
243 * Note2: contentHolder is of type "Object" instead of "ParsedObjectContent" to be able to use the most generic function signature on the C side.
244 * its goal is to give a generic interface to people that would like to use the JNI library
246 * @param contentHolder Object where to store the parsed value
247 * @param fieldName The name of the parsed field
248 * @param floatToAdd The parsed data to add
249 * @param formatToAdd The format of the raw data
251 @SuppressWarnings("unused")
252 static private void addFloatToParsingFromC(Object contentHolder
, String fieldName
, float floatToAdd
) {
253 ((ParsedObjectContent
)contentHolder
).setData( new Float(floatToAdd
));
257 * Add a parsed double value to the Array<br>
259 * Note : this function will be called from the C side.
260 * Note2: contentHolder is of type "Object" instead of "ParsedObjectContent" to be able to use the most generic function signature on the C side.
261 * its goal is to give a generic interface to people that would like to use the JNI library
264 * @param contentHolder Object where to store the parsed value
265 * @param fieldName The name of the parsed field
266 * @param doubleToAdd The parsed data to add
267 * @param formatToAdd The format of the raw data
269 @SuppressWarnings("unused")
270 static private void addDoubleToParsingFromC(Object contentHolder
, String fieldName
, double doubleToAdd
) {
271 ((ParsedObjectContent
)contentHolder
).setData( new Double(doubleToAdd
));
277 * <b><u>ParsedObjectContent</u></b>
279 * ParsedObjectContent class.
280 * This class will only be used locally in this object to parse event data more efficiently in the C
282 class ParsedObjectContent
{
283 private Object parsedData
= null;
285 public Object
getData() {
289 public void setData(Object newData
) {
290 parsedData
= newData
;