KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > visualizers > MailerVisualizer


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/visualizers/MailerVisualizer.java,v 1.20 2004/03/05 01:33:33 sebb Exp $
2
/*
3  * Copyright 2001-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.visualizers;
20
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.GridBagConstraints JavaDoc;
24 import java.awt.GridBagLayout JavaDoc;
25 import java.awt.Insets JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.awt.event.ActionListener JavaDoc;
28 import java.net.UnknownHostException JavaDoc;
29 import java.util.Vector JavaDoc;
30
31 import javax.swing.BorderFactory JavaDoc;
32 import javax.swing.JButton JavaDoc;
33 import javax.swing.JLabel JavaDoc;
34 import javax.swing.JOptionPane JavaDoc;
35 import javax.swing.JPanel JavaDoc;
36 import javax.swing.JTextField JavaDoc;
37 import javax.swing.border.Border JavaDoc;
38 import javax.swing.border.EmptyBorder JavaDoc;
39 import javax.swing.event.ChangeEvent JavaDoc;
40 import javax.swing.event.ChangeListener JavaDoc;
41
42 import org.apache.jmeter.gui.util.VerticalPanel;
43 import org.apache.jmeter.reporters.MailerModel;
44 import org.apache.jmeter.reporters.MailerResultCollector;
45 import org.apache.jmeter.samplers.Clearable;
46 import org.apache.jmeter.samplers.SampleResult;
47 import org.apache.jmeter.testelement.TestElement;
48 import org.apache.jmeter.util.JMeterUtils;
49 import org.apache.jmeter.visualizers.gui.AbstractVisualizer;
50 import org.apache.jorphan.logging.LoggingManager;
51 import org.apache.log.Logger;
52
53
54 /*
55  * TODO :
56  * - Create a subpanel for other visualizers
57  * - connect to the properties.
58  * - Get the specific URL that is failing.
59  * - add a seperate interface to collect the thrown failure messages.
60  * -
61  * - suggestions ;-)
62  */

63
64 /**
65  * This class implements a visualizer that mails a message when an error
66  * occurs.
67  *
68  * @version $Revision: 1.20 $ $Date: 2004/03/05 01:33:33 $
69  */

