public void start() throws Exception {
log.debug("Reading resource: '" + notificationMapResName + "'");
ObjectModelFactory omf = new NotificationBinding();
InputStream is = null;
try
{
// locate notifications.xml
is = this.getClass().getResourceAsStream(notificationMapResName);
// create unmarshaller
Unmarshaller unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller();
// let JBossXB do it's magic using the MappingObjectModelFactory
this.notificationMapList = (ArrayList)unmarshaller.unmarshal(is, omf, null);
}
catch (Exception e)
{
log.error("Accessing resource '" + notificationMapResName + "'");
throw e;
}
finally
{
if (is != null)
{
// close the XML stream
is.close();
}
}
log.debug("Found " + notificationMapList.size() + " notification mappings");
// Initialise the cache with the compiled regular expressions denoting
// notification type specifications
this.mappingRegExpCache =
new ArrayList(notificationMapList.size());
// Initialise the cache with the instantiated notification wrappers
this.notificationWrapperCache =
new ArrayList(notificationMapList.size());
for (Iterator i = notificationMapList.iterator(); i.hasNext(); )
{
Mapping mapping = (Mapping)i.next();
// Compile and add the regular expression
String notificationType = mapping.getNotificationType();
try
{
Pattern re = Pattern.compile(notificationType);
this.mappingRegExpCache.add(re);
}
catch (PatternSyntaxException e)
{
// Fill the slot to keep index count correct
this.mappingRegExpCache.add(null);
log.warn("Error compiling notification mapping for type: " + notificationType, e);
}
// Instantiate and add the wrapper
// Read wrapper class name
String wrapperClassName = mapping.getVarBindList().getWrapperClass();
log.debug("notification wrapper class: " + wrapperClassName);
try
{
NotificationWrapper wrapper =
(NotificationWrapper)Class.forName(wrapperClassName, true, this.getClass().getClassLoader()).newInstance();
// Initialise it
wrapper.set(this.clock, this.trapCount);
// Add the wrapper to the cache
this.notificationWrapperCache.add(wrapper);
}
catch (Exception e)
{
// Fill the slot to keep index count correct
this.notificationWrapperCache.add(null);
log.warn("Error compiling notification mapping for type: " + notificationType, e);
}
}
log.debug("Trap factory going active");
}
Populates the regular expression and wrapper instance collections. Note
that a failure (e.g. to compile a regular expression or to instantiate a
wrapper) generates an error message. Furthermore, the offending
expression or class are skipped and the corresponding collection entry
is null. It is the user's responsibility to track the reported errors in
the logs and act accordingly (i.e. correct them and restart). If not the
corresponding mappings are effectively void and will NOT have effect. |