fixed getInetAddress
[lwes-java.git] / src / main / java / org / lwes / Event.java
blobf3e53f599c4ca4cfda0cdae42529c791fe1b6530
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 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 * Method to check if an attribute is set in the event. This method does not throw
286 * NoSuchAttributeException because it shouldn't really care. If it's not there, it's
287 * not there.
289 * @param attributeName The attribute name to check for existance.
290 * @return true if there is a value, false if not.
292 public boolean isSet(String attributeName) {
293 try {
294 return (get(attributeName) != null);
296 catch (NoSuchAttributeException e) {
297 return false;
302 * Accessor that returns a boolean value for attribute <tt>attributeName</tt>
304 * @param attributeName the name of the attribute to fetch
305 * @return the boolean value
306 * @throws NoSuchAttributeException if the attribute does not exist in this event
308 public Boolean getBoolean(String attributeName) throws NoSuchAttributeException {
309 return (Boolean) get(attributeName);
313 * Accessor that returns an <tt>unsigned short</tt>, in the guise of an <tt>int</tt>, for attribute <tt>attributeName</tt>
315 * @param attributeName the name of the attribute to fetch
316 * @return the unsigned short as an int
317 * @throws NoSuchAttributeException if the attribute does not exist in this event
319 public Integer getUInt16(String attributeName) throws NoSuchAttributeException {
320 return (Integer) get(attributeName);
324 * Accessor that returns an <tt>short</tt>, for attribute <tt>attributeName</tt>
326 * @param attributeName the name of the attribute to fetch
327 * @return the short value
328 * @throws NoSuchAttributeException if the attribute does not exist in this event
330 public Short getInt16(String attributeName) throws NoSuchAttributeException {
331 return (Short) get(attributeName);
335 * Accessor that returns an <tt>unsigned int</tt>, in the guise of an <tt>long</tt>, for attribute <tt>attributeName</tt>
337 * @param attributeName the name of the attribute to fetch
338 * @return the unsigned int as a long
339 * @throws NoSuchAttributeException if the attribute does not exist in this event
341 public Long getUInt32(String attributeName) throws NoSuchAttributeException {
342 return (Long) get(attributeName);
346 * Accessor that returns an <tt>int</tt>, for attribute <tt>attributeName</tt>
348 * @param attributeName the name of the attribute to fetch
349 * @return the int value
350 * @throws NoSuchAttributeException if the attribute does not exist in this event
352 public Integer getInt32(String attributeName) throws NoSuchAttributeException {
353 return (Integer) get(attributeName);
357 * Accessor that returns an <tt>unsigned long</tt>, in the guise of an <tt>BigInteger</tt>, for attribute <tt>attributeName</tt>
359 * @param attributeName the name of the attribute to fetch
360 * @return the unsigned long as a BigInteger
361 * @throws NoSuchAttributeException if the attribute does not exist in this event
363 public BigInteger getUInt64(String attributeName) throws NoSuchAttributeException {
364 return (BigInteger) get(attributeName);
369 * Accessor that returns an <tt>long</tt>, for attribute <tt>attributeName</tt>
371 * @param attributeName the name of the attribute to fetch
372 * @return the long value
373 * @throws NoSuchAttributeException if the attribute does not exist in this event
375 public Long getInt64(String attributeName) throws NoSuchAttributeException {
376 return (Long) get(attributeName);
380 * Accessor that returns an <tt>String</tt>, for attribute <tt>attributeName</tt>
382 * @param attributeName the name of the attribute to fetch
383 * @return the String value
384 * @throws NoSuchAttributeException if the attribute does not exist in this event
386 public String getString(String attributeName) throws NoSuchAttributeException {
387 return (String) get(attributeName);
391 * Accessor that returns an <tt>InetAddress</tt>, for attribute <tt>attributeName</tt>
393 * @param attributeName the name of the attribute to fetch
394 * @return the InetAddress value
395 * @throws NoSuchAttributeException if the attribute does not exist in this event
397 public InetAddress getInetAddress(String attributeName) throws NoSuchAttributeException {
398 IPAddress a = (IPAddress) get(attributeName);
399 if (a != null) {
400 return a.toInetAddress();
402 else {
403 return null;
408 * Accessor that returns an IP address in bytes, for attribute <tt>attributeName</tt>
410 * @param attributeName the name of the attribute to fetch
411 * @return the IP address in bytes
412 * @throws NoSuchAttributeException if the attribute does not exist in this event
414 public byte[] getIPAddress(String attributeName) throws NoSuchAttributeException {
415 return (byte[]) get(attributeName);
420 * Set the object's attribute <tt>attributeName</tt> with the Object given
422 * @param attributeName the name of the attribute to set
423 * @param attributeValue the object to set the attribute with
424 * @throws NoSuchAttributeException if the attribute does not exist in this event
425 * @throws NoSuchAttributeTypeException if there is an attribute with an undefined type
427 public void set(String attributeName, Object attributeValue)
428 throws EventSystemException {
429 if (isValidating() && getEventTemplateDB() != null) {
430 if (getEventTemplateDB().checkForAttribute(getEventName(), attributeName)) {
431 BaseType bt = getEventTemplateDB().getBaseTypeForObjectAttribute(getEventName(),
432 attributeName, attributeValue);
433 set(attributeName, bt);
436 else {
437 throw new NoSuchAttributeException("Must be able to check the EventTemplateDB to use set(String,Object)");
442 * Private method to set a BaseType
444 * @param attribute the name of the attribute to set
445 * @param anObject the BaseType to set in the event
446 * @throws NoSuchAttributeException if the attribute does not exist in this event
447 * @throws NoSuchAttributeTypeException if there is an attribute with an undefined type
449 private void set(String attribute, BaseType anObject)
450 throws EventSystemException {
452 if (isValidating() && getEventTemplateDB() != null) {
453 if (getEventTemplateDB().checkForAttribute(name, attribute)) {
454 if (!getEventTemplateDB().checkTypeForAttribute(name, attribute, anObject)) {
455 throw new NoSuchAttributeTypeException("Wrong type '" + anObject.getTypeName() +
456 "' for " + name + "." + attribute);
459 else {
460 throw new NoSuchAttributeException("Attribute " + attribute + " does not exist for event " + name);
464 if (anObject.getTypeObject() != null) {
465 BaseType oldObject = null;
466 int newSize = bytesStoreSize + ((attribute.length() + 1) + anObject.bytesStoreSize(encoding));
467 if (newSize > MAX_MESSAGE_SIZE) {
468 throw new EventSystemException("Event size limit is " + MAX_MESSAGE_SIZE + " bytes.");
470 if ((oldObject = attributes.remove(attribute)) != null) {
471 bytesStoreSize -= (attribute.length() + 1) + oldObject.bytesStoreSize(encoding);
474 bytesStoreSize += (attribute.length() + 1) + anObject.bytesStoreSize(encoding);
475 attributes.put(attribute, anObject);
480 * Sets the given attribute with a <tt>boolean</tt> value given by <tt>aBool</tt>.
482 * @param attributeName the attribute to set
483 * @param aBool the boolean value to set
484 * @throws NoSuchAttributeException if the attribute does not exist in the event
485 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
487 public void setBoolean(String attributeName, boolean aBool)
488 throws EventSystemException {
489 setBoolean(attributeName, Boolean.valueOf(aBool));
493 * Sets the given attribute with a <tt>Boolean</tt> value given by <tt>aBool</tt>.
495 * @param attributeName the attribute to set
496 * @param aBool the boolean value to set
497 * @throws NoSuchAttributeException
498 * @throws NoSuchAttributeTypeException
500 public void setBoolean(String attributeName, Boolean aBool)
501 throws EventSystemException {
502 set(attributeName, new BaseType(TypeID.BOOLEAN_STRING, TypeID.BOOLEAN_TOKEN, aBool));
506 * Set the given attribute with the <tt>unsigned short</tt> value given by <tt>aNumber</tt>.
507 * Because Java does not support unsigned types, we must use a signed int to cover the range of unsigned short.
509 * @param attributeName the attribute to set
510 * @param aNumber the unsigned short value as an integer
511 * @throws NoSuchAttributeException if the attribute does not exist in the event
512 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
514 public void setUInt16(String attributeName, int aNumber)
515 throws EventSystemException {
516 setUInt16(attributeName, Integer.valueOf(aNumber));
520 * Set the given attribute with the <tt>Integer</tt> value given by <tt>aNumber</tt>.
521 * This should be an <tt>unsigned short</tt>, but is an Integer because Java does not support unsigned types,
522 * and a signed integer is needed to cover the range of an unsigned short.
524 * @param attributeName the attribute to set
525 * @param aNumber the value
527 public void setUInt16(String attributeName, Integer aNumber)
528 throws EventSystemException {
529 set(attributeName, new BaseType(TypeID.UINT16_STRING, TypeID.UINT16_TOKEN, aNumber));
533 * Set the given attribute with the <tt>short</tt> value given by <tt>aNumber</tt>.
535 * @param attributeName the attribute to set
536 * @param aNumber the short value to set
537 * @throws NoSuchAttributeException if the attribute does not exist in the event
538 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
540 public void setInt16(String attributeName, short aNumber)
541 throws EventSystemException {
542 setInt16(attributeName, Short.valueOf(aNumber));
546 * Set the given attribute with the <tt>Short</tt> value given by <tt>aNumber</tt>.
548 * @param attributeName the attribute to set
549 * @param aNumber the short value to set
550 * @throws NoSuchAttributeException if the attribute does not exist in the event
551 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
553 public void setInt16(String attributeName, Short aNumber)
554 throws EventSystemException {
555 set(attributeName, new BaseType(TypeID.INT16_STRING, TypeID.INT16_TOKEN, aNumber));
559 * Set the given attribute with the <tt>unsigned int</tt> value given by <tt>aNumber</tt>.
560 * Because Java does not support unsigned types, we must use a signed long to cover the range of an unsigned int.
562 * @param attributeName the attribute to set
563 * @param aNumber the unsigned int value as a long
564 * @throws NoSuchAttributeException if the attribute does not exist in the event
565 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
567 public void setUInt32(String attributeName, long aNumber)
568 throws EventSystemException {
569 setUInt32(attributeName, Long.valueOf(aNumber));
573 * Set the given attribute with the <tt>Long</tt> value given by <tt>aNumber</tt>.
574 * This should be an <tt>unsigned int</tt>, but is an Long because Java does not support unsigned types,
575 * and a signed long is needed to cover the range of an unsigned int.
577 * @param attributeName the attribute to set
578 * @param aNumber the value
580 public void setUInt32(String attributeName, Long aNumber)
581 throws EventSystemException {
582 set(attributeName, new BaseType(TypeID.UINT32_STRING, TypeID.UINT32_TOKEN, aNumber));
586 * Set the given attribute with the <tt>int</tt> value given by <tt>aNumber</tt>.
588 * @param attributeName the attribute to set
589 * @param aNumber the integer value to set
590 * @throws NoSuchAttributeException if the attribute does not exist in the event
591 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
593 public void setInt32(String attributeName, int aNumber)
594 throws EventSystemException {
595 setInt32(attributeName, Integer.valueOf(aNumber));
599 * Set the given attribute with the <tt>Integer</tt> value given by <tt>aNumber</tt>.
601 * @param attributeName the attribute to set
602 * @param aNumber the Integer value to set
603 * @throws NoSuchAttributeException if the attribute does not exist in the event
604 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
606 public void setInt32(String attributeName, Integer aNumber)
607 throws EventSystemException {
608 set(attributeName, new BaseType(TypeID.INT32_STRING, TypeID.INT32_TOKEN, aNumber));
612 * Set the given attribute with the <tt>unsigned long</tt> value given by <tt>aNumber</tt>.
614 * @param attributeName the attribute to set
615 * @param aNumber the value
616 * @throws NoSuchAttributeException if the attribute does not exist in the event
617 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
619 public void setUInt64(String attributeName, long aNumber)
620 throws EventSystemException {
621 set(attributeName, new BaseType(TypeID.UINT64_STRING, TypeID.UINT64_TOKEN, BigInteger.valueOf(aNumber)));
625 * Set the given attribute with the <tt>Long</tt> value given by <tt>aNumber</tt>.
627 * @param attributeName the attribute to set
628 * @param aNumber the value
629 * @throws NoSuchAttributeException if the attribute does not exist in the event
630 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
632 public void setUInt64(String attributeName, Long aNumber)
633 throws EventSystemException {
634 set(attributeName,
635 new BaseType(TypeID.UINT64_STRING, TypeID.UINT64_TOKEN, BigInteger.valueOf(aNumber.longValue())));
639 * Set the given attribute with the <tt>BigInteger</tt> value given by <tt>aNumber</tt>.
640 * This should be an <tt>unsigned long</tt>, but is an BigInteger because Java does not support unsigned types,
641 * and a BigInteger is needed to cover the range of an unsigned long.
643 * @param attributeName the attribute to set
644 * @param aNumber the value
646 public void setUInt64(String attributeName, BigInteger aNumber)
647 throws EventSystemException {
648 set(attributeName, new BaseType(TypeID.UINT64_STRING, TypeID.UINT64_TOKEN, aNumber));
652 * Set the given attribute with the <tt>long</tt> value given by <tt>aNumber</tt>.
654 * @param attributeName the attribute to set
655 * @param aNumber the long value to set
656 * @throws NoSuchAttributeException if the attribute does not exist in the event
657 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
659 public void setInt64(String attributeName, long aNumber)
660 throws EventSystemException {
661 setInt64(attributeName, Long.valueOf(aNumber));
665 * Set the given attribute with the <tt>Long</tt> value given by <tt>aNumber</tt>.
667 * @param attributeName the attribute to set
668 * @param aNumber the Long value to set
669 * @throws NoSuchAttributeException if the attribute does not exist in the event
670 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
672 public void setInt64(String attributeName, Long aNumber)
673 throws EventSystemException {
674 set(attributeName, new BaseType(TypeID.INT64_STRING, TypeID.INT64_TOKEN, aNumber));
678 * Set the given attribute with a <tt>String</tt>
680 * @param attributeName the attribute to set
681 * @param aString the String value to set
682 * @throws NoSuchAttributeException if the attribute does not exist in the event
683 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
685 public void setString(String attributeName, String aString)
686 throws EventSystemException {
687 set(attributeName, new BaseType(TypeID.STRING_STRING, TypeID.STRING_TOKEN, aString));
691 * Set the given attribute with the <tt>ip address</tt> value given by <tt>address</tt>
693 * @param attributeName the attribute to set
694 * @param address the ip address in bytes
695 * @throws NoSuchAttributeException if the attribute does not exist in the event
696 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
698 public void setIPAddress(String attributeName, byte[] address)
699 throws EventSystemException {
700 setIPAddress(attributeName, new IPAddress(address));
704 * Set the given attribute with the <tt>ip address</tt> value given by <tt>address</tt>
706 * @param attributeName the attribute to set
707 * @param address the ip address in bytes
708 * @throws NoSuchAttributeException if the attribute does not exist in the event
709 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
711 public void setIPAddress(String attributeName, InetAddress address)
712 throws EventSystemException {
713 setIPAddress(attributeName, new IPAddress(address));
717 * Set the given attribute with the <tt>ip address</tt> value given by <tt>address</tt>
719 * @param attributeName the attribute to set
720 * @param address the ip address in bytes
721 * @throws NoSuchAttributeException if the attribute does not exist in the event
722 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
724 public void setIPAddress(String attributeName, IPAddress address)
725 throws EventSystemException {
726 set(attributeName, new BaseType(TypeID.IPADDR_STRING, TypeID.IPADDR_TOKEN, address));
730 * Serializes the Event into a byte array
732 * @return the serialized byte array
734 public byte[] serialize() {
736 * Serialization uses the following protocol
737 * EVENTWORD,<number of elements>,ATTRIBUTEWORD,TYPETOKEN,
738 * (UINT16|INT16|UINT32|INT32|UINT64|INT64|BOOLEAN|STRING)
739 * ...ATTRIBUTEWORD,TYPETOKEN(UINT16|INT16|UINT32|INT32|
740 * UINT64|INT64|BOOLEAN|STRING)
742 * The first attribute will always be the encoding if present.
744 byte[] bytes = new byte[this.bytesStoreSize];
745 int offset = 0;
746 int attributeCount = 0;
747 short encoding = DEFAULT_ENCODING;
749 if (attributes != null) {
750 attributeCount = attributes.size();
753 offset += Serializer.serializeEVENTWORD(name, bytes, offset);
754 offset += Serializer.serializeUINT16((short) (attributeCount), bytes, offset);
757 * Set the encoding attributes in the event
759 if (attributes != null) {
760 BaseType encodingBase = attributes.get(ENCODING);
761 if (encodingBase != null) {
762 Object encodingObj = encodingBase.getTypeObject();
763 byte encodingType = encodingBase.getTypeToken();
764 if (encodingObj != null) {
765 if (encodingType == TypeID.INT16_TOKEN) {
766 encoding = (Short) encodingObj;
767 Log.trace("Character encoding: " + encoding);
768 offset += Serializer.serializeATTRIBUTEWORD(ENCODING, bytes, offset);
769 offset += Serializer.serializeBYTE(encodingType, bytes, offset);
770 offset += Serializer.serializeUINT16(encoding, bytes, offset);
774 else {
775 Log.warning("Character encoding null in event " + name);
778 Enumeration<String> e = attributes.keys();
779 while (e.hasMoreElements()) {
780 String key = e.nextElement();
781 if (key.equals(ENCODING)) {
782 continue;
785 BaseType value = attributes.get(key);
786 Object data = value.getTypeObject();
787 byte typeToken = value.getTypeToken();
789 /* don't try to serialize nulls */
790 if (data == null) {
791 Log.warning("Attribute " + key + " was null in event " + name);
792 continue;
795 offset += Serializer.serializeATTRIBUTEWORD(key, bytes, offset);
796 offset += Serializer.serializeBYTE(typeToken, bytes, offset);
798 switch (typeToken) {
799 case TypeID.BOOLEAN_TOKEN:
800 offset += Serializer.serializeBOOLEAN((Boolean) data, bytes, offset);
801 break;
802 case TypeID.UINT16_TOKEN:
803 offset += Serializer.serializeUINT16((Integer) data, bytes, offset);
804 break;
805 case TypeID.INT16_TOKEN:
806 offset += Serializer.serializeINT16((Short) data, bytes, offset);
807 break;
808 case TypeID.UINT32_TOKEN:
809 offset += Serializer.serializeUINT32((Long) data, bytes, offset);
810 break;
811 case TypeID.INT32_TOKEN:
812 offset += Serializer.serializeINT32((Integer) data, bytes, offset);
813 break;
814 case TypeID.UINT64_TOKEN:
815 offset += Serializer.serializeUINT64((BigInteger) data, bytes, offset);
816 break;
817 case TypeID.INT64_TOKEN:
818 offset += Serializer.serializeINT64((Long) data, bytes, offset);
819 break;
820 case TypeID.STRING_TOKEN:
821 offset += Serializer.serializeSTRING(((String) data), bytes, offset, encoding);
822 break;
823 case TypeID.IPADDR_TOKEN:
824 offset += Serializer.serializeIPADDR(((IPAddress) data), bytes, offset);
825 break;
826 default:
827 Log.warning("Unknown BaseType token: " + typeToken);
828 break;
829 } // switch(typeToken)
831 Log.trace("Serialized attribute " + key);
832 } // while(e.hasMoreElements())
833 } // if(attributes != null)
835 return bytes;
839 * Deserialize the Event from byte array
841 * @param bytes the byte array containing a serialized Event
843 public void deserialize(byte[] bytes)
844 throws EventSystemException {
845 if (bytes == null) {
846 return;
848 if (state == null) {
849 state = new DeserializerState();
852 state.reset();
853 setEventName(Deserializer.deserializeEVENTWORD(state, bytes));
854 long num = Deserializer.deserializeUINT16(state, bytes);
855 if (Log.isLogTrace()) {
856 Log.trace("Event name = " + getEventName());
857 Log.trace("Number of attribute: " + num);
859 for (int i = 0; i < num; ++i) {
860 String attribute = Deserializer.deserializeATTRIBUTEWORD(state, bytes);
862 byte type = Deserializer.deserializeBYTE(state, bytes);
863 if (Log.isLogTrace()) {
864 Log.trace("Attribute: " + attribute);
865 Log.trace("Type: " + TypeID.byteIDToString(type));
866 Log.trace("State: " + state);
868 if (attribute != null) {
869 if (i == 0 && attribute.equals(ENCODING)) {
870 if (type == TypeID.INT16_TOKEN) {
871 setEncoding(Deserializer.deserializeINT16(state, bytes));
872 continue;
874 else {
875 Log.warning("Found encoding, but type was not int16 while deserializing");
879 switch (type) {
880 case TypeID.BOOLEAN_TOKEN:
881 boolean aBool = Deserializer.deserializeBOOLEAN(state, bytes);
882 setBoolean(attribute, aBool);
883 break;
884 case TypeID.UINT16_TOKEN:
885 int uShort = Deserializer.deserializeUINT16(state, bytes);
886 setUInt16(attribute, uShort);
887 break;
888 case TypeID.INT16_TOKEN:
889 short aShort = Deserializer.deserializeINT16(state, bytes);
890 setInt16(attribute, aShort);
891 break;
892 case TypeID.UINT32_TOKEN:
893 long uInt = Deserializer.deserializeUINT32(state, bytes);
894 setUInt32(attribute, uInt);
895 break;
896 case TypeID.INT32_TOKEN:
897 int aInt = Deserializer.deserializeINT32(state, bytes);
898 setInt32(attribute, aInt);
899 break;
900 case TypeID.UINT64_TOKEN:
901 long uLong = Deserializer.deserializeUINT64(state, bytes);
902 setUInt64(attribute, BigInteger.valueOf(uLong));
903 break;
904 case TypeID.INT64_TOKEN:
905 long aLong = Deserializer.deserializeINT64(state, bytes);
906 setInt64(attribute, aLong);
907 break;
908 case TypeID.STRING_TOKEN:
909 String s = Deserializer.deserializeSTRING(state, bytes, encoding);
910 setString(attribute, s);
911 break;
912 case TypeID.IPADDR_TOKEN:
913 byte[] inetAddress = Deserializer.deserializeIPADDR(state, bytes);
914 setIPAddress(attribute, inetAddress);
915 break;
916 default:
917 Log.warning("Unknown type " + type + " in deserialization");
920 } // for (int i =0 ...
925 * Returns a mutable copy of the event. This is a SLOW operation.
927 * @return Event the Event object
928 * @throws NoSuchEventException if the Event does not exist in the EventTemplateDB
929 * @throws NoSuchAttributeException if the attribute does not exist in this event
930 * @throws NoSuchAttributeTypeException if there is an attribute that does not match a type in the EventTemplateDB
932 public Event copy() throws EventSystemException {
933 /* match the type-checking of the original event */
934 Event evt = new Event(name, isValidating(), getEventTemplateDB());
935 for (Enumeration<String> e = attributes.keys(); e.hasMoreElements();) {
936 String key = e.nextElement();
937 BaseType value = attributes.get(key);
938 evt.set(key, value);
941 return evt;
945 * Returns a String representation of this event
947 * @return a String return of this event.
949 public String toString() {
950 if (name == null) {
951 return "";
954 StringBuffer sb = new StringBuffer();
955 sb.append(name);
956 sb.append("\n{\n");
958 if (attributes != null) {
959 int i = 0;
960 String[] keys = new String[attributes.size()];
961 for (Enumeration<String> e = attributes.keys(); e.hasMoreElements();) {
962 keys[i++] = e.nextElement();
965 Arrays.sort(keys);
967 for (i = 0; i < attributes.size(); ++i) {
968 BaseType value = attributes.get(keys[i]);
969 if (isValidating() && getEventTemplateDB() != null) {
970 if (getEventTemplateDB().checkTypeForAttribute(name, keys[i], TypeID.UINT64_STRING)) {
971 try {
972 sb.append("\t")
973 .append(keys[i])
974 .append(" = ")
975 .append(NumberCodec.toHexString(getUInt64(keys[i])))
976 .append(";\n");
978 catch (EventSystemException exc) {
979 Log.warning("Event.toString : ", exc);
982 else {
983 sb.append("\t").append(keys[i]).append(" = ").append(value).append(";\n");
986 else {
987 sb.append("\t").append(keys[i]).append(" = ").append(value).append(";\n");
989 } // for(i = 0; i < attributes.size() ...
990 } // if(attributes != null)
992 sb.append("}");
993 return sb.toString();
996 @Override
997 public int hashCode() {
998 return toString().hashCode();
1001 public boolean equals(Object o) {
1002 if (o == null) {
1003 return false;
1005 if (getClass().getName().equals(o.getClass().getName())) {
1006 return toString().equals(o.toString());
1008 else {
1009 return false;
1014 * This method can be used to validate an event after it has been created.
1016 * @throws EventSystemException
1018 public void validate() throws EventSystemException {
1019 EventTemplateDB templ = getEventTemplateDB();
1020 if (templ == null) {
1021 throw new EventSystemException("No template defined.");
1023 if (!templ.checkForEvent(name)) {
1024 throw new NoSuchEventException("Event " + name + " does not exist in event definition");
1026 for (String key : attributes.keySet()) {
1027 if (!templ.checkForAttribute(name, key)) {
1028 throw new NoSuchAttributeException("Attribute " + key + " does not exist for event " + name);
1030 Object value = get(key);
1031 BaseType expected = templ.getBaseTypeForObjectAttribute(name, key, value);
1032 BaseType bt = BaseType.baseTypeFromObject(value);
1034 * There are no unsigned values in java so they are kind of a special case
1035 * in that i can't guess which one the person meant. This small hack treats
1036 * similar types the same way.
1038 if ((expected.getTypeToken() == TypeID.UINT16_TOKEN &&
1039 bt.getTypeToken() == TypeID.INT32_TOKEN) ||
1040 (expected.getTypeToken() == TypeID.UINT32_TOKEN &&
1041 bt.getTypeToken() == TypeID.INT64_TOKEN) ||
1042 (expected.getTypeToken() == TypeID.UINT64_TOKEN &&
1043 bt.getTypeToken() == TypeID.INT64_TOKEN)) {
1044 bt = expected;
1046 if (!templ.checkTypeForAttribute(name, key, bt)) {
1047 throw new NoSuchAttributeTypeException("Wrong type '" + bt.getTypeName() +
1048 "' for " + name + "." + key);