KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > monitor > alerts > EmailAlertListener


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.monitor.alerts;
23
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28
29 import javax.mail.Address JavaDoc;
30 import javax.mail.Message JavaDoc;
31 import javax.mail.Session JavaDoc;
32 import javax.mail.Transport JavaDoc;
33 import javax.mail.internet.AddressException JavaDoc;
34 import javax.mail.internet.InternetAddress JavaDoc;
35 import javax.mail.internet.MimeMessage JavaDoc;
36 import javax.management.Notification JavaDoc;
37 import javax.naming.InitialContext JavaDoc;
38
39 import org.jboss.monitor.JBossMonitorNotification;
40 import org.jboss.util.Strings;
41
42 /**
43  * Comment
44  *
45  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
46  * @version $Revision: 37459 $
47  *
48  **/

49 public class EmailAlertListener extends JBossAlertListener implements EmailAlertListenerMBean
50 {
51    protected String JavaDoc messageTemplate;
52    protected String JavaDoc subjectTemplate;
53    protected String JavaDoc fromString;
54    protected Address JavaDoc from;
55    protected String JavaDoc replyToString;
56    protected Address JavaDoc replyTo;
57    protected Address JavaDoc[] to;
58    protected HashSet JavaDoc toSet = new HashSet JavaDoc();
59
60
61    public void handleNotification(Notification JavaDoc notification,
62                                   Object JavaDoc handback)
63    {
64       if (!(notification instanceof JBossMonitorNotification)) return;
65       Map JavaDoc substitutions = ((JBossMonitorNotification) notification).substitutionMap();
66       String JavaDoc message = Strings.subst(messageTemplate, substitutions, "%(", ")");
67       String JavaDoc subject = Strings.subst(subjectTemplate, substitutions, "%(", ")");
68       try
69       {
70          Session JavaDoc session = (Session JavaDoc) new InitialContext JavaDoc().lookup("java:/Mail");
71          // create a message
72
//
73
Address JavaDoc replyToList[] = { replyTo };
74          Message JavaDoc newMessage = new MimeMessage JavaDoc(session);
75          newMessage.setFrom(from);
76          newMessage.setReplyTo(replyToList);
77          newMessage.setRecipients(Message.RecipientType.TO, to);
78          newMessage.setSubject(subject);
79          newMessage.setSentDate(new java.util.Date JavaDoc());
80          newMessage.setText(message);
81
82          // Send newMessage
83
//
84
Transport JavaDoc transport = session.getTransport();
85           transport.connect();
86           transport.sendMessage(newMessage, to);
87       }
88       catch (Exception JavaDoc ex)
89       {
90          ex.printStackTrace();
91       }
92
93    }
94
95    public String JavaDoc getTo()
96    {
97       Iterator JavaDoc it = toSet.iterator();
98       String JavaDoc output = "";
99       while (it.hasNext())
100       {
101          output += it.next();
102          if (it.hasNext()) output += ",";
103       }
104       return output;
105    }
106
107    protected void updateTo() throws AddressException JavaDoc
108    {
109       Iterator JavaDoc it = toSet.iterator();
110       Address JavaDoc[] newTo = new Address JavaDoc[toSet.size()];
111       for (int i = 0; it.hasNext(); i++)
112       {
113          String JavaDoc address = (String JavaDoc)it.next();
114          newTo[i] = new InternetAddress JavaDoc(address);
115       }
116       to = newTo;
117    }
118
119    public void setTo(String JavaDoc t) throws AddressException JavaDoc
120    {
121       StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(t, ",");
122       while (tokenizer.hasMoreTokens())
123       {
124          String JavaDoc token = tokenizer.nextToken().trim();
125          toSet.add(token);
126       }
127       updateTo();
128    }
129
130    public void addToAddress(String JavaDoc newAddress) throws AddressException JavaDoc
131    {
132       toSet.add(newAddress);
133       updateTo();
134    }
135
136    public void removeToAddress(String JavaDoc removeAddress) throws AddressException JavaDoc
137    {
138       toSet.remove(removeAddress);
139       updateTo();
140    }
141
142    public String JavaDoc getFrom()
143    {
144       return fromString;
145    }
146
147    public void setFrom(String JavaDoc f) throws AddressException JavaDoc
148    {
149       fromString = f;
150       from = new InternetAddress JavaDoc(f);
151    }
152
153    public String JavaDoc getReplyTo()
154    {
155       return replyToString;
156    }
157
158    public void setReplyTo(String JavaDoc f) throws AddressException JavaDoc
159    {
160       replyToString = f;
161       replyTo = new InternetAddress JavaDoc(f);
162    }
163
164    public String JavaDoc getMessageTemplate()
165    {
166       return messageTemplate;
167    }
168
169    public void setMessageTemplate(String JavaDoc messageTemplate)
170    {
171       this.messageTemplate = messageTemplate;
172    }
173    public String JavaDoc getSubjectTemplate()
174    {
175       return subjectTemplate;
176    }
177
178    public void setSubjectTemplate(String JavaDoc messageTemplate)
179    {
180       this.subjectTemplate = messageTemplate;
181    }
182
183 }
184
Popular Tags