3 import org
.lwes
.BaseType
;
4 import org
.lwes
.EventSystemException
;
5 import org
.lwes
.TypeID
;
6 import org
.lwes
.util
.IPAddress
;
7 import org
.lwes
.util
.Log
;
10 import java
.io
.InputStream
;
11 import java
.math
.BigInteger
;
12 import java
.util
.Arrays
;
13 import java
.util
.Collections
;
14 import java
.util
.Enumeration
;
16 import java
.util
.TreeMap
;
17 import java
.util
.concurrent
.ConcurrentHashMap
;
20 * Provides type checking for the event system. Also provides a place for
21 * globally accessible information.
23 * @author Anthony Molinaro
24 * @author Michael P. Lum
26 public class EventTemplateDB
{
28 * the meta event info inherent to every event
30 private static final String META_EVENT_INFO
= "MetaEventInfo";
33 * esfFile for this event system.
35 private File esfFile
= null;
36 private InputStream esfInputStream
= null;
41 private Map
<String
,Map
<String
,BaseType
>> events
= null;
42 private Map
<String
,BaseType
> knownTypes
= null;
43 private Map
<String
,BaseType
> reservedWords
= null;
46 * This is the EventTemplateDB constructor.
48 public EventTemplateDB() {
49 events
= new ConcurrentHashMap
<String
, Map
<String
,BaseType
>>();
50 knownTypes
= new ConcurrentHashMap
<String
,BaseType
>();
51 reservedWords
= new ConcurrentHashMap
<String
,BaseType
>();
52 initializeKnownTypes();
56 * Sets the Event Specification file for this system
59 * the ESF file for this system.
61 public void setESFFile(File anEsfFile
) {
66 * Gets the ESF file, in case you want to look at it
68 * @return the ESF file used in this system.
70 public File
getESFFile() {
75 * Sets the Event Specification file as an InputStream
77 * @param esfInputStream
78 * the InputStream representing an ESF file
80 public void setESFInputStream(InputStream esfInputStream
) {
81 this.esfInputStream
= esfInputStream
;
85 * Ges the ESF InputStream
87 * @return the ESF InputStream used in the system
89 public InputStream
getESFInputStream() {
90 return esfInputStream
;
94 * Initializes the EventTemplateDB, assumes that setESFFile() has been
97 * @return true if the EventTemplateDB initializes correctly; false if it
98 * does not. false means the event system is unable to perform validation
100 public synchronized boolean initialize() {
102 * Call the parser to parse the file. Any errors cause the
103 * initialization to fail.
107 if (getESFInputStream() != null) {
108 parser
= new ESFParser(getESFInputStream());
109 } else if(getESFFile() != null) {
110 parser
= new ESFParser(
111 new java
.io
.FileInputStream(getESFFile()));
116 parser
.setEventTemplateDB(this);
118 } catch (java
.io
.FileNotFoundException e
) {
119 Log
.warning("File not found ", e
);
122 * treat this as just a warning and allow things to continue, this
123 * allows an empty EventTemplateDB to work when type checking is
126 } catch (ParseException e
) {
127 Log
.warning("Parser error in ESF file " + getESFFile(), e
);
129 } catch (Exception e
) {
130 Log
.error("Error parsing ESF file " + getESFFile(), e
);
131 /* catch IO, NPEs and other exceptions but still continue */
139 * Add an Event to the EventTemplateDB
142 * the name of the Event to add
143 * @return true if the event was added, false if it was not
145 public synchronized boolean addEvent(String anEventName
) {
146 if(anEventName
== null) {
151 if (anEventName
.equals(META_EVENT_INFO
)) {
155 if (events
.containsKey(anEventName
)) {
156 Log
.info("Event " + anEventName
+ " already exists in event DB");
160 Map
<String
, BaseType
> evtHash
= new ConcurrentHashMap
<String
, BaseType
>();
161 if (!(reservedWords
.isEmpty())) {
162 /* insert reserved words into new event */
163 for (String key
: reservedWords
.keySet()) {
165 evtHash
.put(key
, reservedWords
.get(key
));
170 events
.put(anEventName
, evtHash
);
171 } catch(Exception e
) {
172 Log
.warning("Error adding event to EventTemplateDB", e
);
179 * Add an attribute to an Event in the EventTemplateDB
182 * the name of the event to add this attribute to
183 * @param anAttributeName
184 * the name of the attribute to add
185 * @param anAttributeType
186 * the type of the attribute, should be the name of the type
187 * given in the ESF Specification.
188 * @return true if the attribute can be added, false if it can not.
190 public synchronized boolean addEventAttribute(String anEventName
, String anAttributeName
,
191 String anAttributeType
) {
192 if(anEventName
== null || anAttributeName
== null || anAttributeType
== null) {
197 if (anEventName
.equals(META_EVENT_INFO
)) {
198 if (checkForType(anAttributeType
)) {
199 reservedWords
.put(anAttributeName
, knownTypes
200 .get(anAttributeType
));
203 Log
.info("Meta keyword " + anEventName
+ "." + anAttributeName
+
204 "has unknown type " + anAttributeType
+ ", skipping");
209 if (reservedWords
.containsKey(anEventName
)) {
210 Log
.warning("Unable to add attribute named " + anAttributeName
+
211 "as it is a reserved word, skipping");
216 if (events
.containsKey(anEventName
)) {
217 Map
<String
, BaseType
> evtHash
= events
.get(anEventName
);
218 if (checkForType(anAttributeType
)) {
219 evtHash
.put(anAttributeName
, knownTypes
.get(anAttributeType
));
222 Log
.warning("Type " + anAttributeType
+ " does not exist for " +
223 anAttributeName
+ ", skipping");
227 Log
.warning("No such event " + anEventName
+ ", skipping");
230 } catch(Exception e
) {
231 Log
.error("Error adding attribute " + anAttributeName
+ " to " + anEventName
, e
);
237 * Returns an enumeration of all defined events
239 * @return an enumeration of all defined events
241 public Enumeration
<String
> getEventNames() {
242 return Collections
.enumeration(this.events
.keySet());
246 * Returns true if the type given by aTypeName is a valid type in the DB.
249 * a type name according to the ESF Specification
250 * @return true if the type exists in the DB, false otherwise
252 public boolean checkForType(String aTypeName
) {
253 if(aTypeName
== null) return false;
254 return knownTypes
.containsKey(aTypeName
);
258 * Checks to see if an Event exists in the EventTemplateDB
261 * the name of the event to check the existence of
262 * @return true if the event with the name <tt>anEventName</tt> exists in
263 * the EventTemplateDB, false otherwise.
265 public boolean checkForEvent(String anEventName
) {
266 if(anEventName
== null) return false;
267 return events
.containsKey(anEventName
);
271 * Checks to see if an attribute <tt>anAttributeName</tt> exists for the
272 * event <tt>anEventName</tt>
275 * the name of an Event
276 * @param anAttributeName
277 * the name of an attribute of Event to check
278 * @return true if the attribute exists as a member of event, false
281 public boolean checkForAttribute(String anEventName
, String anAttributeName
) {
282 if(anEventName
== null || anAttributeName
== null) return false;
284 if (checkForEvent(anEventName
)) {
285 Map
<String
, BaseType
> evtHash
= events
.get(anEventName
);
286 return evtHash
.containsKey(anAttributeName
);
292 * Checks to see if the type of an attribute is proper. (i.e. if the given
293 * attribute of the given event has the same type assigned to it as the type
294 * of the given objects value)
297 * the name of an Event.
298 * @param anAttributeName
299 * the name of the attribute whose type is being checked
300 * @param anAttributeValue
301 * the Object containing the possible value of the attribute.
303 * @return true if the event and attribute exist and if the type of the
304 * attribute matches the type assigned to this attribute in the
305 * EventTemplateDB, false otherwise.
307 public boolean checkTypeForAttribute(String anEventName
,
308 String anAttributeName
, Object anAttributeValue
) {
309 if(anEventName
== null || anAttributeName
== null || anAttributeValue
== null) return false;
311 if (checkForAttribute(anEventName
, anAttributeName
)) {
312 Map
<String
,BaseType
> evtHash
= events
.get(anEventName
);
313 Object storedTypeObject
= evtHash
.get(anAttributeName
);
314 byte type1
= ((BaseType
) anAttributeValue
).getTypeToken();
315 byte type2
= ((BaseType
) storedTypeObject
).getTypeToken();
316 if (type1
== type2
) {
325 * Checks to see if the type of an attribute is proper. (i.e. if the given
326 * attribute of the given event has the same type assigned to it as the type
330 * the name of an Event.
331 * @param anAttributeName
332 * the name of the attribute whose type is being checked
333 * @param anAttributeType
334 * the String containing the possible type value of the
337 * @return true if the event and attribute exist and if the type of the
338 * attribute matches the type assigned to this attribute in the
339 * EventTemplateDB, false otherwise.
341 public boolean checkTypeForAttribute(String anEventName
,
342 String anAttributeName
, String anAttributeType
) {
343 if(anEventName
== null || anAttributeName
== null || anAttributeType
== null) return false;
345 if (checkForAttribute(anEventName
, anAttributeName
)) {
346 Map
<String
,BaseType
> evtHash
= events
.get(anEventName
);
347 String storedTypeName
= evtHash
.get(anAttributeName
).getTypeName();
348 if (anAttributeType
.equals(storedTypeName
)) {
357 * Given an Object which is the attribute value of the attribute
358 * <tt>attributeName</tt> of event <tt>eventName</tt>, return the internal
359 * representation (i.e. <tt>BaseType</tt>) of this Object
362 * the name of an Event.
363 * @param attributeName
364 * the name of an attribute of <tt>eventName</tt>
365 * @param attributeValue
366 * the value of the attribute
367 * @return the <tt>BaseType</tt> representation of <tt>attributeValue</tt>
369 public BaseType
getBaseTypeForObjectAttribute(String eventName
,
370 String attributeName
, Object attributeValue
) {
371 if(eventName
== null || attributeName
== null || attributeValue
== null) return null;
373 Map
<String
,BaseType
> evtHash
= events
.get(eventName
);
374 BaseType tmpBaseType
= evtHash
.get(attributeName
);
375 BaseType retBaseType
= tmpBaseType
.cloneBaseType();
376 retBaseType
.setTypeObject(attributeValue
);
381 * Returns the base types for this event
383 * @return a map of event fields to base types
385 public Map
<String
,BaseType
> getBaseTypesForEvent(String eventName
) {
386 if(eventName
== null) return null;
388 Map
<String
,BaseType
> map
= events
.get(eventName
);
390 return map
== null ?
new ConcurrentHashMap
<String
,BaseType
>() : map
;
394 * Parses the string representation of an event attribute into the
395 * appropriate objectt.
398 * the name of an Event.
399 * @param anAttributeName
400 * the name of the attribute we are parsing
401 * @param stringAttributeValue
402 * a string representation of the value of the attribute given by
404 * @return the object represented by the string
405 * <tt>stringAttributeValue</tt>
407 public Object
parseAttribute(String anEventName
, String anAttributeName
,
408 String stringAttributeValue
) {
409 Object retObject
= null;
411 if(anEventName
== null || anAttributeName
== null || stringAttributeValue
== null) return null;
413 Log
.trace("parseAttribute: " + anEventName
+ "." + anAttributeName
+ "=" +
414 stringAttributeValue
);
416 if (checkForAttribute(anEventName
, anAttributeName
)) {
417 Log
.trace("parseAttribute: passed first if attribute exists");
419 Map
<String
,BaseType
> evtHash
= events
.get(anEventName
);
421 BaseType bt
= evtHash
.get(anAttributeName
);
423 throw new EventSystemException("Null BaseType for "
426 retObject
= bt
.parseFromString(stringAttributeValue
);
427 Log
.trace("parseAttribute: parsed " + retObject
);
428 } catch (EventSystemException btpe
) {
429 Log
.error("Unable to parseAttribute", btpe
);
433 Log
.trace("parseAttribute: returning " + retObject
);
438 * Returns a HTML rendering of the EventTemplateDB
439 * @return HTML string of the EventTemplateDB
441 public String
toHtmlString() {
442 StringBuffer sb
= new StringBuffer();
443 sb
.append("<table>\n");
444 sb
.append("<tr><th>" + META_EVENT_INFO
445 + "</th><th>Type</th><th>Name</th></tr>\n");
446 for (String key
: reservedWords
.keySet()) {
447 BaseType tv
= reservedWords
.get(key
);
448 String type
= tv
.getTypeName();
449 sb
.append("<tr><td></td><td>").append(type
).append("</td><td>").append(key
).append("</td></tr>\n");
451 for (String EventKey
: events
.keySet()) {
452 sb
.append("<tr><th>").append(EventKey
).append("</th><th>Type</th><th>Name</th></tr>\n");
453 if (EventKey
!= null) {
454 Map
<String
, BaseType
> event
= events
.get(EventKey
);
455 for (Enumeration
<String
> att
= Collections
.enumeration(event
.keySet()); att
456 .hasMoreElements();) {
457 String key
= att
.nextElement();
458 BaseType tv
= event
.get(key
);
459 String type
= tv
.getTypeName();
460 sb
.append("<tr><td></td><td>")
463 .append(key
).append("</td></tr>\n");
468 sb
.append("</table>\n");
469 return sb
.toString();
474 * Returns a rather long string Representation of the EventTemplateDB
476 * @return a string Representation of the EventTemplateDB
478 public String
toString() {
479 StringBuffer sb
= new StringBuffer();
480 sb
.append("\n" + META_EVENT_INFO
+ "\n{\n");
481 String
[] reservedKeys
= new String
[reservedWords
.size()];
484 for (String s1
: reservedWords
.keySet()) {
485 reservedKeys
[i
] = s1
;
488 Arrays
.sort(reservedKeys
);
490 for (i
= 0; i
< reservedKeys
.length
; ++i
) {
491 BaseType tv
= reservedWords
.get(reservedKeys
[i
]);
492 String type
= tv
.getTypeName();
493 sb
.append("\t").append(type
).append(" ").append(reservedKeys
[i
]).append(";\n");
497 String
[] eventKeys
= new String
[events
.size()];
499 for (String s
: events
.keySet()) {
503 Arrays
.sort(eventKeys
);
505 for (i
= 0; i
< eventKeys
.length
; ++i
) {
506 sb
.append(eventKeys
[i
]).append("\n{\n");
507 if (eventKeys
[i
] != null) {
508 Map
<String
, BaseType
> event
= events
.get(eventKeys
[i
]);
510 String
[] attributeKeys
= new String
[event
.size()];
511 for (Enumeration
<String
> att
= Collections
.enumeration(event
.keySet());
512 att
.hasMoreElements();) {
513 attributeKeys
[j
] = att
.nextElement();
516 Arrays
.sort(attributeKeys
);
518 for (j
= 0; j
< attributeKeys
.length
; ++j
) {
519 BaseType tv
= event
.get(attributeKeys
[j
]);
520 String type
= tv
.getTypeName();
521 sb
.append("\t").append(type
).append(" ").append(attributeKeys
[j
]).append(";\n");
526 return sb
.toString();
530 * Creates a map of known types
532 private void initializeKnownTypes() {
533 /* initialize the list of known types */
534 knownTypes
.put(TypeID
.UINT16_STRING
, new BaseType(TypeID
.UINT16_STRING
,
535 TypeID
.UINT16_TOKEN
, 0));
536 knownTypes
.put(TypeID
.INT16_STRING
, new BaseType(TypeID
.INT16_STRING
,
537 TypeID
.INT16_TOKEN
, (short) 0));
538 knownTypes
.put(TypeID
.UINT32_STRING
, new BaseType(TypeID
.UINT32_STRING
,
539 TypeID
.UINT32_TOKEN
, (long) 0));
540 knownTypes
.put(TypeID
.INT32_STRING
, new BaseType(TypeID
.INT32_STRING
,
541 TypeID
.INT32_TOKEN
, 0));
542 knownTypes
.put(TypeID
.STRING_STRING
, new BaseType(TypeID
.STRING_STRING
,
543 TypeID
.STRING_TOKEN
, ""));
544 knownTypes
.put(TypeID
.IPADDR_STRING
, new BaseType(TypeID
.IPADDR_STRING
,
545 TypeID
.IPADDR_TOKEN
, new IPAddress()));
546 knownTypes
.put(TypeID
.INT64_STRING
, new BaseType(TypeID
.INT64_STRING
,
547 TypeID
.INT64_TOKEN
, (long) 0));
548 knownTypes
.put(TypeID
.UINT64_STRING
, new BaseType(TypeID
.UINT64_STRING
,
549 TypeID
.UINT64_TOKEN
, BigInteger
.ZERO
));
550 knownTypes
.put(TypeID
.BOOLEAN_STRING
, new BaseType(TypeID
.BOOLEAN_STRING
,
551 TypeID
.BOOLEAN_TOKEN
, true));
554 public Map
<String
, BaseType
> getMetaFields() {
555 Map
<String
, BaseType
> m
= new TreeMap
<String
, BaseType
>();
556 m
.putAll(reservedWords
);
560 public Map
<String
,Map
<String
,BaseType
>> getEvents() {
561 Map
<String
,Map
<String
,BaseType
>> cp
= new TreeMap
<String
,Map
<String
,BaseType
>>();