70 public class MailerVisualizer extends AbstractVisualizer
71         implements ActionListener JavaDoc, Clearable, ChangeListener JavaDoc
72 {
73     transient private static Logger log = LoggingManager.getLoggerForClass();
74
75     private JButton JavaDoc testerButton;
76     private JTextField JavaDoc addressField;
77     private JTextField JavaDoc fromField;
78     private JTextField JavaDoc smtpHostField;
79     private JTextField JavaDoc failureSubjectField;
80     private JTextField JavaDoc successSubjectField;
81     private JTextField JavaDoc failureField;
82     private JTextField JavaDoc failureLimitField;
83     private JTextField JavaDoc successLimitField;
84
85     //private JPanel mainPanel;
86
//private JLabel panelTitleLabel;
87

88
89     /**
90      * Constructs the MailerVisualizer and initializes its GUI.
91      */

92     public MailerVisualizer()
93     {
94         super();
95
96         // initialize GUI.
97
initGui();
98     }
99
100     public JPanel JavaDoc getControlPanel()
101     {
102         return this;
103     }
104
105     /**
106      * Clears any stored sampling-informations.
107      */

108     public synchronized void clear()
109     {
110         if(getModel() != null)
111         {
112             ((MailerResultCollector)getModel()).getMailerModel().clear();
113         }
114     }
115     
116     public void add(SampleResult res)
117     {
118     }
119
120     public String JavaDoc toString()
121     {
122         return "E-Mail Notification";
123     }
124
125     /**
126      * Initializes the GUI. Lays out components and adds them to the
127      * container.
128      */

129     private void initGui()
130     {
131         this.setLayout(new BorderLayout JavaDoc());
132
133         // MAIN PANEL
134
JPanel JavaDoc mainPanel = new VerticalPanel();
135         Border JavaDoc margin = new EmptyBorder JavaDoc(10, 10, 5, 10);
136
137         this.setBorder(margin);
138
139         // NAME
140
mainPanel.add(makeTitlePanel());
141
142         // mailer panel
143
JPanel JavaDoc mailerPanel = new JPanel JavaDoc();
144
145         mailerPanel.setBorder(
146             BorderFactory.createTitledBorder(
147                 BorderFactory.createEtchedBorder(),
148                 getAttributesTitle()));
149         GridBagLayout JavaDoc g = new GridBagLayout JavaDoc();
150
151         mailerPanel.setLayout(g);
152         GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
153
154         c.anchor = GridBagConstraints.NORTHWEST;
155         c.insets = new Insets JavaDoc(0, 0, 0, 0);
156         c.gridwidth = 1;
157         mailerPanel.add(new JLabel JavaDoc("From:"));
158
159         fromField = new JTextField JavaDoc(25);
160         fromField.setEditable(true);
161         c.gridwidth = GridBagConstraints.REMAINDER;
162         g.setConstraints(fromField, c);
163         mailerPanel.add(fromField);
164
165         c.anchor = GridBagConstraints.NORTHWEST;
166         c.insets = new Insets JavaDoc(0, 0, 0, 0);
167         c.gridwidth = 1;
168         mailerPanel.add(new JLabel JavaDoc("Addressee(s):"));
169
170         addressField = new JTextField JavaDoc(25);
171         addressField.setEditable(true);
172         c.gridwidth = GridBagConstraints.REMAINDER;
173         g.setConstraints(addressField, c);
174         mailerPanel.add(addressField);
175
176         c.gridwidth = 1;
177         mailerPanel.add(new JLabel JavaDoc("SMTP Host:"));
178
179         smtpHostField = new JTextField JavaDoc(25);
180         smtpHostField.setEditable(true);
181         c.gridwidth = GridBagConstraints.REMAINDER;
182         g.setConstraints(smtpHostField, c);
183         mailerPanel.add(smtpHostField);
184
185         c.gridwidth = 1;
186         mailerPanel.add(new JLabel JavaDoc("Failure Subject:"));
187
188         failureSubjectField = new JTextField JavaDoc(25);
189         failureSubjectField.setEditable(true);
190         c.gridwidth = GridBagConstraints.REMAINDER;
191         g.setConstraints(failureSubjectField, c);
192         mailerPanel.add(failureSubjectField);
193
194         c.gridwidth = 1;
195         mailerPanel.add(new JLabel JavaDoc("Success Subject:"));
196
197         successSubjectField = new JTextField JavaDoc(25);
198         successSubjectField.setEditable(true);
199         c.gridwidth = GridBagConstraints.REMAINDER;
200         g.setConstraints(successSubjectField, c);
201         mailerPanel.add(successSubjectField);
202
203         c.gridwidth = 1;
204         mailerPanel.add(new JLabel JavaDoc("Failure Limit:"));
205
206         failureLimitField = new JTextField JavaDoc("2",25);
207         failureLimitField.setEditable(true);
208         c.gridwidth = GridBagConstraints.REMAINDER;
209         g.setConstraints(failureLimitField, c);
210         mailerPanel.add(failureLimitField);
211
212         c.gridwidth = 1;
213         mailerPanel.add(new JLabel JavaDoc("Success Limit:"));
214
215         successLimitField = new JTextField JavaDoc("2",25);
216         successLimitField.setEditable(true);
217         c.gridwidth = GridBagConstraints.REMAINDER;
218         g.setConstraints(successLimitField, c);
219         mailerPanel.add(successLimitField);
220
221         testerButton = new JButton JavaDoc("Test Mail");
222         testerButton.addActionListener(this);
223         testerButton.setEnabled(true);
224         c.gridwidth = 1;
225         g.setConstraints(testerButton, c);
226         mailerPanel.add(testerButton);
227
228         c.gridwidth = 1;
229         mailerPanel.add(new JLabel JavaDoc("Failures:"));
230         failureField = new JTextField JavaDoc(6);
231         failureField.setEditable(false);
232         c.gridwidth = GridBagConstraints.REMAINDER;
233         g.setConstraints(failureField, c);
234         mailerPanel.add(failureField);
235
236         mainPanel.add(mailerPanel);
237
238         this.add(mainPanel,BorderLayout.WEST);
239     }
240
241     public String JavaDoc getLabelResource()
242     {
243         return "mailer_visualizer_title";
244     }
245
246     /**
247      * Returns a String for the title of the attributes-panel
248      * as set up in the properties-file using the lookup-constant
249      * "mailer_attributes_panel".
250      *
251      *@return The title of the component.
252      */

253     public String JavaDoc getAttributesTitle()
254     {
255         return JMeterUtils.getResString("mailer_attributes_panel");
256     }
257
258     // ////////////////////////////////////////////////////////////
259
//
260
// Implementation of the ActionListener-Interface.
261
//
262
// ////////////////////////////////////////////////////////////
263

264     /**
265      * Reacts on an ActionEvent (like pressing a button).
266      *
267      * @param e The ActionEvent with information about the event and its source.
268      */

269     public void actionPerformed(ActionEvent JavaDoc e)
270     {
271         if (e.getSource() == testerButton)
272         {
273             try
274             {
275                 MailerModel model=((MailerResultCollector)getModel()).getMailerModel();
276
277                 String JavaDoc to= addressField.getText();
278                 String JavaDoc from= fromField.getText();
279                 String JavaDoc via= smtpHostField.getText();
280                 String JavaDoc fail= failureSubjectField.getText();
281                 String JavaDoc success= successSubjectField.getText();
282                 
283                 String JavaDoc testString = "JMeter-Testmail" + "\n"
284                         + "To: " + to + "\n"
285                         + "Via: " + via + "\n"
286                         + "Fail Subject: " + fail + "\n"
287                         + "Success Subject: " + success;
288
289                 log.debug(testString);
290                 Vector JavaDoc destination= new Vector JavaDoc();
291                 destination.add(to);
292                 model.sendMail(from, destination, "Testing mail-addresses", testString, via);
293                 log.info("Mail sent successfully!!");
294             }
295             catch (UnknownHostException JavaDoc e1)
296             {
297                 log.error("Invalid Mail Server ", e1);
298                 displayMessage(JMeterUtils.getResString("invalid_mail_server"), true);
299             }
300             catch (Exception JavaDoc ex)
301             {
302                 log.error("Couldn't send mail...", ex);
303                 displayMessage(JMeterUtils.getResString("invalid_mail_server"), true);
304             }
305         }
306     }
307
308     // ////////////////////////////////////////////////////////////
309
//
310
// Methods used to store and retrieve the MailerVisualizer.
311
//
312
// ////////////////////////////////////////////////////////////
313

314     /**
315      * Restores MailerVisualizer.
316      */

317     public void configure(TestElement el)
318     {
319         super.configure(el);
320         updateVisualizer(((MailerResultCollector)el).getMailerModel());
321     }
322
323     /**
324      * Makes MailerVisualizer storable.
325      */

326     public TestElement createTestElement()
327     {
328         if (getModel() == null)
329         {
330             setModel( new MailerResultCollector());
331         }
332         modifyTestElement(getModel());
333         return getModel();
334     }
335
336     /* (non-Javadoc)
337      * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
338      */

339     public void modifyTestElement(TestElement c)
340     {
341         super.modifyTestElement(c);
342         MailerModel mailerModel = ((MailerResultCollector)c).getMailerModel();
343         mailerModel.setFailureLimit(failureLimitField.getText());
344         mailerModel.setFailureSubject(failureSubjectField.getText());
345         mailerModel.setFromAddress(fromField.getText());
346         mailerModel.setSmtpHost(smtpHostField.getText());
347         mailerModel.setSuccessLimit(successLimitField.getText());
348         mailerModel.setSuccessSubject(successSubjectField.getText());
349         mailerModel.setToAddress(addressField.getText());
350     }
351     
352
353
354     // ////////////////////////////////////////////////////////////
355
//
356
// Methods to implement the ModelListener.
357
//
358
// ////////////////////////////////////////////////////////////
359

360     /**
361      * Notifies this Visualizer about model-changes. Causes the Visualizer to
362      * query the model about its new state.
363      */

364     public void updateVisualizer(MailerModel model)
365     {
366         addressField.setText(model.getToAddress());
367         fromField.setText(model.getFromAddress());
368         smtpHostField.setText(model.getSmtpHost());
369         successSubjectField.setText(model.getSuccessSubject());
370         failureSubjectField.setText(model.getFailureSubject());
371         failureLimitField.setText(String.valueOf(model.getFailureLimit()));
372         failureField.setText(String.valueOf(model.getFailureCount()));
373         successLimitField.setText(String.valueOf(model.getSuccessLimit()));
374         repaint();
375     }
376
377     /**
378      * Shows a message using a DialogBox.
379      */

380     public void displayMessage(String JavaDoc message, boolean isError)
381     {
382         int type = 0;
383
384         if (isError)
385         {
386             type = JOptionPane.ERROR_MESSAGE;
387         }
388         else
389         {
390             type = JOptionPane.INFORMATION_MESSAGE;
391         }
392         JOptionPane.showMessageDialog(null, message, "Error", type);
393     }
394
395     /* (non-Javadoc)
396      * @see ChangeListener#stateChanged(ChangeEvent)
397      */

398     public void stateChanged(ChangeEvent JavaDoc e)
399     {
400         if(e.getSource() instanceof MailerModel)
401         {
402             MailerModel testModel = (MailerModel)e.getSource();
403             updateVisualizer(testModel);
404         }
405         else
406         {
407             super.stateChanged(e);
408         }
409     }
410
411 }
412
413
Popular Tags