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.20
Committed: Thu Mar 22 23:35:22 2001 UTC (23 years, 2 months ago) by ajm
Branch: MAIN
Changes since 1.19: +55 -117 lines
Log Message:
Now implemented a skeleton Alerter to allow easier creation of new Alerters.

Email alerter now implements this.

File Contents

# User Rev Content
1 tdb 1.1 //---PACKAGE DECLARATION---
2 tdb 1.16 package uk.org.iscream.client.alerters;
3 tdb 1.1
4     //---IMPORTS---
5 tdb 1.16 import uk.org.iscream.client.*;
6     import uk.org.iscream.core.*;
7     import uk.org.iscream.util.*;
8     import uk.org.iscream.componentmanager.*;
9 ajm 1.20 import java.io.*;
10     import java.util.StringTokenizer;
11 tdb 1.1
12 tdb 1.2
13 tdb 1.1 /**
14 tdb 1.8 * This alerter delivers alerts using e-mail.
15 tdb 1.1 *
16 ajm 1.20 * @author $Author: ajm4 $
17     * @version $Id: EMail__Alerter.java,v 1.19 2001/03/22 22:07:58 ajm4 Exp $
18 tdb 1.1 */
19 ajm 1.20 public class EMail__Alerter extends AlerterSkeleton {
20 tdb 1.1
21     //---FINAL ATTRIBUTES---
22    
23     /**
24     * The current CVS revision of this class
25     */
26 ajm 1.20 public final String REVISION = "$Revision: 1.19 $";
27 tdb 1.1
28 tdb 1.8 public final String DESC = "Sends alerts over e-mail.";
29 tdb 1.1
30 tdb 1.14 public final String NOT_CONFIGURED = "<NOT CONFIGURED>";
31    
32 tdb 1.1 //---STATIC METHODS---
33    
34     //---CONSTRUCTORS---
35    
36     //---PUBLIC METHODS---
37    
38     public void sendAlert(Alert alert) {
39 ajm 1.20 String alertType = Alert.alertLevels[alert.getLevel()];
40     // sort out the subject
41     String subject;
42 tdb 1.14 try {
43 ajm 1.20 subject = _cp.getProperty(_name, "Alerter.EMail.subject");
44 tdb 1.14 } catch (PropertyNotFoundException e) {
45 ajm 1.20 subject = NOT_CONFIGURED;
46     _logger.write(toString(), Logger.WARNING, "Alerter.EMail.subject value unavailable using default of " + subject);
47 tdb 1.14 }
48 ajm 1.20 subject = processAlertMessage(subject, alert);
49    
50     // sort out the message body
51     String message;
52     try {
53     message = _cp.getProperty(_name, "Alerter.EMail.message");
54     } catch (PropertyNotFoundException e) {
55     message = NOT_CONFIGURED;
56     _logger.write(toString(), Logger.WARNING, "Alerter.EMail.message value unavailable using default of " + message);
57     }
58     message = processAlertMessage(message, alert);
59    
60     try {
61     // create SMTP message
62     Smtp smtp = new Smtp(_cp.getProperty(_name, "Alerter.EMail.smtpServer"));
63     // set our sender
64     smtp.setSender(_cp.getProperty(_name, "Alerter.EMail.sender"));
65    
66     String destList = _cp.getProperty("Host."+alert.getSource(), "Alerter.EMail.destList");
67    
68     // set the to: list
69     StringTokenizer st = new StringTokenizer(destList, ";");
70     while (st.hasMoreTokens()) {
71     smtp.setTo(st.nextToken());
72 tdb 1.2 }
73 ajm 1.20
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     _logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ alertType + " level");
84     }
85     catch(IOException e) {
86     _logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e);
87     }
88     catch(PropertyNotFoundException e) {
89     _logger.write(toString(), Logger.ERROR, "Error obtaining essential configuration: "+e);
90 tdb 1.2 }
91 tdb 1.1 }
92    
93     /**
94     * Overrides the {@link java.lang.Object#toString() Object.toString()}
95     * method to provide clean logging (every class should have this).
96     *
97 tdb 1.16 * This uses the uk.org.iscream.util.NameFormat class
98 tdb 1.1 * to format the toString()
99     *
100     * @return the name of this class and its CVS revision
101     */
102     public String toString() {
103     return FormatName.getName(
104     _name,
105     getClass().getName(),
106     REVISION);
107     }
108    
109     /**
110 ajm 1.20 * return the String representation of what the alerter does
111 tdb 1.1 */
112     public String getDescription(){
113     return DESC;
114     }
115    
116     //---PRIVATE METHODS---
117 ajm 1.9
118 tdb 1.1 //---ACCESSOR/MUTATOR METHODS---
119    
120     //---ATTRIBUTES---
121 tdb 1.2
122 tdb 1.1 /**
123     * This is the friendly identifier of the
124     * component this class is running in.
125     * eg, a Filter may be called "filter1",
126     * If this class does not have an owning
127     * component, a name from the configuration
128     * can be placed here. This name could also
129     * be changed to null for utility classes.
130     */
131 ajm 1.20 protected String _name = "EMail";
132 tdb 1.1
133     /**
134     * This holds a reference to the
135     * system logger that is being used.
136     */
137     private Logger _logger = ReferenceManager.getInstance().getLogger();
138 ajm 1.19
139 tdb 1.1 //---STATIC ATTRIBUTES---
140    
141     }