001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.visualizers;
020:
021: import java.awt.BorderLayout;
022: import java.awt.GridBagConstraints;
023: import java.awt.GridBagLayout;
024: import java.awt.Insets;
025: import java.awt.event.ActionEvent;
026: import java.awt.event.ActionListener;
027: import javax.mail.MessagingException;
028: import javax.mail.internet.AddressException;
029: import javax.swing.BorderFactory;
030: import javax.swing.JButton;
031: import javax.swing.JLabel;
032: import javax.swing.JOptionPane;
033: import javax.swing.JPanel;
034: import javax.swing.JTextField;
035: import javax.swing.border.Border;
036: import javax.swing.border.EmptyBorder;
037: import javax.swing.event.ChangeEvent;
038: import javax.swing.event.ChangeListener;
039:
040: import org.apache.jmeter.gui.util.VerticalPanel;
041: import org.apache.jmeter.reporters.MailerModel;
042: import org.apache.jmeter.reporters.MailerResultCollector;
043: import org.apache.jmeter.reporters.ResultCollector;
044: import org.apache.jmeter.samplers.Clearable;
045: import org.apache.jmeter.samplers.SampleResult;
046: import org.apache.jmeter.testelement.TestElement;
047: import org.apache.jmeter.util.JMeterUtils;
048: import org.apache.jmeter.visualizers.gui.AbstractVisualizer;
049: import org.apache.jorphan.logging.LoggingManager;
050: import org.apache.log.Logger;
051:
052: /*
053: * TODO : - Create a subpanel for other visualizers - connect to the properties. -
054: * Get the specific URL that is failing. - add a seperate interface to collect
055: * the thrown failure messages. - - suggestions ;-)
056: */
057:
058: /**
059: * This class implements a visualizer that mails a message when an error occurs.
060: *
061: */
062: public class MailerVisualizer extends AbstractVisualizer implements
063: ActionListener, Clearable, ChangeListener {
064: private static final Logger log = LoggingManager
065: .getLoggerForClass();
066:
067: private JButton testerButton;
068:
069: private JTextField addressField;
070:
071: private JTextField fromField;
072:
073: private JTextField smtpHostField;
074:
075: private JTextField failureSubjectField;
076:
077: private JTextField successSubjectField;
078:
079: private JTextField failureField;
080:
081: private JTextField failureLimitField;
082:
083: private JTextField successLimitField;
084:
085: // private JPanel mainPanel;
086: // private JLabel panelTitleLabel;
087:
088: /**
089: * Constructs the MailerVisualizer and initializes its GUI.
090: */
091: public MailerVisualizer() {
092: super ();
093: setModel(new MailerResultCollector());
094: // initialize GUI.
095: initGui();
096: }
097:
098: public JPanel getControlPanel() {
099: return this ;
100: }
101:
102: /**
103: * Clears any stored sampling-informations.
104: */
105: public synchronized void clearData() {
106: if (getModel() != null) {
107: ((MailerResultCollector) getModel()).getMailerModel()
108: .clear();
109: }
110: }
111:
112: public void add(SampleResult res) {
113: }
114:
115: public String toString() {
116: return "E-Mail Notification";
117: }
118:
119: /**
120: * Initializes the GUI. Lays out components and adds them to the container.
121: */
122: private void initGui() {
123: this .setLayout(new BorderLayout());
124:
125: // MAIN PANEL
126: JPanel mainPanel = new VerticalPanel();
127: Border margin = new EmptyBorder(10, 10, 5, 10);
128:
129: this .setBorder(margin);
130:
131: // NAME
132: mainPanel.add(makeTitlePanel());
133:
134: // mailer panel
135: JPanel mailerPanel = new JPanel();
136:
137: mailerPanel.setBorder(BorderFactory.createTitledBorder(
138: BorderFactory.createEtchedBorder(),
139: getAttributesTitle()));
140: GridBagLayout g = new GridBagLayout();
141:
142: mailerPanel.setLayout(g);
143: GridBagConstraints c = new GridBagConstraints();
144:
145: c.anchor = GridBagConstraints.NORTHWEST;
146: c.insets = new Insets(0, 0, 0, 0);
147: c.gridwidth = 1;
148: mailerPanel.add(new JLabel("From:"));
149:
150: fromField = new JTextField(25);
151: fromField.setEditable(true);
152: c.gridwidth = GridBagConstraints.REMAINDER;
153: g.setConstraints(fromField, c);
154: mailerPanel.add(fromField);
155:
156: c.anchor = GridBagConstraints.NORTHWEST;
157: c.insets = new Insets(0, 0, 0, 0);
158: c.gridwidth = 1;
159: mailerPanel.add(new JLabel("Addressee(s):"));
160:
161: addressField = new JTextField(25);
162: addressField.setEditable(true);
163: c.gridwidth = GridBagConstraints.REMAINDER;
164: g.setConstraints(addressField, c);
165: mailerPanel.add(addressField);
166:
167: c.gridwidth = 1;
168: mailerPanel.add(new JLabel("SMTP Host:"));
169:
170: smtpHostField = new JTextField(25);
171: smtpHostField.setEditable(true);
172: c.gridwidth = GridBagConstraints.REMAINDER;
173: g.setConstraints(smtpHostField, c);
174: mailerPanel.add(smtpHostField);
175:
176: c.gridwidth = 1;
177: mailerPanel.add(new JLabel("Failure Subject:"));
178:
179: failureSubjectField = new JTextField(25);
180: failureSubjectField.setEditable(true);
181: c.gridwidth = GridBagConstraints.REMAINDER;
182: g.setConstraints(failureSubjectField, c);
183: mailerPanel.add(failureSubjectField);
184:
185: c.gridwidth = 1;
186: mailerPanel.add(new JLabel("Success Subject:"));
187:
188: successSubjectField = new JTextField(25);
189: successSubjectField.setEditable(true);
190: c.gridwidth = GridBagConstraints.REMAINDER;
191: g.setConstraints(successSubjectField, c);
192: mailerPanel.add(successSubjectField);
193:
194: c.gridwidth = 1;
195: mailerPanel.add(new JLabel("Failure Limit:"));
196:
197: failureLimitField = new JTextField("2", 25);
198: failureLimitField.setEditable(true);
199: c.gridwidth = GridBagConstraints.REMAINDER;
200: g.setConstraints(failureLimitField, c);
201: mailerPanel.add(failureLimitField);
202:
203: c.gridwidth = 1;
204: mailerPanel.add(new JLabel("Success Limit:"));
205:
206: successLimitField = new JTextField("2", 25);
207: successLimitField.setEditable(true);
208: c.gridwidth = GridBagConstraints.REMAINDER;
209: g.setConstraints(successLimitField, c);
210: mailerPanel.add(successLimitField);
211:
212: testerButton = new JButton("Test Mail");
213: testerButton.addActionListener(this );
214: testerButton.setEnabled(true);
215: c.gridwidth = 1;
216: g.setConstraints(testerButton, c);
217: mailerPanel.add(testerButton);
218:
219: c.gridwidth = 1;
220: mailerPanel.add(new JLabel("Failures:"));
221: failureField = new JTextField(6);
222: failureField.setEditable(false);
223: c.gridwidth = GridBagConstraints.REMAINDER;
224: g.setConstraints(failureField, c);
225: mailerPanel.add(failureField);
226:
227: mainPanel.add(mailerPanel);
228:
229: this .add(mainPanel, BorderLayout.WEST);
230: }
231:
232: public String getLabelResource() {
233: return "mailer_visualizer_title"; //$NON-NLS-1$
234: }
235:
236: /**
237: * Returns a String for the title of the attributes-panel as set up in the
238: * properties-file using the lookup-constant "mailer_attributes_panel".
239: *
240: * @return The title of the component.
241: */
242: public String getAttributesTitle() {
243: return JMeterUtils.getResString("mailer_attributes_panel"); //$NON-NLS-1$
244: }
245:
246: // ////////////////////////////////////////////////////////////
247: //
248: // Implementation of the ActionListener-Interface.
249: //
250: // ////////////////////////////////////////////////////////////
251:
252: /**
253: * Reacts on an ActionEvent (like pressing a button).
254: *
255: * @param e
256: * The ActionEvent with information about the event and its
257: * source.
258: */
259: public void actionPerformed(ActionEvent e) {
260: if (e.getSource() == testerButton) {
261: ResultCollector testElement = getModel();
262: modifyTestElement(testElement);
263: try {
264: MailerModel model = ((MailerResultCollector) testElement)
265: .getMailerModel();
266: model.sendTestMail();
267: displayMessage(
268: JMeterUtils.getResString("mail_sent"), false); //$NON-NLS-1$
269: } catch (AddressException ex) {
270: log.error("Invalid mail address ", ex);
271: displayMessage(JMeterUtils
272: .getResString("invalid_mail_address") //$NON-NLS-1$
273: + "\n" + ex.getMessage(), true); //$NON-NLS-1$
274: } catch (MessagingException ex) {
275: log.error("Couldn't send mail...", ex);
276: displayMessage(JMeterUtils.getResString("invalid_mail") //$NON-NLS-1$
277: + "\n" + ex.getMessage(), true); //$NON-NLS-1$
278: }
279: }
280: }
281:
282: // ////////////////////////////////////////////////////////////
283: //
284: // Methods used to store and retrieve the MailerVisualizer.
285: //
286: // ////////////////////////////////////////////////////////////
287:
288: /**
289: * Restores MailerVisualizer.
290: */
291: public void configure(TestElement el) {
292: super .configure(el);
293: updateVisualizer(((MailerResultCollector) el).getMailerModel());
294: }
295:
296: /**
297: * Makes MailerVisualizer storable.
298: */
299: public TestElement createTestElement() {
300: if (getModel() == null) {
301: setModel(new MailerResultCollector());
302: }
303: modifyTestElement(getModel());
304: return getModel();
305: }
306:
307: /*
308: * (non-Javadoc)
309: *
310: * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
311: */
312: public void modifyTestElement(TestElement c) {
313: super .modifyTestElement(c);
314: MailerModel mailerModel = ((MailerResultCollector) c)
315: .getMailerModel();
316: mailerModel.setFailureLimit(failureLimitField.getText());
317: mailerModel.setFailureSubject(failureSubjectField.getText());
318: mailerModel.setFromAddress(fromField.getText());
319: mailerModel.setSmtpHost(smtpHostField.getText());
320: mailerModel.setSuccessLimit(successLimitField.getText());
321: mailerModel.setSuccessSubject(successSubjectField.getText());
322: mailerModel.setToAddress(addressField.getText());
323: }
324:
325: // ////////////////////////////////////////////////////////////
326: //
327: // Methods to implement the ModelListener.
328: //
329: // ////////////////////////////////////////////////////////////
330:
331: /**
332: * Notifies this Visualizer about model-changes. Causes the Visualizer to
333: * query the model about its new state.
334: */
335: public void updateVisualizer(MailerModel model) {
336: addressField.setText(model.getToAddress());
337: fromField.setText(model.getFromAddress());
338: smtpHostField.setText(model.getSmtpHost());
339: successSubjectField.setText(model.getSuccessSubject());
340: failureSubjectField.setText(model.getFailureSubject());
341: failureLimitField.setText(String.valueOf(model
342: .getFailureLimit()));
343: failureField.setText(String.valueOf(model.getFailureCount()));
344: successLimitField.setText(String.valueOf(model
345: .getSuccessLimit()));
346: repaint();
347: }
348:
349: /**
350: * Shows a message using a DialogBox.
351: */
352: private void displayMessage(String message, boolean isError) {
353: int type = 0;
354:
355: if (isError) {
356: type = JOptionPane.ERROR_MESSAGE;
357: } else {
358: type = JOptionPane.INFORMATION_MESSAGE;
359: }
360: JOptionPane.showMessageDialog(null, message, isError ? "Error"
361: : "Information", type);
362: }
363:
364: /*
365: * (non-Javadoc)
366: *
367: * @see ChangeListener#stateChanged(ChangeEvent)
368: */
369: public void stateChanged(ChangeEvent e) {
370: if (e.getSource() instanceof MailerModel) {
371: MailerModel testModel = (MailerModel) e.getSource();
372: updateVisualizer(testModel);
373: } else {
374: super.stateChanged(e);
375: }
376: }
377:
378: }
|