1 18 19 package org.apache.jmeter.reporters; 20 21 22 import java.io.Serializable ; 23 import java.net.UnknownHostException ; 25 import java.util.Properties ; 26 import java.util.StringTokenizer ; 27 import java.util.Vector ; 28 29 import javax.mail.Message ; 30 import javax.mail.MessagingException ; 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.swing.event.ChangeEvent ; 37 import javax.swing.event.ChangeListener ; 38 39 import org.apache.jmeter.samplers.SampleResult; 40 import org.apache.jmeter.testelement.AbstractTestElement; 41 import org.apache.jmeter.util.JMeterUtils; 42 import org.apache.jorphan.logging.LoggingManager; 43 import org.apache.log.Logger; 44 45 46 52 public class MailerModel extends AbstractTestElement implements Serializable 53 { 54 private long failureCount = 0; 55 private long successCount = 0; 56 private boolean failureMsgSent = false; 57 private boolean siteDown = false; 58 private boolean successMsgSent = false; 59 60 private static final String FROM_KEY = "MailerModel.fromAddress"; 61 private static final String TO_KEY = "MailerModel.addressie"; 62 private static final String HOST_KEY = "MailerModel.smtpHost"; 63 private static final String SUCCESS_SUBJECT = "MailerModel.successSubject"; 64 private static final String FAILURE_SUBJECT = "MailerModel.failureSubject"; 65 private static final String FAILURE_LIMIT_KEY = "MailerModel.failureLimit"; 66 private static final String SUCCESS_LIMIT_KEY = "MailerModel.successLimit"; 67 68 transient private static Logger log = LoggingManager.getLoggerForClass(); 69 70 71 ChangeListener changeListener; 72 73 76 public MailerModel() 77 { 78 super(); 79 80 setProperty( 81 SUCCESS_LIMIT_KEY, 82 JMeterUtils.getPropDefault("mailer.successlimit", "2")); 83 setProperty( 84 FAILURE_LIMIT_KEY, 85 JMeterUtils.getPropDefault("mailer.failurelimit", "2")); 86 } 87 88 public void addChangeListener(ChangeListener list) 89 { 90 changeListener = list; 91 } 92 93 public Object clone() 94 { 95 MailerModel m = (MailerModel)super.clone(); 96 m.changeListener = changeListener; 97 return m; 98 } 99 100 106 public synchronized boolean isFailing() 107 { 108 return (failureCount > getFailureLimit()); 109 } 110 111 public void notifyChangeListeners() 112 { 113 if(changeListener != null) 114 { 115 changeListener.stateChanged(new ChangeEvent (this)); 116 } 117 } 118 119 128 public synchronized Vector getAddressVector() 129 { 130 String theAddressie = getToAddress(); 131 Vector addressVector = new Vector (); 132 133 if (theAddressie != null) 134 { 135 String addressSep = ","; 136 137 StringTokenizer next = 138 new StringTokenizer (theAddressie, addressSep); 139 140 while (next.hasMoreTokens()) 141 { 142 String theToken = next.nextToken().trim(); 143 144 if (theToken.indexOf("@") > 0) 145 { 146 addressVector.addElement(theToken); 147 } 148 } 149 } 150 else 151 { 152 return new Vector (0); 153 } 154 155 return addressVector; 156 } 157 158 167 public synchronized void add(SampleResult sample) 168 { 169 170 if (!sample.isSuccessful()) 173 { 174 failureCount++; 175 successCount = 0; 176 } 177 else 178 { 179 successCount++; 180 } 181 182 if (this.isFailing() && !siteDown && !failureMsgSent) 183 { 184 Vector addressVector = getAddressVector(); 186 187 if (addressVector.size() != 0) 188 { 189 try 190 { 191 sendMail( 192 getFromAddress(), 193 addressVector, 194 getFailureSubject(), 195 "URL Failed: " + sample.getSampleLabel(), 196 getSmtpHost()); 197 } 198 catch (Exception e) 199 { 200 log.error("Problem sending mail", e); 201 } 202 siteDown = true; 203 failureMsgSent = true; 204 successCount = 0; 205 successMsgSent = false; 206 } 207 } 208 209 if (siteDown && (sample.getTime() != -1) & !successMsgSent) 210 { 211 if (successCount > getSuccessLimit()) 213 { 214 Vector addressVector = getAddressVector(); 215 216 try 217 { 218 sendMail( 219 getFromAddress(), 220 addressVector, 221 getSuccessSubject(), 222 "URL Restarted: " + sample.getSampleLabel(), 223 getSmtpHost()); 224 } 225 catch (Exception e) 226 { 227 log.error("Problem sending mail", e); 228 } 229 siteDown = false; 230 successMsgSent = true; 231 failureCount = 0; 232 failureMsgSent = false; 233 } 234 } 235 236 if (successMsgSent && failureMsgSent) 237 { 238 clear(); 239 } 240 notifyChangeListeners(); 241 } 242 243 248 public synchronized void clear() 249 { 250 failureCount = 0; 251 successCount = 0; 252 siteDown = false; 253 successMsgSent = false; 254 failureMsgSent = false; 255 notifyChangeListeners(); 256 } 257 258 266 public String toString() 267 { 268 return "E-Mail Notification"; 269 } 270 271 281 public synchronized void sendMail( 282 String from, 283 Vector vEmails, 284 String subject, 285 String attText, 286 String smtpHost) 287 throws UnknownHostException , AddressException , MessagingException 288 { 289 String host = smtpHost; 290 boolean debug = Boolean.valueOf(host).booleanValue(); 291 293 InternetAddress [] address = new InternetAddress [vEmails.size()]; 294 295 for (int k = 0; k < vEmails.size(); k++) 296 { 297 address[k] = new InternetAddress (vEmails.elementAt(k).toString()); 298 } 299 300 Properties props = new Properties (); 302 303 props.put("mail.smtp.host", host); 304 Session session = Session.getDefaultInstance(props, null); 305 306 session.setDebug(debug); 307 308 Message msg = new MimeMessage (session); 310 311 msg.setFrom(new InternetAddress (from)); 312 msg.setRecipients(Message.RecipientType.TO, address); 313 msg.setSubject(subject); 314 msg.setText(attText); 315 Transport.send(msg); 316 } 317 318 324 public void setToAddress(String str) 325 { 326 setProperty(TO_KEY,str); 327 } 328 329 public void setFromAddress(String str) 330 { 331 setProperty(FROM_KEY,str); 332 } 333 334 public void setSmtpHost(String str) 335 { 336 setProperty(HOST_KEY,str); 337 } 338 339 public void setFailureSubject(String str) 340 { 341 setProperty(FAILURE_SUBJECT,str); 342 } 343 344 public void setSuccessSubject(String str) 345 { 346 setProperty(SUCCESS_SUBJECT,str); 347 } 348 349 public void setSuccessLimit(String limit) 350 { 351 setProperty(SUCCESS_LIMIT_KEY,limit); 352 } 353 354 359 public void setFailureLimit(String limit) 360 { 361 setProperty(FAILURE_LIMIT_KEY,limit); 362 } 363 364 369 public String getToAddress() 370 { 371 return getPropertyAsString(TO_KEY); 372 } 373 374 public String getFromAddress() 375 { 376 return getPropertyAsString(FROM_KEY); 377 } 378 379 public String getSmtpHost() 380 { 381 return getPropertyAsString(HOST_KEY); 382 } 383 384 public String getFailureSubject() 385 { 386 return getPropertyAsString(FAILURE_SUBJECT); 387 } 388 389 public String getSuccessSubject() 390 { 391 return getPropertyAsString(SUCCESS_SUBJECT); 392 } 393 394 public long getSuccessLimit() 395 { 396 return getPropertyAsLong(SUCCESS_LIMIT_KEY); 397 } 398 399 public long getSuccessCount() 400 { 401 return successCount; 402 } 403 404 public long getFailureLimit() 405 { 406 return getPropertyAsLong(FAILURE_LIMIT_KEY); 407 } 408 409 public long getFailureCount() 410 { 411 return this.failureCount; 412 } 413 } 414 415 | Popular Tags |