View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 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.support.action;
    14
    15  import java.io.IOException;
    16  import java.io.InputStream;
    17  import java.lang.reflect.Constructor;
    18  import java.util.HashMap;
    19  import java.util.List;
    20  import java.util.Map;
    21
    22  import com.eviware.soapui.SoapUI;
    23  import com.eviware.soapui.config.ActionMappingPositionTypeConfig;
    24  import com.eviware.soapui.config.SoapUIActionConfig;
    25  import com.eviware.soapui.config.SoapUIActionGroupConfig;
    26  import com.eviware.soapui.config.SoapUIActionMappingConfig;
    27  import com.eviware.soapui.config.SoapUIActionsConfig;
    28  import com.eviware.soapui.config.SoapuiActionsDocumentConfig;
    29  import com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion;
    30  import com.eviware.soapui.model.ModelItem;
    31  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
    32  import com.eviware.soapui.support.action.support.DefaultActionMapping;
    33  import com.eviware.soapui.support.action.support.DefaultSoapUIActionGroup;
    34  import com.eviware.soapui.support.action.support.StandaloneActionMapping;
    35
    36  /***
    37   * Global SoapUIAction Registry
    38   *
    39   * @author ole.matzura
    40   */
    41
    42  @SuppressWarnings( "unchecked" )
    43  public class SoapUIActionRegistry
    44  {
    45  	private Map<String, SoapUIAction> actions = new HashMap<String, SoapUIAction>();
    46  	private Map<String, SoapUIActionGroup> actionGroups = new HashMap<String, SoapUIActionGroup>();
    47
    48  	public void addAction( String soapuiActionID, SoapUIAction action )
    49  	{
    50  		actions.put( soapuiActionID, action );
    51  	}
    52
    53     public void removeAction( String soapuiActionID )
    54     {
    55        actions.remove( soapuiActionID );
    56     }
    57
    58  	public static class SeperatorAction extends AbstractSoapUIAction
    59  	{
    60  		public static final String SOAPUI_ACTION_ID = "SeperatorAction";
    61  		public static SeperatorAction INSTANCE = new SeperatorAction();
    62  		private static SoapUIActionMapping defaultMapping =
    63  			new DefaultActionMapping( SeperatorAction.SOAPUI_ACTION_ID, null, null, false, null );
    64
    65  		private SeperatorAction()
    66  		{
    67  			super( null, null );
    68  		}
    69
    70  		public void perform( ModelItem target, Object param )
    71  		{
    72  		}
    73
    74  		public static SoapUIActionMapping<WsdlMessageAssertion> getDefaultMapping()
    75  		{
    76  			return defaultMapping;
    77  		}
    78  	}
    79
    80  	@SuppressWarnings( "unused" )
    81  	public static class SoapUIActionGroupAction<T extends ModelItem> extends AbstractSoapUIAction<T>
    82  	{
    83  		private SoapUIActionGroup actionGroup;
    84  		private final String actionGroupId;
    85
    86  		public SoapUIActionGroupAction( String name, String description, String actionGroupId )
    87  		{
    88  			super( name, description );
    89  			this.actionGroupId = actionGroupId;
    90  		}
    91
    92  		public SoapUIActionGroup getActionGroup()
    93  		{
    94  			if( actionGroup == null )
    95  				actionGroup = SoapUI.getActionRegistry().getActionGroup( actionGroupId );
    96  			return actionGroup;
    97  		}
    98
    99  		public void perform( T target, Object param )
    100 		{
    101 			SoapUIActionGroup group = getActionGroup();
    102 			List<SoapUIActionMapping<T>> mappings = group.getActionMappings( target );
    103 			for( SoapUIActionMapping<T> mapping : mappings )
    104 			{
    105 				if( mapping.isDefault() )
    106 				{
    107 					((SoapUIAction<T>)mapping.getAction()).perform( target, param );
    108 				}
    109 			}
    110 		}
    111 	}
    112
    113 	public <T extends ModelItem> SoapUIAction<T> getAction( String soapUIActionId )
    114 	{
    115 		SoapUIAction soapUIAction = actions.get( soapUIActionId );
    116 		if( soapUIAction == null )
    117 			System.err.println( "Missing action [" + soapUIActionId + "]" );
    118 		return soapUIAction;
    119 	}
    120
    121 	public SoapUIActionRegistry( InputStream config )
    122 	{
    123 		// default actions
    124 		addAction( SeperatorAction.SOAPUI_ACTION_ID, SeperatorAction.INSTANCE );
    125
    126 		if( config != null )
    127 			addConfig( config, getClass().getClassLoader() );
    128 	}
    129
    130 	public void addConfig( InputStream config, ClassLoader classLoader )
    131 	{
    132 		try
    133 		{
    134 			SoapuiActionsDocumentConfig configDocument = SoapuiActionsDocumentConfig.Factory.parse( config );
    135 			SoapUIActionsConfig soapuiActions = configDocument.getSoapuiActions();
    136
    137 			for( SoapUIActionConfig action : soapuiActions.getActionList() )
    138 			{
    139 				try
    140 				{
    141 					String id = action.getId();
    142 					Class<?> actionClass = Class.forName( action.getActionClass(), true, classLoader );
    143
    144 					addAction( id, ( SoapUIAction ) actionClass.newInstance());
    145 				}
    146 				catch( Exception e )
    147 				{
    148 					System.err.println( "Error initializing Action: " + e );
    149 				}
    150 			}
    151
    152 			for( SoapUIActionGroupConfig group : soapuiActions.getActionGroupList() )
    153 			{
    154 				SoapUIActionGroup actionGroup = null;
    155
    156 				// modify existing?
    157 				if( actionGroups.containsKey( group.getId() ))
    158 				{
    159 					actionGroup = actionGroups.get( group.getId() );
    160 					addMappings( actionGroup, group );
    161 				}
    162 				else
    163 				{
    164 					if( group.isSetClass1())
    165 					{
    166 						Class<SoapUIActionGroup> actionGroupClass = ( Class<SoapUIActionGroup> ) Class.forName( group.getClass1() );
    167
    168 						Constructor<SoapUIActionGroup> constructor = actionGroupClass.getConstructor( new Class[] {String.class, String.class} );
    169 						if( constructor != null )
    170 						{
    171 							actionGroup = constructor.newInstance( new Object[] {group.getId(), group.getName()} );
    172 						}
    173 						else
    174 						{
    175 							actionGroup = actionGroupClass.newInstance();
    176 						}
    177 					}
    178 					else
    179 					{
    180 						actionGroup = new DefaultSoapUIActionGroup( group.getId(), group.getName() );
    181 					}
    182
    183 					addMappings( actionGroup, group );
    184 					actionGroups.put( group.getId(), actionGroup );
    185 				}
    186 			}
    187 		}
    188 		catch( Exception e )
    189 		{
    190 			SoapUI.logError( e );
    191 		}
    192 		finally
    193 		{
    194 			try
    195 			{
    196 				config.close();
    197 			}
    198 			catch( IOException e )
    199 			{
    200 				SoapUI.logError( e );
    201 			}
    202 		}
    203 	}
    204
    205 	private void addMappings( SoapUIActionGroup actionGroup, SoapUIActionGroupConfig groupConfig )
    206 	{
    207 		for( SoapUIActionMappingConfig mapping : groupConfig.getActionMappingList() )
    208 		{
    209 			try
    210 			{
    211 				int insertIndex = -1;
    212 				if( mapping.isSetPosition() && mapping.isSetPositionRef() )
    213 				{
    214 					insertIndex = actionGroup.getMappingIndex( mapping.getPositionRef() );
    215 					if( mapping.getPosition() == ActionMappingPositionTypeConfig.AFTER )
    216 						insertIndex++;
    217 				}
    218
    219 				if( mapping.isSetGroupId() )
    220 				{
    221 					SoapUIActionGroupAction actionListAction = new SoapUIActionGroupAction(
    222 								mapping.getName(), mapping.getDescription(), mapping.getGroupId() );
    223 					StandaloneActionMapping actionMapping = new StandaloneActionMapping( actionListAction );
    224
    225 					actionGroup.addMapping( mapping.getGroupId(), insertIndex, actionMapping);
    226
    227 					if( mapping.isSetName() )
    228 						actionMapping.setName( mapping.getName() );
    229
    230 					if( mapping.isSetDescription() )
    231 						actionMapping.setDescription( mapping.getDescription() );
    232 				}
    233 				else if( mapping.getActionId().equals( SeperatorAction.SOAPUI_ACTION_ID ))
    234 				{
    235 					actionGroup.addMapping( SeperatorAction.SOAPUI_ACTION_ID, insertIndex,
    236 								( SoapUIActionMapping ) SeperatorAction.getDefaultMapping() );
    237 				}
    238 				else
    239 				{
    240 					DefaultActionMapping actionMapping = new DefaultActionMapping( mapping.getActionId(), mapping.getKeyStroke(),
    241 																	mapping.getIconPath(), mapping.getActionId().equals( groupConfig.getDefault() ),
    242 																	mapping.getParam() );
    243 					actionGroup.addMapping(  mapping.getActionId(), insertIndex, actionMapping );
    244
    245 					if( mapping.isSetName() )
    246 						actionMapping.setName( mapping.getName() );
    247
    248 					if( mapping.isSetDescription() )
    249 						actionMapping.setDescription( mapping.getDescription() );
    250 				}
    251 			}
    252 			catch( Exception e )
    253 			{
    254 				System.err.println( "Error initializing ActionMapping: " + e );
    255 				SoapUI.logError( e );
    256 			}
    257 		}
    258 	}
    259
    260 	public <T extends ModelItem> SoapUIActionGroup<T> getActionGroup( String groupId )
    261 	{
    262 		return actionGroups.get( groupId );
    263 	}
    264 }