001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations
015: * under the License.
016: */
017: package org.apache.jmeter.testbeans;
018:
019: import java.beans.BeanInfo;
020: import java.beans.IntrospectionException;
021: import java.beans.Introspector;
022: import java.beans.PropertyDescriptor;
023: import java.lang.reflect.InvocationTargetException;
024: import java.lang.reflect.Method;
025:
026: import org.apache.jmeter.testelement.TestElement;
027: import org.apache.jmeter.testelement.property.JMeterProperty;
028: import org.apache.jorphan.logging.LoggingManager;
029: import org.apache.jorphan.util.Converter;
030: import org.apache.log.Logger;
031:
032: /**
033: * This is an experimental class. An attempt to address the complexity of
034: * writing new JMeter components.
035: * <p>
036: * TestBean currently extends AbstractTestElement to support
037: * backward-compatibility, but the property-value-map may later on be separated
038: * from the test beans themselves. To ensure this will be doable with minimum
039: * damage, all inherited methods are deprecated.
040: *
041: * @author <a href="mailto:jsalvata@apache.org">Jordi Salvat i Alabart </a>
042: * @version $Revision: 514119 $ updated on $Date: 2007-03-03 10:39:09 +0000 (Sat, 03 Mar 2007) $
043: */
044: public class TestBeanHelper {
045: protected static final Logger log = LoggingManager
046: .getLoggerForClass();
047:
048: /**
049: * Prepare the bean for work by populating the bean's properties from the
050: * property value map.
051: * <p>
052: *
053: * @deprecated to limit it's usage in expectation of moving it elsewhere.
054: */
055: public static void prepare(TestElement el) {
056: if (!(el instanceof TestBean)) {
057: return;
058: }
059: try {
060: BeanInfo beanInfo = Introspector.getBeanInfo(el.getClass());
061: PropertyDescriptor[] desc = beanInfo
062: .getPropertyDescriptors();
063: Object[] param = new Object[1];
064:
065: if (log.isDebugEnabled())
066: log.debug("Preparing " + el.getClass());
067:
068: for (int x = 0; x < desc.length; x++) {
069: // Obtain a value of the appropriate type for this property.
070: JMeterProperty jprop = el
071: .getProperty(desc[x].getName());
072: Class type = desc[x].getPropertyType();
073: Object value = Converter.convert(
074: jprop.getStringValue(), type);
075:
076: if (log.isDebugEnabled())
077: log.debug("Setting " + jprop.getName() + "="
078: + value);
079:
080: // Set the bean's property to the value we just obtained:
081: if (value != null || !type.isPrimitive())
082: // We can't assign null to primitive types.
083: {
084: param[0] = value;
085: Method writeMethod = desc[x].getWriteMethod();
086: if (writeMethod != null)
087: invokeOrBailOut(el, writeMethod, param);
088: }
089: }
090: } catch (IntrospectionException e) {
091: log.error("Couldn't set properties for "
092: + el.getClass().getName(), e);
093: }
094: }
095:
096: /**
097: * Utility method that invokes a method and does the error handling around
098: * the invocation.
099: *
100: * @param method
101: * @param params
102: * @return the result of the method invocation.
103: */
104: private static Object invokeOrBailOut(Object invokee,
105: Method method, Object[] params) {
106: try {
107: return method.invoke(invokee, params);
108: } catch (IllegalArgumentException e) {
109: log.error("This should never happen.", e);
110: throw new Error(e.toString()); // Programming error: bail out.
111: } catch (IllegalAccessException e) {
112: log.error("This should never happen.", e);
113: throw new Error(e.toString()); // Programming error: bail out.
114: } catch (InvocationTargetException e) {
115: log.error("This should never happen.", e);
116: throw new Error(e.toString()); // Programming error: bail out.
117: }
118: }
119: }
|