1 22 package org.jboss.monitor.alerts; 23 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 import java.util.StringTokenizer ; 28 29 import javax.mail.Address ; 30 import javax.mail.Message ; 31 import javax.mail.Session ; 32 import javax.mail.Transport ; 33 import javax.mail.internet.AddressException ; 34 import javax.mail.internet.InternetAddress ; 35 import javax.mail.internet.MimeMessage ; 36 import javax.management.Notification ; 37 import javax.naming.InitialContext ; 38 39 import org.jboss.monitor.JBossMonitorNotification; 40 import org.jboss.util.Strings; 41 42 49 public class EmailAlertListener extends JBossAlertListener implements EmailAlertListenerMBean 50 { 51 protected String messageTemplate; 52 protected String subjectTemplate; 53 protected String fromString; 54 protected Address from; 55 protected String replyToString; 56 protected Address replyTo; 57 protected Address [] to; 58 protected HashSet toSet = new HashSet (); 59 60 61 public void handleNotification(Notification notification, 62 Object handback) 63 { 64 if (!(notification instanceof JBossMonitorNotification)) return; 65 Map substitutions = ((JBossMonitorNotification) notification).substitutionMap(); 66 String message = Strings.subst(messageTemplate, substitutions, "%(", ")"); 67 String subject = Strings.subst(subjectTemplate, substitutions, "%(", ")"); 68 try 69 { 70 Session session = (Session ) new InitialContext ().lookup("java:/Mail"); 71 Address replyToList[] = { replyTo }; 74 Message newMessage = new MimeMessage (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 ()); 80 newMessage.setText(message); 81 82 Transport transport = session.getTransport(); 85 transport.connect(); 86 transport.sendMessage(newMessage, to); 87 } 88 catch (Exception ex) 89 { 90 ex.printStackTrace(); 91 } 92 93 } 94 95 public String getTo() 96 { 97 Iterator it = toSet.iterator(); 98 String 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 108 { 109 Iterator it = toSet.iterator(); 110 Address [] newTo = new Address [toSet.size()]; 111 for (int i = 0; it.hasNext(); i++) 112 { 113 String address = (String )it.next(); 114 newTo[i] = new InternetAddress (address); 115 } 116 to = newTo; 117 } 118 119 public void setTo(String t) throws AddressException 120 { 121 StringTokenizer tokenizer = new StringTokenizer (t, ","); 122 while (tokenizer.hasMoreTokens()) 123 { 124 String token = tokenizer.nextToken().trim(); 125 toSet.add(token); 126 } 127 updateTo(); 128 } 129 130 public void addToAddress(String newAddress) throws AddressException 131 { 132 toSet.add(newAddress); 133 updateTo(); 134 } 135 136 public void removeToAddress(String removeAddress) throws AddressException 137 { 138 toSet.remove(removeAddress); 139 updateTo(); 140 } 141 142 public String getFrom() 143 { 144 return fromString; 145 } 146 147 public void setFrom(String f) throws AddressException 148 { 149 fromString = f; 150 from = new InternetAddress (f); 151 } 152 153 public String getReplyTo() 154 { 155 return replyToString; 156 } 157 158 public void setReplyTo(String f) throws AddressException 159 { 160 replyToString = f; 161 replyTo = new InternetAddress (f); 162 } 163 164 public String getMessageTemplate() 165 { 166 return messageTemplate; 167 } 168 169 public void setMessageTemplate(String messageTemplate) 170 { 171 this.messageTemplate = messageTemplate; 172 } 173 public String getSubjectTemplate() 174 { 175 return subjectTemplate; 176 } 177 178 public void setSubjectTemplate(String messageTemplate) 179 { 180 this.subjectTemplate = messageTemplate; 181 } 182 183 } 184 | Popular Tags |