Commit | Line | Data |
---|---|---|
5d10d135 ASL |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009 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 | * William Bourque (wbourque@gmail.com) - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.lttng.event; | |
14 | ||
28b94d61 FC |
15 | import java.util.HashMap; |
16 | ||
0188b342 | 17 | import org.eclipse.linuxtools.lttng.jni.JniEvent; |
5d10d135 | 18 | import org.eclipse.linuxtools.tmf.event.TmfEventContent; |
cbd4ad82 | 19 | import org.eclipse.linuxtools.tmf.event.TmfNoSuchFieldException; |
5d10d135 ASL |
20 | |
21 | /** | |
07d9e2ee FC |
22 | * <b><u>LttngEventContent</u></b><p> |
23 | * | |
24 | * Lttng specific implementation of the TmfEventContent.<p> | |
5d10d135 ASL |
25 | */ |
26 | public class LttngEventContent extends TmfEventContent { | |
27 | ||
28b94d61 FC |
28 | // Hash map that contain the (parsed) fields. This is the actual payload of the event. |
29 | HashMap<String, LttngEventField> fFieldsMap = new HashMap<String, LttngEventField>(); | |
30 | ||
5d10d135 | 31 | /** |
28b94d61 | 32 | * Default constructor.<p> |
07d9e2ee | 33 | * |
5d10d135 | 34 | * |
5d10d135 | 35 | */ |
28b94d61 FC |
36 | public LttngEventContent() { |
37 | super(null, null); | |
5d10d135 | 38 | } |
28b94d61 | 39 | |
5d10d135 | 40 | /** |
07d9e2ee | 41 | * Constructor with parameters.<p> |
5d10d135 | 42 | * |
28b94d61 FC |
43 | * @param thisParent Parent event for this content. |
44 | * | |
45 | * @see org.eclipse.linuxtools.lttng.event.LttngEvent | |
46 | */ | |
47 | public LttngEventContent(LttngEvent thisParent) { | |
48 | super(thisParent, null); | |
49 | } | |
50 | ||
51 | /** | |
52 | * Constructor with parameters, with optional content.<p> | |
53 | * | |
54 | * @param thisParent Parent event for this content. | |
55 | * @param thisContent Already parsed content. | |
56 | * | |
57 | * @see org.eclipse.linuxtools.lttng.event.LttngEvent | |
5d10d135 | 58 | */ |
28b94d61 FC |
59 | public LttngEventContent(LttngEvent thisParent, HashMap<String, LttngEventField> thisContent) { |
60 | super(thisParent, null); | |
3fbd810a | 61 | |
28b94d61 | 62 | fFieldsMap = thisContent; |
5d10d135 ASL |
63 | } |
64 | ||
3fbd810a | 65 | /** |
07d9e2ee | 66 | * Copy Constructor.<p> |
3fbd810a | 67 | * |
07d9e2ee | 68 | * @param oldContent Content to copy from |
3fbd810a FC |
69 | */ |
70 | public LttngEventContent(LttngEventContent oldContent) { | |
28b94d61 FC |
71 | this((LttngEvent)oldContent.getEvent(), oldContent.getRawContent() ); |
72 | } | |
73 | ||
74 | ||
b5983975 FC |
75 | @Override |
76 | public LttngEvent getEvent() { | |
28b94d61 FC |
77 | return (LttngEvent)fParentEvent; |
78 | } | |
79 | ||
80 | public void setEvent(LttngEvent newParent) { | |
81 | fParentEvent = newParent; | |
82 | } | |
83 | ||
84 | ||
85 | // *** VERIFY *** | |
86 | // These are not very useful, are they? | |
b5983975 FC |
87 | @Override |
88 | public LttngEventType getType() { | |
28b94d61 | 89 | return (LttngEventType)fParentEvent.getType(); |
28b94d61 FC |
90 | } |
91 | public void setType(LttngEventType newType) { | |
92 | ((LttngEvent)fParentEvent).setType(newType); | |
3fbd810a FC |
93 | } |
94 | ||
28b94d61 FC |
95 | |
96 | // ***TODO*** | |
97 | // Find a better way to ensure content is sane!! | |
98 | public void emptyContent() { | |
99 | fFieldsMap.clear(); | |
100 | } | |
101 | ||
102 | // ***VERIFY*** | |
0188b342 | 103 | // A bit weird to return the _currently_parsed fields (unlike all fields like getFields() ) |
28b94d61 | 104 | // Should we keep this? |
07d9e2ee | 105 | /** |
28b94d61 | 106 | * Return currently parsed fields in an object array format.<p> |
07d9e2ee | 107 | * |
28b94d61 | 108 | * @return Currently parsed fields. |
5d10d135 ASL |
109 | */ |
110 | @Override | |
28b94d61 FC |
111 | public Object[] getContent() { |
112 | Object[] returnedContent = fFieldsMap.values().toArray( new Object[fFieldsMap.size()] ); | |
5d10d135 | 113 | |
28b94d61 | 114 | return returnedContent; |
5d10d135 | 115 | } |
28b94d61 | 116 | |
07d9e2ee | 117 | /** |
28b94d61 | 118 | * Return currently parsed fields in the internal hashmap format.<p> |
07d9e2ee | 119 | * |
28b94d61 FC |
120 | * @return Currently parsed fields. |
121 | */ | |
122 | public HashMap<String, LttngEventField> getRawContent() { | |
123 | return fFieldsMap; | |
124 | } | |
125 | ||
126 | // @SuppressWarnings("unchecked") | |
127 | // @Override | |
128 | // public LttngEventField[] getFields() { | |
129 | // LttngEventField tmpField = null; | |
130 | // | |
131 | // // *** TODO *** | |
132 | // // SLOW! SLOW! SLOW! We should prevent the user to use this!! | |
133 | // HashMap<String, Object> parsedContent = parseContent(); | |
134 | // | |
135 | // String contentKey = null; | |
136 | // Iterator<String> contentItr = parsedContent.keySet().iterator(); | |
137 | // while ( contentItr.hasNext() ) { | |
138 | // contentKey = contentItr.next(); | |
139 | // | |
140 | // tmpField = new LttngEventField(this, contentKey, parsedContent.get(contentKey)); | |
141 | // ((HashMap<String, LttngEventField>)fFields).put(contentKey, tmpField); | |
142 | // } | |
143 | // | |
144 | // return fFields.values().toArray(new LttngEventField[fFields.size()]); | |
145 | // } | |
146 | ||
147 | /** | |
148 | * Parse all fields and return them as an array of LttngFields.<p> | |
07d9e2ee | 149 | * |
28b94d61 | 150 | * Note : This function is heavy and should only be called if all fields are really needed. |
07d9e2ee | 151 | * |
28b94d61 | 152 | * @return All fields. |
07d9e2ee | 153 | * |
28b94d61 | 154 | * @see @see org.eclipse.linuxtools.lttng.event.LttngEventField |
5d10d135 ASL |
155 | */ |
156 | @Override | |
a55a769e | 157 | public synchronized LttngEventField[] getFields() { |
0188b342 WB |
158 | if ( fFieldsMap.size() < fParentEvent.getType().getNbFields() ) { |
159 | LttngEventField tmpField = null; | |
160 | LttngEventType tmpType = (LttngEventType)fParentEvent.getType(); | |
161 | ||
162 | for ( int pos=0; pos<tmpType.getNbFields(); pos++ ) { | |
cbd4ad82 | 163 | String name = null; |
64fe8e8a FC |
164 | LttngEvent lttngTmpEvent = (LttngEvent)getEvent(); //added for easier debugging |
165 | JniEvent tmpEvent = (lttngTmpEvent).convertEventTmfToJni(); | |
0a8bb596 WB |
166 | |
167 | // tmpEvent == null probably mean there is a discrepancy between Eclipse and C library | |
168 | // An error was probably printed in convertEventTmfToJni() already, but keep in mind this is SERIOUS | |
169 | if ( tmpEvent != null ) { | |
170 | try { | |
171 | name = tmpType.getLabel(pos); | |
172 | ||
173 | Object newValue = tmpEvent.parseFieldByName(name); | |
174 | tmpField = new LttngEventField(this, name, newValue ); | |
175 | fFieldsMap.put(name, tmpField); | |
176 | } | |
177 | catch (TmfNoSuchFieldException e) { | |
9c4eb5f7 | 178 | System.out.println("Invalid field position requested : " + pos + ", ignoring (getFields)."); //$NON-NLS-1$//$NON-NLS-2$ |
0a8bb596 | 179 | } |
0188b342 WB |
180 | } |
181 | } | |
5d10d135 | 182 | } |
28b94d61 | 183 | return fFieldsMap.values().toArray(new LttngEventField[fFieldsMap.size()]); |
5d10d135 ASL |
184 | } |
185 | ||
186 | /** | |
28b94d61 | 187 | * Parse a single field from its given position.<p> |
07d9e2ee | 188 | * |
28b94d61 | 189 | * @return The parsed field or null. |
07d9e2ee | 190 | * |
28b94d61 | 191 | * @see @see org.eclipse.linuxtools.lttng.event.LttngEventField |
5d10d135 | 192 | */ |
28b94d61 FC |
193 | @Override |
194 | public LttngEventField getField(int position) { | |
195 | LttngEventField returnedField = null; | |
cbd4ad82 FC |
196 | String label = null; |
197 | try { | |
198 | label = fParentEvent.getType().getLabel(position); | |
0a8bb596 | 199 | |
d86b9d98 | 200 | returnedField = (LttngEventField) this.getField(label); |
0a8bb596 WB |
201 | } |
202 | catch (TmfNoSuchFieldException e) { | |
9c4eb5f7 | 203 | System.out.println("Invalid field position requested : " + position + ", ignoring (getField)."); //$NON-NLS-1$//$NON-NLS-2$ |
cbd4ad82 | 204 | } |
88144d4a | 205 | |
28b94d61 | 206 | return returnedField; |
5d10d135 ASL |
207 | } |
208 | ||
209 | /** | |
28b94d61 | 210 | * Parse a single field from its given name.<p> |
07d9e2ee | 211 | * |
28b94d61 | 212 | * @return The parsed field or null. |
07d9e2ee | 213 | * |
28b94d61 | 214 | * @see @see org.eclipse.linuxtools.lttng.event.LttngEventField |
5d10d135 | 215 | */ |
28b94d61 | 216 | @Override |
d86b9d98 | 217 | public synchronized Object getField(String name) { |
4bf17f4a | 218 | |
219 | // Check for generic table header fields | |
220 | if (name.equals(LttngEventType.CONTENT_LABEL) || name.equals(FIELD_ID_CONTENT)) { | |
221 | return fParentEvent.getContent().toString(); | |
222 | } else if (name.equals(LttngEventType.MARKER_LABEL) || name.equals(FIELD_ID_TYPE)) { | |
223 | return fParentEvent.getType().getTypeId().toString(); | |
224 | } else if (name.equals(LttngEventType.TRACE_LABEL) || name.equals(FIELD_ID_REFERENCE)) { | |
225 | return fParentEvent.getReference().getReference().toString(); | |
226 | } else if (name.equals(LttngEventType.TIMESTAMP_LABEL) || name.equals(FIELD_ID_TIMESTAMP)) { | |
227 | return new Long(fParentEvent.getTimestamp().getValue()).toString(); | |
228 | } else if (name.equals(FIELD_ID_SOURCE)) { | |
229 | return fParentEvent.getSource().getSourceId().toString(); | |
230 | } | |
d86b9d98 | 231 | |
e31e01e8 | 232 | // *** VERIFY *** |
28b94d61 FC |
233 | // Should we check if the field exists in LttngType before parsing? |
234 | // It could avoid calling parse for non-existent fields but would waste some cpu cycle on check? | |
235 | LttngEventField returnedField = fFieldsMap.get(name); | |
5d10d135 | 236 | |
28b94d61 FC |
237 | if ( returnedField == null ) { |
238 | // *** VERIFY *** | |
239 | // Should we really make sure we didn't get null before creating/inserting a field? | |
0188b342 WB |
240 | JniEvent tmpEvent = ((LttngEvent)getEvent()).convertEventTmfToJni(); |
241 | ||
242 | if ( tmpEvent != null) { | |
243 | Object newValue = tmpEvent.parseFieldByName(name); | |
244 | ||
245 | if ( newValue!= null ) { | |
246 | returnedField = new LttngEventField(this, name, newValue); | |
247 | fFieldsMap.put(name, returnedField ); | |
248 | } | |
249 | } | |
5d10d135 | 250 | } |
28b94d61 | 251 | |
5d10d135 ASL |
252 | return returnedField; |
253 | } | |
254 | ||
28b94d61 FC |
255 | // *** VERIFY *** |
256 | // *** Is this even useful? | |
257 | @Override | |
258 | protected void parseContent() { | |
259 | fFields = getFields(); | |
260 | } | |
261 | ||
5d10d135 | 262 | /** |
28b94d61 | 263 | * toString() method to print the content |
5d10d135 | 264 | * |
28b94d61 | 265 | * Note : this function parse all fields and so is very heavy to use. |
5d10d135 | 266 | */ |
6d848cce | 267 | @Override |
3b38ea61 | 268 | @SuppressWarnings("nls") |
28b94d61 | 269 | public String toString() { |
28b94d61 FC |
270 | LttngEventField[] allFields = getFields(); |
271 | ||
47e81b11 | 272 | StringBuffer strBuffer = new StringBuffer(); |
28b94d61 | 273 | for ( int pos=0; pos < allFields.length; pos++) { |
550d787e | 274 | if (pos != 0) strBuffer.append(","); |
47e81b11 | 275 | strBuffer.append(allFields[pos].toString()); |
28b94d61 FC |
276 | } |
277 | ||
47e81b11 | 278 | return strBuffer.toString(); |
5d10d135 | 279 | } |
1a971e96 FC |
280 | |
281 | @Override | |
282 | public LttngEventContent clone() { | |
283 | LttngEventContent clone = (LttngEventContent) super.clone(); | |
284 | LttngEventField[] fields = getFields(); | |
285 | clone.fFields = new LttngEventField[fields.length]; | |
286 | for (int i = 0; i < fields.length; i++) { | |
287 | clone.fFields[i] = fields[i].clone(); | |
288 | } | |
289 | clone.fFieldsMap = new HashMap<String, LttngEventField>(); | |
290 | for (String key : fFieldsMap.keySet()) { | |
291 | clone.fFieldsMap.put(new String(key), ((LttngEventField) fFieldsMap.get(key)).clone()); | |
292 | } | |
293 | return clone; | |
294 | } | |
295 | ||
5d10d135 | 296 | } |