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
;
17 import java
.util
.concurrent
.ConcurrentHashMap
;
21 public static final int MAX_MESSAGE_SIZE
= 65507;
24 * Reserved metadata keywords
26 public static final String ENCODING
= "enc";
27 public static final String RECEIPT_TIME
= "ReceiptTime";
28 public static final String SENDER_IP
= "SenderIP";
29 public static final String SENDER_PORT
= "SenderPort";
34 public static final short ISO_8859_1
= 0;
35 public static final short UTF_8
= 1;
36 public static final short DEFAULT_ENCODING
= UTF_8
;
37 public static final CharacterEncoding
[] ENCODING_STRINGS
= {
38 CharacterEncoding
.ISO_8859_1
, CharacterEncoding
.UTF_8
};
43 private ConcurrentHashMap
<String
, BaseType
> attributes
= new ConcurrentHashMap
<String
, BaseType
>();
44 private String name
= null;
45 private EventTemplateDB eventTemplateDB
= null;
46 private short encoding
= DEFAULT_ENCODING
;
49 * If this is set to true, types and attributes are validated against the EventTemplateDB
51 private boolean validating
= true;
54 * Internal object for deserialization state
56 private DeserializerState state
= null;
59 * the size of the event in bytes
61 private int bytesStoreSize
= 0;
64 * Create an event called <tt>eventName</tt>
66 * @param eventName the name of the event
67 * @param eventTemplateDB the EventTemplateDB to use for validation
68 * @throws NoSuchEventException if the Event does not exist in the EventTemplateDB
69 * @throws NoSuchAttributeException if an attribute does not exist in the EventTemplateDB
70 * @throws NoSuchAttributeTypeException if an attribute type does not exist in the EventTemplateDB
72 public Event(String eventName
, EventTemplateDB eventTemplateDB
)
73 throws EventSystemException
{
74 this(eventName
, true, eventTemplateDB
);
78 * Create an event called <tt>eventName</tt>
80 * @param eventName the name of the event
81 * @param validate true if the EventTemplateDB should be checked for types before all mutations
82 * @param eventTemplateDB the EventTemplateDB to use for validation
83 * @throws NoSuchEventException if the Event does not exist in the EventTemplateDB
84 * @throws NoSuchAttributeException if an attribute does not exist in the EventTemplateDB
85 * @throws NoSuchAttributeTypeException if an attribute type does not exist in the EventTemplateDB
87 public Event(String eventName
, boolean validate
, EventTemplateDB eventTemplateDB
)
88 throws EventSystemException
{
89 this(eventName
, validate
, eventTemplateDB
, DEFAULT_ENCODING
);
93 * Create an event called <tt>eventName</tt>
95 * @param eventName the name of the event
96 * @param validate true if the EventTemplateDB should be checked for types before all mutations
97 * @param encoding the character encoding used by the event
98 * @throws NoSuchEventException if the Event does not exist in the EventTemplateDB
99 * @throws NoSuchAttributeException if an attribute does not exist in the EventTemplateDB
100 * @throws NoSuchAttributeTypeException if an attribute type does not exist in the EventTemplateDB
102 public Event(String eventName
, boolean validate
, EventTemplateDB eventTemplateDB
, short encoding
)
103 throws EventSystemException
{
104 setEventTemplateDB(eventTemplateDB
);
105 validating
= validate
;
106 setEventName(eventName
);
107 setEncoding(encoding
);
111 * Creates an event by deserializing a raw byte array.
113 * @param bytes the raw bytes to convert
114 * @param eventTemplateDB the EventTemplateDB to use to validate the event
115 * @throws NoSuchEventException
116 * @throws NoSuchAttributeException
117 * @throws NoSuchAttributeTypeException
119 public Event(byte[] bytes
, EventTemplateDB eventTemplateDB
)
120 throws EventSystemException
{
121 this(bytes
, true, eventTemplateDB
);
126 * Creates an event by deserializing a raw byte array.
128 * @param bytes the raw bytes to convert
129 * @param validate whether or not to validate the event
130 * @param eventTemplateDB the EventTemplateDB to use to validate the event
131 * @throws NoSuchEventException
132 * @throws NoSuchAttributeException
133 * @throws NoSuchAttributeTypeException
135 public Event(byte[] bytes
, boolean validate
, EventTemplateDB eventTemplateDB
)
136 throws EventSystemException
{
137 setEventTemplateDB(eventTemplateDB
);
138 validating
= validate
;
143 * Returns an enumeration of all the event attribute names
145 * @return an enumeration of attribute strings
147 public Enumeration
<String
> getEventAttributeNames() {
148 if (attributes
== null) {
152 return attributes
.keys();
156 * Returns the number of attributes in the event
158 * @return number of attributes in the event
161 if (attributes
== null) {
164 return attributes
.size();
168 * Returns true if the event validates against the EventTemplateDB before making changes
170 * @return the validating state
172 public boolean isValidating() {
173 return this.validating
;
177 * Set to true if the event should validate against the EventTemplateDB before making changes
179 * @param validate the validating value
181 public void setValidating(boolean validate
) {
182 this.validating
= validate
;
186 * Returns the EventTemplateDB for this event, used for validation of types and attributes.
188 * @return the EventTemplateDB
190 public EventTemplateDB
getEventTemplateDB() {
191 return this.eventTemplateDB
;
195 * Sets the EventTemplateDB for this event, used for validation of types and attributes.
197 * @param eventTemplateDB the EventTemplateDB to be used for validation
199 public void setEventTemplateDB(EventTemplateDB eventTemplateDB
) {
200 this.eventTemplateDB
= eventTemplateDB
;
204 * Returns the name of the event
206 * @return the name of the event
208 public synchronized String
getEventName() {
213 * Sets the name of the Event
215 * @param name the name of the event
216 * @throws NoSuchEventException if the event is validating and does not exist in the EventTemplateDB
218 public synchronized void setEventName(String name
) throws NoSuchEventException
{
219 if (isValidating() && getEventTemplateDB() != null) {
220 if (!getEventTemplateDB().checkForEvent(name
)) {
221 throw new NoSuchEventException("Event " + name
+ " does not exist in event definition");
225 /* determine if we already have the name and are just resetting it */
226 if (this.name
!= null) {
227 bytesStoreSize
-= (this.name
.length() + 1 + 2);
230 bytesStoreSize
+= (name
.length() + 1 + 2);
236 * Get the character encoding for this event
238 * @return the encoding
240 public short getEncoding() {
241 return this.encoding
;
245 * Set the character encoding for event strings
247 * @param encoding the character encoding
248 * @throws NoSuchAttributeTypeException if the type for the encoding attribute does not exist
249 * @throws NoSuchAttributeException if the encoding attribute does not exist
251 public void setEncoding(short encoding
) throws EventSystemException
{
252 this.encoding
= encoding
;
253 setInt16(ENCODING
, this.encoding
);
257 * Generic accessor, checks if an attribute exists and returns its value. The user must do their
260 * @param attributeName name of the attribute to lookup
261 * @return the object poitned to by attributeName
262 * @throws NoSuchAttributeException if the attribute does not exist in this event
264 public Object
get(String attributeName
) throws NoSuchAttributeException
{
265 if (attributes
== null) {
269 if (attributes
.containsKey(attributeName
)) {
270 return attributes
.get(attributeName
).getTypeObject();
273 if (isValidating() && getEventTemplateDB() != null) {
274 if (getEventTemplateDB().checkForAttribute(name
, attributeName
)) {
278 throw new NoSuchAttributeException("Attribute " + attributeName
+ " does not exist for event " + name
);
285 public short[] getInt16Array(String attributeName
)
286 throws NoSuchAttributeException
{
287 Object o
= get(attributeName
);
288 if (o
!= null && o
instanceof short[]) {
295 public int[] getInt32Array(String attributeName
)
296 throws NoSuchAttributeException
{
297 Object o
= get(attributeName
);
298 if (o
!= null && o
instanceof int[]) {
305 public long[] getInt64Array(String attributeName
)
306 throws NoSuchAttributeException
{
307 Object o
= get(attributeName
);
308 if (o
!= null && o
instanceof long[]) {
315 public int[] getUInt16Array(String attributeName
)
316 throws NoSuchAttributeException
{
317 Object o
= get(attributeName
);
318 if (o
!= null && o
instanceof int[]) {
325 public long[] getUInt32Array(String attributeName
)
326 throws NoSuchAttributeException
{
327 Object o
= get(attributeName
);
328 if (o
!= null && o
instanceof long[]) {
335 public long[] getUInt64Array(String attributeName
)
336 throws NoSuchAttributeException
{
337 Object o
= get(attributeName
);
338 if (o
!= null && o
instanceof long[]) {
345 public String
[] getStringArray(String attributeName
)
346 throws NoSuchAttributeException
{
347 Object o
= get(attributeName
);
348 if (o
!= null && o
instanceof String
[]) {
357 * Method to check if an attribute is set in the event. This method does not throw
358 * NoSuchAttributeException because it shouldn't really care. If it's not there, it's
361 * @param attributeName The attribute name to check for existance.
362 * @return true if there is a value, false if not.
364 public boolean isSet(String attributeName
) {
366 return (get(attributeName
) != null);
368 catch (NoSuchAttributeException e
) {
374 * Accessor that returns a boolean value for attribute <tt>attributeName</tt>
376 * @param attributeName the name of the attribute to fetch
377 * @return the boolean value
378 * @throws NoSuchAttributeException if the attribute does not exist in this event
380 public Boolean
getBoolean(String attributeName
) throws NoSuchAttributeException
{
381 return (Boolean
) get(attributeName
);
385 * Accessor that returns an <tt>unsigned short</tt>, in the guise of an <tt>int</tt>, for attribute <tt>attributeName</tt>
387 * @param attributeName the name of the attribute to fetch
388 * @return the unsigned short as an int
389 * @throws NoSuchAttributeException if the attribute does not exist in this event
391 public Integer
getUInt16(String attributeName
) throws NoSuchAttributeException
{
392 return (Integer
) get(attributeName
);
396 * Accessor that returns an <tt>short</tt>, for attribute <tt>attributeName</tt>
398 * @param attributeName the name of the attribute to fetch
399 * @return the short value
400 * @throws NoSuchAttributeException if the attribute does not exist in this event
402 public Short
getInt16(String attributeName
) throws NoSuchAttributeException
{
403 return (Short
) get(attributeName
);
407 * Accessor that returns an <tt>unsigned int</tt>, in the guise of an <tt>long</tt>, for attribute <tt>attributeName</tt>
409 * @param attributeName the name of the attribute to fetch
410 * @return the unsigned int as a long
411 * @throws NoSuchAttributeException if the attribute does not exist in this event
413 public Long
getUInt32(String attributeName
) throws NoSuchAttributeException
{
414 return (Long
) get(attributeName
);
418 * Accessor that returns an <tt>int</tt>, for attribute <tt>attributeName</tt>
420 * @param attributeName the name of the attribute to fetch
421 * @return the int value
422 * @throws NoSuchAttributeException if the attribute does not exist in this event
424 public Integer
getInt32(String attributeName
) throws NoSuchAttributeException
{
425 return (Integer
) get(attributeName
);
429 * Accessor that returns an <tt>unsigned long</tt>, in the guise of an <tt>BigInteger</tt>, for attribute <tt>attributeName</tt>
431 * @param attributeName the name of the attribute to fetch
432 * @return the unsigned long as a BigInteger
433 * @throws NoSuchAttributeException if the attribute does not exist in this event
435 public BigInteger
getUInt64(String attributeName
) throws NoSuchAttributeException
{
436 return (BigInteger
) get(attributeName
);
441 * Accessor that returns an <tt>long</tt>, for attribute <tt>attributeName</tt>
443 * @param attributeName the name of the attribute to fetch
444 * @return the long value
445 * @throws NoSuchAttributeException if the attribute does not exist in this event
447 public Long
getInt64(String attributeName
) throws NoSuchAttributeException
{
448 return (Long
) get(attributeName
);
452 * Accessor that returns an <tt>String</tt>, for attribute <tt>attributeName</tt>
454 * @param attributeName the name of the attribute to fetch
455 * @return the String value
456 * @throws NoSuchAttributeException if the attribute does not exist in this event
458 public String
getString(String attributeName
) throws NoSuchAttributeException
{
459 return (String
) get(attributeName
);
463 * Accessor that returns an <tt>InetAddress</tt>, for attribute <tt>attributeName</tt>
465 * @param attributeName the name of the attribute to fetch
466 * @return the InetAddress value
467 * @throws NoSuchAttributeException if the attribute does not exist in this event
469 public InetAddress
getInetAddress(String attributeName
) throws NoSuchAttributeException
{
470 IPAddress a
= (IPAddress
) get(attributeName
);
472 return a
.toInetAddress();
480 * Accessor that returns an IP address in bytes, for attribute <tt>attributeName</tt>
482 * @param attributeName the name of the attribute to fetch
483 * @return the IP address in bytes
484 * @throws NoSuchAttributeException if the attribute does not exist in this event
486 public byte[] getIPAddress(String attributeName
) throws NoSuchAttributeException
{
487 return (byte[]) get(attributeName
);
492 * Set the object's attribute <tt>attributeName</tt> with the Object given
494 * @param attributeName the name of the attribute to set
495 * @param attributeValue the object to set the attribute with
496 * @throws NoSuchAttributeException if the attribute does not exist in this event
497 * @throws NoSuchAttributeTypeException if there is an attribute with an undefined type
499 public void set(String attributeName
, Object attributeValue
)
500 throws EventSystemException
{
501 if (isValidating() && getEventTemplateDB() != null) {
502 if (getEventTemplateDB().checkForAttribute(getEventName(), attributeName
)) {
503 BaseType bt
= getEventTemplateDB().getBaseTypeForObjectAttribute(getEventName(),
504 attributeName
, attributeValue
);
505 set(attributeName
, bt
);
509 throw new NoSuchAttributeException("Must be able to check the EventTemplateDB to use set(String,Object)");
514 * Private method to set a BaseType
516 * @param attribute the name of the attribute to set
517 * @param anObject the BaseType to set in the event
518 * @throws NoSuchAttributeException if the attribute does not exist in this event
519 * @throws NoSuchAttributeTypeException if there is an attribute with an undefined type
521 private void set(String attribute
, BaseType anObject
)
522 throws EventSystemException
{
524 if (isValidating() && getEventTemplateDB() != null) {
525 if (getEventTemplateDB().checkForAttribute(name
, attribute
)) {
526 if (!getEventTemplateDB().checkTypeForAttribute(name
, attribute
, anObject
)) {
527 throw new NoSuchAttributeTypeException("Wrong type '" + anObject
.getTypeName() +
528 "' for " + name
+ "." + attribute
);
532 throw new NoSuchAttributeException("Attribute " + attribute
+ " does not exist for event " + name
);
534 getEventTemplateDB().checkForSize(name
, attribute
, anObject
);
537 if (anObject
.getTypeObject() != null) {
538 BaseType oldObject
= null;
539 int newSize
= bytesStoreSize
+ ((attribute
.length() + 1) + anObject
.bytesStoreSize(encoding
));
540 if (newSize
> MAX_MESSAGE_SIZE
) {
541 throw new EventSystemException("Event size limit is " + MAX_MESSAGE_SIZE
+ " bytes.");
543 if ((oldObject
= attributes
.remove(attribute
)) != null) {
544 bytesStoreSize
-= (attribute
.length() + 1) + oldObject
.bytesStoreSize(encoding
);
547 bytesStoreSize
+= (attribute
.length() + 1) + anObject
.bytesStoreSize(encoding
);
548 attributes
.put(attribute
, anObject
);
552 public void setInt16Array(String attributeName
, short[] value
) throws EventSystemException
{
553 set(attributeName
, new BaseType(TypeID
.INT16_ARRAY_STRING
,
554 TypeID
.INT16_ARRAY_TOKEN
,
557 public void setInt32Array(String attributeName
, int[] value
) throws EventSystemException
{
558 set(attributeName
, new BaseType(TypeID
.INT32_ARRAY_STRING
,
559 TypeID
.INT32_ARRAY_TOKEN
,
562 public void setInt64Array(String attributeName
, long[] value
) throws EventSystemException
{
563 set(attributeName
, new BaseType(TypeID
.INT64_ARRAY_STRING
,
564 TypeID
.INT64_ARRAY_TOKEN
,
567 public void setUInt16Array(String attributeName
, int[] value
) throws EventSystemException
{
568 set(attributeName
, new BaseType(TypeID
.UINT16_ARRAY_STRING
,
569 TypeID
.UINT16_ARRAY_TOKEN
,
572 public void setUInt32Array(String attributeName
, long[] value
) throws EventSystemException
{
573 set(attributeName
, new BaseType(TypeID
.UINT32_ARRAY_STRING
,
574 TypeID
.UINT32_ARRAY_TOKEN
,
577 public void setUInt64Array(String attributeName
, long[] value
) throws EventSystemException
{
578 set(attributeName
, new BaseType(TypeID
.UINT64_ARRAY_STRING
,
579 TypeID
.UINT64_ARRAY_TOKEN
,
582 public void setStringArray(String attributeName
, String
[] value
)
583 throws EventSystemException
{
584 set(attributeName
, new BaseType(TypeID
.STRING_ARRAY_STRING
,
585 TypeID
.STRING_ARRAY_TOKEN
,
590 * Sets the given attribute with a <tt>boolean</tt> value given by <tt>aBool</tt>.
592 * @param attributeName the attribute to set
593 * @param aBool the boolean value to set
594 * @throws NoSuchAttributeException if the attribute does not exist in the event
595 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
597 public void setBoolean(String attributeName
, boolean aBool
)
598 throws EventSystemException
{
599 setBoolean(attributeName
, Boolean
.valueOf(aBool
));
603 * Sets the given attribute with a <tt>Boolean</tt> value given by <tt>aBool</tt>.
605 * @param attributeName the attribute to set
606 * @param aBool the boolean value to set
607 * @throws NoSuchAttributeException
608 * @throws NoSuchAttributeTypeException
610 public void setBoolean(String attributeName
, Boolean aBool
)
611 throws EventSystemException
{
612 set(attributeName
, new BaseType(TypeID
.BOOLEAN_STRING
, TypeID
.BOOLEAN_TOKEN
, aBool
));
616 * Set the given attribute with the <tt>unsigned short</tt> value given by <tt>aNumber</tt>.
617 * Because Java does not support unsigned types, we must use a signed int to cover the range of unsigned short.
619 * @param attributeName the attribute to set
620 * @param aNumber the unsigned short value as an integer
621 * @throws NoSuchAttributeException if the attribute does not exist in the event
622 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
624 public void setUInt16(String attributeName
, int aNumber
)
625 throws EventSystemException
{
626 setUInt16(attributeName
, Integer
.valueOf(aNumber
));
630 * Set the given attribute with the <tt>Integer</tt> value given by <tt>aNumber</tt>.
631 * This should be an <tt>unsigned short</tt>, but is an Integer because Java does not support unsigned types,
632 * and a signed integer is needed to cover the range of an unsigned short.
634 * @param attributeName the attribute to set
635 * @param aNumber the value
637 public void setUInt16(String attributeName
, Integer aNumber
)
638 throws EventSystemException
{
639 set(attributeName
, new BaseType(TypeID
.UINT16_STRING
, TypeID
.UINT16_TOKEN
, aNumber
));
643 * Set the given attribute with the <tt>short</tt> value given by <tt>aNumber</tt>.
645 * @param attributeName the attribute to set
646 * @param aNumber the short value to set
647 * @throws NoSuchAttributeException if the attribute does not exist in the event
648 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
650 public void setInt16(String attributeName
, short aNumber
)
651 throws EventSystemException
{
652 setInt16(attributeName
, Short
.valueOf(aNumber
));
656 * Set the given attribute with the <tt>Short</tt> value given by <tt>aNumber</tt>.
658 * @param attributeName the attribute to set
659 * @param aNumber the short value to set
660 * @throws NoSuchAttributeException if the attribute does not exist in the event
661 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
663 public void setInt16(String attributeName
, Short aNumber
)
664 throws EventSystemException
{
665 set(attributeName
, new BaseType(TypeID
.INT16_STRING
, TypeID
.INT16_TOKEN
, aNumber
));
669 * Set the given attribute with the <tt>unsigned int</tt> value given by <tt>aNumber</tt>.
670 * Because Java does not support unsigned types, we must use a signed long to cover the range of an unsigned int.
672 * @param attributeName the attribute to set
673 * @param aNumber the unsigned int value as a long
674 * @throws NoSuchAttributeException if the attribute does not exist in the event
675 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
677 public void setUInt32(String attributeName
, long aNumber
)
678 throws EventSystemException
{
679 setUInt32(attributeName
, Long
.valueOf(aNumber
));
683 * Set the given attribute with the <tt>Long</tt> value given by <tt>aNumber</tt>.
684 * This should be an <tt>unsigned int</tt>, but is an Long because Java does not support unsigned types,
685 * and a signed long is needed to cover the range of an unsigned int.
687 * @param attributeName the attribute to set
688 * @param aNumber the value
690 public void setUInt32(String attributeName
, Long aNumber
)
691 throws EventSystemException
{
692 set(attributeName
, new BaseType(TypeID
.UINT32_STRING
, TypeID
.UINT32_TOKEN
, aNumber
));
696 * Set the given attribute with the <tt>int</tt> value given by <tt>aNumber</tt>.
698 * @param attributeName the attribute to set
699 * @param aNumber the integer value to set
700 * @throws NoSuchAttributeException if the attribute does not exist in the event
701 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
703 public void setInt32(String attributeName
, int aNumber
)
704 throws EventSystemException
{
705 setInt32(attributeName
, Integer
.valueOf(aNumber
));
709 * Set the given attribute with the <tt>Integer</tt> value given by <tt>aNumber</tt>.
711 * @param attributeName the attribute to set
712 * @param aNumber the Integer value to set
713 * @throws NoSuchAttributeException if the attribute does not exist in the event
714 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
716 public void setInt32(String attributeName
, Integer aNumber
)
717 throws EventSystemException
{
718 set(attributeName
, new BaseType(TypeID
.INT32_STRING
, TypeID
.INT32_TOKEN
, aNumber
));
722 * Set the given attribute with the <tt>unsigned long</tt> value given by <tt>aNumber</tt>.
724 * @param attributeName the attribute to set
725 * @param aNumber the value
726 * @throws NoSuchAttributeException if the attribute does not exist in the event
727 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
729 public void setUInt64(String attributeName
, long aNumber
)
730 throws EventSystemException
{
731 set(attributeName
, new BaseType(TypeID
.UINT64_STRING
, TypeID
.UINT64_TOKEN
, BigInteger
.valueOf(aNumber
)));
735 * Set the given attribute with the <tt>Long</tt> value given by <tt>aNumber</tt>.
737 * @param attributeName the attribute to set
738 * @param aNumber the value
739 * @throws NoSuchAttributeException if the attribute does not exist in the event
740 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
742 public void setUInt64(String attributeName
, Long aNumber
)
743 throws EventSystemException
{
745 new BaseType(TypeID
.UINT64_STRING
, TypeID
.UINT64_TOKEN
, BigInteger
.valueOf(aNumber
.longValue())));
749 * Set the given attribute with the <tt>BigInteger</tt> value given by <tt>aNumber</tt>.
750 * This should be an <tt>unsigned long</tt>, but is an BigInteger because Java does not support unsigned types,
751 * and a BigInteger is needed to cover the range of an unsigned long.
753 * @param attributeName the attribute to set
754 * @param aNumber the value
756 public void setUInt64(String attributeName
, BigInteger aNumber
)
757 throws EventSystemException
{
758 set(attributeName
, new BaseType(TypeID
.UINT64_STRING
, TypeID
.UINT64_TOKEN
, aNumber
));
762 * Set the given attribute with the <tt>long</tt> value given by <tt>aNumber</tt>.
764 * @param attributeName the attribute to set
765 * @param aNumber the long value to set
766 * @throws NoSuchAttributeException if the attribute does not exist in the event
767 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
769 public void setInt64(String attributeName
, long aNumber
)
770 throws EventSystemException
{
771 setInt64(attributeName
, Long
.valueOf(aNumber
));
775 * Set the given attribute with the <tt>Long</tt> value given by <tt>aNumber</tt>.
777 * @param attributeName the attribute to set
778 * @param aNumber the Long value to set
779 * @throws NoSuchAttributeException if the attribute does not exist in the event
780 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
782 public void setInt64(String attributeName
, Long aNumber
)
783 throws EventSystemException
{
784 set(attributeName
, new BaseType(TypeID
.INT64_STRING
, TypeID
.INT64_TOKEN
, aNumber
));
788 * Set the given attribute with a <tt>String</tt>
790 * @param attributeName the attribute to set
791 * @param aString the String value to set
792 * @throws NoSuchAttributeException if the attribute does not exist in the event
793 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
795 public void setString(String attributeName
, String aString
)
796 throws EventSystemException
{
797 set(attributeName
, new BaseType(TypeID
.STRING_STRING
, TypeID
.STRING_TOKEN
, aString
));
801 * Set the given attribute with the <tt>ip address</tt> value given by <tt>address</tt>
803 * @param attributeName the attribute to set
804 * @param address the ip address in bytes
805 * @throws NoSuchAttributeException if the attribute does not exist in the event
806 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
808 public void setIPAddress(String attributeName
, byte[] address
)
809 throws EventSystemException
{
810 setIPAddress(attributeName
, new IPAddress(address
));
814 * Set the given attribute with the <tt>ip address</tt> value given by <tt>address</tt>
816 * @param attributeName the attribute to set
817 * @param address the ip address in bytes
818 * @throws NoSuchAttributeException if the attribute does not exist in the event
819 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
821 public void setIPAddress(String attributeName
, InetAddress address
)
822 throws EventSystemException
{
823 setIPAddress(attributeName
, new IPAddress(address
));
827 * Set the given attribute with the <tt>ip address</tt> value given by <tt>address</tt>
829 * @param attributeName the attribute to set
830 * @param address the ip address in bytes
831 * @throws NoSuchAttributeException if the attribute does not exist in the event
832 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
834 public void setIPAddress(String attributeName
, IPAddress address
)
835 throws EventSystemException
{
836 set(attributeName
, new BaseType(TypeID
.IPADDR_STRING
, TypeID
.IPADDR_TOKEN
, address
));
840 * Serializes the Event into a byte array
842 * @return the serialized byte array
844 public byte[] serialize() {
846 * Serialization uses the following protocol
847 * EVENTWORD,<number of elements>,ATTRIBUTEWORD,TYPETOKEN,
848 * (UINT16|INT16|UINT32|INT32|UINT64|INT64|BOOLEAN|STRING)
849 * ...ATTRIBUTEWORD,TYPETOKEN(UINT16|INT16|UINT32|INT32|
850 * UINT64|INT64|BOOLEAN|STRING)
852 * The first attribute will always be the encoding if present.
854 byte[] bytes
= new byte[this.bytesStoreSize
];
856 int attributeCount
= 0;
857 short encoding
= DEFAULT_ENCODING
;
859 if (attributes
!= null) {
860 attributeCount
= attributes
.size();
863 offset
+= Serializer
.serializeEVENTWORD(name
, bytes
, offset
);
864 offset
+= Serializer
.serializeUINT16((short) (attributeCount
), bytes
, offset
);
867 * Set the encoding attributes in the event
869 if (attributes
!= null) {
870 BaseType encodingBase
= attributes
.get(ENCODING
);
871 if (encodingBase
!= null) {
872 Object encodingObj
= encodingBase
.getTypeObject();
873 byte encodingType
= encodingBase
.getTypeToken();
874 if (encodingObj
!= null) {
875 if (encodingType
== TypeID
.INT16_TOKEN
) {
876 encoding
= (Short
) encodingObj
;
877 Log
.trace("Character encoding: " + encoding
);
878 offset
+= Serializer
.serializeATTRIBUTEWORD(ENCODING
, bytes
, offset
);
879 offset
+= Serializer
.serializeBYTE(encodingType
, bytes
, offset
);
880 offset
+= Serializer
.serializeUINT16(encoding
, bytes
, offset
);
885 Log
.warning("Character encoding null in event " + name
);
888 Enumeration
<String
> e
= attributes
.keys();
889 while (e
.hasMoreElements()) {
890 String key
= e
.nextElement();
891 if (key
.equals(ENCODING
)) {
895 BaseType value
= attributes
.get(key
);
896 Object data
= value
.getTypeObject();
897 byte typeToken
= value
.getTypeToken();
899 /* don't try to serialize nulls */
901 Log
.warning("Attribute " + key
+ " was null in event " + name
);
905 offset
+= Serializer
.serializeATTRIBUTEWORD(key
, bytes
, offset
);
906 offset
+= Serializer
.serializeBYTE(typeToken
, bytes
, offset
);
909 case TypeID
.BOOLEAN_TOKEN
:
910 offset
+= Serializer
.serializeBOOLEAN((Boolean
) data
, bytes
, offset
);
912 case TypeID
.UINT16_TOKEN
:
913 offset
+= Serializer
.serializeUINT16((Integer
) data
, bytes
, offset
);
915 case TypeID
.INT16_TOKEN
:
916 offset
+= Serializer
.serializeINT16((Short
) data
, bytes
, offset
);
918 case TypeID
.UINT32_TOKEN
:
919 offset
+= Serializer
.serializeUINT32((Long
) data
, bytes
, offset
);
921 case TypeID
.INT32_TOKEN
:
922 offset
+= Serializer
.serializeINT32((Integer
) data
, bytes
, offset
);
924 case TypeID
.UINT64_TOKEN
:
925 offset
+= Serializer
.serializeUINT64((BigInteger
) data
, bytes
, offset
);
927 case TypeID
.INT64_TOKEN
:
928 offset
+= Serializer
.serializeINT64((Long
) data
, bytes
, offset
);
930 case TypeID
.STRING_TOKEN
:
931 offset
+= Serializer
.serializeSTRING(((String
) data
), bytes
, offset
, encoding
);
933 case TypeID
.IPADDR_TOKEN
:
934 offset
+= Serializer
.serializeIPADDR(((IPAddress
) data
), bytes
, offset
);
936 case TypeID
.STRING_ARRAY_TOKEN
:
937 offset
+= Serializer
.serializeStringArray
938 (((String
[]) data
), bytes
, offset
, encoding
);
940 case TypeID
.INT16_ARRAY_TOKEN
:
941 offset
+= Serializer
.serializeInt16Array((short[]) data
, bytes
, offset
);
943 case TypeID
.INT32_ARRAY_TOKEN
:
944 offset
+= Serializer
.serializeInt32Array((int[]) data
, bytes
, offset
);
946 case TypeID
.INT64_ARRAY_TOKEN
:
947 offset
+= Serializer
.serializeInt64Array((long[]) data
, bytes
, offset
);
949 case TypeID
.UINT16_ARRAY_TOKEN
:
950 offset
+= Serializer
.serializeUInt16Array((int[]) data
, bytes
, offset
);
952 case TypeID
.UINT32_ARRAY_TOKEN
:
953 offset
+= Serializer
.serializeUInt32Array((long[]) data
, bytes
, offset
);
955 case TypeID
.UINT64_ARRAY_TOKEN
:
956 offset
+= Serializer
.serializeUInt64Array((long[]) data
, bytes
, offset
);
959 Log
.warning("Unknown BaseType token: " + typeToken
);
961 } // switch(typeToken)
963 Log
.trace("Serialized attribute " + key
);
964 } // while(e.hasMoreElements())
965 } // if(attributes != null)
971 * Deserialize the Event from byte array
973 * @param bytes the byte array containing a serialized Event
975 public void deserialize(byte[] bytes
)
976 throws EventSystemException
{
981 state
= new DeserializerState();
985 setEventName(Deserializer
.deserializeEVENTWORD(state
, bytes
));
986 long num
= Deserializer
.deserializeUINT16(state
, bytes
);
987 if (Log
.isLogTrace()) {
988 Log
.trace("Event name = " + getEventName());
989 Log
.trace("Number of attribute: " + num
);
991 for (int i
= 0; i
< num
; ++i
) {
992 String attribute
= Deserializer
.deserializeATTRIBUTEWORD(state
, bytes
);
994 byte type
= Deserializer
.deserializeBYTE(state
, bytes
);
995 if (Log
.isLogTrace()) {
996 Log
.trace("Attribute: " + attribute
);
997 Log
.trace("Type: " + TypeID
.byteIDToString(type
));
998 Log
.trace("State: " + state
);
1000 if (attribute
!= null) {
1001 if (i
== 0 && attribute
.equals(ENCODING
)) {
1002 if (type
== TypeID
.INT16_TOKEN
) {
1003 setEncoding(Deserializer
.deserializeINT16(state
, bytes
));
1007 Log
.warning("Found encoding, but type was not int16 while deserializing");
1012 case TypeID
.BOOLEAN_TOKEN
:
1013 boolean aBool
= Deserializer
.deserializeBOOLEAN(state
, bytes
);
1014 setBoolean(attribute
, aBool
);
1016 case TypeID
.UINT16_TOKEN
:
1017 int uShort
= Deserializer
.deserializeUINT16(state
, bytes
);
1018 setUInt16(attribute
, uShort
);
1020 case TypeID
.INT16_TOKEN
:
1021 short aShort
= Deserializer
.deserializeINT16(state
, bytes
);
1022 setInt16(attribute
, aShort
);
1024 case TypeID
.UINT32_TOKEN
:
1025 long uInt
= Deserializer
.deserializeUINT32(state
, bytes
);
1026 setUInt32(attribute
, uInt
);
1028 case TypeID
.INT32_TOKEN
:
1029 int aInt
= Deserializer
.deserializeINT32(state
, bytes
);
1030 setInt32(attribute
, aInt
);
1032 case TypeID
.UINT64_TOKEN
:
1033 long uLong
= Deserializer
.deserializeUINT64(state
, bytes
);
1034 setUInt64(attribute
, BigInteger
.valueOf(uLong
));
1036 case TypeID
.INT64_TOKEN
:
1037 long aLong
= Deserializer
.deserializeINT64(state
, bytes
);
1038 setInt64(attribute
, aLong
);
1040 case TypeID
.STRING_TOKEN
:
1041 String s
= Deserializer
.deserializeSTRING(state
, bytes
, encoding
);
1042 setString(attribute
, s
);
1044 case TypeID
.IPADDR_TOKEN
:
1045 byte[] inetAddress
= Deserializer
.deserializeIPADDR(state
, bytes
);
1046 setIPAddress(attribute
, inetAddress
);
1048 case TypeID
.STRING_ARRAY_TOKEN
:
1049 String
[] sArray
= Deserializer
.deserializeStringArray(state
, bytes
, encoding
);
1050 setStringArray(attribute
, sArray
);
1052 case TypeID
.INT16_ARRAY_TOKEN
:
1053 short[] as
= Deserializer
.deserializeInt16Array(state
, bytes
);
1054 setInt16Array(attribute
, as
);
1056 case TypeID
.INT32_ARRAY_TOKEN
:
1057 int[] ai
= Deserializer
.deserializeInt32Array(state
, bytes
);
1058 setInt32Array(attribute
, ai
);
1060 case TypeID
.INT64_ARRAY_TOKEN
:
1061 long[] al
= Deserializer
.deserializeInt64Array(state
, bytes
);
1062 setInt64Array(attribute
, al
);
1064 case TypeID
.UINT16_ARRAY_TOKEN
:
1065 int[] uas
= Deserializer
.deserializeUInt16Array(state
, bytes
);
1066 setUInt16Array(attribute
, uas
);
1068 case TypeID
.UINT32_ARRAY_TOKEN
:
1069 long[] uai
= Deserializer
.deserializeUInt32Array(state
, bytes
);
1070 setUInt32Array(attribute
, uai
);
1072 case TypeID
.UINT64_ARRAY_TOKEN
:
1073 long[] ual
= Deserializer
.deserializeUInt64Array(state
, bytes
);
1074 setUInt64Array(attribute
, ual
);
1077 Log
.warning("Unknown type " + type
+ " in deserialization");
1080 } // for (int i =0 ...
1085 * Returns a mutable copy of the event. This is a SLOW operation.
1087 * @return Event the Event object
1088 * @throws NoSuchEventException if the Event does not exist in the EventTemplateDB
1089 * @throws NoSuchAttributeException if the attribute does not exist in this event
1090 * @throws NoSuchAttributeTypeException if there is an attribute that does not match a type in the EventTemplateDB
1092 public Event
copy() throws EventSystemException
{
1093 /* match the type-checking of the original event */
1094 Event evt
= new Event(name
, isValidating(), getEventTemplateDB());
1095 for (Enumeration
<String
> e
= attributes
.keys(); e
.hasMoreElements();) {
1096 String key
= e
.nextElement();
1097 BaseType value
= attributes
.get(key
);
1098 evt
.set(key
, value
);
1105 * Returns a String representation of this event
1107 * @return a String return of this event.
1109 public String
toString() {
1114 StringBuffer sb
= new StringBuffer();
1118 if (attributes
!= null) {
1120 String
[] keys
= new String
[attributes
.size()];
1121 for (Enumeration
<String
> e
= attributes
.keys(); e
.hasMoreElements();) {
1122 keys
[i
++] = e
.nextElement();
1127 for (i
= 0; i
< attributes
.size(); ++i
) {
1128 BaseType value
= attributes
.get(keys
[i
]);
1129 if (isValidating() && getEventTemplateDB() != null) {
1130 if (getEventTemplateDB().checkTypeForAttribute(name
, keys
[i
], TypeID
.UINT64_STRING
)) {
1135 .append(NumberCodec
.toHexString(getUInt64(keys
[i
])))
1138 catch (EventSystemException exc
) {
1139 Log
.warning("Event.toString : ", exc
);
1143 sb
.append("\t").append(keys
[i
]).append(" = ").append(value
).append(";\n");
1147 sb
.append("\t").append(keys
[i
]).append(" = ").append(value
).append(";\n");
1149 } // for(i = 0; i < attributes.size() ...
1150 } // if(attributes != null)
1153 return sb
.toString();
1157 public int hashCode() {
1158 return toString().hashCode();
1161 public boolean equals(Object o
) {
1165 if (getClass().getName().equals(o
.getClass().getName())) {
1166 return toString().equals(o
.toString());
1174 * This method can be used to validate an event after it has been created.
1176 * @throws EventSystemException
1178 public void validate() throws EventSystemException
{
1179 EventTemplateDB templ
= getEventTemplateDB();
1180 if (templ
== null) {
1181 throw new EventSystemException("No template defined.");
1183 if (!templ
.checkForEvent(name
)) {
1184 throw new NoSuchEventException("Event " + name
+ " does not exist in event definition");
1186 for (String key
: attributes
.keySet()) {
1187 if (!templ
.checkForAttribute(name
, key
)) {
1188 throw new NoSuchAttributeException("Attribute " + key
+ " does not exist for event " + name
);
1190 Object value
= get(key
);
1191 BaseType expected
= templ
.getBaseTypeForObjectAttribute(name
, key
, value
);
1192 BaseType bt
= BaseType
.baseTypeFromObject(value
);
1195 * There are no unsigned values in java so they are kind of a special case
1196 * in that i can't guess which one the person meant. This small hack treats
1197 * similar types the same way.
1199 if ((expected
.getTypeToken() == TypeID
.UINT16_TOKEN
&&
1200 bt
.getTypeToken() == TypeID
.INT32_TOKEN
) ||
1201 (expected
.getTypeToken() == TypeID
.UINT32_TOKEN
&&
1202 bt
.getTypeToken() == TypeID
.INT64_TOKEN
) ||
1203 (expected
.getTypeToken() == TypeID
.UINT64_TOKEN
&&
1204 bt
.getTypeToken() == TypeID
.INT64_TOKEN
) ) {
1207 if (!templ
.checkTypeForAttribute(name
, key
, bt
)) {
1208 throw new NoSuchAttributeTypeException("Wrong type '" + bt
.getTypeName() +
1209 "' for " + name
+ "." + key
);
1212 Map
<String
, BaseType
> map
= templ
.getEvents().get(name
);
1213 for (String key
: map
.keySet()) {
1214 BaseType bt
= map
.get(key
);
1215 if (bt
.isRequired()) {
1216 if (!attributes
.containsKey(key
)) {
1217 throw new AttributeRequiredException(key
);