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.support.action;
14  
15  import java.io.IOException;
16  import java.io.InputStream;
17  import java.lang.reflect.Constructor;
18  import java.lang.reflect.InvocationTargetException;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import com.eviware.soapui.SoapUI;
24  import com.eviware.soapui.config.ActionMappingPositionTypeConfig;
25  import com.eviware.soapui.config.SoapUIActionConfig;
26  import com.eviware.soapui.config.SoapUIActionGroupConfig;
27  import com.eviware.soapui.config.SoapUIActionMappingConfig;
28  import com.eviware.soapui.config.SoapUIActionsConfig;
29  import com.eviware.soapui.config.SoapuiActionsDocumentConfig;
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 = new DefaultActionMapping( SeperatorAction.SOAPUI_ACTION_ID,
63  				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 getDefaultMapping()
75  		{
76  			return defaultMapping;
77  		}
78  	}
79  
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 
161 					if( group.isSetClass1() )
162 					{
163 						actionGroup = createActionGroupClassFromConfig( group );
164 						actionGroups.put( group.getId(), actionGroup );
165 					}
166 
167 					addMappings( actionGroup, group );
168 				}
169 				else
170 				{
171 					if( group.isSetClass1() )
172 					{
173 						actionGroup = createActionGroupClassFromConfig( group );
174 					}
175 					else
176 					{
177 						actionGroup = new DefaultSoapUIActionGroup( group.getId(), group.getName() );
178 					}
179 
180 					addMappings( actionGroup, group );
181 					actionGroups.put( group.getId(), actionGroup );
182 				}
183 			}
184 		}
185 		catch( Exception e )
186 		{
187 			SoapUI.logError( e );
188 		}
189 		finally
190 		{
191 			try
192 			{
193 				config.close();
194 			}
195 			catch( IOException e )
196 			{
197 				SoapUI.logError( e );
198 			}
199 		}
200 	}
201 
202 	private SoapUIActionGroup createActionGroupClassFromConfig( SoapUIActionGroupConfig group )
203 			throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException,
204 			InvocationTargetException
205 	{
206 		SoapUIActionGroup actionGroup;
207 		Class<SoapUIActionGroup> actionGroupClass = ( Class<SoapUIActionGroup> )Class.forName( group.getClass1() );
208 
209 		Constructor<SoapUIActionGroup> constructor = actionGroupClass.getConstructor( new Class[] { String.class,
210 				String.class } );
211 		if( constructor != null )
212 		{
213 			actionGroup = constructor.newInstance( new Object[] { group.getId(), group.getName() } );
214 		}
215 		else
216 		{
217 			actionGroup = actionGroupClass.newInstance();
218 		}
219 		return actionGroup;
220 	}
221 
222 	private void addMappings( SoapUIActionGroup actionGroup, SoapUIActionGroupConfig groupConfig )
223 	{
224 		for( SoapUIActionMappingConfig mapping : groupConfig.getActionMappingList() )
225 		{
226 			try
227 			{
228 				int insertIndex = -1;
229 				if( mapping.isSetPosition() && mapping.isSetPositionRef() )
230 				{
231 					insertIndex = actionGroup.getMappingIndex( mapping.getPositionRef() );
232 					if( mapping.getPosition() == ActionMappingPositionTypeConfig.AFTER )
233 						insertIndex++ ;
234 				}
235 
236 				if( mapping.isSetGroupId() )
237 				{
238 					SoapUIActionGroupAction actionListAction = new SoapUIActionGroupAction( mapping.getName(), mapping
239 							.getDescription(), mapping.getGroupId() );
240 					StandaloneActionMapping actionMapping = new StandaloneActionMapping( actionListAction );
241 
242 					actionGroup.addMapping( mapping.getGroupId(), insertIndex, actionMapping );
243 
244 					if( mapping.isSetName() )
245 						actionMapping.setName( mapping.getName() );
246 
247 					if( mapping.isSetDescription() )
248 						actionMapping.setDescription( mapping.getDescription() );
249 				}
250 				else if( mapping.getActionId().equals( SeperatorAction.SOAPUI_ACTION_ID ) )
251 				{
252 					actionGroup.addMapping( SeperatorAction.SOAPUI_ACTION_ID, insertIndex,
253 							( SoapUIActionMapping )SeperatorAction.getDefaultMapping() );
254 				}
255 				else
256 				{
257 					DefaultActionMapping actionMapping = new DefaultActionMapping( mapping.getActionId(), mapping
258 							.getKeyStroke(), mapping.getIconPath(), mapping.getActionId().equals( groupConfig.getDefault() ),
259 							mapping.getParam() );
260 					actionGroup.addMapping( mapping.getActionId(), insertIndex, actionMapping );
261 
262 					if( mapping.isSetName() )
263 						actionMapping.setName( mapping.getName() );
264 
265 					if( mapping.isSetDescription() )
266 						actionMapping.setDescription( mapping.getDescription() );
267 				}
268 			}
269 			catch( Exception e )
270 			{
271 				System.err.println( "Error initializing ActionMapping: " + e );
272 				SoapUI.logError( e );
273 			}
274 		}
275 	}
276 
277 	public <T extends ModelItem> SoapUIActionGroup<T> getActionGroup( String groupId )
278 	{
279 		return actionGroups.get( groupId );
280 	}
281 }