added ipv4 type
[lwes-java.git] / src / main / java / org / lwes / Event.java
blobd62ce025671090331062c5b6d0f71a23e00746bd
1 package org.lwes;
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.lwes.db.EventTemplateDB;
6 import org.lwes.serializer.Deserializer;
7 import org.lwes.serializer.DeserializerState;
8 import org.lwes.serializer.Serializer;
9 import org.lwes.util.CharacterEncoding;
10 import org.lwes.util.IPAddress;
11 import org.lwes.util.NumberCodec;
13 import java.math.BigInteger;
14 import java.net.InetAddress;
15 import java.net.UnknownHostException;
16 import java.util.Arrays;
17 import java.util.Enumeration;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.concurrent.ConcurrentHashMap;
22 public class Event {
24 private static transient Log log = LogFactory.getLog(Event.class);
26 public static final int MAX_MESSAGE_SIZE = 65507;
28 /**
29 * Reserved metadata keywords
31 public static final String ENCODING = "enc";
32 public static final String RECEIPT_TIME = "ReceiptTime";
33 public static final String SENDER_IP = "SenderIP";
34 public static final String SENDER_PORT = "SenderPort";
36 /**
37 * Encoding variables
39 public static final short ISO_8859_1 = 0;
40 public static final short UTF_8 = 1;
41 public static final short DEFAULT_ENCODING = UTF_8;
42 public static final CharacterEncoding[] ENCODING_STRINGS = {
43 CharacterEncoding.ISO_8859_1, CharacterEncoding.UTF_8};
45 /**
46 * Event data
48 private ConcurrentHashMap<String, BaseType> attributes = new ConcurrentHashMap<String, BaseType>();
49 private String name = null;
50 private EventTemplateDB eventTemplateDB = null;
51 private short encoding = DEFAULT_ENCODING;
53 /**
54 * If this is set to true, types and attributes are validated against the EventTemplateDB
56 private boolean validating = true;
58 /**
59 * Internal object for deserialization state
61 private DeserializerState state = null;
63 /**
64 * the size of the event in bytes
66 private int bytesStoreSize = 0;
68 /**
69 * Create an event called <tt>eventName</tt>
71 * @param eventName the name of the event
72 * @param eventTemplateDB the EventTemplateDB to use for validation
73 * @throws NoSuchEventException if the Event does not exist in the EventTemplateDB
74 * @throws NoSuchAttributeException if an attribute does not exist in the EventTemplateDB
75 * @throws NoSuchAttributeTypeException if an attribute type does not exist in the EventTemplateDB
77 public Event(String eventName, EventTemplateDB eventTemplateDB)
78 throws EventSystemException {
79 this(eventName, true, eventTemplateDB);
82 /**
83 * Create an event called <tt>eventName</tt>
85 * @param eventName the name of the event
86 * @param validate true if the EventTemplateDB should be checked for types before all mutations
87 * @param eventTemplateDB the EventTemplateDB to use for validation
88 * @throws NoSuchEventException if the Event does not exist in the EventTemplateDB
89 * @throws NoSuchAttributeException if an attribute does not exist in the EventTemplateDB
90 * @throws NoSuchAttributeTypeException if an attribute type does not exist in the EventTemplateDB
92 public Event(String eventName, boolean validate, EventTemplateDB eventTemplateDB)
93 throws EventSystemException {
94 this(eventName, validate, eventTemplateDB, DEFAULT_ENCODING);
97 /**
98 * Create an event called <tt>eventName</tt>
100 * @param eventName the name of the event
101 * @param validate true if the EventTemplateDB should be checked for types before all mutations
102 * @param encoding the character encoding used by the event
103 * @throws NoSuchEventException if the Event does not exist in the EventTemplateDB
104 * @throws NoSuchAttributeException if an attribute does not exist in the EventTemplateDB
105 * @throws NoSuchAttributeTypeException if an attribute type does not exist in the EventTemplateDB
107 public Event(String eventName, boolean validate, EventTemplateDB eventTemplateDB, short encoding)
108 throws EventSystemException {
109 setEventTemplateDB(eventTemplateDB);
110 validating = validate;
111 setEventName(eventName);
112 setEncoding(encoding);
113 setDefaultValues(eventTemplateDB);
117 * Creates an event by deserializing a raw byte array.
119 * @param bytes the raw bytes to convert
120 * @param eventTemplateDB the EventTemplateDB to use to validate the event
121 * @throws NoSuchEventException
122 * @throws NoSuchAttributeException
123 * @throws NoSuchAttributeTypeException
125 public Event(byte[] bytes, EventTemplateDB eventTemplateDB)
126 throws EventSystemException {
127 this(bytes, true, eventTemplateDB);
132 * Creates an event by deserializing a raw byte array.
134 * @param bytes the raw bytes to convert
135 * @param validate whether or not to validate the event
136 * @param eventTemplateDB the EventTemplateDB to use to validate the event
137 * @throws NoSuchEventException
138 * @throws NoSuchAttributeException
139 * @throws NoSuchAttributeTypeException
141 public Event(byte[] bytes, boolean validate, EventTemplateDB eventTemplateDB)
142 throws EventSystemException {
143 setEventTemplateDB(eventTemplateDB);
144 validating = validate;
145 deserialize(bytes);
146 setDefaultValues(eventTemplateDB);
149 protected void setDefaultValues(EventTemplateDB template) throws EventSystemException {
150 if (template == null) {
151 return;
153 Map<String, BaseType> m = template.getBaseTypesForEvent(getEventName());
154 for (String key : m.keySet()) {
155 BaseType b = m.get(key);
156 log.debug("checking for default: " + key + "=" + b.getDefaultValue());
157 if (b.getDefaultValue() != null) {
158 if (log.isDebugEnabled()) {
159 log.debug("Setting default value: " + key + "=" + b.getDefaultValue());
161 set(key, b.getDefaultValue());
167 * Returns an enumeration of all the event attribute names
169 * @return an enumeration of attribute strings
171 public Enumeration<String> getEventAttributeNames() {
172 if (attributes == null) {
173 return null;
176 return attributes.keys();
180 * Returns the number of attributes in the event
182 * @return number of attributes in the event
184 public int size() {
185 if (attributes == null) {
186 return 0;
188 return attributes.size();
192 * Returns true if the event validates against the EventTemplateDB before making changes
194 * @return the validating state
196 public boolean isValidating() {
197 return this.validating;
201 * Set to true if the event should validate against the EventTemplateDB before making changes
203 * @param validate the validating value
205 public void setValidating(boolean validate) {
206 this.validating = validate;
210 * Returns the EventTemplateDB for this event, used for validation of types and attributes.
212 * @return the EventTemplateDB
214 public EventTemplateDB getEventTemplateDB() {
215 return this.eventTemplateDB;
219 * Sets the EventTemplateDB for this event, used for validation of types and attributes.
221 * @param eventTemplateDB the EventTemplateDB to be used for validation
223 public void setEventTemplateDB(EventTemplateDB eventTemplateDB) {
224 this.eventTemplateDB = eventTemplateDB;
228 * Returns the name of the event
230 * @return the name of the event
232 public synchronized String getEventName() {
233 return this.name;
237 * Sets the name of the Event
239 * @param name the name of the event
240 * @throws NoSuchEventException if the event is validating and does not exist in the EventTemplateDB
242 public synchronized void setEventName(String name) throws NoSuchEventException {
243 if (isValidating() && getEventTemplateDB() != null) {
244 if (!getEventTemplateDB().checkForEvent(name)) {
245 throw new NoSuchEventException("Event " + name + " does not exist in event definition");
249 /* determine if we already have the name and are just resetting it */
250 if (this.name != null) {
251 bytesStoreSize -= (this.name.length() + 1 + 2);
254 bytesStoreSize += (name.length() + 1 + 2);
256 this.name = name;
260 * Get the character encoding for this event
262 * @return the encoding
264 public short getEncoding() {
265 return this.encoding;
269 * Set the character encoding for event strings
271 * @param encoding the character encoding
272 * @throws NoSuchAttributeTypeException if the type for the encoding attribute does not exist
273 * @throws NoSuchAttributeException if the encoding attribute does not exist
275 public void setEncoding(short encoding) throws EventSystemException {
276 this.encoding = encoding;
277 setInt16(ENCODING, this.encoding);
281 * Generic accessor, checks if an attribute exists and returns its value. The user must do their
282 * own type checking.
284 * @param attributeName name of the attribute to lookup
285 * @return the object poitned to by attributeName
286 * @throws NoSuchAttributeException if the attribute does not exist in this event
288 public Object get(String attributeName) throws NoSuchAttributeException {
289 if (attributes == null) {
290 return null;
293 if (attributes.containsKey(attributeName)) {
294 return attributes.get(attributeName).getTypeObject();
297 if (isValidating() && getEventTemplateDB() != null) {
298 if (getEventTemplateDB().checkForAttribute(name, attributeName)) {
299 return null;
301 else {
302 throw new NoSuchAttributeException("Attribute " + attributeName + " does not exist for event " + name);
306 return null;
309 public List getArray(String attributeName) throws NoSuchAttributeException {
310 Object o = get(attributeName);
311 if (o != null && o instanceof List) {
312 return (List) o;
314 else {
315 return null;
319 public List getInt16Array(String attributeName) throws NoSuchAttributeException {
320 return getArray(attributeName);
323 public List getInt32Array(String attributeName) throws NoSuchAttributeException {
324 return getArray(attributeName);
327 public List getInt64Array(String attributeName) throws NoSuchAttributeException {
328 return getArray(attributeName);
331 public List getUInt16Array(String attributeName) throws NoSuchAttributeException {
332 return getArray(attributeName);
335 public List getUInt32Array(String attributeName) throws NoSuchAttributeException {
336 return getArray(attributeName);
339 public List getUInt64Array(String attributeName) throws NoSuchAttributeException {
340 return getArray(attributeName);
343 public List getStringArray(String attributeName) throws NoSuchAttributeException {
344 return getArray(attributeName);
347 public List getByteArray(String attributeName) throws NoSuchAttributeException {
348 return getArray(attributeName);
351 public List getBooleanArray(String attributeName) throws NoSuchAttributeException {
352 return getArray(attributeName);
355 public List getDoubleArray(String attributeName) throws NoSuchAttributeException {
356 return getArray(attributeName);
359 public List getFloatArray(String attributeName) throws NoSuchAttributeException {
360 return getArray(attributeName);
363 public List getIPV4Array(String attributeName) throws NoSuchAttributeException {
364 return getArray(attributeName);
368 * Method to check if an attribute is set in the event. This method does not throw
369 * NoSuchAttributeException because it shouldn't really care. If it's not there, it's
370 * not there.
372 * @param attributeName The attribute name to check for existence.
373 * @return true if there is a value, false if not.
375 public boolean isSet(String attributeName) {
376 try {
377 return (get(attributeName) != null);
379 catch (NoSuchAttributeException e) {
380 return false;
384 public Double getDouble(String attributeName) throws NoSuchAttributeException {
385 return (Double) get(attributeName);
388 public Float getFloat(String attributeName) throws NoSuchAttributeException {
389 return (Float) get(attributeName);
392 public Byte getByte(String attributeName) throws NoSuchAttributeException {
393 return (Byte) get(attributeName);
397 * Accessor that returns a boolean value for attribute <tt>attributeName</tt>
399 * @param attributeName the name of the attribute to fetch
400 * @return the boolean value
401 * @throws NoSuchAttributeException if the attribute does not exist in this event
403 public Boolean getBoolean(String attributeName) throws NoSuchAttributeException {
404 return (Boolean) get(attributeName);
408 * Accessor that returns an <tt>unsigned short</tt>, in the guise of an <tt>int</tt>, for attribute <tt>attributeName</tt>
410 * @param attributeName the name of the attribute to fetch
411 * @return the unsigned short as an int
412 * @throws NoSuchAttributeException if the attribute does not exist in this event
414 public Integer getUInt16(String attributeName) throws NoSuchAttributeException {
415 return (Integer) get(attributeName);
419 * Accessor that returns an <tt>short</tt>, for attribute <tt>attributeName</tt>
421 * @param attributeName the name of the attribute to fetch
422 * @return the short value
423 * @throws NoSuchAttributeException if the attribute does not exist in this event
425 public Short getInt16(String attributeName) throws NoSuchAttributeException {
426 return (Short) get(attributeName);
430 * Accessor that returns an <tt>unsigned int</tt>, in the guise of an <tt>long</tt>, for attribute <tt>attributeName</tt>
432 * @param attributeName the name of the attribute to fetch
433 * @return the unsigned int as a long
434 * @throws NoSuchAttributeException if the attribute does not exist in this event
436 public Long getUInt32(String attributeName) throws NoSuchAttributeException {
437 return (Long) get(attributeName);
441 * Accessor that returns an <tt>int</tt>, for attribute <tt>attributeName</tt>
443 * @param attributeName the name of the attribute to fetch
444 * @return the int value
445 * @throws NoSuchAttributeException if the attribute does not exist in this event
447 public Integer getInt32(String attributeName) throws NoSuchAttributeException {
448 return (Integer) get(attributeName);
452 * Accessor that returns an <tt>unsigned long</tt>, in the guise of an <tt>BigInteger</tt>, for attribute <tt>attributeName</tt>
454 * @param attributeName the name of the attribute to fetch
455 * @return the unsigned long as a BigInteger
456 * @throws NoSuchAttributeException if the attribute does not exist in this event
458 public BigInteger getUInt64(String attributeName) throws NoSuchAttributeException {
459 return (BigInteger) get(attributeName);
464 * Accessor that returns an <tt>long</tt>, for attribute <tt>attributeName</tt>
466 * @param attributeName the name of the attribute to fetch
467 * @return the long value
468 * @throws NoSuchAttributeException if the attribute does not exist in this event
470 public Long getInt64(String attributeName) throws NoSuchAttributeException {
471 return (Long) get(attributeName);
475 * Accessor that returns an <tt>String</tt>, for attribute <tt>attributeName</tt>
477 * @param attributeName the name of the attribute to fetch
478 * @return the String value
479 * @throws NoSuchAttributeException if the attribute does not exist in this event
481 public String getString(String attributeName) throws NoSuchAttributeException {
482 return (String) get(attributeName);
486 * Accessor that returns an <tt>InetAddress</tt>, for attribute <tt>attributeName</tt>
488 * @param attributeName the name of the attribute to fetch
489 * @return the InetAddress value
490 * @throws NoSuchAttributeException if the attribute does not exist in this event
492 public InetAddress getInetAddress(String attributeName) throws NoSuchAttributeException {
493 IPAddress a = (IPAddress) get(attributeName);
494 if (a != null) {
495 return a.toInetAddress();
497 else {
498 return null;
503 * Accessor that returns an IP address in bytes, for attribute <tt>attributeName</tt>
505 * @param attributeName the name of the attribute to fetch
506 * @return the IP address in bytes
507 * @throws NoSuchAttributeException if the attribute does not exist in this event
508 * @deprecated
510 public byte[] getIPAddress(String attributeName) throws NoSuchAttributeException {
511 return ((IPAddress) get(attributeName)).getInetAddressAsBytes();
515 * Accessor that returns the ip address as an IPAddress object.
517 * @param attributeName name of the attribute to fetch
518 * @return IPAddress
519 * @throws NoSuchAttributeException if the attribute is not set.
520 * @deprecated
522 public IPAddress getIPAddressObj(String attributeName) throws NoSuchAttributeException {
523 return (IPAddress) get(attributeName);
527 * Accessor that returns the ip address as an InetAddress.
529 * @param attributeName name of the attribute in the event.
530 * @return InetAddress
531 * @throws NoSuchAttributeException if the attribute is not set.
533 public InetAddress getIPV4Address(String attributeName) throws NoSuchAttributeException {
534 return (InetAddress) get(attributeName);
538 * Set the object's attribute <tt>attributeName</tt> with the Object given
540 * @param attributeName the name of the attribute to set
541 * @param attributeValue the object to set the attribute with
542 * @throws NoSuchAttributeException if the attribute does not exist in this event
543 * @throws NoSuchAttributeTypeException if there is an attribute with an undefined type
545 public void set(String attributeName, Object attributeValue)
546 throws EventSystemException {
547 if (isValidating() && getEventTemplateDB() != null) {
548 if (getEventTemplateDB().checkForAttribute(getEventName(), attributeName)) {
549 BaseType bt = getEventTemplateDB().getBaseTypeForObjectAttribute(getEventName(),
550 attributeName, attributeValue);
551 set(attributeName, bt);
554 else {
555 throw new NoSuchAttributeException("Must be able to check the EventTemplateDB to use set(String,Object)");
560 * Private method to set a BaseType
562 * @param attribute the name of the attribute to set
563 * @param anObject the BaseType to set in the event
564 * @throws NoSuchAttributeException if the attribute does not exist in this event
565 * @throws NoSuchAttributeTypeException if there is an attribute with an undefined type
567 private void set(String attribute, BaseType anObject)
568 throws EventSystemException {
570 if (isValidating() && getEventTemplateDB() != null) {
571 if (getEventTemplateDB().checkForAttribute(name, attribute)) {
572 if (!getEventTemplateDB().checkTypeForAttribute(name, attribute, anObject)) {
573 throw new NoSuchAttributeTypeException("Wrong type '" + anObject.getTypeName() +
574 "' for " + name + "." + attribute);
577 else {
578 throw new NoSuchAttributeException("Attribute " + attribute + " does not exist for event " + name);
580 getEventTemplateDB().checkForSize(name, attribute, anObject);
583 if (anObject.getTypeObject() != null) {
584 BaseType oldObject = null;
585 int newSize = bytesStoreSize + ((attribute.length() + 1) + anObject.bytesStoreSize(encoding));
586 if (newSize > MAX_MESSAGE_SIZE) {
587 throw new EventSystemException("Event size limit is " + MAX_MESSAGE_SIZE + " bytes.");
589 if ((oldObject = attributes.remove(attribute)) != null) {
590 bytesStoreSize -= (attribute.length() + 1) + oldObject.bytesStoreSize(encoding);
593 bytesStoreSize += (attribute.length() + 1) + anObject.bytesStoreSize(encoding);
594 attributes.put(attribute, anObject);
598 public void setInt16Array(String attributeName, List value) throws EventSystemException {
599 set(attributeName, new BaseType(TypeID.INT16_ARRAY_STRING,
600 TypeID.INT16_ARRAY_TOKEN,
601 value));
604 public void setInt32Array(String attributeName, List value) throws EventSystemException {
605 set(attributeName, new BaseType(TypeID.INT32_ARRAY_STRING,
606 TypeID.INT32_ARRAY_TOKEN,
607 value));
610 public void setInt64Array(String attributeName, List value) throws EventSystemException {
611 set(attributeName, new BaseType(TypeID.INT64_ARRAY_STRING,
612 TypeID.INT64_ARRAY_TOKEN,
613 value));
616 public void setUInt16Array(String attributeName, List value) throws EventSystemException {
617 set(attributeName, new BaseType(TypeID.UINT16_ARRAY_STRING,
618 TypeID.UINT16_ARRAY_TOKEN,
619 value));
622 public void setUInt32Array(String attributeName, List value) throws EventSystemException {
623 set(attributeName, new BaseType(TypeID.UINT32_ARRAY_STRING,
624 TypeID.UINT32_ARRAY_TOKEN,
625 value));
628 public void setUInt64Array(String attributeName, List value) throws EventSystemException {
629 set(attributeName, new BaseType(TypeID.UINT64_ARRAY_STRING,
630 TypeID.UINT64_ARRAY_TOKEN,
631 value));
634 public void setStringArray(String attributeName, List value)
635 throws EventSystemException {
636 set(attributeName, new BaseType(TypeID.STRING_ARRAY_STRING,
637 TypeID.STRING_ARRAY_TOKEN,
638 value));
641 public void setBooleanArray(String attributeName, List value)
642 throws EventSystemException {
643 set(attributeName, new BaseType(TypeID.BOOLEAN_ARRAY_STRING,
644 TypeID.BOOLEAN_ARRAY_TOKEN,
645 value));
648 public void setByteArray(String attributeName, List value)
649 throws EventSystemException {
650 set(attributeName, new BaseType(TypeID.BYTE_ARRAY_STRING,
651 TypeID.BYTE_ARRAY_TOKEN,
652 value));
655 public void setDoubleArray(String attributeName, List value)
656 throws EventSystemException {
657 set(attributeName, new BaseType(TypeID.DOUBLE_ARRAY_STRING,
658 TypeID.DOUBLE_ARRAY_TOKEN,
659 value));
662 public void setFloatArray(String attributeName, List value)
663 throws EventSystemException {
664 set(attributeName, new BaseType(TypeID.FLOAT_ARRAY_STRING,
665 TypeID.FLOAT_ARRAY_TOKEN,
666 value));
669 public void setIPV4AddressArray(String attributeName, List value)
670 throws EventSystemException {
671 set(attributeName, new BaseType(TypeID.IPV4_ARRAY_STRING,
672 TypeID.IPV4_ARRAY_TOKEN,
673 value));
676 public void setDouble(String attributeName, Double value) throws EventSystemException {
677 set(attributeName, new BaseType(TypeID.DOUBLE_STRING, TypeID.DOUBLE_TOKEN, value));
680 public void setFloat(String attributeName, Float value) throws EventSystemException {
681 set(attributeName, new BaseType(TypeID.FLOAT_STRING, TypeID.FLOAT_TOKEN, value));
685 * Sets the given attribute with a <tt>Boolean</tt> value given by <tt>aBool</tt>.
687 * @param attributeName the attribute to set
688 * @param aBool the boolean value to set
689 * @throws NoSuchAttributeException
690 * @throws NoSuchAttributeTypeException
692 public void setBoolean(String attributeName, Boolean aBool)
693 throws EventSystemException {
694 set(attributeName, new BaseType(TypeID.BOOLEAN_STRING, TypeID.BOOLEAN_TOKEN, aBool));
698 * Set the given attribute with the <tt>Integer</tt> value given by <tt>aNumber</tt>.
699 * This should be an <tt>unsigned short</tt>, but is an Integer because Java does not support unsigned types,
700 * and a signed integer is needed to cover the range of an unsigned short.
702 * @param attributeName the attribute to set
703 * @param aNumber the value
704 * @throws NoSuchAttributeException if the attribute does not exist in the event
705 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
707 public void setUInt16(String attributeName, Integer aNumber)
708 throws EventSystemException {
709 set(attributeName, new BaseType(TypeID.UINT16_STRING, TypeID.UINT16_TOKEN, aNumber));
713 * Set the given attribute with the <tt>Short</tt> value given by <tt>aNumber</tt>.
715 * @param attributeName the attribute to set
716 * @param aNumber the short value to set
717 * @throws NoSuchAttributeException if the attribute does not exist in the event
718 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
720 public void setInt16(String attributeName, Short aNumber)
721 throws EventSystemException {
722 set(attributeName, new BaseType(TypeID.INT16_STRING, TypeID.INT16_TOKEN, aNumber));
726 * Set the given attribute with the <tt>Long</tt> value given by <tt>aNumber</tt>.
727 * This should be an <tt>unsigned int</tt>, but is an Long because Java does not support unsigned types,
728 * and a signed long is needed to cover the range of an unsigned int.
730 * @param attributeName the attribute to set
731 * @param aNumber the value
732 * @throws NoSuchAttributeException if the attribute does not exist in the event
733 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
735 public void setUInt32(String attributeName, Long aNumber)
736 throws EventSystemException {
737 set(attributeName, new BaseType(TypeID.UINT32_STRING, TypeID.UINT32_TOKEN, aNumber));
741 * Set the given attribute with the <tt>Integer</tt> value given by <tt>aNumber</tt>.
743 * @param attributeName the attribute to set
744 * @param aNumber the Integer value to set
745 * @throws NoSuchAttributeException if the attribute does not exist in the event
746 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
748 public void setInt32(String attributeName, Integer aNumber)
749 throws EventSystemException {
750 set(attributeName, new BaseType(TypeID.INT32_STRING, TypeID.INT32_TOKEN, aNumber));
754 * Set the given attribute with the <tt>Long</tt> value given by <tt>aNumber</tt>.
756 * @param attributeName the attribute to set
757 * @param aNumber the value
758 * @throws NoSuchAttributeException if the attribute does not exist in the event
759 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
761 public void setUInt64(String attributeName, Long aNumber)
762 throws EventSystemException {
763 set(attributeName,
764 new BaseType(TypeID.UINT64_STRING, TypeID.UINT64_TOKEN, BigInteger.valueOf(aNumber)));
768 * Set the given attribute with the <tt>BigInteger</tt> value given by <tt>aNumber</tt>.
769 * This should be an <tt>unsigned long</tt>, but is an BigInteger because Java does not support unsigned types,
770 * and a BigInteger is needed to cover the range of an unsigned long.
772 * @param attributeName the attribute to set
773 * @param aNumber the value
774 * @throws NoSuchAttributeException if the attribute does not exist in the event
775 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
777 public void setUInt64(String attributeName, BigInteger aNumber)
778 throws EventSystemException {
779 set(attributeName, new BaseType(TypeID.UINT64_STRING, TypeID.UINT64_TOKEN, aNumber));
783 * Set the given attribute with the <tt>Long</tt> value given by <tt>aNumber</tt>.
785 * @param attributeName the attribute to set
786 * @param aNumber the Long value to set
787 * @throws NoSuchAttributeException if the attribute does not exist in the event
788 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
790 public void setInt64(String attributeName, Long aNumber)
791 throws EventSystemException {
792 set(attributeName, new BaseType(TypeID.INT64_STRING, TypeID.INT64_TOKEN, aNumber));
796 * Set the given attribute with a <tt>String</tt>
798 * @param attributeName the attribute to set
799 * @param aString the String value to set
800 * @throws NoSuchAttributeException if the attribute does not exist in the event
801 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
803 public void setString(String attributeName, String aString)
804 throws EventSystemException {
805 set(attributeName, new BaseType(TypeID.STRING_STRING, TypeID.STRING_TOKEN, aString));
809 * Set the given attribute with the <tt>ip address</tt> value given by <tt>address</tt>
811 * @param attributeName the attribute to set
812 * @param address the ip address in bytes
813 * @throws NoSuchAttributeException if the attribute does not exist in the event
814 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
815 * @deprecated
817 public void setIPAddress(String attributeName, byte[] address)
818 throws EventSystemException {
819 setIPAddress(attributeName, new IPAddress(address));
823 * Set the given attribute with the <tt>ip address</tt> value given by <tt>address</tt>
825 * @param attributeName the attribute to set
826 * @param address the ip address in bytes
827 * @throws NoSuchAttributeException if the attribute does not exist in the event
828 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
829 * @deprecated
831 public void setIPAddress(String attributeName, InetAddress address)
832 throws EventSystemException {
833 setIPAddress(attributeName, new IPAddress(address));
837 * Set the given attribute with the <tt>ip address</tt> value given by <tt>address</tt>
839 * @param attributeName the attribute to set
840 * @param address the ip address in bytes
841 * @throws NoSuchAttributeException if the attribute does not exist in the event
842 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
843 * @deprecated
845 public void setIPAddress(String attributeName, IPAddress address)
846 throws EventSystemException {
847 set(attributeName, new BaseType(TypeID.IPADDR_STRING, TypeID.IPADDR_TOKEN, address));
851 public void setIPV4Address(String attributeName, InetAddress address)
852 throws EventSystemException {
853 set(attributeName, new BaseType(TypeID.IPV4_STRING, TypeID.IPV4_TOKEN, address));
857 * Serializes the Event into a byte array
859 * @return the serialized byte array
861 public byte[] serialize() {
863 * Serialization uses the following protocol
864 * EVENTWORD,<number of elements>,ATTRIBUTEWORD,TYPETOKEN,
865 * (UINT16|INT16|UINT32|INT32|UINT64|INT64|BOOLEAN|STRING)
866 * ...ATTRIBUTEWORD,TYPETOKEN(UINT16|INT16|UINT32|INT32|
867 * UINT64|INT64|BOOLEAN|STRING)
869 * The first attribute will always be the encoding if present.
871 byte[] bytes = new byte[this.bytesStoreSize];
872 int offset = 0;
873 int attributeCount = 0;
874 short encoding = DEFAULT_ENCODING;
876 if (attributes != null) {
877 attributeCount = attributes.size();
880 offset += Serializer.serializeEVENTWORD(name, bytes, offset);
881 offset += Serializer.serializeUINT16((short) (attributeCount), bytes, offset);
884 * Set the encoding attributes in the event
886 if (attributes != null) {
887 BaseType encodingBase = attributes.get(ENCODING);
888 if (encodingBase != null) {
889 Object encodingObj = encodingBase.getTypeObject();
890 byte encodingType = encodingBase.getTypeToken();
891 if (encodingObj != null) {
892 if (encodingType == TypeID.INT16_TOKEN) {
893 encoding = (Short) encodingObj;
894 log.trace("Character encoding: " + encoding);
895 offset += Serializer.serializeATTRIBUTEWORD(ENCODING, bytes, offset);
896 offset += Serializer.serializeBYTE(encodingType, bytes, offset);
897 offset += Serializer.serializeUINT16(encoding, bytes, offset);
901 else {
902 log.warn("Character encoding null in event " + name);
905 Enumeration<String> e = attributes.keys();
906 while (e.hasMoreElements()) {
907 String key = e.nextElement();
908 if (key.equals(ENCODING)) {
909 continue;
912 BaseType value = attributes.get(key);
913 Object data = value.getTypeObject();
914 byte typeToken = value.getTypeToken();
916 /* don't try to serialize nulls */
917 if (data == null) {
918 log.warn("Attribute " + key + " was null in event " + name);
919 continue;
922 offset += Serializer.serializeATTRIBUTEWORD(key, bytes, offset);
923 offset += Serializer.serializeBYTE(typeToken, bytes, offset);
925 switch (typeToken) {
926 case TypeID.BOOLEAN_TOKEN:
927 offset += Serializer.serializeBOOLEAN((Boolean) data, bytes, offset);
928 break;
929 case TypeID.UINT16_TOKEN:
930 offset += Serializer.serializeUINT16((Integer) data, bytes, offset);
931 break;
932 case TypeID.INT16_TOKEN:
933 offset += Serializer.serializeINT16((Short) data, bytes, offset);
934 break;
935 case TypeID.UINT32_TOKEN:
936 offset += Serializer.serializeUINT32((Long) data, bytes, offset);
937 break;
938 case TypeID.INT32_TOKEN:
939 offset += Serializer.serializeINT32((Integer) data, bytes, offset);
940 break;
941 case TypeID.UINT64_TOKEN:
942 offset += Serializer.serializeUINT64((BigInteger) data, bytes, offset);
943 break;
944 case TypeID.INT64_TOKEN:
945 offset += Serializer.serializeINT64((Long) data, bytes, offset);
946 break;
947 case TypeID.STRING_TOKEN:
948 offset += Serializer.serializeSTRING(((String) data), bytes, offset, encoding);
949 break;
950 case TypeID.DOUBLE_TOKEN:
951 offset += Serializer.serializeDOUBLE(((Double) data), bytes, offset);
952 break;
953 case TypeID.FLOAT_TOKEN:
954 offset += Serializer.serializeFLOAT(((Float) data), bytes, offset);
955 break;
956 case TypeID.IPADDR_TOKEN:
957 offset += Serializer.serializeIPADDR(((IPAddress) data), bytes, offset);
958 break;
959 case TypeID.IPV4_TOKEN:
960 offset += Serializer.serializeIPV4((InetAddress) data, bytes, offset);
961 break;
962 case TypeID.STRING_ARRAY_TOKEN:
963 offset += Serializer.serializeStringArray
964 (((List) data), bytes, offset, encoding);
965 break;
966 case TypeID.INT16_ARRAY_TOKEN:
967 offset += Serializer.serializeInt16Array((List) data, bytes, offset);
968 break;
969 case TypeID.INT32_ARRAY_TOKEN:
970 offset += Serializer.serializeInt32Array((List) data, bytes, offset);
971 break;
972 case TypeID.INT64_ARRAY_TOKEN:
973 offset += Serializer.serializeInt64Array((List) data, bytes, offset);
974 break;
975 case TypeID.UINT16_ARRAY_TOKEN:
976 offset += Serializer.serializeUInt16Array((List) data, bytes, offset);
977 break;
978 case TypeID.UINT32_ARRAY_TOKEN:
979 offset += Serializer.serializeUInt32Array((List) data, bytes, offset);
980 break;
981 case TypeID.UINT64_ARRAY_TOKEN:
982 offset += Serializer.serializeUInt64Array((List) data, bytes, offset);
983 break;
984 case TypeID.BOOLEAN_ARRAY_TOKEN:
985 offset += Serializer.serializeBooleanArray((List) data, bytes, offset);
986 break;
987 case TypeID.BYTE_ARRAY_TOKEN:
988 offset += Serializer.serializeByteArray((List) data, bytes, offset);
989 break;
990 case TypeID.DOUBLE_ARRAY_TOKEN:
991 offset += Serializer.serializeDoubleArray((List) data, bytes, offset);
992 break;
993 case TypeID.FLOAT_ARRAY_TOKEN:
994 offset += Serializer.serializeFloatArray((List) data, bytes, offset);
995 break;
996 case TypeID.IPV4_ARRAY_TOKEN:
997 offset += Serializer.serializeIPV4Array((List) data, bytes, offset);
998 break;
999 default:
1000 log.warn("Unknown BaseType token: " + typeToken);
1001 break;
1002 } // switch(typeToken)
1004 log.trace("Serialized attribute " + key);
1005 } // while(e.hasMoreElements())
1006 } // if(attributes != null)
1008 return bytes;
1012 * Deserialize the Event from byte array
1014 * @param bytes the byte array containing a serialized Event
1015 * @throws EventSystemException
1017 public void deserialize(byte[] bytes)
1018 throws EventSystemException {
1019 if (bytes == null) {
1020 return;
1022 if (state == null) {
1023 state = new DeserializerState();
1026 state.reset();
1027 setEventName(Deserializer.deserializeEVENTWORD(state, bytes));
1028 long num = Deserializer.deserializeUINT16(state, bytes);
1029 if (log.isTraceEnabled()) {
1030 log.trace("Event name = " + getEventName());
1031 log.trace("Number of attribute: " + num);
1033 for (int i = 0; i < num; ++i) {
1034 String attribute = Deserializer.deserializeATTRIBUTEWORD(state, bytes);
1036 byte type = Deserializer.deserializeBYTE(state, bytes);
1037 if (log.isTraceEnabled()) {
1038 log.trace("Attribute: " + attribute);
1039 log.trace("Type: " + TypeID.byteIDToString(type));
1040 log.trace("State: " + state);
1042 if (attribute != null) {
1043 if (i == 0 && attribute.equals(ENCODING)) {
1044 if (type == TypeID.INT16_TOKEN) {
1045 setEncoding(Deserializer.deserializeINT16(state, bytes));
1046 continue;
1048 else {
1049 log.warn("Found encoding, but type was not int16 while deserializing");
1053 switch (type) {
1054 case TypeID.BOOLEAN_TOKEN:
1055 boolean aBool = Deserializer.deserializeBOOLEAN(state, bytes);
1056 setBoolean(attribute, aBool);
1057 break;
1058 case TypeID.UINT16_TOKEN:
1059 int uShort = Deserializer.deserializeUINT16(state, bytes);
1060 setUInt16(attribute, uShort);
1061 break;
1062 case TypeID.INT16_TOKEN:
1063 short aShort = Deserializer.deserializeINT16(state, bytes);
1064 setInt16(attribute, aShort);
1065 break;
1066 case TypeID.UINT32_TOKEN:
1067 long uInt = Deserializer.deserializeUINT32(state, bytes);
1068 setUInt32(attribute, uInt);
1069 break;
1070 case TypeID.INT32_TOKEN:
1071 int aInt = Deserializer.deserializeINT32(state, bytes);
1072 setInt32(attribute, aInt);
1073 break;
1074 case TypeID.UINT64_TOKEN:
1075 long uLong = Deserializer.deserializeUINT64(state, bytes);
1076 setUInt64(attribute, BigInteger.valueOf(uLong));
1077 break;
1078 case TypeID.INT64_TOKEN:
1079 long aLong = Deserializer.deserializeINT64(state, bytes);
1080 setInt64(attribute, aLong);
1081 break;
1082 case TypeID.STRING_TOKEN:
1083 String s = Deserializer.deserializeSTRING(state, bytes, encoding);
1084 setString(attribute, s);
1085 break;
1086 case TypeID.IPADDR_TOKEN:
1087 byte[] inetAddress = Deserializer.deserializeIPADDR(state, bytes);
1088 setIPAddress(attribute, inetAddress);
1089 break;
1090 case TypeID.IPV4_TOKEN:
1091 InetAddress ipv4 = null;
1092 try {
1093 ipv4 = Deserializer.deserializeIPV4(state, bytes);
1094 setIPV4Address(attribute, ipv4);
1096 catch (UnknownHostException e) {
1097 throw new EventSystemException(e);
1099 break;
1100 case TypeID.STRING_ARRAY_TOKEN:
1101 List sArray = Deserializer.deserializeStringArray(state, bytes, encoding);
1102 setStringArray(attribute, sArray);
1103 break;
1104 case TypeID.INT16_ARRAY_TOKEN:
1105 List as = Deserializer.deserializeInt16Array(state, bytes);
1106 setInt16Array(attribute, as);
1107 break;
1108 case TypeID.INT32_ARRAY_TOKEN:
1109 List ai = Deserializer.deserializeInt32Array(state, bytes);
1110 setInt32Array(attribute, ai);
1111 break;
1112 case TypeID.INT64_ARRAY_TOKEN:
1113 List al = Deserializer.deserializeInt64Array(state, bytes);
1114 setInt64Array(attribute, al);
1115 break;
1116 case TypeID.UINT16_ARRAY_TOKEN:
1117 List uas = Deserializer.deserializeUInt16Array(state, bytes);
1118 setUInt16Array(attribute, uas);
1119 break;
1120 case TypeID.UINT32_ARRAY_TOKEN:
1121 List uai = Deserializer.deserializeUInt32Array(state, bytes);
1122 setUInt32Array(attribute, uai);
1123 break;
1124 case TypeID.UINT64_ARRAY_TOKEN:
1125 List ual = Deserializer.deserializeUInt64Array(state, bytes);
1126 setUInt64Array(attribute, ual);
1127 break;
1128 case TypeID.BOOLEAN_ARRAY_TOKEN:
1129 List ba = Deserializer.deserializeBooleanArray(state, bytes);
1130 setBooleanArray(attribute, ba);
1131 break;
1132 case TypeID.BYTE_ARRAY_TOKEN:
1133 List bar = Deserializer.deserializeByteArray(state, bytes);
1134 setByteArray(attribute, bar);
1135 break;
1136 case TypeID.DOUBLE_ARRAY_TOKEN:
1137 List da = Deserializer.deserializeDoubleArray(state, bytes);
1138 setDoubleArray(attribute, da);
1139 break;
1140 case TypeID.FLOAT_ARRAY_TOKEN:
1141 List fa = Deserializer.deserializeFloatArray(state, bytes);
1142 setFloatArray(attribute, fa);
1143 break;
1144 case TypeID.IPV4_ARRAY_TOKEN:
1145 List ipv4a = Deserializer.deserializeIPV4Array(state, bytes);
1146 setIPV4AddressArray(attribute, ipv4a);
1147 break;
1148 default:
1149 log.warn("Unknown type " + type + " in deserialization");
1152 } // for (int i =0 ...
1157 * Returns a mutable copy of the event. This is a SLOW operation.
1159 * @return Event the Event object
1160 * @throws NoSuchEventException if the Event does not exist in the EventTemplateDB
1161 * @throws NoSuchAttributeException if the attribute does not exist in this event
1162 * @throws NoSuchAttributeTypeException if there is an attribute that does not match a type in the EventTemplateDB
1164 public Event copy() throws EventSystemException {
1165 /* match the type-checking of the original event */
1166 Event evt = new Event(name, isValidating(), getEventTemplateDB());
1167 for (Enumeration<String> e = attributes.keys(); e.hasMoreElements();) {
1168 String key = e.nextElement();
1169 BaseType value = attributes.get(key);
1170 evt.set(key, value);
1173 return evt;
1177 * Returns a String representation of this event
1179 * @return a String return of this event.
1181 public String toString() {
1182 if (name == null) {
1183 return "";
1186 StringBuffer sb = new StringBuffer();
1187 sb.append(name);
1188 sb.append("\n{\n");
1190 if (attributes != null) {
1191 int i = 0;
1192 String[] keys = new String[attributes.size()];
1193 for (Enumeration<String> e = attributes.keys(); e.hasMoreElements();) {
1194 keys[i++] = e.nextElement();
1197 Arrays.sort(keys);
1199 for (i = 0; i < attributes.size(); ++i) {
1200 BaseType value = attributes.get(keys[i]);
1201 if (isValidating() && getEventTemplateDB() != null) {
1202 if (getEventTemplateDB().checkTypeForAttribute(name, keys[i], TypeID.UINT64_STRING)) {
1203 try {
1204 sb.append("\t")
1205 .append(keys[i])
1206 .append(" = ")
1207 .append(NumberCodec.toHexString(getUInt64(keys[i])))
1208 .append(";\n");
1210 catch (EventSystemException exc) {
1211 log.warn("Event.toString : ", exc);
1214 else {
1215 sb.append("\t").append(keys[i]).append(" = ").append(value).append(";\n");
1218 else {
1219 sb.append("\t").append(keys[i]).append(" = ").append(value).append(";\n");
1221 } // for(i = 0; i < attributes.size() ...
1222 } // if(attributes != null)
1224 sb.append("}");
1225 return sb.toString();
1228 public String toOneLineString() {
1229 return toString().replaceAll("\n", " ");
1232 @Override
1233 public int hashCode() {
1234 return toString().hashCode();
1237 public boolean equals(Object o) {
1238 if (o == null) {
1239 return false;
1241 if (getClass().getName().equals(o.getClass().getName())) {
1242 return toString().equals(o.toString());
1244 else {
1245 return false;
1250 * This method can be used to validate an event after it has been created.
1252 * @throws ValidationExceptions A list of validation errors
1254 public void validate() throws ValidationExceptions {
1255 ValidationExceptions ve = new ValidationExceptions(name);
1257 EventTemplateDB templ = getEventTemplateDB();
1258 if (templ == null) {
1259 ve.addException(new EventSystemException("No template defined."));
1260 throw ve;
1262 if (!templ.checkForEvent(name)) {
1263 ve.addException(new NoSuchEventException("Event " + name + " does not exist in event definition"));
1264 throw ve;
1266 for (String key : attributes.keySet()) {
1267 if (!templ.checkForAttribute(name, key)) {
1268 ve.addException(new NoSuchAttributeException("Attribute " + key + " does not exist for event " + name));
1269 continue;
1271 Object value;
1272 try {
1273 value = get(key);
1275 catch (NoSuchAttributeException e) {
1276 ve.addException(e);
1277 continue;
1280 BaseType expected = templ.getBaseTypeForObjectAttribute(name, key, value);
1281 BaseType bt = BaseType.baseTypeFromObject(value);
1284 * There are no unsigned values in java so they are kind of a special case
1285 * in that i can't guess which one the person meant. This small hack treats
1286 * similar types the same way.
1288 if ((expected.getTypeToken() == TypeID.UINT16_TOKEN &&
1289 bt.getTypeToken() == TypeID.INT32_TOKEN) ||
1290 (expected.getTypeToken() == TypeID.UINT32_TOKEN &&
1291 bt.getTypeToken() == TypeID.INT64_TOKEN) ||
1292 (expected.getTypeToken() == TypeID.UINT64_TOKEN &&
1293 bt.getTypeToken() == TypeID.INT64_TOKEN)) {
1294 bt = expected;
1296 if (!templ.checkTypeForAttribute(name, key, bt)) {
1297 ve.addException(new NoSuchAttributeTypeException("Wrong type '" + bt.getTypeName() +
1298 "' for " + name + "." + key));
1301 Map<String, BaseType> map = templ.getEvents().get(name);
1302 for (String key : map.keySet()) {
1303 BaseType bt = map.get(key);
1304 if (bt.isRequired()) {
1305 if (!attributes.containsKey(key)) {
1306 ve.addException(new AttributeRequiredException(key));
1311 if (ve.hasExceptions()) {
1312 throw ve;