Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.testbeans » [javadoc | source]
    1   // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/testbeans/BeanInfoSupport.java,v 1.9 2005/07/12 20:50:43 mstover1 Exp $
    2   /*
    3    * Copyright 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   package org.apache.jmeter.testbeans;
   18   
   19   import java.awt.Image;
   20   import java.beans.BeanDescriptor;
   21   import java.beans.BeanInfo;
   22   import java.beans.EventSetDescriptor;
   23   import java.beans.IntrospectionException;
   24   import java.beans.Introspector;
   25   import java.beans.MethodDescriptor;
   26   import java.beans.PropertyDescriptor;
   27   import java.beans.SimpleBeanInfo;
   28   import java.util.MissingResourceException;
   29   import java.util.ResourceBundle;
   30   
   31   import org.apache.jmeter.testbeans.gui.GenericTestBeanCustomizer;
   32   import org.apache.jmeter.util.JMeterUtils;
   33   import org.apache.jorphan.logging.LoggingManager;
   34   import org.apache.log.Logger;
   35   
   36   /**
   37    * Support class for test bean beanInfo objects. It will help using the
   38    * introspector to get most of the information, to then modify it at will.
   39    * <p>
   40    * To use, subclass it, create a subclass with a parameter-less constructor
   41    * that:
   42    * <ol>
   43    * <li>Calls super(beanClass)
   44    * <li>Modifies the property descriptors, bean descriptor, etc. at will.
   45    * </ol>
   46    * <p>
   47    * Even before any such modifications, a resource bundle named xxxResources
   48    * (where xxx is the fully qualified bean class name) will be obtained if
   49    * available and used to localize the following:
   50    * <ul>
   51    * <li>Bean's display name -- from property <b>displayName</b>.
   52    * <li>Properties' display names -- from properties <b><i>propertyName</i>.displayName</b>.
   53    * <li>Properties' short descriptions -- from properties <b><i>propertyName</i>.shortDescription</b>.
   54    * </ul>
   55    * <p>
   56    * The resource bundle will be stored as the bean descriptor's "resourceBundle"
   57    * attribute, so that it can be used for further localization. TestBeanGUI, for
   58    * example, uses it to obtain the group's display names from properties <b><i>groupName</i>.displayName</b>.
   59    * 
   60    * @author <a href="mailto:jsalvata@apache.org">Jordi Salvat i Alabart</a>
   61    * @version $Revision: 1.9 $ updated on $Date: 2005/07/12 20:50:43 $
   62    */
   63   public abstract class BeanInfoSupport extends SimpleBeanInfo {
   64   
   65   	private static transient Logger log = LoggingManager.getLoggerForClass();
   66   
   67   	// Some known attribute names, just for convenience:
   68   	public static final String TAGS = GenericTestBeanCustomizer.TAGS;
   69   
   70   	public static final String NOT_UNDEFINED = GenericTestBeanCustomizer.NOT_UNDEFINED;
   71   
   72   	public static final String NOT_EXPRESSION = GenericTestBeanCustomizer.NOT_EXPRESSION;
   73   
   74   	public static final String NOT_OTHER = GenericTestBeanCustomizer.NOT_OTHER;
   75   
   76   	public static final String DEFAULT = GenericTestBeanCustomizer.DEFAULT;
   77   
   78   	public static final String RESOURCE_BUNDLE = GenericTestBeanCustomizer.RESOURCE_BUNDLE;
   79   
   80   	/** The class for which we're providing the bean info. */
   81   	// NOTREAD private Class beanClass;
   82   	/** The BeanInfo for our class as obtained by the introspector. */
   83   	private BeanInfo rootBeanInfo;
   84   
   85   	/** The icons for this bean. */
   86   	private Image[] icons = new Image[5];
   87   
   88   	/**
   89   	 * Construct a BeanInfo for the given class.
   90   	 */
   91   	protected BeanInfoSupport(Class beanClass) {
   92   		// NOTREAD this.beanClass= beanClass;
   93   
   94   		try {
   95   			rootBeanInfo = Introspector.getBeanInfo(beanClass, Introspector.IGNORE_IMMEDIATE_BEANINFO);
   96   		} catch (IntrospectionException e) {
   97   			log.error("Can't introspect.", e);
   98   			throw new Error(e.toString()); // Programming error: bail out.
   99   		}
  100   
  101   		try {
  102   			ResourceBundle resourceBundle = ResourceBundle.getBundle(beanClass.getName() + "Resources", JMeterUtils
  103   					.getLocale());
  104   
  105   			// Store the resource bundle as an attribute of the BeanDescriptor:
  106   			getBeanDescriptor().setValue(RESOURCE_BUNDLE, resourceBundle);
  107   			// Localize the bean name
  108   			try {
  109   				getBeanDescriptor().setDisplayName(resourceBundle.getString("displayName"));
  110   			} catch (MissingResourceException e) {
  111   				log.debug("Localized display name not available for bean " + beanClass.getName());
  112   			}
  113   			// Localize the property names and descriptions:
  114   			PropertyDescriptor[] properties = getPropertyDescriptors();
  115   			for (int i = 0; i < properties.length; i++) {
  116   				String name = properties[i].getName();
  117   				try {
  118   					properties[i].setDisplayName(resourceBundle.getString(name + ".displayName"));
  119   				} catch (MissingResourceException e) {
  120   					log.debug("Localized display name not available for property " + name);
  121   				}
  122   
  123   				try {
  124   					properties[i].setShortDescription(resourceBundle.getString(name + ".shortDescription"));
  125   				} catch (MissingResourceException e) {
  126   					log.debug("Localized short description not available for property " + name);
  127   				}
  128   			}
  129   		} catch (MissingResourceException e) {
  130   			log.warn("Localized strings not available for bean " + beanClass, e);
  131   		} catch (Exception e) {
  132   			log.warn("Something bad happened when loading bean info", e);
  133   		}
  134   	}
  135   
  136   	/**
  137   	 * Get the property descriptor for the property of the given name.
  138   	 * 
  139   	 * @param name
  140   	 *            property name
  141   	 * @return descriptor for a property of that name, or null if there's none
  142   	 */
  143   	protected PropertyDescriptor property(String name) {
  144   		PropertyDescriptor[] properties = getPropertyDescriptors();
  145   		for (int i = 0; i < properties.length; i++) {
  146   			if (properties[i].getName().equals(name)) {
  147   				return properties[i];
  148   			}
  149   		}
  150   		return null;
  151   	}
  152   
  153   	/**
  154   	 * Set the bean's 16x16 colour icon.
  155   	 * 
  156   	 * @param resourceName
  157   	 *            A pathname relative to the directory holding the class file of
  158   	 *            the current class.
  159   	 */
  160   	protected void setIcon(String resourceName) {
  161   		icons[ICON_COLOR_16x16] = loadImage(resourceName);
  162   	}
  163   
  164   	/** Number of groups created so far by createPropertyGroup. */
  165   	private int numCreatedGroups = 0;
  166   
  167   	/**
  168   	 * Utility method to group and order properties.
  169   	 * <p>
  170   	 * It will assing the given group name to each of the named properties, and
  171   	 * set their order attribute so that they are shown in the given order.
  172   	 * <p>
  173   	 * The created groups will get order 1, 2, 3,... in the order in which they
  174   	 * are created.
  175   	 * 
  176   	 * @param group
  177   	 *            name of the group
  178   	 * @param names
  179   	 *            property names in the desired order
  180   	 */
  181   	protected void createPropertyGroup(String group, String[] names) {
  182   		for (int i = 0; i < names.length; i++) {
  183   			log.debug("Getting property for: " + names[i]);
  184   			PropertyDescriptor p = property(names[i]);
  185   			p.setValue(GenericTestBeanCustomizer.GROUP, group);
  186   			p.setValue(GenericTestBeanCustomizer.ORDER, new Integer(i));
  187   		}
  188   		numCreatedGroups++;
  189   		getBeanDescriptor().setValue(GenericTestBeanCustomizer.ORDER(group), new Integer(numCreatedGroups));
  190   	}
  191   
  192   	public BeanInfo[] getAdditionalBeanInfo() {
  193   		return rootBeanInfo.getAdditionalBeanInfo();
  194   	}
  195   
  196   	public BeanDescriptor getBeanDescriptor() {
  197   		return rootBeanInfo.getBeanDescriptor();
  198   	}
  199   
  200   	public int getDefaultEventIndex() {
  201   		return rootBeanInfo.getDefaultEventIndex();
  202   	}
  203   
  204   	public int getDefaultPropertyIndex() {
  205   		return rootBeanInfo.getDefaultPropertyIndex();
  206   	}
  207   
  208   	public EventSetDescriptor[] getEventSetDescriptors() {
  209   		return rootBeanInfo.getEventSetDescriptors();
  210   	}
  211   
  212   	public Image getIcon(int iconKind) {
  213   		return icons[iconKind];
  214   	}
  215   
  216   	public MethodDescriptor[] getMethodDescriptors() {
  217   		return rootBeanInfo.getMethodDescriptors();
  218   	}
  219   
  220   	public PropertyDescriptor[] getPropertyDescriptors() {
  221   		return rootBeanInfo.getPropertyDescriptors();
  222   	}
  223   }

Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.testbeans » [javadoc | source]