rev version
[lwes-java.git] / src / org / lwes / Event.java
blob53a42071afc8ad81ea9b1ad2d8056ac403794d8c
1 package org.lwes;
3 import org.lwes.db.EventTemplateDB;
4 import org.lwes.serializer.Deserializer;
5 import org.lwes.serializer.DeserializerState;
6 import org.lwes.serializer.Serializer;
7 import org.lwes.util.CharacterEncoding;
8 import org.lwes.util.IPAddress;
9 import org.lwes.util.Log;
10 import org.lwes.util.NumberCodec;
12 import java.math.BigInteger;
13 import java.net.InetAddress;
14 import java.util.Arrays;
15 import java.util.Enumeration;
16 import java.util.concurrent.ConcurrentHashMap;
18 public class Event {
20 public static final int MAX_MESSAGE_SIZE = 65507;
22 /**
23 * Reserved metadata keywords
25 public static final String ENCODING = "enc";
26 public static final String RECEIPT_TIME = "ReceiptTime";
27 public static final String SENDER_IP = "SenderIP";
28 public static final String SENDER_PORT = "SenderPort";
30 /**
31 * Encoding variables
33 public static final short ISO_8859_1 = 0;
34 public static final short UTF_8 = 1;
35 public static final short DEFAULT_ENCODING = UTF_8;
36 public static final CharacterEncoding[] ENCODING_STRINGS = {
37 CharacterEncoding.ISO_8859_1, CharacterEncoding.UTF_8};
39 /**
40 * Event data
42 private ConcurrentHashMap<String, BaseType> attributes = new ConcurrentHashMap<String, BaseType>();
43 private String name = null;
44 private EventTemplateDB eventTemplateDB = null;
45 private short encoding = DEFAULT_ENCODING;
47 /**
48 * If this is set to true, types and attributes are validated against the EventTemplateDB
50 private boolean validating = true;
52 /**
53 * Internal object for deserialization state
55 private DeserializerState state = null;
57 /**
58 * the size of the event in bytes
60 private int bytesStoreSize = 0;
62 /**
63 * Create an event called <tt>eventName</tt>
65 * @param eventName the name of the event
66 * @param eventTemplateDB the EventTemplateDB to use for validation
67 * @throws NoSuchEventException if the Event does not exist in the EventTemplateDB
68 * @throws NoSuchAttributeException if an attribute does not exist in the EventTemplateDB
69 * @throws NoSuchAttributeTypeException if an attribute type does not exist in the EventTemplateDB
71 public Event(String eventName, EventTemplateDB eventTemplateDB)
72 throws EventSystemException {
73 this(eventName, true, eventTemplateDB);
76 /**
77 * Create an event called <tt>eventName</tt>
79 * @param eventName the name of the event
80 * @param validate true if the EventTemplateDB should be checked for types before all mutations
81 * @param eventTemplateDB the EventTemplateDB to use for validation
82 * @throws NoSuchEventException if the Event does not exist in the EventTemplateDB
83 * @throws NoSuchAttributeException if an attribute does not exist in the EventTemplateDB
84 * @throws NoSuchAttributeTypeException if an attribute type does not exist in the EventTemplateDB
86 public Event(String eventName, boolean validate, EventTemplateDB eventTemplateDB)
87 throws EventSystemException {
88 this(eventName, validate, eventTemplateDB, DEFAULT_ENCODING);
91 /**
92 * Create an event called <tt>eventName</tt>
94 * @param eventName the name of the event
95 * @param validate true if the EventTemplateDB should be checked for types before all mutations
96 * @param encoding the character encoding used by the event
97 * @throws NoSuchEventException if the Event does not exist in the EventTemplateDB
98 * @throws NoSuchAttributeException if an attribute does not exist in the EventTemplateDB
99 * @throws NoSuchAttributeTypeException if an attribute type does not exist in the EventTemplateDB
101 public Event(String eventName, boolean validate, EventTemplateDB eventTemplateDB, short encoding)
102 throws EventSystemException {
103 setEventTemplateDB(eventTemplateDB);
104 validating = validate;
105 setEventName(eventName);
106 setEncoding(encoding);
110 * Creates an event by deserializing a raw byte array.
112 * @param bytes the raw bytes to convert
113 * @param eventTemplateDB the EventTemplateDB to use to validate the event
114 * @throws NoSuchEventException
115 * @throws NoSuchAttributeException
116 * @throws NoSuchAttributeTypeException
118 public Event(byte[] bytes, EventTemplateDB eventTemplateDB)
119 throws EventSystemException {
120 this(bytes, true, eventTemplateDB);
125 * Creates an event by deserializing a raw byte array.
127 * @param bytes the raw bytes to convert
128 * @param validate whether or not to validate the event
129 * @param eventTemplateDB the EventTemplateDB to use to validate the event
130 * @throws NoSuchEventException
131 * @throws NoSuchAttributeException
132 * @throws NoSuchAttributeTypeException
134 public Event(byte[] bytes, boolean validate, EventTemplateDB eventTemplateDB)
135 throws EventSystemException {
136 setEventTemplateDB(eventTemplateDB);
137 validating = validate;
138 deserialize(bytes);
142 * Returns an enumeration of all the event attribute names
144 * @return an enumeration of attribute strings
146 public Enumeration<String> getEventAttributeNames() {
147 if (attributes == null) {
148 return null;
151 return attributes.keys();
155 * Returns the number of attributes in the event
157 * @return number of attributes in the event
159 public int size() {
160 if (attributes == null) {
161 return 0;
163 return attributes.size();
167 * Returns true if the event validates against the EventTemplateDB before making changes
169 * @return the validating state
171 public boolean isValidating() {
172 return this.validating;
176 * Set to true if the event should validate against the EventTemplateDB before making changes
178 * @param validate the validating value
180 public void setValidating(boolean validate) {
181 this.validating = validate;
185 * Returns the EventTemplateDB for this event, used for validation of types and attributes.
187 * @return the EventTemplateDB
189 public EventTemplateDB getEventTemplateDB() {
190 return this.eventTemplateDB;
194 * Sets the EventTemplateDB for this event, used for validation of types and attributes.
196 * @param eventTemplateDB the EventTemplateDB to be used for validation
198 public void setEventTemplateDB(EventTemplateDB eventTemplateDB) {
199 this.eventTemplateDB = eventTemplateDB;
203 * Returns the name of the event
205 * @return the name of the event
207 public synchronized String getEventName() {
208 return this.name;
212 * Sets the name of the Event
214 * @param name the name of the event
215 * @throws NoSuchEventException if the event is validating and does not exist in the EventTemplateDB
217 public synchronized void setEventName(String name) throws NoSuchEventException {
218 if (isValidating() && getEventTemplateDB() != null) {
219 if (!getEventTemplateDB().checkForEvent(name)) {
220 throw new NoSuchEventException("Event " + name + " does not exist in event definition");
224 /* determine if we already have the name and are just resetting it */
225 if (this.name != null) {
226 bytesStoreSize -= (this.name.length() + 1 + 2);
229 bytesStoreSize += (name.length() + 1 + 2);
231 this.name = name;
235 * Get the character encoding for this event
237 * @return the encoding
239 public short getEncoding() {
240 return this.encoding;
244 * Set the character encoding for event strings
246 * @param encoding the character encoding
247 * @throws NoSuchAttributeTypeException if the type for the encoding attribute does not exist
248 * @throws NoSuchAttributeException if the encoding attribute does not exist
250 public void setEncoding(short encoding) throws EventSystemException {
251 this.encoding = encoding;
252 setInt16(ENCODING, this.encoding);
256 * Generic accessor, checks if an attribute exists and returns its value. The user must do their
257 * own type checking.
259 * @param attributeName name of the attribute to lookup
260 * @return the object poitned to by attributeName
261 * @throws NoSuchAttributeException if the attribute does not exist in this event
263 public Object get(String attributeName) throws NoSuchAttributeException {
264 if (attributes == null) {
265 return null;
268 if (attributes.containsKey(attributeName)) {
269 return ((BaseType) (attributes.get(attributeName))).getTypeObject();
272 if (isValidating() && getEventTemplateDB() != null) {
273 if (getEventTemplateDB().checkForAttribute(name, attributeName)) {
274 return null;
276 else {
277 throw new NoSuchAttributeException("Attribute " + attributeName + " does not exist for event " + name);
281 return null;
285 * Accessor that returns a boolean value for attribute <tt>attributeName</tt>
287 * @param attributeName the name of the attribute to fetch
288 * @return the boolean value
289 * @throws AttributeNotSetException if the attribute has not been set in this event
290 * @throws NoSuchAttributeException if the attribute does not exist in this event
292 public boolean getBoolean(String attributeName) throws AttributeNotSetException, NoSuchAttributeException {
293 Object o = get(attributeName);
294 if (o != null) {
295 return ((Boolean) o).booleanValue();
297 else {
298 throw new AttributeNotSetException("Attribute " + attributeName + " not set");
303 * Accessor that returns an <tt>unsigned short</tt>, in the guise of an <tt>int</tt>, for attribute <tt>attributeName</tt>
305 * @param attributeName the name of the attribute to fetch
306 * @return the unsigned short as an int
307 * @throws AttributeNotSetException if the attribute has not been set in this event
308 * @throws NoSuchAttributeException if the attribute does not exist in this event
310 public int getUInt16(String attributeName) throws AttributeNotSetException, NoSuchAttributeException {
311 Object o = get(attributeName);
312 if (o != null) {
313 return ((Integer) o).intValue();
315 else {
316 throw new AttributeNotSetException("Attribute " + attributeName + " not set");
321 * Accessor that returns an <tt>short</tt>, for attribute <tt>attributeName</tt>
323 * @param attributeName the name of the attribute to fetch
324 * @return the short value
325 * @throws AttributeNotSetException if the attribute has not been set in this event
326 * @throws NoSuchAttributeException if the attribute does not exist in this event
328 public short getInt16(String attributeName) throws AttributeNotSetException, NoSuchAttributeException {
329 Object o = get(attributeName);
330 if (o != null) {
331 return ((Short) o).shortValue();
333 else {
334 throw new AttributeNotSetException("Attribute " + attributeName + " not set");
339 * Accessor that returns an <tt>unsigned int</tt>, in the guise of an <tt>long</tt>, for attribute <tt>attributeName</tt>
341 * @param attributeName the name of the attribute to fetch
342 * @return the unsigned int as a long
343 * @throws AttributeNotSetException if the attribute has not been set in this event
344 * @throws NoSuchAttributeException if the attribute does not exist in this event
346 public long getUInt32(String attributeName) throws AttributeNotSetException, NoSuchAttributeException {
347 Object o = get(attributeName);
348 if (o != null) {
349 return ((Long) o).longValue();
351 else {
352 throw new AttributeNotSetException("Attribute " + attributeName + " not set");
357 * Accessor that returns an <tt>int</tt>, for attribute <tt>attributeName</tt>
359 * @param attributeName the name of the attribute to fetch
360 * @return the int value
361 * @throws AttributeNotSetException if the attribute has not been set in this event
362 * @throws NoSuchAttributeException if the attribute does not exist in this event
364 public int getInt32(String attributeName) throws AttributeNotSetException, NoSuchAttributeException {
365 Object o = get(attributeName);
366 if (o != null) {
367 return ((Integer) o).intValue();
369 else {
370 throw new AttributeNotSetException("Attribute " + attributeName + " not set");
375 * Accessor that returns an <tt>unsigned long</tt>, in the guise of an <tt>BigInteger</tt>, for attribute <tt>attributeName</tt>
377 * @param attributeName the name of the attribute to fetch
378 * @return the unsigned long as a BigInteger
379 * @throws AttributeNotSetException if the attribute has not been set in this event
380 * @throws NoSuchAttributeException if the attribute does not exist in this event
382 public BigInteger getUInt64(String attributeName) throws AttributeNotSetException, NoSuchAttributeException {
383 Object o = get(attributeName);
384 if (o != null) {
385 return (BigInteger) o;
387 else {
388 throw new AttributeNotSetException("Attribute " + attributeName + " not set");
394 * Accessor that returns an <tt>long</tt>, for attribute <tt>attributeName</tt>
396 * @param attributeName the name of the attribute to fetch
397 * @return the long value
398 * @throws AttributeNotSetException if the attribute has not been set in this event
399 * @throws NoSuchAttributeException if the attribute does not exist in this event
401 public long getInt64(String attributeName) throws AttributeNotSetException, NoSuchAttributeException {
402 Object o = get(attributeName);
403 if (o != null) {
404 return ((Long) o).longValue();
406 else {
407 throw new AttributeNotSetException("Attribute " + attributeName + " not set");
412 * Accessor that returns an <tt>String</tt>, for attribute <tt>attributeName</tt>
414 * @param attributeName the name of the attribute to fetch
415 * @return the String value
416 * @throws AttributeNotSetException if the attribute has not been set in this event
417 * @throws NoSuchAttributeException if the attribute does not exist in this event
419 public String getString(String attributeName) throws AttributeNotSetException, NoSuchAttributeException {
420 Object o = get(attributeName);
421 if (o != null) {
422 return (String) o;
424 else {
425 throw new AttributeNotSetException("Attribute " + attributeName + " not set");
430 * Accessor that returns an <tt>InetAddress</tt>, for attribute <tt>attributeName</tt>
432 * @param attributeName the name of the attribute to fetch
433 * @return the InetAddress value
434 * @throws AttributeNotSetException if the attribute has not been set in this event
435 * @throws NoSuchAttributeException if the attribute does not exist in this event
437 public InetAddress getInetAddress(String attributeName) throws AttributeNotSetException, NoSuchAttributeException {
438 Object o = get(attributeName);
439 if (o != null) {
440 return ((IPAddress) o).toInetAddress();
442 else {
443 throw new AttributeNotSetException("Attribute " + attributeName + " not set");
448 * Accessor that returns an IP address in bytes, for attribute <tt>attributeName</tt>
450 * @param attributeName the name of the attribute to fetch
451 * @return the IP address in bytes
452 * @throws AttributeNotSetException if the attribute has not been set in this event
453 * @throws NoSuchAttributeException if the attribute does not exist in this event
455 public byte[] getIPAddress(String attributeName) throws AttributeNotSetException, NoSuchAttributeException {
456 Object o = get(attributeName);
457 if (o != null) {
458 return ((IPAddress) o).getInetAddressAsBytes();
460 else {
461 throw new AttributeNotSetException("Attribute " + attributeName + " not set");
467 * Set the object's attribute <tt>attributeName</tt> with the Object given
469 * @param attributeName the name of the attribute to set
470 * @param attributeValue the object to set the attribute with
471 * @throws NoSuchAttributeException if the attribute does not exist in this event
472 * @throws NoSuchAttributeTypeException if there is an attribute with an undefined type
474 public void set(String attributeName, Object attributeValue)
475 throws EventSystemException {
476 if (isValidating() && getEventTemplateDB() != null) {
477 if (getEventTemplateDB().checkForAttribute(getEventName(), attributeName)) {
478 BaseType bt = getEventTemplateDB().getBaseTypeForObjectAttribute(getEventName(),
479 attributeName, attributeValue);
480 set(attributeName, bt);
483 else {
484 throw new NoSuchAttributeException("Must be able to check the EventTemplateDB to use set(String,Object)");
489 * Private method to set a BaseType
491 * @param attribute the name of the attribute to set
492 * @param anObject the BaseType to set in the event
493 * @throws NoSuchAttributeException if the attribute does not exist in this event
494 * @throws NoSuchAttributeTypeException if there is an attribute with an undefined type
496 private void set(String attribute, BaseType anObject)
497 throws EventSystemException {
498 if (isValidating() && getEventTemplateDB() != null) {
499 if (getEventTemplateDB().checkForAttribute(name, attribute)) {
500 if (!getEventTemplateDB().checkTypeForAttribute(name, attribute, anObject)) {
501 throw new NoSuchAttributeTypeException("Wrong type '" + anObject.getTypeName() +
502 "' for " + name + "." + attribute);
505 else {
506 throw new NoSuchAttributeException("Attribute " + attribute + " does not exist for event " + name);
510 if (anObject.getTypeObject() != null) {
511 BaseType oldObject = null;
512 int newSize = bytesStoreSize + ((attribute.length() + 1) + anObject.bytesStoreSize(encoding));
513 if (newSize > MAX_MESSAGE_SIZE) {
514 throw new EventSystemException("Event size limit is " + MAX_MESSAGE_SIZE + " bytes.");
516 if ((oldObject = attributes.remove(attribute)) != null) {
517 bytesStoreSize -= (attribute.length() + 1) + oldObject.bytesStoreSize(encoding);
520 bytesStoreSize += (attribute.length() + 1) + anObject.bytesStoreSize(encoding);
521 attributes.put(attribute, anObject);
526 * Sets the given attribute with a <tt>boolean</tt> value given by <tt>aBool</tt>.
528 * @param attributeName the attribute to set
529 * @param aBool the boolean value to set
530 * @throws NoSuchAttributeException if the attribute does not exist in the event
531 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
533 public void setBoolean(String attributeName, boolean aBool)
534 throws EventSystemException {
535 setBoolean(attributeName, Boolean.valueOf(aBool));
539 * Sets the given attribute with a <tt>Boolean</tt> value given by <tt>aBool</tt>.
541 * @param attributeName the attribute to set
542 * @param aBool the boolean value to set
543 * @throws NoSuchAttributeException
544 * @throws NoSuchAttributeTypeException
546 public void setBoolean(String attributeName, Boolean aBool)
547 throws EventSystemException {
548 set(attributeName, new BaseType(TypeID.BOOLEAN_STRING, TypeID.BOOLEAN_TOKEN, aBool));
552 * Set the given attribute with the <tt>unsigned short</tt> value given by <tt>aNumber</tt>.
553 * Because Java does not support unsigned types, we must use a signed int to cover the range of unsigned short.
555 * @param attributeName the attribute to set
556 * @param aNumber the unsigned short value as an integer
557 * @throws NoSuchAttributeException if the attribute does not exist in the event
558 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
560 public void setUInt16(String attributeName, int aNumber)
561 throws EventSystemException {
562 setUInt16(attributeName, Integer.valueOf(aNumber));
566 * Set the given attribute with the <tt>Integer</tt> value given by <tt>aNumber</tt>.
567 * This should be an <tt>unsigned short</tt>, but is an Integer because Java does not support unsigned types,
568 * and a signed integer is needed to cover the range of an unsigned short.
570 * @param attributeName the attribute to set
571 * @param aNumber the value
573 public void setUInt16(String attributeName, Integer aNumber)
574 throws EventSystemException {
575 set(attributeName, new BaseType(TypeID.UINT16_STRING, TypeID.UINT16_TOKEN, aNumber));
579 * Set the given attribute with the <tt>short</tt> value given by <tt>aNumber</tt>.
581 * @param attributeName the attribute to set
582 * @param aNumber the short value to set
583 * @throws NoSuchAttributeException if the attribute does not exist in the event
584 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
586 public void setInt16(String attributeName, short aNumber)
587 throws EventSystemException {
588 setInt16(attributeName, Short.valueOf(aNumber));
592 * Set the given attribute with the <tt>Short</tt> value given by <tt>aNumber</tt>.
594 * @param attributeName the attribute to set
595 * @param aNumber the short value to set
596 * @throws NoSuchAttributeException if the attribute does not exist in the event
597 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
599 public void setInt16(String attributeName, Short aNumber)
600 throws EventSystemException {
601 set(attributeName, new BaseType(TypeID.INT16_STRING, TypeID.INT16_TOKEN, aNumber));
605 * Set the given attribute with the <tt>unsigned int</tt> value given by <tt>aNumber</tt>.
606 * Because Java does not support unsigned types, we must use a signed long to cover the range of an unsigned int.
608 * @param attributeName the attribute to set
609 * @param aNumber the unsigned int value as a long
610 * @throws NoSuchAttributeException if the attribute does not exist in the event
611 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
613 public void setUInt32(String attributeName, long aNumber)
614 throws EventSystemException {
615 setUInt32(attributeName, Long.valueOf(aNumber));
619 * Set the given attribute with the <tt>Long</tt> value given by <tt>aNumber</tt>.
620 * This should be an <tt>unsigned int</tt>, but is an Long because Java does not support unsigned types,
621 * and a signed long is needed to cover the range of an unsigned int.
623 * @param attributeName the attribute to set
624 * @param aNumber the value
626 public void setUInt32(String attributeName, Long aNumber)
627 throws EventSystemException {
628 set(attributeName, new BaseType(TypeID.UINT32_STRING, TypeID.UINT32_TOKEN, aNumber));
632 * Set the given attribute with the <tt>int</tt> value given by <tt>aNumber</tt>.
634 * @param attributeName the attribute to set
635 * @param aNumber the integer value to set
636 * @throws NoSuchAttributeException if the attribute does not exist in the event
637 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
639 public void setInt32(String attributeName, int aNumber)
640 throws EventSystemException {
641 setInt32(attributeName, Integer.valueOf(aNumber));
645 * Set the given attribute with the <tt>Integer</tt> value given by <tt>aNumber</tt>.
647 * @param attributeName the attribute to set
648 * @param aNumber the Integer value to set
649 * @throws NoSuchAttributeException if the attribute does not exist in the event
650 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
652 public void setInt32(String attributeName, Integer aNumber)
653 throws EventSystemException {
654 set(attributeName, new BaseType(TypeID.INT32_STRING, TypeID.INT32_TOKEN, aNumber));
658 * Set the given attribute with the <tt>unsigned long</tt> value given by <tt>aNumber</tt>.
660 * @param attributeName the attribute to set
661 * @param aNumber the value
662 * @throws NoSuchAttributeException if the attribute does not exist in the event
663 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
665 public void setUInt64(String attributeName, long aNumber)
666 throws EventSystemException {
667 set(attributeName, new BaseType(TypeID.UINT64_STRING, TypeID.UINT64_TOKEN, BigInteger.valueOf(aNumber)));
671 * Set the given attribute with the <tt>Long</tt> value given by <tt>aNumber</tt>.
673 * @param attributeName the attribute to set
674 * @param aNumber the value
675 * @throws NoSuchAttributeException if the attribute does not exist in the event
676 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
678 public void setUInt64(String attributeName, Long aNumber)
679 throws EventSystemException {
680 set(attributeName,
681 new BaseType(TypeID.UINT64_STRING, TypeID.UINT64_TOKEN, BigInteger.valueOf(aNumber.longValue())));
685 * Set the given attribute with the <tt>BigInteger</tt> value given by <tt>aNumber</tt>.
686 * This should be an <tt>unsigned long</tt>, but is an BigInteger because Java does not support unsigned types,
687 * and a BigInteger is needed to cover the range of an unsigned long.
689 * @param attributeName the attribute to set
690 * @param aNumber the value
692 public void setUInt64(String attributeName, BigInteger aNumber)
693 throws EventSystemException {
694 set(attributeName, new BaseType(TypeID.UINT64_STRING, TypeID.UINT64_TOKEN, aNumber));
698 * Set the given attribute with the <tt>long</tt> value given by <tt>aNumber</tt>.
700 * @param attributeName the attribute to set
701 * @param aNumber the long value to set
702 * @throws NoSuchAttributeException if the attribute does not exist in the event
703 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
705 public void setInt64(String attributeName, long aNumber)
706 throws EventSystemException {
707 setInt64(attributeName, Long.valueOf(aNumber));
711 * Set the given attribute with the <tt>Long</tt> value given by <tt>aNumber</tt>.
713 * @param attributeName the attribute to set
714 * @param aNumber the Long value to set
715 * @throws NoSuchAttributeException if the attribute does not exist in the event
716 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
718 public void setInt64(String attributeName, Long aNumber)
719 throws EventSystemException {
720 set(attributeName, new BaseType(TypeID.INT64_STRING, TypeID.INT64_TOKEN, aNumber));
724 * Set the given attribute with a <tt>String</tt>
726 * @param attributeName the attribute to set
727 * @param aString the String value to set
728 * @throws NoSuchAttributeException if the attribute does not exist in the event
729 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
731 public void setString(String attributeName, String aString)
732 throws EventSystemException {
733 set(attributeName, new BaseType(TypeID.STRING_STRING, TypeID.STRING_TOKEN, aString));
737 * Set the given attribute with the <tt>ip address</tt> value given by <tt>address</tt>
739 * @param attributeName the attribute to set
740 * @param address the ip address in bytes
741 * @throws NoSuchAttributeException if the attribute does not exist in the event
742 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
744 public void setIPAddress(String attributeName, byte[] address)
745 throws EventSystemException {
746 setIPAddress(attributeName, new IPAddress(address));
750 * Set the given attribute with the <tt>ip address</tt> value given by <tt>address</tt>
752 * @param attributeName the attribute to set
753 * @param address the ip address in bytes
754 * @throws NoSuchAttributeException if the attribute does not exist in the event
755 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
757 public void setIPAddress(String attributeName, InetAddress address)
758 throws EventSystemException {
759 setIPAddress(attributeName, new IPAddress(address));
763 * Set the given attribute with the <tt>ip address</tt> value given by <tt>address</tt>
765 * @param attributeName the attribute to set
766 * @param address the ip address in bytes
767 * @throws NoSuchAttributeException if the attribute does not exist in the event
768 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
770 public void setIPAddress(String attributeName, IPAddress address)
771 throws EventSystemException {
772 set(attributeName, new BaseType(TypeID.IPADDR_STRING, TypeID.IPADDR_TOKEN, address));
776 * Serializes the Event into a byte array
778 * @return the serialized byte array
780 public byte[] serialize() {
782 * Serialization uses the following protocol
783 * EVENTWORD,<number of elements>,ATTRIBUTEWORD,TYPETOKEN,
784 * (UINT16|INT16|UINT32|INT32|UINT64|INT64|BOOLEAN|STRING)
785 * ...ATTRIBUTEWORD,TYPETOKEN(UINT16|INT16|UINT32|INT32|
786 * UINT64|INT64|BOOLEAN|STRING)
788 * The first attribute will always be the encoding if present.
790 byte[] bytes = new byte[this.bytesStoreSize];
791 int offset = 0;
792 int attributeCount = 0;
793 short encoding = DEFAULT_ENCODING;
795 if (attributes != null) {
796 attributeCount = attributes.size();
799 offset += Serializer.serializeEVENTWORD(name, bytes, offset);
800 offset += Serializer.serializeUINT16((short) (attributeCount), bytes, offset);
803 * Set the encoding attributes in the event
805 if (attributes != null) {
806 BaseType encodingBase = attributes.get(ENCODING);
807 if (encodingBase != null) {
808 Object encodingObj = encodingBase.getTypeObject();
809 byte encodingType = encodingBase.getTypeToken();
810 if (encodingObj != null) {
811 if (encodingType == TypeID.INT16_TOKEN) {
812 encoding = (Short) encodingObj;
813 Log.trace("Character encoding: " + encoding);
814 offset += Serializer.serializeATTRIBUTEWORD(ENCODING, bytes, offset);
815 offset += Serializer.serializeBYTE(encodingType, bytes, offset);
816 offset += Serializer.serializeUINT16(encoding, bytes, offset);
820 else {
821 Log.warning("Character encoding null in event " + name);
824 Enumeration<String> e = attributes.keys();
825 while (e.hasMoreElements()) {
826 String key = e.nextElement();
827 if (key.equals(ENCODING)) {
828 continue;
831 BaseType value = (BaseType) attributes.get(key);
832 Object data = value.getTypeObject();
833 byte typeToken = value.getTypeToken();
835 /* don't try to serialize nulls */
836 if (data == null) {
837 Log.warning("Attribute " + key + " was null in event " + name);
838 continue;
841 offset += Serializer.serializeATTRIBUTEWORD(key, bytes, offset);
842 offset += Serializer.serializeBYTE(typeToken, bytes, offset);
844 switch (typeToken) {
845 case TypeID.BOOLEAN_TOKEN:
846 offset += Serializer.serializeBOOLEAN((Boolean) data, bytes, offset);
847 break;
848 case TypeID.UINT16_TOKEN:
849 offset += Serializer.serializeUINT16((Integer) data, bytes, offset);
850 break;
851 case TypeID.INT16_TOKEN:
852 offset += Serializer.serializeINT16((Short) data, bytes, offset);
853 break;
854 case TypeID.UINT32_TOKEN:
855 offset += Serializer.serializeUINT32((Long) data, bytes, offset);
856 break;
857 case TypeID.INT32_TOKEN:
858 offset += Serializer.serializeINT32((Integer) data, bytes, offset);
859 break;
860 case TypeID.UINT64_TOKEN:
861 offset += Serializer.serializeUINT64((BigInteger) data, bytes, offset);
862 break;
863 case TypeID.INT64_TOKEN:
864 offset += Serializer.serializeINT64((Long) data, bytes, offset);
865 break;
866 case TypeID.STRING_TOKEN:
867 offset += Serializer.serializeSTRING(((String) data), bytes, offset, encoding);
868 break;
869 case TypeID.IPADDR_TOKEN:
870 offset += Serializer.serializeIPADDR(((IPAddress) data), bytes, offset);
871 break;
872 default:
873 Log.warning("Unknown BaseType token: " + typeToken);
874 break;
875 } // switch(typeToken)
877 Log.trace("Serialized attribute " + key);
878 } // while(e.hasMoreElements())
879 } // if(attributes != null)
881 return bytes;
885 * Deserialize the Event from byte array
887 * @param bytes the byte array containing a serialized Event
889 public void deserialize(byte[] bytes)
890 throws EventSystemException {
891 if (bytes == null) {
892 return;
894 if (state == null) {
895 state = new DeserializerState();
898 state.reset();
899 setEventName(Deserializer.deserializeEVENTWORD(state, bytes));
900 long num = Deserializer.deserializeUINT16(state, bytes);
901 if (Log.isLogTrace()) {
902 Log.trace("Event name = " + getEventName());
903 Log.trace("Number of attribute: " + num);
905 for (int i = 0; i < num; ++i) {
906 String attribute = Deserializer.deserializeATTRIBUTEWORD(state, bytes);
908 byte type = Deserializer.deserializeBYTE(state, bytes);
909 if (Log.isLogTrace()) {
910 Log.trace("Attribute: " + attribute);
911 Log.trace("Type: " + TypeID.byteIDToString(type));
912 Log.trace("State: " + state);
914 if (attribute != null) {
915 if (i == 0 && attribute.equals(ENCODING)) {
916 if (type == TypeID.INT16_TOKEN) {
917 setEncoding(Deserializer.deserializeINT16(state, bytes));
918 continue;
920 else {
921 Log.warning("Found encoding, but type was not int16 while deserializing");
925 switch (type) {
926 case TypeID.BOOLEAN_TOKEN:
927 boolean aBool = Deserializer.deserializeBOOLEAN(state, bytes);
928 setBoolean(attribute, aBool);
929 break;
930 case TypeID.UINT16_TOKEN:
931 int uShort = Deserializer.deserializeUINT16(state, bytes);
932 setUInt16(attribute, uShort);
933 break;
934 case TypeID.INT16_TOKEN:
935 short aShort = Deserializer.deserializeINT16(state, bytes);
936 setInt16(attribute, aShort);
937 break;
938 case TypeID.UINT32_TOKEN:
939 long uInt = Deserializer.deserializeUINT32(state, bytes);
940 setUInt32(attribute, uInt);
941 break;
942 case TypeID.INT32_TOKEN:
943 int aInt = Deserializer.deserializeINT32(state, bytes);
944 setInt32(attribute, aInt);
945 break;
946 case TypeID.UINT64_TOKEN:
947 long uLong = Deserializer.deserializeUINT64(state, bytes);
948 setUInt64(attribute, BigInteger.valueOf(uLong));
949 break;
950 case TypeID.INT64_TOKEN:
951 long aLong = Deserializer.deserializeINT64(state, bytes);
952 setInt64(attribute, aLong);
953 break;
954 case TypeID.STRING_TOKEN:
955 String s = Deserializer.deserializeSTRING(state, bytes, encoding);
956 setString(attribute, s);
957 break;
958 case TypeID.IPADDR_TOKEN:
959 byte[] inetAddress = Deserializer.deserializeIPADDR(state, bytes);
960 setIPAddress(attribute, inetAddress);
961 break;
962 default:
963 Log.warning("Unknown type " + type + " in deserialization");
966 } // for (int i =0 ...
971 * Returns a mutable copy of the event. This is a SLOW operation.
973 * @return Event the Event object
974 * @throws NoSuchEventException if the Event does not exist in the EventTemplateDB
975 * @throws NoSuchAttributeException if the attribute does not exist in this event
976 * @throws NoSuchAttributeTypeException if there is an attribute that does not match a type in the EventTemplateDB
978 public Event copy() throws EventSystemException {
979 /* match the type-checking of the original event */
980 Event evt = new Event(name, isValidating(), getEventTemplateDB());
981 for (Enumeration<String> e = attributes.keys(); e.hasMoreElements();) {
982 String key = e.nextElement();
983 BaseType value = attributes.get(key);
984 evt.set(key, value);
987 return evt;
991 * Returns a String representation of this event
993 * @return a String return of this event.
995 public String toString() {
996 if (name == null) {
997 return "";
1000 StringBuffer sb = new StringBuffer();
1001 sb.append(name);
1002 sb.append("\n{\n");
1004 if (attributes != null) {
1005 int i = 0;
1006 String[] keys = new String[attributes.size()];
1007 for (Enumeration<String> e = attributes.keys(); e.hasMoreElements();) {
1008 keys[i++] = e.nextElement();
1011 Arrays.sort(keys);
1013 for (i = 0; i < attributes.size(); ++i) {
1014 BaseType value = attributes.get(keys[i]);
1015 if (isValidating() && getEventTemplateDB() != null) {
1016 if (getEventTemplateDB().checkTypeForAttribute(name, keys[i], TypeID.UINT64_STRING)) {
1017 try {
1018 sb.append("\t")
1019 .append(keys[i])
1020 .append(" = ")
1021 .append(NumberCodec.toHexString(getUInt64(keys[i])))
1022 .append(";\n");
1023 } catch(EventSystemException exc) {
1024 Log.warning("Event.toString : ", exc);
1027 else {
1028 sb.append("\t").append(keys[i]).append(" = ").append(value).append(";\n");
1030 } else {
1031 sb.append("\t").append(keys[i]).append(" = ").append(value).append(";\n");
1033 } // for(i = 0; i < attributes.size() ...
1034 } // if(attributes != null)
1036 sb.append("}");
1037 return sb.toString();
1040 @Override
1041 public int hashCode() {
1042 return toString().hashCode();
1045 public boolean equals(Object o) {
1046 if (o == null) {
1047 return false;
1049 if(getClass().getName().equals(o.getClass().getName())) {
1050 return toString().equals(o.toString());
1051 } else {
1052 return false;