KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > reporters > MailerModel


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/reporters/MailerModel.java,v 1.5 2004/02/27 00:46:47 sebb Exp $
2
/*
3  * Copyright 2003-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.reporters;
20
21
22 import java.io.Serializable JavaDoc;
23 //import java.net.InetAddress;
24
import java.net.UnknownHostException JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import javax.mail.Message JavaDoc;
30 import javax.mail.MessagingException 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.swing.event.ChangeEvent JavaDoc;
37 import javax.swing.event.ChangeListener JavaDoc;
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 /**
47  * The model for a MailerVisualizer.
48  *
49  * @author <a HREF="mailto:wolfram.rittmeyer@web.de">Wolfram Rittmeyer</a>
50  * @version $Revision: 1.5 $ $Date: 2004/02/27 00:46:47 $
51  */

52 public class MailerModel extends AbstractTestElement implements Serializable JavaDoc
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 JavaDoc FROM_KEY = "MailerModel.fromAddress";
61     private static final String JavaDoc TO_KEY = "MailerModel.addressie";
62     private static final String JavaDoc HOST_KEY = "MailerModel.smtpHost";
63     private static final String JavaDoc SUCCESS_SUBJECT = "MailerModel.successSubject";
64     private static final String JavaDoc FAILURE_SUBJECT = "MailerModel.failureSubject";
65     private static final String JavaDoc FAILURE_LIMIT_KEY = "MailerModel.failureLimit";
66     private static final String JavaDoc SUCCESS_LIMIT_KEY = "MailerModel.successLimit";
67
68     transient private static Logger log = LoggingManager.getLoggerForClass();
69
70     /** The listener for changes. */
71     ChangeListener JavaDoc changeListener;
72
73     /**
74      * Constructs a MailerModel.
75      */

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 JavaDoc list)
89     {
90         changeListener = list;
91     }
92     
93     public Object JavaDoc clone()
94     {
95         MailerModel m = (MailerModel)super.clone();
96         m.changeListener = changeListener;
97         return m;
98     }
99
100     /**
101      * Returns wether there had been more failures than acceptable.
102      *
103      * @return a boolean value indicating whether the limit of acceptable
104      * failures has been reached.
105      */

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 JavaDoc(this));
116         }
117     }
118
119     /**
120      * Gets a Vector of String-objects. Each String is one mail-address
121      * of the addresses-String set by <code>setToAddress(str)</code>.
122      * The addresses must be seperated by commas. Only String-objects
123      * containing a "@" are added to the returned Vector.
124      *
125      * @return a Vector of String-objects wherein each String represents a
126      * mail-address.
127      */

