ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/client/alerters/IRC__Alerter.java
(Generate patch)

Comparing projects/cms/source/server/uk/org/iscream/cms/server/client/alerters/IRC__Alerter.java (file contents):
Revision 1.24 by ajm, Thu Mar 22 22:07:58 2001 UTC vs.
Revision 1.27 by tdb, Sat Mar 24 18:43:38 2001 UTC

# Line 6 | Line 6 | import uk.org.iscream.client.*;
6   import uk.org.iscream.core.*;
7   import uk.org.iscream.util.*;
8   import uk.org.iscream.componentmanager.*;
9
9   import java.io.*;
10   import java.net.*;
11   import java.util.*;
12 < import java.text.*;
12 > import java.text.DateFormat;
13  
14   /**
15   * This Alert sends an IRC message.
# Line 21 | Line 20 | import java.text.*;
20   * @author  $Author$
21   * @version $Id$
22   */
23 < public class IRC__Alerter extends Thread implements PluginAlerter {
23 > public class IRC__Alerter extends AlerterSkeleton {
24  
25   //---FINAL ATTRIBUTES---
26  
# Line 40 | Line 39 | public class IRC__Alerter extends Thread implements Pl
39       */
40      public final int DEFAULT_RECONNECT_DELAY = 30;
41      
43    public final String DEFAULT_LEVEL = Alert.alertLevels[0];
44    
45    public final String NOT_CONFIGURED = "<NOT CONFIGURED>";
46    
42   //---STATIC METHODS---
43  
44   //---CONSTRUCTORS---
45  
46      public IRC__Alerter() {
47 <                        
47 >        super();
48          // connect to the IRC server
49          _ircbot = new IRCBot();
50          // set it's name and start it
51          _ircbot.setName("client.IRC__Alerter$IRCBot");
52          _ircbot.start();
53          _startTime = System.currentTimeMillis();
59        this.start();
54          _logger.write(toString(), Logger.SYSINIT, "IRC Alerter started");
55      }
56  
57   //---PUBLIC METHODS---
58  
59 <    public void run() {
60 <        while(_running) {
61 <            try {
62 <                sendAlert((Alert) getQueue().get(getQueueId()));
63 <            } catch (InvalidQueueException e) {
64 <                _logger.write(this.toString(), Logger.ERROR, "Unable to get queue.");
65 <            }
72 <        }
73 <    }
74 <
59 >    /**
60 >     * Implements the abstract method from the skeleton class.
61 >     * This method will attempt to send an alert
62 >     * message over the IRC channel.
63 >     *
64 >     * @param alert the alert to send
65 >     */
66      public void sendAlert(Alert alert) {
67 +        // sort out the message      
68 +        String alertType = Alert.alertLevels[alert.getLevel()];        
69 +        String message;
70 +        try {
71 +            message = _cp.getProperty(_name, "Alerter.IRC.message");
72 +        } catch (PropertyNotFoundException e) {
73 +            message = NOT_CONFIGURED;
74 +            _logger.write(toString(), Logger.WARNING, "Alerter.IRC.message value unavailable using default of " + message);
75 +        }
76 +        message = processAlertMessage(message, alert);        
77          // only send alerts if we're active
78 <        if(_active) {
79 <            ConfigurationProxy cp = ConfigurationProxy.getInstance();
80 <            
81 <            String levelName;
82 <            try {
83 <                levelName = cp.getProperty(_name, "Alerter.IRC.level");
84 <            } catch (PropertyNotFoundException e) {
85 <                levelName = DEFAULT_LEVEL;
86 <                _logger.write(toString(), Logger.WARNING, "Alerter.IRC.level value unavailable using default of " + levelName);
86 <            }
87 <            int level = StringUtils.getStringPos(levelName, Alert.alertLevels);
88 <            // only send if it's equal (or above) our level
89 <            if(((alert.getLevel() == 0) && (alert.getLastLevel() >= level)) || (alert.getLevel() >= level)) {
90 <                String alertType = Alert.alertLevels[alert.getLevel()];
91 <                String thresholdType = Alert.thresholdLevels[alert.getThreshold()];
92 <                String timeFirstSince = DateUtils.formatTime((System.currentTimeMillis() - alert.getInitialAlertTime())/1000, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs");
93 <                String timeFirstOccured = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(alert.getInitialAlertTime()));
94 <                // sort out the message              
95 <                String message;
96 <                try {
97 <                    message = cp.getProperty(_name, "Alerter.IRC.message");
98 <                } catch (PropertyNotFoundException e) {
99 <                    message = NOT_CONFIGURED;
100 <                    _logger.write(toString(), Logger.WARNING, "Alerter.IRC.message value unavailable using default of " + message);
101 <                }
102 <                
103 <                message = StringUtils.replaceText(message, "%level%", alertType);
104 <                message = StringUtils.replaceText(message, "%threshold%", thresholdType);
105 <                message = StringUtils.replaceText(message, "%source%", alert.getSource());
106 <                message = StringUtils.replaceText(message, "%value%", alert.getValue());
107 <                message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue());
108 <                message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName());
109 <                message = StringUtils.replaceText(message, "%timeTillNextAlert%",  DateUtils.getTimeString(Long.parseLong(alert.getTimeTillNextAlert())));
110 <                message = StringUtils.replaceText(message, "%timeSinceFirstAlert%", timeFirstSince);
111 <                message = StringUtils.replaceText(message, "%timeOfFirstAlert%", timeFirstOccured);
112 <                
113 <                // send the message
114 <                _logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ alertType + " level");
115 <                _ircbot.sendMsg(message);
116 <                _lastAlert = message;
117 <                _lastAlertTime = System.currentTimeMillis();
118 <                _alertCount ++;
119 <            }
78 >        if(_active) {          
79 >            // send the message
80 >            _logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ alertType + " level");
81 >            _ircbot.sendMsg(message);
82 >            // count sent alerts
83 >            _alertCount++;
84 >        } else {
85 >            // don't send, but keep a count that we ignored it
86 >            _ignoredCount++;
87          }
88 <        else {
89 <            _ignoredCount ++;
90 <        }
88 >        // we'll always store the "last alert", regardless
89 >        // of whether we actually display it or not
90 >        _lastAlert = message;
91 >        _lastAlertTime = System.currentTimeMillis();
92      }
93  
94      /**
# Line 139 | Line 107 | public class IRC__Alerter extends Thread implements Pl
107              REVISION);
108      }
109  
110 <    /**
111 <     * return the String representation of what the filter does
110 >    /**
111 >     * Return the String representation of what the alerter does
112 >     *
113 >     * @return the description
114       */
115      public String getDescription(){
116          return DESC;
# Line 150 | Line 120 | public class IRC__Alerter extends Thread implements Pl
120  
121   //---ACCESSOR/MUTATOR METHODS---
122  
153    protected Queue getQueue() {
154        return AlerterManager.getInstance().getQueue();
155    }
156    
157    protected int getQueueId() {
158        if (_qID == -1) {
159            _qID = getQueue().getQueue();
160            _logger.write(toString(), Logger.DEBUG, "Assigned Queue - " + _qID);
161        }
162        return _qID;
163    }
164
123   //---ATTRIBUTES---
124      
125      /**
# Line 200 | Line 158 | public class IRC__Alerter extends Thread implements Pl
158      private long _startTime;
159      
160      /**
203     * The running status of the alerter
204     */
205    private boolean _running = true;
206    
207    /**
161       * This is the friendly identifier of the
162       * component this class is running in.
163       * eg, a Filter may be called "filter1",
# Line 213 | Line 166 | public class IRC__Alerter extends Thread implements Pl
166       * can be placed here.  This name could also
167       * be changed to null for utility classes.
168       */
169 <    private String _name = "IRC Alert";
169 >    private String _name = "IRC";
170  
218    /**
219     * This holds a reference to the
220     * system logger that is being used.
221     */
222    private Logger _logger = ReferenceManager.getInstance().getLogger();
223    
224    /**
225     * The queue id for this alerters queue in the alert queue
226     */
227    private int _qID = -1;
228
171   //---STATIC ATTRIBUTES---
172  
173   //---INNER CLASSES---
# Line 685 | Line 627 | public class IRC__Alerter extends Thread implements Pl
627           * A reminder of our current nickname...
628           */
629          private String _nickname;
630 +        
631 +        /**
632 +         * This holds a reference to the
633 +         * system logger that is being used.
634 +         */
635 +        protected Logger _logger = ReferenceManager.getInstance().getLogger();
636          
637      }
638  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines