--- projects/cms/source/server/uk/org/iscream/cms/server/client/alerters/IRC__Alerter.java 2001/03/22 22:07:58 1.24 +++ projects/cms/source/server/uk/org/iscream/cms/server/client/alerters/IRC__Alerter.java 2001/03/23 01:09:51 1.25 @@ -6,11 +6,10 @@ import uk.org.iscream.client.*; import uk.org.iscream.core.*; import uk.org.iscream.util.*; import uk.org.iscream.componentmanager.*; - import java.io.*; import java.net.*; import java.util.*; -import java.text.*; +import java.text.DateFormat; /** * This Alert sends an IRC message. @@ -19,16 +18,16 @@ import java.text.*; * IRCBot inner class. * * @author $Author: ajm $ - * @version $Id: IRC__Alerter.java,v 1.24 2001/03/22 22:07:58 ajm Exp $ + * @version $Id: IRC__Alerter.java,v 1.25 2001/03/23 01:09:51 ajm Exp $ */ -public class IRC__Alerter extends Thread implements PluginAlerter { +public class IRC__Alerter extends AlerterSkeleton { //---FINAL ATTRIBUTES--- /** * The current CVS revision of this class */ - public final String REVISION = "$Revision: 1.24 $"; + public final String REVISION = "$Revision: 1.25 $"; /** * A description of this alerter @@ -40,85 +39,51 @@ public class IRC__Alerter extends Thread implements Pl */ public final int DEFAULT_RECONNECT_DELAY = 30; - public final String DEFAULT_LEVEL = Alert.alertLevels[0]; - - public final String NOT_CONFIGURED = ""; - //---STATIC METHODS--- //---CONSTRUCTORS--- public IRC__Alerter() { - + super(); // connect to the IRC server _ircbot = new IRCBot(); // set it's name and start it _ircbot.setName("client.IRC__Alerter$IRCBot"); _ircbot.start(); _startTime = System.currentTimeMillis(); - this.start(); _logger.write(toString(), Logger.SYSINIT, "IRC Alerter started"); } //---PUBLIC METHODS--- - public void run() { - while(_running) { - try { - sendAlert((Alert) getQueue().get(getQueueId())); - } catch (InvalidQueueException e) { - _logger.write(this.toString(), Logger.ERROR, "Unable to get queue."); - } - } - } - + /** + * Implements the abstract method from the skeleton class. + * This method will attempt to send an alert + * message over the IRC channel. + * + * @param alert the alert to send + */ public void sendAlert(Alert alert) { // only send alerts if we're active if(_active) { - ConfigurationProxy cp = ConfigurationProxy.getInstance(); - - String levelName; + // sort out the message + String alertType = Alert.alertLevels[alert.getLevel()]; + String message; try { - levelName = cp.getProperty(_name, "Alerter.IRC.level"); + message = _cp.getProperty(_name, "Alerter.IRC.message"); } catch (PropertyNotFoundException e) { - levelName = DEFAULT_LEVEL; - _logger.write(toString(), Logger.WARNING, "Alerter.IRC.level value unavailable using default of " + levelName); + message = NOT_CONFIGURED; + _logger.write(toString(), Logger.WARNING, "Alerter.IRC.message value unavailable using default of " + message); } - int level = StringUtils.getStringPos(levelName, Alert.alertLevels); - // only send if it's equal (or above) our level - if(((alert.getLevel() == 0) && (alert.getLastLevel() >= level)) || (alert.getLevel() >= level)) { - String alertType = Alert.alertLevels[alert.getLevel()]; - String thresholdType = Alert.thresholdLevels[alert.getThreshold()]; - String timeFirstSince = DateUtils.formatTime((System.currentTimeMillis() - alert.getInitialAlertTime())/1000, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); - String timeFirstOccured = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(alert.getInitialAlertTime())); - // sort out the message - String message; - try { - message = cp.getProperty(_name, "Alerter.IRC.message"); - } catch (PropertyNotFoundException e) { - message = NOT_CONFIGURED; - _logger.write(toString(), Logger.WARNING, "Alerter.IRC.message value unavailable using default of " + message); - } - - message = StringUtils.replaceText(message, "%level%", alertType); - message = StringUtils.replaceText(message, "%threshold%", thresholdType); - message = StringUtils.replaceText(message, "%source%", alert.getSource()); - message = StringUtils.replaceText(message, "%value%", alert.getValue()); - message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue()); - message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName()); - message = StringUtils.replaceText(message, "%timeTillNextAlert%", DateUtils.getTimeString(Long.parseLong(alert.getTimeTillNextAlert()))); - message = StringUtils.replaceText(message, "%timeSinceFirstAlert%", timeFirstSince); - message = StringUtils.replaceText(message, "%timeOfFirstAlert%", timeFirstOccured); - - // send the message - _logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ alertType + " level"); - _ircbot.sendMsg(message); - _lastAlert = message; - _lastAlertTime = System.currentTimeMillis(); - _alertCount ++; - } - } - else { + message = processAlertMessage(message, alert); + + // send the message + _logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ alertType + " level"); + _ircbot.sendMsg(message); + _lastAlert = message; + _lastAlertTime = System.currentTimeMillis(); + _alertCount ++; + } else { _ignoredCount ++; } } @@ -139,8 +104,10 @@ public class IRC__Alerter extends Thread implements Pl REVISION); } - /** - * return the String representation of what the filter does + /** + * Return the String representation of what the alerter does + * + * @return the description */ public String getDescription(){ return DESC; @@ -150,18 +117,6 @@ public class IRC__Alerter extends Thread implements Pl //---ACCESSOR/MUTATOR METHODS--- - protected Queue getQueue() { - return AlerterManager.getInstance().getQueue(); - } - - protected int getQueueId() { - if (_qID == -1) { - _qID = getQueue().getQueue(); - _logger.write(toString(), Logger.DEBUG, "Assigned Queue - " + _qID); - } - return _qID; - } - //---ATTRIBUTES--- /** @@ -200,11 +155,6 @@ public class IRC__Alerter extends Thread implements Pl private long _startTime; /** - * The running status of the alerter - */ - private boolean _running = true; - - /** * This is the friendly identifier of the * component this class is running in. * eg, a Filter may be called "filter1", @@ -213,19 +163,8 @@ public class IRC__Alerter extends Thread implements Pl * can be placed here. This name could also * be changed to null for utility classes. */ - private String _name = "IRC Alert"; + private String _name = "IRC"; - /** - * This holds a reference to the - * system logger that is being used. - */ - private Logger _logger = ReferenceManager.getInstance().getLogger(); - - /** - * The queue id for this alerters queue in the alert queue - */ - private int _qID = -1; - //---STATIC ATTRIBUTES--- //---INNER CLASSES--- @@ -685,6 +624,12 @@ public class IRC__Alerter extends Thread implements Pl * A reminder of our current nickname... */ private String _nickname; + + /** + * This holds a reference to the + * system logger that is being used. + */ + protected Logger _logger = ReferenceManager.getInstance().getLogger(); }