1 18 19 package org.apache.jmeter.protocol.java.config.gui; 20 21 import java.awt.BorderLayout ; 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.util.List ; 25 import java.util.Map ; 26 27 import javax.swing.JComboBox ; 28 import javax.swing.JLabel ; 29 import javax.swing.JPanel ; 30 31 import org.apache.jmeter.config.Argument; 32 import org.apache.jmeter.config.Arguments; 33 import org.apache.jmeter.config.gui.AbstractConfigGui; 34 import org.apache.jmeter.config.gui.ArgumentsPanel; 35 import org.apache.jmeter.gui.util.HorizontalPanel; 36 import org.apache.jmeter.protocol.java.config.JavaConfig; 37 import org.apache.jmeter.protocol.java.sampler.JavaSampler; 38 import org.apache.jmeter.protocol.java.sampler.JavaSamplerClient; 39 import org.apache.jmeter.testelement.TestElement; 40 import org.apache.jmeter.testelement.property.PropertyIterator; 41 import org.apache.jmeter.util.JMeterUtils; 42 import org.apache.jorphan.reflect.ClassFinder; 43 import org.apache.jorphan.logging.LoggingManager; 44 import org.apache.log.Logger; 45 46 47 53 public class JavaConfigGui extends AbstractConfigGui implements ActionListener 54 { 55 56 private static transient Logger log = LoggingManager.getLoggerForClass(); 57 58 59 private static final String CLASSNAMECOMBO = "classnamecombo"; 60 61 62 private JComboBox classnameCombo; 63 64 69 private boolean displayName = true; 70 71 72 private ArgumentsPanel argsPanel; 73 74 77 public JavaConfigGui() 78 { 79 this(true); 80 } 81 82 91 public JavaConfigGui(boolean displayNameField) 92 { 93 this.displayName = displayNameField; 94 init(); 95 } 96 97 public String getLabelResource() 98 { 99 return "java_request_defaults"; 100 } 101 102 105 protected void init() 106 { 107 setLayout(new BorderLayout (0, 5)); 108 109 if (displayName) 110 { 111 setBorder(makeBorder()); 112 add(makeTitlePanel(), BorderLayout.NORTH); 113 } 114 115 JPanel classnameRequestPanel = new JPanel (new BorderLayout (0, 5)); 116 classnameRequestPanel.add(createClassnamePanel(), BorderLayout.NORTH); 117 classnameRequestPanel.add(createParameterPanel(), BorderLayout.CENTER); 118 119 add(classnameRequestPanel, BorderLayout.CENTER); 120 } 121 122 128 private JPanel createClassnamePanel() 129 { 130 List possibleClasses = null; 131 132 try 133 { 134 possibleClasses = 137 ClassFinder.findClassesThatExtend( 138 JMeterUtils.getSearchPaths(), 139 new Class [] { JavaSamplerClient.class }); 140 141 144 possibleClasses.remove( 145 JavaSampler.class.getName() + "$ErrorSamplerClient"); 146 } 147 catch (Exception e) 148 { 149 log.debug("Exception getting interfaces.", e); 150 } 151 152 JLabel label = 153 new JLabel (JMeterUtils.getResString("protocol_java_classname")); 154 155 classnameCombo = new JComboBox (possibleClasses.toArray()); 156 classnameCombo.addActionListener(this); 157 classnameCombo.setName(CLASSNAMECOMBO); 158 classnameCombo.setEditable(false); 159 label.setLabelFor(classnameCombo); 160 161 HorizontalPanel panel = new HorizontalPanel(); 162 panel.add(label); 163 panel.add(classnameCombo); 164 165 return panel; 166 } 167 168 174 public void actionPerformed(ActionEvent evt) 175 { 176 if (evt.getSource() == classnameCombo) 177 { 178 String className = 179 ((String ) classnameCombo.getSelectedItem()).trim(); 180 try 181 { 182 JavaSamplerClient client = (JavaSamplerClient) Class.forName( 183 className, 184 true, 185 Thread.currentThread().getContextClassLoader() 186 ).newInstance(); 187 188 Arguments currArgs = new Arguments(); 189 argsPanel.modifyTestElement(currArgs); 190 Map currArgsMap = currArgs.getArgumentsAsMap(); 191 192 Arguments newArgs = new Arguments(); 193 Arguments testParams = null; 194 try 195 { 196 testParams = client.getDefaultParameters(); 197 } 198 catch (AbstractMethodError e) 199 { 200 log.warn ("JavaSamplerClient doesn't implement " 201 + "getDefaultParameters. Default parameters won't " 202 + "be shown. Please update your client class: " 203 + className); 204 } 205 206 if (testParams != null) 207 { 208 PropertyIterator i = testParams.getArguments().iterator(); 209 while (i.hasNext()) 210 { 211 Argument arg = (Argument) i.next().getObjectValue(); 212 String name = arg.getName(); 213 String value = arg.getValue(); 214 215 if (currArgsMap.containsKey(name)) 220 { 221 String newVal = (String ) currArgsMap.get(name); 222 if (newVal != null 223 && newVal.length() > 0) 224 { 225 value = newVal; 226 } 227 } 228 newArgs.addArgument(name, value); 229 } 230 } 231 232 argsPanel.configure(newArgs); 233 } 234 catch (Exception e) 235 { 236 log.error("Error getting argument list for " + className, e); 237 } 238 } 239 } 240 241 247 private JPanel createParameterPanel() 248 { 249 argsPanel = new ArgumentsPanel(JMeterUtils.getResString("paramtable")); 250 return argsPanel; 251 } 252 253 254 public void configure(TestElement config) 255 { 256 super.configure(config); 257 258 argsPanel.configure( 259 (Arguments) config 260 .getProperty(JavaSampler.ARGUMENTS) 261 .getObjectValue()); 262 263 classnameCombo.setSelectedItem( 264 config.getPropertyAsString(JavaSampler.CLASSNAME)); 265 } 266 267 268 public TestElement createTestElement() 269 { 270 JavaConfig config = new JavaConfig(); 271 modifyTestElement(config); 272 return config; 273 } 274 275 276 public void modifyTestElement(TestElement config) 277 { 278 configureTestElement(config); 279 ((JavaConfig) config).setArguments( 280 (Arguments) argsPanel.createTestElement()); 281 ((JavaConfig) config).setClassname( 282 classnameCombo.getSelectedItem().toString()); 283 } 284 } 285 | Popular Tags |