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.24
Committed: Tue May 29 17:02:34 2001 UTC (22 years, 11 months ago) by tdb
Branch: MAIN
Branch point for: SERVER_PIRCBOT
Changes since 1.23: +9 -9 lines
Log Message:
Major change in the java package naming. This has been held off for some time
now, but it really needed doing. The future packaging of all i-scream products
will be;

uk.org.iscream.<product>.<subpart>.*

In the case of the central monitoring system server this will be;

uk.org.iscream.cms.server.*

The whole server has been changed to follow this structure, and tested to a
smallish extent. Further changes in other parts of the CMS will follow.

File Contents

# User Rev Content
1 tdb 1.1 //---PACKAGE DECLARATION---
2 tdb 1.24 package uk.org.iscream.cms.server.client.alerters;
3 tdb 1.1
4     //---IMPORTS---
5 tdb 1.24 import uk.org.iscream.cms.server.client.*;
6     import uk.org.iscream.cms.server.core.*;
7     import uk.org.iscream.cms.server.util.*;
8     import uk.org.iscream.cms.server.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 tdb 1.24 * @author $Author: tdb1 $
17     * @version $Id: EMail__Alerter.java,v 1.23 2001/05/13 01:47:44 tdb1 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 tdb 1.24 public final String REVISION = "$Revision: 1.23 $";
27 tdb 1.1
28 ajm 1.22 /**
29     * A description of this alerter
30     */
31 tdb 1.8 public final String DESC = "Sends alerts over e-mail.";
32 tdb 1.1
33     //---STATIC METHODS---
34    
35     //---CONSTRUCTORS---
36    
37     //---PUBLIC METHODS---
38    
39 ajm 1.22 /**
40     * Implements the abstract method from the skeleton class.
41     * This method will attempt to send an alert
42     * message using the configured email configuration.
43     *
44     * @param alert the alert to send
45     */
46 tdb 1.1 public void sendAlert(Alert alert) {
47 tdb 1.21 // get the subject and replace fields
48 ajm 1.20 String subject;
49 tdb 1.14 try {
50 ajm 1.20 subject = _cp.getProperty(_name, "Alerter.EMail.subject");
51 tdb 1.14 } catch (PropertyNotFoundException e) {
52 ajm 1.20 subject = NOT_CONFIGURED;
53     _logger.write(toString(), Logger.WARNING, "Alerter.EMail.subject value unavailable using default of " + subject);
54 tdb 1.14 }
55 ajm 1.20 subject = processAlertMessage(subject, alert);
56    
57 tdb 1.21 // get the message body and replace fields
58 ajm 1.20 String message;
59     try {
60     message = _cp.getProperty(_name, "Alerter.EMail.message");
61     } catch (PropertyNotFoundException e) {
62     message = NOT_CONFIGURED;
63     _logger.write(toString(), Logger.WARNING, "Alerter.EMail.message value unavailable using default of " + message);
64     }
65     message = processAlertMessage(message, alert);
66 tdb 1.21
67     // attempt to send the actual message
68 ajm 1.20 try {
69     // create SMTP message
70     Smtp smtp = new Smtp(_cp.getProperty(_name, "Alerter.EMail.smtpServer"));
71     // set our sender
72     smtp.setSender(_cp.getProperty(_name, "Alerter.EMail.sender"));
73    
74 tdb 1.21 // list of destination addresses
75 ajm 1.20 String destList = _cp.getProperty("Host."+alert.getSource(), "Alerter.EMail.destList");
76    
77     // set the to: list
78     StringTokenizer st = new StringTokenizer(destList, ";");
79     while (st.hasMoreTokens()) {
80     smtp.setTo(st.nextToken());
81 tdb 1.2 }
82 ajm 1.20
83     // prepare to print the message
84     PrintWriter out = smtp.getOutputStream();
85 tdb 1.21 out.println("Subject: "+subject+"\n");
86 ajm 1.20
87     // send the message
88     out.println(message);
89     smtp.sendMessage();
90     smtp.close();
91 tdb 1.21 _logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ Alert.alertLevels[alert.getLevel()] + " level");
92 ajm 1.20 }
93     catch(IOException e) {
94     _logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e);
95     }
96     catch(PropertyNotFoundException e) {
97     _logger.write(toString(), Logger.ERROR, "Error obtaining essential configuration: "+e);
98 tdb 1.2 }
99 tdb 1.1 }
100    
101     /**
102     * Overrides the {@link java.lang.Object#toString() Object.toString()}
103     * method to provide clean logging (every class should have this).
104     *
105 tdb 1.24 * This uses the uk.org.iscream.cms.server.util.NameFormat class
106 tdb 1.1 * to format the toString()
107     *
108     * @return the name of this class and its CVS revision
109     */
110     public String toString() {
111     return FormatName.getName(
112     _name,
113     getClass().getName(),
114     REVISION);
115     }
116    
117     /**
118 ajm 1.22 * Return the String representation of what the alerter does
119     *
120     * @return the description
121 tdb 1.1 */
122     public String getDescription(){
123     return DESC;
124     }
125    
126     //---PRIVATE METHODS---
127 ajm 1.9
128 tdb 1.1 //---ACCESSOR/MUTATOR METHODS---
129 tdb 1.23
130     /**
131     * Returns the "friendly" name of this class. This
132     * is simply an accessor for _name, required due to
133     * inheritance issues with extending AlerterSkeleton.
134     *
135     * @return the friendly name
136     */
137     protected String getFName() {
138     return _name;
139     }
140 tdb 1.1
141     //---ATTRIBUTES---
142 tdb 1.2
143 tdb 1.1 /**
144     * This is the friendly identifier of the
145     * component this class is running in.
146     * eg, a Filter may be called "filter1",
147     * If this class does not have an owning
148     * component, a name from the configuration
149     * can be placed here. This name could also
150     * be changed to null for utility classes.
151     */
152 ajm 1.20 protected String _name = "EMail";
153 ajm 1.19
154 tdb 1.1 //---STATIC ATTRIBUTES---
155    
156     }