1 17 package org.apache.jmeter.testbeans.gui; 18 19 import java.awt.BorderLayout ; 20 import java.awt.Component ; 21 import java.beans.BeanInfo ; 22 import java.beans.Customizer ; 23 import java.beans.IntrospectionException ; 24 import java.beans.Introspector ; 25 import java.beans.PropertyDescriptor ; 26 import java.beans.PropertyEditorManager ; 27 import java.util.Arrays ; 28 import java.util.Collection ; 29 import java.util.HashMap ; 30 import java.util.LinkedList ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 import javax.swing.JPopupMenu ; 35 36 import org.apache.commons.collections.LRUMap; 37 import org.apache.jmeter.assertions.Assertion; 38 import org.apache.jmeter.config.ConfigElement; 39 import org.apache.jmeter.control.Controller; 40 import org.apache.jmeter.gui.AbstractJMeterGuiComponent; 41 import org.apache.jmeter.gui.JMeterGUIComponent; 42 import org.apache.jmeter.gui.util.MenuFactory; 43 import org.apache.jmeter.processor.PostProcessor; 44 import org.apache.jmeter.processor.PreProcessor; 45 import org.apache.jmeter.samplers.Sampler; 46 import org.apache.jmeter.testbeans.TestBean; 47 import org.apache.jmeter.testelement.TestElement; 48 import org.apache.jmeter.testelement.property.BooleanProperty; 49 import org.apache.jmeter.testelement.property.DoubleProperty; 50 import org.apache.jmeter.testelement.property.FloatProperty; 51 import org.apache.jmeter.testelement.property.IntegerProperty; 52 import org.apache.jmeter.testelement.property.JMeterProperty; 53 import org.apache.jmeter.testelement.property.LongProperty; 54 import org.apache.jmeter.testelement.property.NullProperty; 55 import org.apache.jmeter.testelement.property.PropertyIterator; 56 import org.apache.jmeter.testelement.property.StringProperty; 57 import org.apache.jmeter.testelement.property.TestElementProperty; 58 import org.apache.jmeter.timers.Timer; 59 import org.apache.jmeter.util.JMeterUtils; 60 import org.apache.jmeter.visualizers.Visualizer; 61 import org.apache.jorphan.logging.LoggingManager; 62 import org.apache.log.Logger; 63 64 85 public class TestBeanGUI 86 extends AbstractJMeterGuiComponent 87 implements JMeterGUIComponent 88 { 89 private static Logger log = LoggingManager.getLoggerForClass(); 90 91 private Class testBeanClass; 92 93 private BeanInfo beanInfo; 94 95 private Class customizerClass; 96 97 101 private Customizer customizer= null; 102 103 108 private Map customizers= new LRUMap(20); 109 110 113 private int customizerIndexInPanel; 114 115 119 private Map propertyMap= new HashMap (); 120 121 124 private boolean initialized= false; 125 126 static 127 { 128 List paths= new LinkedList (); 129 paths.add("org.apache.jmeter.testbeans.gui"); 130 paths.addAll(Arrays.asList(PropertyEditorManager.getEditorSearchPath())); 131 String s= JMeterUtils.getPropDefault("propertyEditorSearchPath", null) ; 132 if (s != null) 133 { 134 paths.addAll(Arrays.asList(JMeterUtils.split(s, ",", ""))); 135 } 136 PropertyEditorManager.setEditorSearchPath((String [])paths.toArray(new String [0])); 137 } 138 139 public TestBeanGUI() 141 { 142 log.warn("Only for use in testing"); 143 } 144 145 public TestBeanGUI(Class testBeanClass) 146 { 147 super(); 148 149 if (! TestBean.class.isAssignableFrom(testBeanClass)) 151 { 152 Error e= new Error (); 153 log.error("This should never happen!", e); 154 throw e; } 156 157 this.testBeanClass= testBeanClass; 158 159 try 161 { 162 beanInfo= Introspector.getBeanInfo(testBeanClass); 163 } 164 catch (IntrospectionException e) 165 { 166 log.error("Can't get beanInfo for "+testBeanClass.getName(), 167 e); 168 throw new Error (e.toString()); } 170 171 customizerClass= beanInfo.getBeanDescriptor().getCustomizerClass(); 172 173 initialized= false; 177 } 178 179 private Customizer createCustomizer() 180 { 181 try 182 { 183 return (Customizer )customizerClass.newInstance(); 184 } 185 catch (InstantiationException e) 186 { 187 log.error("Could not instantiate customizer of class "+customizerClass, e); 188 throw new Error (e.toString()); 189 } 190 catch (IllegalAccessException e) 191 { 192 log.error("Could not instantiate customizer of class "+customizerClass, e); 193 throw new Error (e.toString()); 194 } 195 } 196 197 200 public String getStaticLabel() 201 { 202 if (beanInfo == null) return "null"; 203 return beanInfo.getBeanDescriptor().getDisplayName(); 204 } 205 206 209 public TestElement createTestElement() 210 { 211 try 212 { 213 TestElement element= (TestElement)testBeanClass.newInstance(); 214 configure(element); 215 super.clear(); configureTestElement(element); 217 modifyTestElement(element); return element; 219 } 220 catch (InstantiationException e) 221 { 222 log.error("Can't create test element", e); 223 throw new Error (e.toString()); } 225 catch (IllegalAccessException e) 226 { 227 log.error("Can't create test element", e); 228 throw new Error (e.toString()); } 230 } 231 232 235 public void modifyTestElement(TestElement element) 236 { 237 configureTestElement(element); 238 239 PropertyDescriptor [] props= beanInfo.getPropertyDescriptors(); 241 for (int i=0; i<props.length; i++) 242 { 243 String name= props[i].getName(); 244 Object value= propertyMap.get(name); 245 log.debug("Modify "+name+" to "+value); 246 if (value == null) 247 { 248 element.removeProperty(name); 249 } 250 else 251 { 252 JMeterProperty jprop= wrapInProperty(propertyMap.get(name)); 253 jprop.setName(name); 254 element.setProperty(jprop); 255 } 256 } 257 } 258 259 268 private static JMeterProperty wrapInProperty(Object value) 269 { 270 272 if (value instanceof JMeterProperty) 273 { 274 return (JMeterProperty)value; 275 } 276 277 JMeterProperty property; 278 if (value == null) 279 { 280 property= new NullProperty(); 281 } 282 else if (value instanceof Boolean ) 283 { 284 property= new BooleanProperty(); 285 } 286 else if (value instanceof Double ) 287 { 288 property= new DoubleProperty(); 289 } 290 else if (value instanceof Float ) 291 { 292 property= new FloatProperty(); 293 } 294 else if (value instanceof Integer ) 295 { 296 property= new IntegerProperty(); 297 } 298 else if (value instanceof Long ) 299 { 300 property= new LongProperty(); 301 } 302 else if (value instanceof String ) 303 { 304 property= new StringProperty(); 305 } 306 else if (value instanceof TestElement) 307 { 308 property= new TestElementProperty(); 309 } 310 else throw new Error ("Ouch!"); 311 312 property.setObjectValue(value); 313 314 return property; 315 } 316 317 320 public JPopupMenu createPopupMenu() 321 { 322 if (Timer.class.isAssignableFrom(testBeanClass)) { 326 return MenuFactory.getDefaultTimerMenu(); 327 } 328 return MenuFactory.getDefaultControllerMenu(); 329 } 330 331 334 public void configure(TestElement element) 335 { 336 if (! initialized) init(); 337 338 super.configure(element); 339 340 propertyMap.clear(); 342 for (PropertyIterator jprops= element.propertyIterator(); jprops.hasNext(); ) 343 { 344 JMeterProperty jprop= jprops.next(); 345 propertyMap.put(jprop.getName(), jprop.getObjectValue()); 346 } 347 348 if (customizer != null) 349 { 350 customizer.setObject(propertyMap); 351 } 352 else 353 { 354 if (initialized) remove(customizerIndexInPanel); 355 Customizer c= (Customizer )customizers.get(element); 356 if (c == null) 357 { 358 c= createCustomizer(); 359 c.setObject(propertyMap); 360 customizers.put(element, c); 361 } 362 add((Component )c, BorderLayout.CENTER); 363 } 364 365 initialized= true; 366 } 367 368 371 public Collection getMenuCategories() 372 { 373 List menuCategories= new LinkedList (); 374 375 if (beanInfo.getBeanDescriptor().isExpert() 378 && ! JMeterUtils.isExpertMode()) 379 { 380 return null; 381 } 382 383 if (Assertion.class.isAssignableFrom(testBeanClass)) 385 { 386 menuCategories.add(MenuFactory.ASSERTIONS); 387 } 388 if (ConfigElement.class.isAssignableFrom(testBeanClass)) 389 { 390 menuCategories.add(MenuFactory.CONFIG_ELEMENTS); 391 } 392 if (Controller.class.isAssignableFrom(testBeanClass)) 393 { 394 menuCategories.add(MenuFactory.CONTROLLERS); 395 } 396 if (Visualizer.class.isAssignableFrom(testBeanClass)) 397 { 398 menuCategories.add(MenuFactory.LISTENERS); 399 } 400 if (PostProcessor.class.isAssignableFrom(testBeanClass)) 401 { 402 menuCategories.add(MenuFactory.POST_PROCESSORS); 403 } 404 if (PreProcessor.class.isAssignableFrom(testBeanClass)) 405 { 406 menuCategories.add(MenuFactory.PRE_PROCESSORS); 407 } 408 if (Sampler.class.isAssignableFrom(testBeanClass)) 409 { 410 menuCategories.add(MenuFactory.SAMPLERS); 411 } 412 if (Timer.class.isAssignableFrom(testBeanClass)) 413 { 414 menuCategories.add(MenuFactory.TIMERS); 415 } 416 return menuCategories; 417 } 418 419 private void init() 420 { 421 setLayout(new BorderLayout (0, 5)); 422 423 setBorder(makeBorder()); 424 add(makeTitlePanel(), BorderLayout.NORTH); 425 426 customizerIndexInPanel= getComponentCount(); 427 428 if (customizerClass == null) 429 { 430 customizer= new GenericTestBeanCustomizer(beanInfo); 431 } 432 else if (SharedCustomizer.class.isAssignableFrom(customizerClass)) 433 { 434 customizer= createCustomizer(); 435 } 436 437 if (customizer != null) add((Component )customizer, BorderLayout.CENTER); 438 } 439 440 443 public String getLabelResource() { 444 return null; 446 } 447 } 448 | Popular Tags |