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
;
20 public static final int MAX_MESSAGE_SIZE
= 65507;
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";
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
};
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
;
48 * If this is set to true, types and attributes are validated against the EventTemplateDB
50 private boolean validating
= true;
53 * Internal object for deserialization state
55 private DeserializerState state
= null;
58 * the size of the event in bytes
60 private int bytesStoreSize
= 0;
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
);
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
);
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
;
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) {
151 return attributes
.keys();
155 * Returns the number of attributes in the event
157 * @return number of attributes in the event
160 if (attributes
== null) {
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() {
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);
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
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) {
268 if (attributes
.containsKey(attributeName
)) {
269 return attributes
.get(attributeName
).getTypeObject();
272 if (isValidating() && getEventTemplateDB() != null) {
273 if (getEventTemplateDB().checkForAttribute(name
, attributeName
)) {
277 throw new NoSuchAttributeException("Attribute " + attributeName
+ " does not exist for event " + name
);
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
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
) {
294 return (get(attributeName
) != null);
296 catch (NoSuchAttributeException e
) {
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 return (InetAddress
) get(attributeName
);
402 * Accessor that returns an IP address in bytes, for attribute <tt>attributeName</tt>
404 * @param attributeName the name of the attribute to fetch
405 * @return the IP address in bytes
406 * @throws NoSuchAttributeException if the attribute does not exist in this event
408 public byte[] getIPAddress(String attributeName
) throws NoSuchAttributeException
{
409 return (byte[]) get(attributeName
);
414 * Set the object's attribute <tt>attributeName</tt> with the Object given
416 * @param attributeName the name of the attribute to set
417 * @param attributeValue the object to set the attribute with
418 * @throws NoSuchAttributeException if the attribute does not exist in this event
419 * @throws NoSuchAttributeTypeException if there is an attribute with an undefined type
421 public void set(String attributeName
, Object attributeValue
)
422 throws EventSystemException
{
423 if (isValidating() && getEventTemplateDB() != null) {
424 if (getEventTemplateDB().checkForAttribute(getEventName(), attributeName
)) {
425 BaseType bt
= getEventTemplateDB().getBaseTypeForObjectAttribute(getEventName(),
426 attributeName
, attributeValue
);
427 set(attributeName
, bt
);
431 throw new NoSuchAttributeException("Must be able to check the EventTemplateDB to use set(String,Object)");
436 * Private method to set a BaseType
438 * @param attribute the name of the attribute to set
439 * @param anObject the BaseType to set in the event
440 * @throws NoSuchAttributeException if the attribute does not exist in this event
441 * @throws NoSuchAttributeTypeException if there is an attribute with an undefined type
443 private void set(String attribute
, BaseType anObject
)
444 throws EventSystemException
{
446 if (isValidating() && getEventTemplateDB() != null) {
447 if (getEventTemplateDB().checkForAttribute(name
, attribute
)) {
448 if (!getEventTemplateDB().checkTypeForAttribute(name
, attribute
, anObject
)) {
449 throw new NoSuchAttributeTypeException("Wrong type '" + anObject
.getTypeName() +
450 "' for " + name
+ "." + attribute
);
454 throw new NoSuchAttributeException("Attribute " + attribute
+ " does not exist for event " + name
);
458 if (anObject
.getTypeObject() != null) {
459 BaseType oldObject
= null;
460 int newSize
= bytesStoreSize
+ ((attribute
.length() + 1) + anObject
.bytesStoreSize(encoding
));
461 if (newSize
> MAX_MESSAGE_SIZE
) {
462 throw new EventSystemException("Event size limit is " + MAX_MESSAGE_SIZE
+ " bytes.");
464 if ((oldObject
= attributes
.remove(attribute
)) != null) {
465 bytesStoreSize
-= (attribute
.length() + 1) + oldObject
.bytesStoreSize(encoding
);
468 bytesStoreSize
+= (attribute
.length() + 1) + anObject
.bytesStoreSize(encoding
);
469 attributes
.put(attribute
, anObject
);
474 * Sets the given attribute with a <tt>boolean</tt> value given by <tt>aBool</tt>.
476 * @param attributeName the attribute to set
477 * @param aBool the boolean value to set
478 * @throws NoSuchAttributeException if the attribute does not exist in the event
479 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
481 public void setBoolean(String attributeName
, boolean aBool
)
482 throws EventSystemException
{
483 setBoolean(attributeName
, Boolean
.valueOf(aBool
));
487 * Sets the given attribute with a <tt>Boolean</tt> value given by <tt>aBool</tt>.
489 * @param attributeName the attribute to set
490 * @param aBool the boolean value to set
491 * @throws NoSuchAttributeException
492 * @throws NoSuchAttributeTypeException
494 public void setBoolean(String attributeName
, Boolean aBool
)
495 throws EventSystemException
{
496 set(attributeName
, new BaseType(TypeID
.BOOLEAN_STRING
, TypeID
.BOOLEAN_TOKEN
, aBool
));
500 * Set the given attribute with the <tt>unsigned short</tt> value given by <tt>aNumber</tt>.
501 * Because Java does not support unsigned types, we must use a signed int to cover the range of unsigned short.
503 * @param attributeName the attribute to set
504 * @param aNumber the unsigned short value as an integer
505 * @throws NoSuchAttributeException if the attribute does not exist in the event
506 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
508 public void setUInt16(String attributeName
, int aNumber
)
509 throws EventSystemException
{
510 setUInt16(attributeName
, Integer
.valueOf(aNumber
));
514 * Set the given attribute with the <tt>Integer</tt> value given by <tt>aNumber</tt>.
515 * This should be an <tt>unsigned short</tt>, but is an Integer because Java does not support unsigned types,
516 * and a signed integer is needed to cover the range of an unsigned short.
518 * @param attributeName the attribute to set
519 * @param aNumber the value
521 public void setUInt16(String attributeName
, Integer aNumber
)
522 throws EventSystemException
{
523 set(attributeName
, new BaseType(TypeID
.UINT16_STRING
, TypeID
.UINT16_TOKEN
, aNumber
));
527 * Set the given attribute with the <tt>short</tt> value given by <tt>aNumber</tt>.
529 * @param attributeName the attribute to set
530 * @param aNumber the short value to set
531 * @throws NoSuchAttributeException if the attribute does not exist in the event
532 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
534 public void setInt16(String attributeName
, short aNumber
)
535 throws EventSystemException
{
536 setInt16(attributeName
, Short
.valueOf(aNumber
));
540 * Set the given attribute with the <tt>Short</tt> value given by <tt>aNumber</tt>.
542 * @param attributeName the attribute to set
543 * @param aNumber the short value to set
544 * @throws NoSuchAttributeException if the attribute does not exist in the event
545 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
547 public void setInt16(String attributeName
, Short aNumber
)
548 throws EventSystemException
{
549 set(attributeName
, new BaseType(TypeID
.INT16_STRING
, TypeID
.INT16_TOKEN
, aNumber
));
553 * Set the given attribute with the <tt>unsigned int</tt> value given by <tt>aNumber</tt>.
554 * Because Java does not support unsigned types, we must use a signed long to cover the range of an unsigned int.
556 * @param attributeName the attribute to set
557 * @param aNumber the unsigned int value as a long
558 * @throws NoSuchAttributeException if the attribute does not exist in the event
559 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
561 public void setUInt32(String attributeName
, long aNumber
)
562 throws EventSystemException
{
563 setUInt32(attributeName
, Long
.valueOf(aNumber
));
567 * Set the given attribute with the <tt>Long</tt> value given by <tt>aNumber</tt>.
568 * This should be an <tt>unsigned int</tt>, but is an Long because Java does not support unsigned types,
569 * and a signed long is needed to cover the range of an unsigned int.
571 * @param attributeName the attribute to set
572 * @param aNumber the value
574 public void setUInt32(String attributeName
, Long aNumber
)
575 throws EventSystemException
{
576 set(attributeName
, new BaseType(TypeID
.UINT32_STRING
, TypeID
.UINT32_TOKEN
, aNumber
));
580 * Set the given attribute with the <tt>int</tt> value given by <tt>aNumber</tt>.
582 * @param attributeName the attribute to set
583 * @param aNumber the integer value to set
584 * @throws NoSuchAttributeException if the attribute does not exist in the event
585 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
587 public void setInt32(String attributeName
, int aNumber
)
588 throws EventSystemException
{
589 setInt32(attributeName
, Integer
.valueOf(aNumber
));
593 * Set the given attribute with the <tt>Integer</tt> value given by <tt>aNumber</tt>.
595 * @param attributeName the attribute to set
596 * @param aNumber the Integer value to set
597 * @throws NoSuchAttributeException if the attribute does not exist in the event
598 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
600 public void setInt32(String attributeName
, Integer aNumber
)
601 throws EventSystemException
{
602 set(attributeName
, new BaseType(TypeID
.INT32_STRING
, TypeID
.INT32_TOKEN
, aNumber
));
606 * Set the given attribute with the <tt>unsigned long</tt> value given by <tt>aNumber</tt>.
608 * @param attributeName the attribute to set
609 * @param aNumber the value
610 * @throws NoSuchAttributeException if the attribute does not exist in the event
611 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
613 public void setUInt64(String attributeName
, long aNumber
)
614 throws EventSystemException
{
615 set(attributeName
, new BaseType(TypeID
.UINT64_STRING
, TypeID
.UINT64_TOKEN
, BigInteger
.valueOf(aNumber
)));
619 * Set the given attribute with the <tt>Long</tt> value given by <tt>aNumber</tt>.
621 * @param attributeName the attribute to set
622 * @param aNumber the value
623 * @throws NoSuchAttributeException if the attribute does not exist in the event
624 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
626 public void setUInt64(String attributeName
, Long aNumber
)
627 throws EventSystemException
{
629 new BaseType(TypeID
.UINT64_STRING
, TypeID
.UINT64_TOKEN
, BigInteger
.valueOf(aNumber
.longValue())));
633 * Set the given attribute with the <tt>BigInteger</tt> value given by <tt>aNumber</tt>.
634 * This should be an <tt>unsigned long</tt>, but is an BigInteger because Java does not support unsigned types,
635 * and a BigInteger is needed to cover the range of an unsigned long.
637 * @param attributeName the attribute to set
638 * @param aNumber the value
640 public void setUInt64(String attributeName
, BigInteger aNumber
)
641 throws EventSystemException
{
642 set(attributeName
, new BaseType(TypeID
.UINT64_STRING
, TypeID
.UINT64_TOKEN
, aNumber
));
646 * Set the given attribute with the <tt>long</tt> value given by <tt>aNumber</tt>.
648 * @param attributeName the attribute to set
649 * @param aNumber the long value to set
650 * @throws NoSuchAttributeException if the attribute does not exist in the event
651 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
653 public void setInt64(String attributeName
, long aNumber
)
654 throws EventSystemException
{
655 setInt64(attributeName
, Long
.valueOf(aNumber
));
659 * Set the given attribute with the <tt>Long</tt> value given by <tt>aNumber</tt>.
661 * @param attributeName the attribute to set
662 * @param aNumber the Long value to set
663 * @throws NoSuchAttributeException if the attribute does not exist in the event
664 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
666 public void setInt64(String attributeName
, Long aNumber
)
667 throws EventSystemException
{
668 set(attributeName
, new BaseType(TypeID
.INT64_STRING
, TypeID
.INT64_TOKEN
, aNumber
));
672 * Set the given attribute with a <tt>String</tt>
674 * @param attributeName the attribute to set
675 * @param aString the String value to set
676 * @throws NoSuchAttributeException if the attribute does not exist in the event
677 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
679 public void setString(String attributeName
, String aString
)
680 throws EventSystemException
{
681 set(attributeName
, new BaseType(TypeID
.STRING_STRING
, TypeID
.STRING_TOKEN
, aString
));
685 * Set the given attribute with the <tt>ip address</tt> value given by <tt>address</tt>
687 * @param attributeName the attribute to set
688 * @param address the ip address in bytes
689 * @throws NoSuchAttributeException if the attribute does not exist in the event
690 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
692 public void setIPAddress(String attributeName
, byte[] address
)
693 throws EventSystemException
{
694 setIPAddress(attributeName
, new IPAddress(address
));
698 * Set the given attribute with the <tt>ip address</tt> value given by <tt>address</tt>
700 * @param attributeName the attribute to set
701 * @param address the ip address in bytes
702 * @throws NoSuchAttributeException if the attribute does not exist in the event
703 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
705 public void setIPAddress(String attributeName
, InetAddress address
)
706 throws EventSystemException
{
707 setIPAddress(attributeName
, new IPAddress(address
));
711 * Set the given attribute with the <tt>ip address</tt> value given by <tt>address</tt>
713 * @param attributeName the attribute to set
714 * @param address the ip address in bytes
715 * @throws NoSuchAttributeException if the attribute does not exist in the event
716 * @throws NoSuchAttributeTypeException if the attribute type does not match the EventTemplateDB
718 public void setIPAddress(String attributeName
, IPAddress address
)
719 throws EventSystemException
{
720 set(attributeName
, new BaseType(TypeID
.IPADDR_STRING
, TypeID
.IPADDR_TOKEN
, address
));
724 * Serializes the Event into a byte array
726 * @return the serialized byte array
728 public byte[] serialize() {
730 * Serialization uses the following protocol
731 * EVENTWORD,<number of elements>,ATTRIBUTEWORD,TYPETOKEN,
732 * (UINT16|INT16|UINT32|INT32|UINT64|INT64|BOOLEAN|STRING)
733 * ...ATTRIBUTEWORD,TYPETOKEN(UINT16|INT16|UINT32|INT32|
734 * UINT64|INT64|BOOLEAN|STRING)
736 * The first attribute will always be the encoding if present.
738 byte[] bytes
= new byte[this.bytesStoreSize
];
740 int attributeCount
= 0;
741 short encoding
= DEFAULT_ENCODING
;
743 if (attributes
!= null) {
744 attributeCount
= attributes
.size();
747 offset
+= Serializer
.serializeEVENTWORD(name
, bytes
, offset
);
748 offset
+= Serializer
.serializeUINT16((short) (attributeCount
), bytes
, offset
);
751 * Set the encoding attributes in the event
753 if (attributes
!= null) {
754 BaseType encodingBase
= attributes
.get(ENCODING
);
755 if (encodingBase
!= null) {
756 Object encodingObj
= encodingBase
.getTypeObject();
757 byte encodingType
= encodingBase
.getTypeToken();
758 if (encodingObj
!= null) {
759 if (encodingType
== TypeID
.INT16_TOKEN
) {
760 encoding
= (Short
) encodingObj
;
761 Log
.trace("Character encoding: " + encoding
);
762 offset
+= Serializer
.serializeATTRIBUTEWORD(ENCODING
, bytes
, offset
);
763 offset
+= Serializer
.serializeBYTE(encodingType
, bytes
, offset
);
764 offset
+= Serializer
.serializeUINT16(encoding
, bytes
, offset
);
769 Log
.warning("Character encoding null in event " + name
);
772 Enumeration
<String
> e
= attributes
.keys();
773 while (e
.hasMoreElements()) {
774 String key
= e
.nextElement();
775 if (key
.equals(ENCODING
)) {
779 BaseType value
= attributes
.get(key
);
780 Object data
= value
.getTypeObject();
781 byte typeToken
= value
.getTypeToken();
783 /* don't try to serialize nulls */
785 Log
.warning("Attribute " + key
+ " was null in event " + name
);
789 offset
+= Serializer
.serializeATTRIBUTEWORD(key
, bytes
, offset
);
790 offset
+= Serializer
.serializeBYTE(typeToken
, bytes
, offset
);
793 case TypeID
.BOOLEAN_TOKEN
:
794 offset
+= Serializer
.serializeBOOLEAN((Boolean
) data
, bytes
, offset
);
796 case TypeID
.UINT16_TOKEN
:
797 offset
+= Serializer
.serializeUINT16((Integer
) data
, bytes
, offset
);
799 case TypeID
.INT16_TOKEN
:
800 offset
+= Serializer
.serializeINT16((Short
) data
, bytes
, offset
);
802 case TypeID
.UINT32_TOKEN
:
803 offset
+= Serializer
.serializeUINT32((Long
) data
, bytes
, offset
);
805 case TypeID
.INT32_TOKEN
:
806 offset
+= Serializer
.serializeINT32((Integer
) data
, bytes
, offset
);
808 case TypeID
.UINT64_TOKEN
:
809 offset
+= Serializer
.serializeUINT64((BigInteger
) data
, bytes
, offset
);
811 case TypeID
.INT64_TOKEN
:
812 offset
+= Serializer
.serializeINT64((Long
) data
, bytes
, offset
);
814 case TypeID
.STRING_TOKEN
:
815 offset
+= Serializer
.serializeSTRING(((String
) data
), bytes
, offset
, encoding
);
817 case TypeID
.IPADDR_TOKEN
:
818 offset
+= Serializer
.serializeIPADDR(((IPAddress
) data
), bytes
, offset
);
821 Log
.warning("Unknown BaseType token: " + typeToken
);
823 } // switch(typeToken)
825 Log
.trace("Serialized attribute " + key
);
826 } // while(e.hasMoreElements())
827 } // if(attributes != null)
833 * Deserialize the Event from byte array
835 * @param bytes the byte array containing a serialized Event
837 public void deserialize(byte[] bytes
)
838 throws EventSystemException
{
843 state
= new DeserializerState();
847 setEventName(Deserializer
.deserializeEVENTWORD(state
, bytes
));
848 long num
= Deserializer
.deserializeUINT16(state
, bytes
);
849 if (Log
.isLogTrace()) {
850 Log
.trace("Event name = " + getEventName());
851 Log
.trace("Number of attribute: " + num
);
853 for (int i
= 0; i
< num
; ++i
) {
854 String attribute
= Deserializer
.deserializeATTRIBUTEWORD(state
, bytes
);
856 byte type
= Deserializer
.deserializeBYTE(state
, bytes
);
857 if (Log
.isLogTrace()) {
858 Log
.trace("Attribute: " + attribute
);
859 Log
.trace("Type: " + TypeID
.byteIDToString(type
));
860 Log
.trace("State: " + state
);
862 if (attribute
!= null) {
863 if (i
== 0 && attribute
.equals(ENCODING
)) {
864 if (type
== TypeID
.INT16_TOKEN
) {
865 setEncoding(Deserializer
.deserializeINT16(state
, bytes
));
869 Log
.warning("Found encoding, but type was not int16 while deserializing");
874 case TypeID
.BOOLEAN_TOKEN
:
875 boolean aBool
= Deserializer
.deserializeBOOLEAN(state
, bytes
);
876 setBoolean(attribute
, aBool
);
878 case TypeID
.UINT16_TOKEN
:
879 int uShort
= Deserializer
.deserializeUINT16(state
, bytes
);
880 setUInt16(attribute
, uShort
);
882 case TypeID
.INT16_TOKEN
:
883 short aShort
= Deserializer
.deserializeINT16(state
, bytes
);
884 setInt16(attribute
, aShort
);
886 case TypeID
.UINT32_TOKEN
:
887 long uInt
= Deserializer
.deserializeUINT32(state
, bytes
);
888 setUInt32(attribute
, uInt
);
890 case TypeID
.INT32_TOKEN
:
891 int aInt
= Deserializer
.deserializeINT32(state
, bytes
);
892 setInt32(attribute
, aInt
);
894 case TypeID
.UINT64_TOKEN
:
895 long uLong
= Deserializer
.deserializeUINT64(state
, bytes
);
896 setUInt64(attribute
, BigInteger
.valueOf(uLong
));
898 case TypeID
.INT64_TOKEN
:
899 long aLong
= Deserializer
.deserializeINT64(state
, bytes
);
900 setInt64(attribute
, aLong
);
902 case TypeID
.STRING_TOKEN
:
903 String s
= Deserializer
.deserializeSTRING(state
, bytes
, encoding
);
904 setString(attribute
, s
);
906 case TypeID
.IPADDR_TOKEN
:
907 byte[] inetAddress
= Deserializer
.deserializeIPADDR(state
, bytes
);
908 setIPAddress(attribute
, inetAddress
);
911 Log
.warning("Unknown type " + type
+ " in deserialization");
914 } // for (int i =0 ...
919 * Returns a mutable copy of the event. This is a SLOW operation.
921 * @return Event the Event object
922 * @throws NoSuchEventException if the Event does not exist in the EventTemplateDB
923 * @throws NoSuchAttributeException if the attribute does not exist in this event
924 * @throws NoSuchAttributeTypeException if there is an attribute that does not match a type in the EventTemplateDB
926 public Event
copy() throws EventSystemException
{
927 /* match the type-checking of the original event */
928 Event evt
= new Event(name
, isValidating(), getEventTemplateDB());
929 for (Enumeration
<String
> e
= attributes
.keys(); e
.hasMoreElements();) {
930 String key
= e
.nextElement();
931 BaseType value
= attributes
.get(key
);
939 * Returns a String representation of this event
941 * @return a String return of this event.
943 public String
toString() {
948 StringBuffer sb
= new StringBuffer();
952 if (attributes
!= null) {
954 String
[] keys
= new String
[attributes
.size()];
955 for (Enumeration
<String
> e
= attributes
.keys(); e
.hasMoreElements();) {
956 keys
[i
++] = e
.nextElement();
961 for (i
= 0; i
< attributes
.size(); ++i
) {
962 BaseType value
= attributes
.get(keys
[i
]);
963 if (isValidating() && getEventTemplateDB() != null) {
964 if (getEventTemplateDB().checkTypeForAttribute(name
, keys
[i
], TypeID
.UINT64_STRING
)) {
969 .append(NumberCodec
.toHexString(getUInt64(keys
[i
])))
972 catch (EventSystemException exc
) {
973 Log
.warning("Event.toString : ", exc
);
977 sb
.append("\t").append(keys
[i
]).append(" = ").append(value
).append(";\n");
981 sb
.append("\t").append(keys
[i
]).append(" = ").append(value
).append(";\n");
983 } // for(i = 0; i < attributes.size() ...
984 } // if(attributes != null)
987 return sb
.toString();
991 public int hashCode() {
992 return toString().hashCode();
995 public boolean equals(Object o
) {
999 if (getClass().getName().equals(o
.getClass().getName())) {
1000 return toString().equals(o
.toString());
1008 * This method can be used to validate an event after it has been created.
1010 * @throws EventSystemException
1012 public void validate() throws EventSystemException
{
1013 EventTemplateDB templ
= getEventTemplateDB();
1014 if (templ
== null) {
1015 throw new EventSystemException("No template defined.");
1017 if (!templ
.checkForEvent(name
)) {
1018 throw new NoSuchEventException("Event " + name
+ " does not exist in event definition");
1020 for (String key
: attributes
.keySet()) {
1021 if (!templ
.checkForAttribute(name
, key
)) {
1022 throw new NoSuchAttributeException("Attribute " + key
+ " does not exist for event " + name
);
1024 Object value
= get(key
);
1025 BaseType expected
= templ
.getBaseTypeForObjectAttribute(name
, key
, value
);
1026 BaseType bt
= BaseType
.baseTypeFromObject(value
);
1028 * There are no unsigned values in java so they are kind of a special case
1029 * in that i can't guess which one the person meant. This small hack treats
1030 * similar types the same way.
1032 if ((expected
.getTypeToken() == TypeID
.UINT16_TOKEN
&&
1033 bt
.getTypeToken() == TypeID
.INT32_TOKEN
) ||
1034 (expected
.getTypeToken() == TypeID
.UINT32_TOKEN
&&
1035 bt
.getTypeToken() == TypeID
.INT64_TOKEN
) ||
1036 (expected
.getTypeToken() == TypeID
.UINT64_TOKEN
&&
1037 bt
.getTypeToken() == TypeID
.INT64_TOKEN
)) {
1040 if (!templ
.checkTypeForAttribute(name
, key
, bt
)) {
1041 throw new NoSuchAttributeTypeException("Wrong type '" + bt
.getTypeName() +
1042 "' for " + name
+ "." + key
);