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.model.ModelItem;
    30  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
    31  import com.eviware.soapui.support.action.support.DefaultActionMapping;
    32  import com.eviware.soapui.support.action.support.DefaultSoapUIActionGroup;
    33  import com.eviware.soapui.support.action.support.StandaloneActionMapping;
    34
    35  /***
    36   * Global SoapUIAction Registry
    37   *
    38   * @author ole.matzura
    39   */
    40
    41  @SuppressWarnings( "unchecked" )
    42  public class SoapUIActionRegistry
    43  {
    44  	private Map<String, SoapUIAction> actions = new HashMap<String, SoapUIAction>();
    45  	private Map<String, SoapUIActionGroup> actionGroups = new HashMap<String, SoapUIActionGroup>();
    46
    47  	public void addAction( String soapuiActionID, SoapUIAction action )
    48  	{
    49  		actions.put( soapuiActionID, action );
    50  	}
    51
    52     public void removeAction( String soapuiActionID )
    53     {
    54        actions.remove( soapuiActionID );
    55     }
    56
    57  	public static class SeperatorAction extends AbstractSoapUIAction
    58  	{
    59  		public static final String SOAPUI_ACTION_ID = "SeperatorAction";
    60  		public static SeperatorAction INSTANCE = new SeperatorAction();
    61  		private static SoapUIActionMapping defaultMapping =
    62  			new DefaultActionMapping( SeperatorAction.SOAPUI_ACTION_ID, null, null, false, null );
    63
    64  		private SeperatorAction()
    65  		{
    66  			super( null, null );
    67  		}
    68
    69  		public void perform( ModelItem target, Object param )
    70  		{
    71  		}
    72
    73  		public static SoapUIActionMapping getDefaultMapping()
    74  		{
    75  			return defaultMapping;
    76  		}
    77  	}
    78
    79  	@SuppressWarnings( "unused" )
    80  	public static class SoapUIActionGroupAction<T extends ModelItem> extends AbstractSoapUIAction<T>
    81  	{
    82  		private SoapUIActionGroup actionGroup;
    83  		private final String actionGroupId;
    84
    85  		public SoapUIActionGroupAction( String name, String description, String actionGroupId )
    86  		{
    87  			super( name, description );
    88  			this.actionGroupId = actionGroupId;
    89  		}
    90
    91  		public SoapUIActionGroup getActionGroup()
    92  		{
    93  			if( actionGroup == null )
    94  				actionGroup = SoapUI.getActionRegistry().getActionGroup( actionGroupId );
    95  			return actionGroup;
    96  		}
    97
    98  		public void perform( T target, Object param )
    99  		{
    100 			SoapUIActionGroup group = getActionGroup();
    101 			List<SoapUIActionMapping<T>> mappings = group.getActionMappings( target );
    102 			for( SoapUIActionMapping<T> mapping : mappings )
    103 			{
    104 				if( mapping.isDefault() )
    105 				{
    106 					((SoapUIAction<T>)mapping.getAction()).perform( target, param );
    107 				}
    108 			}
    109 		}
    110 	}
    111
    112 	public <T extends ModelItem> SoapUIAction<T> getAction( String soapUIActionId )
    113 	{
    114 		SoapUIAction soapUIAction = actions.get( soapUIActionId );
    115 		if( soapUIAction == null )
    116 			System.err.println( "Missing action [" + soapUIActionId + "]" );
    117 		return soapUIAction;
    118 	}
    119
    120 	public SoapUIActionRegistry( InputStream config )
    121 	{
    122 		// default actions
    123 		addAction( SeperatorAction.SOAPUI_ACTION_ID, SeperatorAction.INSTANCE );
    124
    125 		if( config != null )
    126 			addConfig( config, SoapUI.class.getClassLoader() );
    127 	}
    128
    129 	public void addConfig( InputStream config, ClassLoader classLoader )
    130 	{
    131 		try
    132 		{
    133 			SoapuiActionsDocumentConfig configDocument = SoapuiActionsDocumentConfig.Factory.parse( config );
    134 			SoapUIActionsConfig soapuiActions = configDocument.getSoapuiActions();
    135
    136 			for( SoapUIActionConfig action : soapuiActions.getActionList() )
    137 			{
    138 				try
    139 				{
    140 					String id = action.getId();
    141 					Class<?> actionClass = Class.forName( action.getActionClass(), true, classLoader );
    142
    143 					addAction( id, ( SoapUIAction ) actionClass.newInstance());
    144 				}
    145 				catch( Exception e )
    146 				{
    147 					SoapUI.logError( e );
    148 					e.printStackTrace();
    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 }