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/EMail__Alerter.java
Revision: 1.8
Committed: Sat Mar 3 01:09:53 2001 UTC (23 years, 3 months ago) by tdb
Branch: MAIN
Changes since 1.7: +13 -47 lines
Log Message:
Both alerting mechanisms now make full use of the ConfigurationProxy class,
which in turn gives them dynamic updates of configuration :)

File Contents

# User Rev Content
1 tdb 1.1 //---PACKAGE DECLARATION---
2     package uk.ac.ukc.iscream.client.alerters;
3    
4     //---IMPORTS---
5     import uk.ac.ukc.iscream.client.*;
6     import uk.ac.ukc.iscream.core.*;
7     import uk.ac.ukc.iscream.util.*;
8     import uk.ac.ukc.iscream.componentmanager.*;
9    
10 tdb 1.2 import java.util.*;
11     import java.io.*;
12    
13 tdb 1.1 /**
14 tdb 1.8 * This alerter delivers alerts using e-mail.
15 tdb 1.1 *
16 tdb 1.2 * @author $Author: tdb1 $
17 tdb 1.8 * @version $Id: EMail__Alerter.java,v 1.7 2001/03/02 02:46:56 tdb1 Exp $
18 tdb 1.1 */
19 tdb 1.2 public class EMail__Alerter implements PluginAlerter {
20 tdb 1.1
21     //---FINAL ATTRIBUTES---
22    
23     /**
24     * The current CVS revision of this class
25     */
26 tdb 1.8 public final String REVISION = "$Revision: 1.7 $";
27 tdb 1.1
28 tdb 1.8 public final String DESC = "Sends alerts over e-mail.";
29 tdb 1.1
30     //---STATIC METHODS---
31    
32     //---CONSTRUCTORS---
33    
34 tdb 1.8 public EMail__Alerter() {
35 tdb 1.6 _logger.write(toString(), Logger.SYSINIT, "IRC Alerter started");
36 tdb 1.2 }
37    
38 tdb 1.1 //---PUBLIC METHODS---
39    
40     public void sendAlert(Alert alert) {
41 tdb 1.8 ConfigurationProxy cp = ConfigurationProxy.getInstance();
42     int level = Integer.parseInt(cp.getProperty(_name, "Alerter.EMail.level"));
43 tdb 1.2 // only send if it's equal (or above) our level
44 tdb 1.8 if(alert.getLevel() >= level) {
45 tdb 1.7 String alertType = Alert.alerts[alert.getLevel()]+"["+String.valueOf(alert.getLevel())+"]";
46 tdb 1.2 // sort out the subject
47 tdb 1.8 String subject = cp.getProperty(_name, "Alerter.EMail.subject");
48 tdb 1.7 subject = StringUtils.replaceText(subject, "%level%", alertType);
49 tdb 1.4 subject = StringUtils.replaceText(subject, "%source%", alert.getSource());
50 tdb 1.3 subject = StringUtils.replaceText(subject, "%value%", alert.getValue());
51     subject = StringUtils.replaceText(subject, "%thresholdValue%", alert.getThresholdValue());
52     subject = StringUtils.replaceText(subject, "%attributeName%", alert.getAttributeName());
53 tdb 1.2
54     // sort out the message body
55 tdb 1.8 String message = cp.getProperty(_name, "Alerter.EMail.message");
56 tdb 1.7 message = StringUtils.replaceText(message, "%level%", alertType);
57 tdb 1.4 message = StringUtils.replaceText(message, "%source%", alert.getSource());
58 tdb 1.3 message = StringUtils.replaceText(message, "%value%", alert.getValue());
59     message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue());
60     message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName());
61 tdb 1.2
62     try {
63     // create SMTP message
64 tdb 1.8 Smtp smtp = new Smtp(cp.getProperty(_name, "Alerter.EMail.smtpServer"));
65 tdb 1.2 // set our sender
66 tdb 1.8 smtp.setSender(cp.getProperty(_name, "Alerter.EMail.sender"));
67 tdb 1.2
68     // set the to list
69 tdb 1.8 StringTokenizer st = new StringTokenizer(cp.getProperty(_name, "Alerter.EMail.destList"), ";");
70 tdb 1.2 while (st.hasMoreTokens()) {
71     smtp.setTo(st.nextToken());
72     }
73    
74     // prepare to print the message
75     PrintWriter out = smtp.getOutputStream();
76     out.println("Subject: "+subject);
77     out.println();
78    
79     // send the message
80     out.println(message);
81     smtp.sendMessage();
82     smtp.close();
83 tdb 1.5 _logger.write(toString(), Logger.DEBUG, "Sending IRC Alert at level"+String.valueOf(alert.getLevel()));
84 tdb 1.2 }
85     catch(IOException e) {
86     _logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e);
87     }
88     }
89 tdb 1.1 }
90    
91     /**
92     * Overrides the {@link java.lang.Object#toString() Object.toString()}
93     * method to provide clean logging (every class should have this).
94     *
95     * This uses the uk.ac.ukc.iscream.util.NameFormat class
96     * to format the toString()
97     *
98     * @return the name of this class and its CVS revision
99     */
100     public String toString() {
101     return FormatName.getName(
102     _name,
103     getClass().getName(),
104     REVISION);
105     }
106    
107     /**
108     * return the String representation of what the filter does
109     */
110     public String getDescription(){
111     return DESC;
112     }
113    
114     //---PRIVATE METHODS---
115    
116     //---ACCESSOR/MUTATOR METHODS---
117    
118     //---ATTRIBUTES---
119 tdb 1.2
120 tdb 1.1 /**
121     * This is the friendly identifier of the
122     * component this class is running in.
123     * eg, a Filter may be called "filter1",
124     * If this class does not have an owning
125     * component, a name from the configuration
126     * can be placed here. This name could also
127     * be changed to null for utility classes.
128     */
129     private String _name = ClientMain.NAME;
130    
131     /**
132     * This holds a reference to the
133     * system logger that is being used.
134     */
135     private Logger _logger = ReferenceManager.getInstance().getLogger();
136    
137     //---STATIC ATTRIBUTES---
138    
139     }