1 18 19 package org.apache.jmeter.visualizers; 20 21 22 import java.awt.BorderLayout ; 23 import java.awt.GridBagConstraints ; 24 import java.awt.GridBagLayout ; 25 import java.awt.Insets ; 26 import java.awt.event.ActionEvent ; 27 import java.awt.event.ActionListener ; 28 import java.net.UnknownHostException ; 29 import java.util.Vector ; 30 31 import javax.swing.BorderFactory ; 32 import javax.swing.JButton ; 33 import javax.swing.JLabel ; 34 import javax.swing.JOptionPane ; 35 import javax.swing.JPanel ; 36 import javax.swing.JTextField ; 37 import javax.swing.border.Border ; 38 import javax.swing.border.EmptyBorder ; 39 import javax.swing.event.ChangeEvent ; 40 import javax.swing.event.ChangeListener ; 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 63 64 70 public class MailerVisualizer extends AbstractVisualizer 71 implements ActionListener , Clearable, ChangeListener 72 { 73 transient private static Logger log = LoggingManager.getLoggerForClass(); 74 75 private JButton testerButton; 76 private JTextField addressField; 77 private JTextField fromField; 78 private JTextField smtpHostField; 79 private JTextField failureSubjectField; 80 private JTextField successSubjectField; 81 private JTextField failureField; 82 private JTextField failureLimitField; 83 private JTextField successLimitField; 84 85 88 89 92 public MailerVisualizer() 93 { 94 super(); 95 96 initGui(); 98 } 99 100 public JPanel getControlPanel() 101 { 102 return this; 103 } 104 105 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 toString() 121 { 122 return "E-Mail Notification"; 123 } 124 125 129 private void initGui() 130 { 131 this.setLayout(new BorderLayout ()); 132 133 JPanel mainPanel = new VerticalPanel(); 135 Border margin = new EmptyBorder (10, 10, 5, 10); 136 137 this.setBorder(margin); 138 139 mainPanel.add(makeTitlePanel()); 141 142 JPanel mailerPanel = new JPanel (); 144 145 mailerPanel.setBorder( 146 BorderFactory.createTitledBorder( 147 BorderFactory.createEtchedBorder(), 148 getAttributesTitle())); 149 GridBagLayout g = new GridBagLayout (); 150 151 mailerPanel.setLayout(g); 152 GridBagConstraints c = new GridBagConstraints (); 153 154 c.anchor = GridBagConstraints.NORTHWEST; 155 c.insets = new Insets (0, 0, 0, 0); 156 c.gridwidth = 1; 157 mailerPanel.add(new JLabel ("From:")); 158 159 fromField = new JTextField (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 (0, 0, 0, 0); 167 c.gridwidth = 1; 168 mailerPanel.add(new JLabel ("Addressee(s):")); 169 170 addressField = new JTextField (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 ("SMTP Host:")); 178 179 smtpHostField = new JTextField (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 ("Failure Subject:")); 187 188 failureSubjectField = new JTextField (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 ("Success Subject:")); 196 197 successSubjectField = new JTextField (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 ("Failure Limit:")); 205 206 failureLimitField = new JTextField ("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 ("Success Limit:")); 214 215 successLimitField = new JTextField ("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 ("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 ("Failures:")); 230 failureField = new JTextField (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 getLabelResource() 242 { 243 return "mailer_visualizer_title"; 244 } 245 246 253 public String getAttributesTitle() 254 { 255 return JMeterUtils.getResString("mailer_attributes_panel"); 256 } 257 258 264 269 public void actionPerformed(ActionEvent e) 270 { 271 if (e.getSource() == testerButton) 272 { 273 try 274 { 275 MailerModel model=((MailerResultCollector)getModel()).getMailerModel(); 276 277 String to= addressField.getText(); 278 String from= fromField.getText(); 279 String via= smtpHostField.getText(); 280 String fail= failureSubjectField.getText(); 281 String success= successSubjectField.getText(); 282 283 String 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 destination= new Vector (); 291 destination.add(to); 292 model.sendMail(from, destination, "Testing mail-addresses", testString, via); 293 log.info("Mail sent successfully!!"); 294 } 295 catch (UnknownHostException e1) 296 { 297 log.error("Invalid Mail Server ", e1); 298 displayMessage(JMeterUtils.getResString("invalid_mail_server"), true); 299 } 300 catch (Exception ex) 301 { 302 log.error("Couldn't send mail...", ex); 303 displayMessage(JMeterUtils.getResString("invalid_mail_server"), true); 304 } 305 } 306 } 307 308 314 317 public void configure(TestElement el) 318 { 319 super.configure(el); 320 updateVisualizer(((MailerResultCollector)el).getMailerModel()); 321 } 322 323 326 public TestElement createTestElement() 327 { 328 if (getModel() == null) 329 { 330 setModel( new MailerResultCollector()); 331 } 332 modifyTestElement(getModel()); 333 return getModel(); 334 } 335 336 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 360 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 380 public void displayMessage(String 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 398 public void stateChanged(ChangeEvent 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 |