View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.model.support;
14  
15  import org.apache.commons.beanutils.PropertyUtils;
16  
17  import com.eviware.soapui.SoapUI;
18  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
19  import com.eviware.soapui.model.ModelItem;
20  
21  /***
22   * TestStepProperty implementation that maps to a standard javabean property
23   * 
24   * @author Ole.Matzura
25   */
26  
27  public class TestStepBeanProperty extends DefaultTestStepProperty
28  {
29  	public TestStepBeanProperty( String name, boolean isReadOnly, Object targetObject, String targetName,
30  			WsdlTestStep testStep )
31  	{
32  		super( name, isReadOnly, new BeanPropertyHandler( targetObject, targetName ), testStep );
33  	}
34  
35  	/***
36  	 * PropertyHandler for setting/getting bean properties
37  	 * 
38  	 * @author Ole.Matzura
39  	 */
40  
41  	public static class BeanPropertyHandler implements PropertyHandler
42  	{
43  		private final Object target;
44  		private final String targetName;
45  
46  		public BeanPropertyHandler( Object targetObject, String targetName )
47  		{
48  			this.target = targetObject;
49  			this.targetName = targetName;
50  		}
51  
52  		public String getValue( DefaultTestStepProperty prop )
53  		{
54  			try
55  			{
56  				Object property = PropertyUtils.getProperty( target, targetName );
57  				return property == null ? null : property.toString();
58  			}
59  			catch( Exception e )
60  			{
61  				if( target instanceof ModelItem )
62  				{
63  					SoapUI.logError( new Exception( "Error getting property [" + targetName + "] from modelItem ["
64  							+ ( ( ModelItem )target ).getName() + "]", e ) );
65  				}
66  				else
67  				{
68  					SoapUI.logError( new Exception(
69  							"Error getting property [" + targetName + "] from bean [" + target + "]", e ) );
70  				}
71  
72  				return null;
73  			}
74  		}
75  
76  		public void setValue( DefaultTestStepProperty property, String value )
77  		{
78  			try
79  			{
80  				PropertyUtils.setProperty( target, targetName, value );
81  			}
82  			catch( Exception e )
83  			{
84  				SoapUI.logError( e );
85  			}
86  		}
87  	}
88  
89  }