Source code: org/altara/mars/plugin/mailnotify/MailNotifyPlugin.java
1 /* MARS Mail Notification Plugin
2 Copyright (C) 2002 Leapfrog Research & Development, LLC
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, it is available at
16 http:///www.gnu.org/copyleft/gpl.html, or by writing to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 */
20
21 package org.altara.mars.plugin.mailnotify;
22
23 import org.jdom.*;
24 import org.altara.mars.*;
25 import org.altara.mars.engine.*;
26 import org.altara.mars.plugin.*;
27 import org.altara.mars.swingui.*;
28 import java.util.*;
29 import java.text.*;
30 import java.net.*;
31 import java.io.*;
32 import java.awt.*;
33 import java.awt.event.*;
34 import javax.swing.*;
35 import javax.swing.event.*;
36
37
38 public class MailNotifyPlugin implements Plugin, NotificationListener {
39
40 private static final DateFormat df = DateFormat.getTimeInstance();
41
42 private static String getFirstAddress(String addressList) {
43 int comPos = addressList.indexOf(',');
44 if (comPos == -1)
45 return addressList;
46 else
47 return addressList.substring(0,comPos);
48 }
49
50 private InetAddress mailServer;
51 private String mailAddress;
52 private String fromAddress;
53 private boolean enabled;
54 private boolean notifyBackUp;
55
56 private SimpleSmtpClient client;
57
58 public MailNotifyPlugin() {
59 mailServer = null;
60 mailAddress = null;
61 enabled = false;
62 notifyBackUp = false;
63 }
64
65 public String getElementName() {
66 return "mailnotify";
67 }
68
69 public String getDisplayName() {
70 return "Mail Notification";
71 }
72
73 public void notifyStatusChanged(StatusChangeEvent sce) {
74 // do nothing if not enabled
75 if (!enabled) return;
76
77 // check one: we just went down.
78 if (sce.getOldStatus().getCode() == Status.UP &&
79 sce.getNewStatus().isFault()) {
80 sendMessage(sce, "Fault");
81 } else
82 // check two: we came back up, and we care about such things
83 if (notifyBackUp && sce.getOldStatus().isFault() &&
84 sce.getNewStatus().getCode() == Status.UP) {
85 sendMessage(sce, "Notice");
86 }
87 }
88
89 private void sendMessage (StatusChangeEvent sce, String subjdesc) {
90 Status newStatus = sce.getNewStatus();
91 Service service = sce.getService();
92 String serviceDesc =
93 service.getName()+" on "+service.getHost().getName();
94 String subject = "[Mars] "+subjdesc+": "+serviceDesc;
95 String message = serviceDesc+" is "+newStatus.toString()+
96 " at "+df.format(new Date(newStatus.getTimestamp()))+".";
97 Status sendStatus =
98 client.sendMessage(fromAddress,mailAddress,subject,message);
99 if (sendStatus.isFault()) {
100 Main.getMain().showStatus("Mail notification failure: "+sendStatus);
101 } else {
102 Main.getMain().showStatus(service+": Mail notification sent");
103 }
104 }
105
106 private void setServer(InetAddress mailServer) {
107 this.mailServer = mailServer;
108 if (mailServer != null) {
109 this.client = new SimpleSmtpClient(mailServer);
110 }
111 }
112
113 /* ------------------------------------------------------
114 XML config serialization
115 ------------------------------------------------------ */
116
117 public Element getConfig() {
118 Element out = new Element(getElementName(),MarsModel.NAMESPACE);
119 out.setAttribute("enabled",String.valueOf(enabled));
120 out.setAttribute("notifyBackUp",String.valueOf(notifyBackUp));
121 if (mailServer != null)
122 out.setAttribute("server",mailServer.getHostName());
123 if (mailAddress != null)
124 out.setAttribute("address",mailAddress);
125 if (fromAddress != null)
126 out.setAttribute("fromAddress", fromAddress);
127 return out;
128 }
129
130 public void setConfig(Element in) throws UnknownHostException,
131 InvalidDocumentException {
132 boolean enabled, notifyBackUp;
133 InetAddress mailServer = null;
134 String mailAddress = null;
135 String fromAddress = null;
136 String backUpStr = null;
137
138 String enabledStr = in.getAttributeValue("enabled");
139 if (enabledStr == null)
140 throw new InvalidDocumentException("Missing mail enabled");
141 enabled = Boolean.valueOf(enabledStr).booleanValue();
142
143 String mailServerStr = in.getAttributeValue("server");
144 if (mailServerStr == null && enabled == true)
145 throw new InvalidDocumentException("Missing mail server");
146 if (mailServerStr != null)
147 mailServer = InetAddress.getByName(mailServerStr);
148
149 mailAddress = in.getAttributeValue("address");
150 if (mailAddress == null && enabled == true)
151 throw new InvalidDocumentException("Missing mail address");
152
153 // don't fail here - default to recipient
154 fromAddress = in.getAttributeValue("fromAddress");
155 if (fromAddress == null && enabled == true)
156 fromAddress = getFirstAddress(mailAddress);
157
158 // don't fail here - for backwards compatibility, default to no
159 backUpStr = in.getAttributeValue("notifyBackUp");
160 if (backUpStr == null) {
161 notifyBackUp = false;
162 } else {
163 notifyBackUp = Boolean.valueOf(backUpStr).booleanValue();
164 }
165
166 // everything's here, set config
167 this.enabled = enabled;
168 this.notifyBackUp = notifyBackUp;
169 this.mailAddress = mailAddress;
170 this.fromAddress = fromAddress;
171 setServer(mailServer);
172 }
173
174 /* ------------------------------------------------------
175 Editor
176 ------------------------------------------------------ */
177
178 public Editor getEditor() {
179 return new MailNotifyEditor();
180 }
181
182 private class MailNotifyEditor extends JPanel implements Editor {
183
184 private JCheckBox enabledBox;
185 private JTextField serverField;
186 private JTextField addressField;
187 private JTextField fromAddressField;
188 private JCheckBox backUpBox;
189
190 private MailNotifyEditor() {
191 // create editable fields
192 enabledBox = new JCheckBox("Enabled",enabled);
193 String initServerField = "";
194 String initAddressField = "";
195 String initFromAddressField = "";
196 if (mailServer != null) initServerField = mailServer.getHostName();
197 if (mailAddress != null) initAddressField = mailAddress;
198 if (fromAddress != null) initFromAddressField = fromAddress;
199
200 serverField = new JTextField(initServerField,25);
201 addressField = new JTextField(initAddressField,25);
202 fromAddressField = new JTextField(initFromAddressField,25);
203
204 backUpBox = new JCheckBox("Notify when a service comes back up",
205 notifyBackUp);
206
207 // set default field enable states
208 serverField.setEnabled(enabled);
209 addressField.setEnabled(enabled);
210 backUpBox.setEnabled(enabled);
211
212 // set up an action listener on the checkbox
213 enabledBox.addActionListener(new ActionListener() {
214 public void actionPerformed(ActionEvent ae) {
215 serverField.setEnabled(enabledBox.isSelected());
216 addressField.setEnabled(enabledBox.isSelected());
217 fromAddressField.setEnabled(enabledBox.isSelected());
218 backUpBox.setEnabled(enabledBox.isSelected());
219 }
220 });
221
222 // set up the layout manager
223 setLayout(new GridBagLayout());
224 GridBagConstraints c = new GridBagConstraints();
225 c.anchor = c.NORTHWEST; c.fill = c.BOTH;
226 c.weightx = 1.0; c.weighty = 0.0;
227 c.insets = new Insets(4,4,6,4);
228
229 // lay out the panel
230 c.gridx = 1; c.gridy = 0; c.gridwidth = 1; c.gridheight = 1;
231 add(enabledBox,c);
232 c.gridx = 0; c.gridy = 1; c.gridwidth = 1; c.gridheight = 1;
233 add(new JLabel("SMTP Server"),c);
234 c.gridx = 1; c.gridy = 1; c.gridwidth = 1; c.gridheight = 1;
235 add(serverField,c);
236 c.gridx = 1; c.gridy = 2; c.gridwidth = 1; c.gridheight = 1;
237 JLabel serverHelp =
238 new JLabel("Must be configured to relay for this machine.");
239 serverHelp.setFont(serverHelp.getFont().deriveFont(Font.PLAIN,9));
240 add(serverHelp,c);
241
242 c.gridx = 0; c.gridy = 3; c.gridwidth = 1; c.gridheight = 1;
243 add(new JLabel("Sender"),c);
244 c.gridx = 1; c.gridy = 3; c.gridwidth = 1; c.gridheight = 1;
245 add(fromAddressField,c);
246 c.gridx = 1; c.gridy = 4; c.gridwidth = 1; c.gridheight = 1;
247 JLabel fromAddressHelp = new JLabel("Optional, defaults to first recipient.");
248 fromAddressHelp.setFont(fromAddressHelp.getFont().deriveFont(Font.PLAIN,9));
249 add(fromAddressHelp,c);
250
251 c.gridx = 0; c.gridy = 5; c.gridwidth = 1; c.gridheight = 1;
252 add(new JLabel("Recipient(s)"),c);
253 c.gridx = 1; c.gridy = 5; c.gridwidth = 1; c.gridheight = 1;
254 add(addressField,c);
255 c.gridx = 1; c.gridy = 6; c.gridwidth = 1; c.gridheight = 1;
256 JLabel addressHelp =
257 new JLabel("Separate multiple addresses with commas.");
258 addressHelp.setFont(addressHelp.getFont().deriveFont(Font.PLAIN,9));
259 add(addressHelp,c);
260
261
262 c.gridx = 1; c.gridy = 7; c.gridwidth = 1; c.gridheight = 1;
263 add(backUpBox,c);
264 }
265
266 public void commit() throws UnknownHostException {
267 setServer(InetAddress.getByName(serverField.getText()));
268 mailAddress = addressField.getText();
269 fromAddress = fromAddressField.getText();
270 if (fromAddress.length() == 0) fromAddress =
271 getFirstAddress(mailAddress);
272 enabled = enabledBox.isSelected();
273 notifyBackUp = backUpBox.isSelected();
274 }
275
276 public String getEditorTitle() {
277 return "Configure Mail Notification";
278 }
279 }
280 }