View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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  	@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, SoapUI.class.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 					SoapUI.logError( e );
149 					e.printStackTrace();
150 				}
151 			}
152 
153 			for( SoapUIActionGroupConfig group : soapuiActions.getActionGroupList() )
154 			{
155 				SoapUIActionGroup actionGroup = null;
156 
157 				// modify existing?
158 				if( actionGroups.containsKey( group.getId() ) )
159 				{
160 					actionGroup = actionGroups.get( group.getId() );
161 
162 					if( group.isSetClass1() )
163 					{
164 						actionGroup = createActionGroupClassFromConfig( group );
165 						actionGroups.put( group.getId(), actionGroup );
166 					}
167 
168 					addMappings( actionGroup, group );
169 				}
170 				else
171 				{
172 					if( group.isSetClass1() )
173 					{
174 						actionGroup = createActionGroupClassFromConfig( group );
175 					}
176 					else
177 					{
178 						actionGroup = new DefaultSoapUIActionGroup( group.getId(), group.getName() );
179 					}
180 
181 					addMappings( actionGroup, group );
182 					actionGroups.put( group.getId(), actionGroup );
183 				}
184 			}
185 		}
186 		catch( Exception e )
187 		{
188 			SoapUI.logError( e );
189 		}
190 		finally
191 		{
192 			try
193 			{
194 				config.close();
195 			}
196 			catch( IOException e )
197 			{
198 				SoapUI.logError( e );
199 			}
200 		}
201 	}
202 
203 	private SoapUIActionGroup createActionGroupClassFromConfig( SoapUIActionGroupConfig group )
204 			throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException,
205 			InvocationTargetException
206 	{
207 		SoapUIActionGroup actionGroup;
208 		Class<SoapUIActionGroup> actionGroupClass = ( Class<SoapUIActionGroup> )Class.forName( group.getClass1() );
209 
210 		Constructor<SoapUIActionGroup> constructor = actionGroupClass.getConstructor( new Class[] { String.class,
211 				String.class } );
212 		if( constructor != null )
213 		{
214 			actionGroup = constructor.newInstance( new Object[] { group.getId(), group.getName() } );
215 		}
216 		else
217 		{
218 			actionGroup = actionGroupClass.newInstance();
219 		}
220 		return actionGroup;
221 	}
222 
223 	private void addMappings( SoapUIActionGroup actionGroup, SoapUIActionGroupConfig groupConfig )
224 	{
225 		for( SoapUIActionMappingConfig mapping : groupConfig.getActionMappingList() )
226 		{
227 			try
228 			{
229 				int insertIndex = -1;
230 				if( mapping.isSetPosition() && mapping.isSetPositionRef() )
231 				{
232 					insertIndex = actionGroup.getMappingIndex( mapping.getPositionRef() );
233 					if( mapping.getPosition() == ActionMappingPositionTypeConfig.AFTER )
234 						insertIndex++ ;
235 				}
236 
237 				if( mapping.isSetGroupId() )
238 				{
239 					SoapUIActionGroupAction actionListAction = new SoapUIActionGroupAction( mapping.getName(), mapping
240 							.getDescription(), mapping.getGroupId() );
241 					StandaloneActionMapping actionMapping = new StandaloneActionMapping( actionListAction );
242 
243 					actionGroup.addMapping( mapping.getGroupId(), insertIndex, actionMapping );
244 
245 					if( mapping.isSetName() )
246 						actionMapping.setName( mapping.getName() );
247 
248 					if( mapping.isSetDescription() )
249 						actionMapping.setDescription( mapping.getDescription() );
250 				}
251 				else if( mapping.getActionId().equals( SeperatorAction.SOAPUI_ACTION_ID ) )
252 				{
253 					actionGroup.addMapping( SeperatorAction.SOAPUI_ACTION_ID, insertIndex,
254 							( SoapUIActionMapping )SeperatorAction.getDefaultMapping() );
255 				}
256 				else
257 				{
258 					DefaultActionMapping actionMapping = new DefaultActionMapping( mapping.getActionId(), mapping
259 							.getKeyStroke(), mapping.getIconPath(), mapping.getActionId().equals( groupConfig.getDefault() ),
260 							mapping.getParam() );
261 					actionGroup.addMapping( mapping.getActionId(), insertIndex, actionMapping );
262 
263 					if( mapping.isSetName() )
264 						actionMapping.setName( mapping.getName() );
265 
266 					if( mapping.isSetDescription() )
267 						actionMapping.setDescription( mapping.getDescription() );
268 				}
269 			}
270 			catch( Exception e )
271 			{
272 				System.err.println( "Error initializing ActionMapping: " + e );
273 				SoapUI.logError( e );
274 			}
275 		}
276 	}
277 
278 	public <T extends ModelItem> SoapUIActionGroup<T> getActionGroup( String groupId )
279 	{
280 		return actionGroups.get( groupId );
281 	}
282 }