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/TestBeanHelper.java,v 1.3 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"); you may not
    6    * use this file except in compliance with the License. You may obtain a copy
    7    * 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, WITHOUT
   13    * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   14    * License for the specific language governing permissions and limitations
   15    * under the License.
   16    */
   17   package org.apache.jmeter.testbeans;
   18   
   19   import java.beans.BeanInfo;
   20   import java.beans.IntrospectionException;
   21   import java.beans.Introspector;
   22   import java.beans.PropertyDescriptor;
   23   import java.lang.reflect.InvocationTargetException;
   24   import java.lang.reflect.Method;
   25   
   26   import org.apache.jmeter.testelement.TestElement;
   27   import org.apache.jmeter.testelement.property.JMeterProperty;
   28   import org.apache.jorphan.logging.LoggingManager;
   29   import org.apache.jorphan.util.Converter;
   30   import org.apache.log.Logger;
   31   
   32   /**
   33    * This is an experimental class. An attempt to address the complexity of
   34    * writing new JMeter components.
   35    * <p>
   36    * TestBean currently extends AbstractTestElement to support
   37    * backward-compatibility, but the property-value-map may later on be separated
   38    * from the test beans themselves. To ensure this will be doable with minimum
   39    * damage, all inherited methods are deprecated.
   40    * 
   41    * @author <a href="mailto:jsalvata@apache.org">Jordi Salvat i Alabart </a>
   42    * @version $Revision: 1.3 $ updated on $Date: 2005/07/12 20:50:43 $
   43    */
   44   public class TestBeanHelper {
   45   	protected static final Logger log = LoggingManager.getLoggerForClass();
   46   
   47   	/**
   48   	 * Prepare the bean for work by populating the bean's properties from the
   49   	 * property value map.
   50   	 * <p>
   51   	 * 
   52   	 * @deprecated to limit it's usage in expectation of moving it elsewhere.
   53   	 */
   54   	public static void prepare(TestElement el) {
   55   		if (!(el instanceof TestBean)) {
   56   			return;
   57   		}
   58   		try {
   59   			BeanInfo beanInfo = Introspector.getBeanInfo(el.getClass());
   60   			PropertyDescriptor[] desc = beanInfo.getPropertyDescriptors();
   61   			Object[] param = new Object[1];
   62   
   63   			if (log.isDebugEnabled())
   64   				log.debug("Preparing " + el.getClass());
   65   
   66   			for (int x = 0; x < desc.length; x++) {
   67   				// Obtain a value of the appropriate type for this property.
   68   				JMeterProperty jprop = el.getProperty(desc[x].getName());
   69   				Class type = desc[x].getPropertyType();
   70   				Object value = Converter.convert(jprop.getStringValue(), type);
   71   
   72   				if (log.isDebugEnabled())
   73   					log.debug("Setting " + jprop.getName() + "=" + value);
   74   
   75   				// Set the bean's property to the value we just obtained:
   76   				if (value != null || !type.isPrimitive())
   77   				// We can't assign null to primitive types.
   78   				{
   79   					param[0] = value;
   80   					invokeOrBailOut(el, desc[x].getWriteMethod(), param);
   81   				}
   82   			}
   83   		} catch (IntrospectionException e) {
   84   			log.error("Couldn't set properties for " + el.getClass().getName(), e);
   85   		}
   86   	}
   87   
   88   	/**
   89   	 * Utility method that invokes a method and does the error handling around
   90   	 * the invocation.
   91   	 * 
   92   	 * @param method
   93   	 * @param params
   94   	 * @return the result of the method invocation.
   95   	 */
   96   	private static Object invokeOrBailOut(Object invokee, Method method, Object[] params) {
   97   		try {
   98   			return method.invoke(invokee, params);
   99   		} catch (IllegalArgumentException e) {
  100   			log.error("This should never happen.", e);
  101   			throw new Error(e.toString()); // Programming error: bail out.
  102   		} catch (IllegalAccessException e) {
  103   			log.error("This should never happen.", e);
  104   			throw new Error(e.toString()); // Programming error: bail out.
  105   		} catch (InvocationTargetException e) {
  106   			log.error("This should never happen.", e);
  107   			throw new Error(e.toString()); // Programming error: bail out.
  108   		}
  109   	}
  110   
  111   	/**
  112   	 * Utility method to obtain the value of a property in the given type.
  113   	 * <p>
  114   	 * I plan to get rid of this sooner than later, so please don't use it much.
  115   	 * 
  116   	 * @param property
  117   	 *            Property to get the value of.
  118   	 * @param type
  119   	 *            Type of the result.
  120   	 * @return an object of the given type if it is one of the known supported
  121   	 *         types, or the value returned by property.getObjectValue
  122   	 * @deprecated
  123   	 */
  124   	private static Object unwrapProperty(JMeterProperty property, Class type) {
  125   		return Converter.convert(property.getObjectValue(), type);
  126   	}
  127   }

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