128     public synchronized Vector JavaDoc getAddressVector()
129     {
130         String JavaDoc theAddressie = getToAddress();
131         Vector JavaDoc addressVector = new Vector JavaDoc();
132
133         if (theAddressie != null)
134         {
135             String JavaDoc addressSep = ",";
136
137             StringTokenizer JavaDoc next =
138                 new StringTokenizer JavaDoc(theAddressie, addressSep);
139
140             while (next.hasMoreTokens())
141             {
142                 String JavaDoc 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 JavaDoc(0);
153         }
154
155         return addressVector;
156     }
157
158     /**
159      * Adds a SampleResult. If SampleResult represents a change concerning
160      * the failure/success of the sampling a message might be send to the
161      * addressies according to the settings of <code>successCount</code>
162      * and <code>failureCount</code>.
163      *
164      * @param sample the SampleResult encapsulating informations about the last
165      * sample.
166      */

167     public synchronized void add(SampleResult sample)
168     {
169
170         // -1 is the code for a failed sample.
171
//
172
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             // Send the mail ...
185
Vector JavaDoc 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 JavaDoc 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             // Send the mail ...
212
if (successCount > getSuccessLimit())
213             {
214                 Vector JavaDoc 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 JavaDoc 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     /**
244      * Resets the state of this object to its default. But: This method does not
245      * reset any mail-specific attributes (like sender, mail-subject...)
246      * since they are independent of the sampling.
247      */

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     /**
259      * Returns a String-representation of this object. Returns always
260      * "E-Mail-Notification". Might be enhanced in future versions to return
261      * some kind of String-representation of the mail-parameters (like
262      * sender, addressies, smtpHost...).
263      *
264      * @return A String-representation of this object.
265      */

266     public String JavaDoc toString()
267     {
268         return "E-Mail Notification";
269     }
270
271     /**
272      * Sends a mail with the given parameters using SMTP.
273      *
274      * @param from the sender of the mail as shown in the mail-client.
275      * @param vEmails all receivers of the mail. The receivers are seperated
276      * by commas.
277      * @param subject the subject of the mail.
278      * @param attText the message-body.
279      * @param smtpHost the smtp-server used to send the mail.
280      */

281     public synchronized void sendMail(
282         String JavaDoc from,
283         Vector JavaDoc vEmails,
284         String JavaDoc subject,
285         String JavaDoc attText,
286         String JavaDoc smtpHost)
287         throws UnknownHostException JavaDoc, AddressException JavaDoc, MessagingException JavaDoc
288     {
289         String JavaDoc host = smtpHost;
290         boolean debug = Boolean.valueOf(host).booleanValue();
291         //InetAddress remote = InetAddress.getByName(host);
292

293         InternetAddress JavaDoc[] address = new InternetAddress JavaDoc[vEmails.size()];
294
295         for (int k = 0; k < vEmails.size(); k++)
296         {
297             address[k] = new InternetAddress JavaDoc(vEmails.elementAt(k).toString());
298         }
299
300         // create some properties and get the default Session
301
Properties JavaDoc props = new Properties JavaDoc();
302
303         props.put("mail.smtp.host", host);
304         Session JavaDoc session = Session.getDefaultInstance(props, null);
305
306         session.setDebug(debug);
307
308         // create a message
309
Message JavaDoc msg = new MimeMessage JavaDoc(session);
310
311         msg.setFrom(new InternetAddress JavaDoc(from));
312         msg.setRecipients(Message.RecipientType.TO, address);
313         msg.setSubject(subject);
314         msg.setText(attText);
315         Transport.send(msg);
316     }
317
318     // ////////////////////////////////////////////////////////////
319
//
320
// setter/getter - JavaDoc-Comments not needed...
321
//
322
// ////////////////////////////////////////////////////////////
323

324     public void setToAddress(String JavaDoc str)
325     {
326        setProperty(TO_KEY,str);
327     }
328
329     public void setFromAddress(String JavaDoc str)
330     {
331         setProperty(FROM_KEY,str);
332     }
333
334     public void setSmtpHost(String JavaDoc str)
335     {
336         setProperty(HOST_KEY,str);
337     }
338
339     public void setFailureSubject(String JavaDoc str)
340     {
341         setProperty(FAILURE_SUBJECT,str);
342     }
343
344     public void setSuccessSubject(String JavaDoc str)
345     {
346         setProperty(SUCCESS_SUBJECT,str);
347     }
348
349     public void setSuccessLimit(String JavaDoc limit)
350     {
351         setProperty(SUCCESS_LIMIT_KEY,limit);
352     }
353
354 // private void setSuccessCount(long count)
355
// {
356
// this.successCount = count;
357
// }
358

359     public void setFailureLimit(String JavaDoc limit)
360     {
361         setProperty(FAILURE_LIMIT_KEY,limit);
362     }
363
364 // private void setFailureCount(long count)
365
// {
366
// this.failureCount = count;
367
// }
368

369     public String JavaDoc getToAddress()
370     {
371         return getPropertyAsString(TO_KEY);
372     }
373
374     public String JavaDoc getFromAddress()
375     {
376         return getPropertyAsString(FROM_KEY);
377     }
378
379     public String JavaDoc getSmtpHost()
380     {
381         return getPropertyAsString(HOST_KEY);
382     }
383
384     public String JavaDoc getFailureSubject()
385     {
386         return getPropertyAsString(FAILURE_SUBJECT);
387     }
388
389     public String JavaDoc 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