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
.event
;
15 import java
.util
.HashMap
;
17 import org
.eclipse
.linuxtools
.lttng
.jni
.JniEvent
;
18 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEventContent
;
19 import org
.eclipse
.linuxtools
.tmf
.event
.TmfNoSuchFieldException
;
22 * <b><u>LttngEventContent</u></b><p>
24 * Lttng specific implementation of the TmfEventContent.<p>
26 public class LttngEventContent
extends TmfEventContent
{
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
>();
32 * Default constructor.<p>
36 public LttngEventContent() {
41 * Constructor with parameters.<p>
43 * @param thisParent Parent event for this content.
45 * @see org.eclipse.linuxtools.lttng.event.LttngEvent
47 public LttngEventContent(LttngEvent thisParent
) {
48 super(thisParent
, null);
52 * Constructor with parameters, with optional content.<p>
54 * @param thisParent Parent event for this content.
55 * @param thisContent Already parsed content.
57 * @see org.eclipse.linuxtools.lttng.event.LttngEvent
59 public LttngEventContent(LttngEvent thisParent
, HashMap
<String
, LttngEventField
> thisContent
) {
60 super(thisParent
, null);
62 fFieldsMap
= thisContent
;
66 * Copy Constructor.<p>
68 * @param oldContent Content to copy from
70 public LttngEventContent(LttngEventContent oldContent
) {
71 this((LttngEvent
)oldContent
.getEvent(), oldContent
.getRawContent() );
76 public LttngEvent
getEvent() {
77 return (LttngEvent
)fParentEvent
;
80 public void setEvent(LttngEvent newParent
) {
81 fParentEvent
= newParent
;
86 // These are not very useful, are they?
88 public LttngEventType
getType() {
89 return (LttngEventType
)fParentEvent
.getType();
91 public void setType(LttngEventType newType
) {
92 ((LttngEvent
)fParentEvent
).setType(newType
);
97 // Find a better way to ensure content is sane!!
98 public void emptyContent() {
103 // A bit weird to return the _currently_parsed fields (unlike all fields like getFields() )
104 // Should we keep this?
106 * Return currently parsed fields in an object array format.<p>
108 * @return Currently parsed fields.
111 public Object
[] getContent() {
112 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
> getRawContent() {
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 if ( fFieldsMap
.size() < fParentEvent
.getType().getNbFields() ) {
159 LttngEventField tmpField
= null;
160 LttngEventType tmpType
= (LttngEventType
)fParentEvent
.getType();
162 for ( int pos
=0; pos
<tmpType
.getNbFields(); pos
++ ) {
164 LttngEvent lttngTmpEvent
= (LttngEvent
)getEvent(); //added for easier debugging
165 JniEvent tmpEvent
= (lttngTmpEvent
).convertEventTmfToJni();
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 ) {
171 name
= tmpType
.getLabel(pos
);
173 Object newValue
= tmpEvent
.parseFieldByName(name
);
174 tmpField
= new LttngEventField(this, name
, newValue
);
175 fFieldsMap
.put(name
, tmpField
);
177 catch (TmfNoSuchFieldException e
) {
178 System
.out
.println("Invalid field position requested : " + pos
+ ", ignoring (getFields)."); //$NON-NLS-1$//$NON-NLS-2$
183 return fFieldsMap
.values().toArray(new LttngEventField
[fFieldsMap
.size()]);
187 * Parse a single field from its given position.<p>
189 * @return The parsed field or null.
191 * @see @see org.eclipse.linuxtools.lttng.event.LttngEventField
194 public LttngEventField
getField(int position
) {
195 LttngEventField returnedField
= null;
198 label
= fParentEvent
.getType().getLabel(position
);
200 returnedField
= (LttngEventField
) this.getField(label
);
202 catch (TmfNoSuchFieldException e
) {
203 System
.out
.println("Invalid field position requested : " + position
+ ", ignoring (getField)."); //$NON-NLS-1$//$NON-NLS-2$
206 return returnedField
;
210 * Parse a single field from its given name.<p>
212 * @return The parsed field or null.
214 * @see @see org.eclipse.linuxtools.lttng.event.LttngEventField
217 public synchronized Object
getField(String name
) {
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();
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
);
237 if ( returnedField
== null ) {
239 // Should we really make sure we didn't get null before creating/inserting a field?
240 JniEvent tmpEvent
= ((LttngEvent
)getEvent()).convertEventTmfToJni();
242 if ( tmpEvent
!= null) {
243 Object newValue
= tmpEvent
.parseFieldByName(name
);
245 if ( newValue
!= null ) {
246 returnedField
= new LttngEventField(this, name
, newValue
);
247 fFieldsMap
.put(name
, returnedField
);
252 return returnedField
;
256 // *** Is this even useful?
258 protected void parseContent() {
259 fFields
= getFields();
263 * toString() method to print the content
265 * Note : this function parse all fields and so is very heavy to use.
268 @SuppressWarnings("nls")
269 public String
toString() {
270 LttngEventField
[] allFields
= getFields();
272 StringBuffer strBuffer
= new StringBuffer();
273 for ( int pos
=0; pos
< allFields
.length
; pos
++) {
274 if (pos
!= 0) strBuffer
.append(",");
275 strBuffer
.append(allFields
[pos
].toString());
278 return strBuffer
.toString();
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();
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());