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
.core
.event
;
15 import java
.util
.HashMap
;
17 import org
.eclipse
.linuxtools
.lttng
.jni
.JniEvent
;
18 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventField
;
23 * <b><u>LttngEventContent</u></b><p>
25 * Lttng specific implementation of the TmfEventContent.<p>
27 public class LttngEventContent
extends TmfEventField
{
29 private LttngEvent fParentEvent
;
31 // Hash map that contain the (parsed) fields. This is the actual payload of the event.
32 private HashMap
<String
, LttngEventField
> fFieldsMap
= new HashMap
<String
, LttngEventField
>();
35 * Default constructor.<p>
38 public LttngEventContent() {
39 super(ITmfEventField
.ROOT_ID
, null);
43 * Constructor with parameters.<p>
45 * @param thisParent Parent event for this content.
47 * @see org.eclipse.linuxtools.lttng.core.event.LttngEvent
49 public LttngEventContent(LttngEvent thisParent
) {
50 super(ITmfEventField
.ROOT_ID
, null);
51 fParentEvent
= thisParent
;
55 * Constructor with parameters, with optional content.<p>
57 * @param thisParent Parent event for this content.
58 * @param thisContent Already parsed content.
60 * @see org.eclipse.linuxtools.lttng.core.event.LttngEvent
62 public LttngEventContent(LttngEvent thisParent
, HashMap
<String
, LttngEventField
> thisContent
) {
63 super(ITmfEventField
.ROOT_ID
, null);
64 fParentEvent
= thisParent
;
65 fFieldsMap
= thisContent
;
69 * Copy Constructor.<p>
71 * @param oldContent Content to copy from
73 public LttngEventContent(LttngEventContent oldContent
) {
74 this((LttngEvent
) oldContent
.getEvent(), oldContent
.getMapContent());
77 public LttngEvent
getEvent() {
81 public void setEvent(LttngEvent newParent
) {
82 fParentEvent
= newParent
;
87 // These are not very useful, are they?
89 // public LttngEventType getType() {
90 // return (LttngEventType)fParentEvent.getType();
93 // public void setType(LttngEventType newType) {
94 // ((LttngEvent)fParentEvent).setType(newType);
99 // Find a better way to ensure content is sane!!
100 public void emptyContent() {
105 // A bit weird to return the _currently_parsed fields (unlike all fields like getFields() )
106 // Should we keep this?
108 * Return currently parsed fields in an object array format.<p>
110 * @return Currently parsed fields.
112 public Object
[] getRawContent() {
113 Object
[] returnedContent
= fFieldsMap
.values().toArray(new Object
[fFieldsMap
.size()]);
114 return returnedContent
;
118 * Return currently parsed fields in the internal hashmap format.<p>
120 * @return Currently parsed fields.
122 public HashMap
<String
, LttngEventField
> getMapContent() {
126 // @SuppressWarnings("unchecked")
128 // public LttngEventField[] getFields() {
129 // LttngEventField tmpField = null;
132 // // SLOW! SLOW! SLOW! We should prevent the user to use this!!
133 // HashMap<String, Object> parsedContent = parseContent();
135 // String contentKey = null;
136 // Iterator<String> contentItr = parsedContent.keySet().iterator();
137 // while ( contentItr.hasNext() ) {
138 // contentKey = contentItr.next();
140 // tmpField = new LttngEventField(this, contentKey, parsedContent.get(contentKey));
141 // ((HashMap<String, LttngEventField>)fFields).put(contentKey, tmpField);
144 // return fFields.values().toArray(new LttngEventField[fFields.size()]);
148 * Parse all fields and return them as an array of LttngFields.<p>
150 * Note : This function is heavy and should only be called if all fields are really needed.
152 * @return All fields.
154 * @see @see org.eclipse.linuxtools.lttng.event.LttngEventField
157 public synchronized LttngEventField
[] getFields() {
158 int nbFields
= fParentEvent
.getType().getFieldNames().length
;
160 if (fFieldsMap
.size() < nbFields
) {
161 LttngEventField tmpField
= null;
162 LttngEventType tmpType
= (LttngEventType
)fParentEvent
.getType();
164 for (int pos
=0; pos
< nbFields
; pos
++) {
166 LttngEvent lttngTmpEvent
= (LttngEvent
)getEvent(); //added for easier debugging
167 JniEvent tmpEvent
= (lttngTmpEvent
).convertEventTmfToJni();
169 // tmpEvent == null probably mean there is a discrepancy between Eclipse and C library
170 // An error was probably printed in convertEventTmfToJni() already, but keep in mind this is SERIOUS
171 if ( tmpEvent
!= null ) {
173 name
= tmpType
.getFieldName(pos
);
175 Object newValue
= tmpEvent
.parseFieldByName(name
);
176 tmpField
= new LttngEventField(name
, newValue
, null);
177 fFieldsMap
.put(name
, tmpField
);
179 // catch (TmfNoSuchFieldException e) {
180 // System.out.println("Invalid field position requested : " + pos + ", ignoring (getFields)."); //$NON-NLS-1$//$NON-NLS-2$
185 return fFieldsMap
.values().toArray(new LttngEventField
[fFieldsMap
.size()]);
189 * Parse a single field from its given position.<p>
191 * @return The parsed field or null.
193 * @see @see org.eclipse.linuxtools.lttng.event.LttngEventField
196 public LttngEventField
getField(int position
) {
197 LttngEventField returnedField
= null;
200 label
= fParentEvent
.getType().getFieldName(position
);
201 returnedField
= (LttngEventField
) this.getField(label
);
203 // catch (TmfNoSuchFieldException e) {
204 // System.out.println("Invalid field position requested : " + position + ", ignoring (getField)."); //$NON-NLS-1$//$NON-NLS-2$
207 return returnedField
;
211 * Parse a single field from its given name.<p>
213 * @return The parsed field or null.
215 * @see @see org.eclipse.linuxtools.lttng.event.LttngEventField
218 public synchronized LttngEventField
getField(String name
) {
220 // Check for generic table header fields
221 if (name
.equals(LttngEventType
.CONTENT_LABEL
) || name
.equals(ITmfEvent
.EVENT_FIELD_CONTENT
)) {
222 return new LttngEventField(toString());
223 } else if (name
.equals(LttngEventType
.MARKER_LABEL
) || name
.equals(ITmfEvent
.EVENT_FIELD_TYPE
)) {
224 return new LttngEventField(fParentEvent
.getType().getName());
225 } else if (name
.equals(LttngEventType
.TRACE_LABEL
) || name
.equals(ITmfEvent
.EVENT_FIELD_REFERENCE
)) {
226 return new LttngEventField(fParentEvent
.getReference());
227 } else if (name
.equals(LttngEventType
.TIMESTAMP_LABEL
) || name
.equals(ITmfEvent
.EVENT_FIELD_TIMESTAMP
)) {
228 return new LttngEventField(fParentEvent
.getTimestamp().toString());
229 } else if (name
.equals(ITmfEvent
.EVENT_FIELD_SOURCE
)) {
230 return new LttngEventField(fParentEvent
.getSource());
234 // Should we check if the field exists in LttngType before parsing?
235 // It could avoid calling parse for non-existent fields but would waste some cpu cycle on check?
236 LttngEventField returnedField
= fFieldsMap
.get(name
);
238 if ( returnedField
== null ) {
240 // Should we really make sure we didn't get null before creating/inserting a field?
241 JniEvent tmpEvent
= ((LttngEvent
)getEvent()).convertEventTmfToJni();
243 if ( tmpEvent
!= null) {
244 Object newValue
= tmpEvent
.parseFieldByName(name
);
246 if ( newValue
!= null ) {
247 returnedField
= new LttngEventField(name
, newValue
);
248 returnedField
= new LttngEventField(name
, newValue
);
249 fFieldsMap
.put(name
, returnedField
);
254 return returnedField
;
258 // // *** Is this even useful?
259 // protected void parseContent() {
260 // fSubfields = getFields();
264 * toString() method to print the content
266 * Note : this function parse all fields and so is very heavy to use.
269 @SuppressWarnings("nls")
270 public String
toString() {
271 LttngEventField
[] allFields
= getFields();
273 StringBuffer strBuffer
= new StringBuffer();
274 for ( int pos
=0; pos
< allFields
.length
; pos
++) {
275 if (pos
!= 0) strBuffer
.append(",");
276 strBuffer
.append(allFields
[pos
].toString());
279 return strBuffer
.toString();
283 public LttngEventContent
clone() {
284 LttngEventContent clone
= (LttngEventContent
) super.clone();
285 LttngEventField
[] fields
= getFields();
286 LttngEventField
[] subfields
= new LttngEventField
[fields
.length
];
287 for (int i
= 0; i
< fields
.length
; i
++) {
288 subfields
[i
] = (LttngEventField
) fields
[i
].clone();
290 clone
.setValue(getValue(), subfields
);
291 clone
.fFieldsMap
= new HashMap
<String
, LttngEventField
>();
292 for (String key
: fFieldsMap
.keySet()) {
293 clone
.fFieldsMap
.put(new String(key
), ((LttngEventField
) fFieldsMap
.get(key
)).clone());