View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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.impl.wsdl.actions.iface.tools.support;
14  
15  import java.awt.event.ActionEvent;
16  import java.io.File;
17  import java.io.FilenameFilter;
18  
19  import javax.swing.AbstractAction;
20  import javax.swing.Action;
21  
22  import org.apache.log4j.Logger;
23  
24  import com.eviware.soapui.SoapUI;
25  import com.eviware.soapui.actions.SoapUIPreferencesAction;
26  import com.eviware.soapui.impl.support.AbstractInterface;
27  import com.eviware.soapui.impl.support.actions.ShowOnlineHelpAction;
28  import com.eviware.soapui.impl.wsdl.WsdlInterface;
29  import com.eviware.soapui.impl.wsdl.support.PathUtils;
30  import com.eviware.soapui.model.ModelItem;
31  import com.eviware.soapui.model.iface.Interface;
32  import com.eviware.soapui.settings.ProjectSettings;
33  import com.eviware.soapui.support.StringUtils;
34  import com.eviware.soapui.support.Tools;
35  import com.eviware.soapui.support.UISupport;
36  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
37  import com.eviware.soapui.support.action.swing.ActionList;
38  import com.eviware.soapui.support.action.swing.DefaultActionList;
39  import com.eviware.soapui.support.types.StringToStringMap;
40  import com.eviware.x.form.XForm;
41  import com.eviware.x.form.XFormDialog;
42  import com.eviware.x.form.XFormDialogBuilder;
43  import com.eviware.x.form.XFormField;
44  import com.eviware.x.form.XFormTextField;
45  
46  /***
47   * Abstract base class for Tool Actions
48   * 
49   * @author Ole.Matzura
50   */
51  
52  public abstract class AbstractToolsAction<T extends ModelItem> extends AbstractSoapUIAction<T>
53  {
54     @SuppressWarnings("unused")
55  	private static final Logger log = Logger.getLogger(AbstractToolsAction.class);
56     
57  	protected static final String WSDL = "WSDL";
58  	protected static final String CACHED_WSDL = "Use cached WSDL";
59  	protected static final String JAVA_ARGS = "Java Args";
60  	protected static final String TOOL_ARGS = "Tool Args";
61  
62  	protected XFormDialog dialog;
63  	protected String valuesSettingID;
64  	private XFormField useCached;
65  	private T modelItem;
66  
67  	// Configure behavior of this action:
68  	private boolean fixedWSDL = false;
69  	private Action toolsSettingsAction = new ShowIntegratedToolsSettingsAction();;
70  
71  	public AbstractToolsAction( String name, String description)
72  	{
73  		super(name, description);
74  	}
75  
76  	public String getValuesSettingID()
77  	{
78  		return valuesSettingID;
79  	}
80  
81  	public void setValuesSettingID(String valuesSettingID)
82  	{
83  		this.valuesSettingID = valuesSettingID;
84  	}
85  
86  	/***
87  	 * Set this to true to not let the user edit the WSDL.
88  	 * 
89  	 * @param b
90  	 */
91  	public void setFixedWSDL(boolean b)
92  	{
93  		this.fixedWSDL = b;
94  	}
95  	
96  	public T getModelItem()
97  	{
98  		return modelItem;
99  	}
100 
101 	public void perform( T target, Object param )
102 	{
103 		this.valuesSettingID = this.getClass().getName() + "@values";
104 		if (target == null)
105 			this.valuesSettingID += "-global";
106 		else
107 			this.valuesSettingID += "-local";
108 		
109 		modelItem = target;
110 		
111 		// Could reuse the dialog in Swing, but not in Eclipse.
112 		// if( dialog == null )
113 		dialog = buildDialog( ( T ) target );
114 
115 		if (dialog == null)
116 		{
117 			try
118 			{
119 				generate(initValues( ( T ) target, param ), UISupport.getToolHost(), ( T ) target );
120 			}
121 			catch (Exception e1)
122 			{
123 				UISupport.showErrorMessage(e1);
124 			}
125 		}
126 		else
127 		{
128 			StringToStringMap values = initValues( ( T ) target, param );
129 
130 			dialog.setValues(values);
131 			dialog.setVisible(true);
132 		}
133 	}
134 
135 	protected StringToStringMap initValues( T modelItem, Object param )
136 	{
137 		String settingValues = modelItem == null ? SoapUI.getSettings().getString(valuesSettingID, null) : modelItem
138 				.getSettings().getString(valuesSettingID, null);
139 
140 		StringToStringMap result = settingValues == null ? new StringToStringMap() : StringToStringMap
141 				.fromXml(settingValues);
142 
143 		if (modelItem instanceof WsdlInterface)
144 		{
145 			initWSDL(result, (WsdlInterface) modelItem);
146 		}
147 
148 		if (dialog != null && modelItem != null)
149 		{
150 			String projectRoot = modelItem.getSettings().getString( ProjectSettings.PROJECT_ROOT, null);
151 			if( projectRoot != null ) 
152 				dialog.setFormFieldProperty(ProjectSettings.PROJECT_ROOT, projectRoot);
153 		}
154 
155 		return result;
156 	}
157 
158 	protected XFormDialog buildDialog( T modelItem )
159 	{
160 		return null;
161 	}
162 
163 	protected void addWSDLFields(XForm mainForm, T modelItem )
164 	{
165 		if (!fixedWSDL)
166 		{
167 			XFormTextField tf = mainForm.addTextField(WSDL, "url to wsdl", XForm.FieldType.URL);
168 
169 			if (modelItem instanceof Interface)
170 			{
171 				useCached = mainForm.addCheckBox(CACHED_WSDL, null);
172 				useCached.addComponentEnabler(tf, "false");
173 			}
174 		}
175 		else
176 		{
177 			if (modelItem instanceof Interface)
178 			{
179 				useCached = mainForm.addCheckBox(CACHED_WSDL, null);
180 			}
181 		}
182 	}
183 
184 	protected void initWSDL(StringToStringMap values, WsdlInterface iface)
185 	{
186 		boolean cached = iface.isCached();
187       if (useCached != null)
188 			useCached.setEnabled(cached);
189 
190 		if (!values.containsKey(CACHED_WSDL))
191 			values.put(CACHED_WSDL, Boolean.toString(cached));
192 
193 		if (values.getBoolean(CACHED_WSDL) || !values.hasValue(WSDL))
194 			values.put(WSDL, PathUtils.expandPath(iface.getDefinition(), iface) );
195 	}
196 
197 	protected abstract void generate(StringToStringMap values, ToolHost toolHost, T modelItem ) throws Exception;
198 
199 	public void run(ToolHost toolHost, T modelItem, Object param) throws Exception
200 	{
201 		generate(initValues( modelItem, param ), toolHost, modelItem );
202 	}
203 
204 	/***
205 	 * To be overridden..
206 	 */
207 
208 	public void onClose( T modelItem )
209 	{
210 		if (dialog == null)
211 			return;
212 
213 		if (modelItem == null)
214 		{
215 			SoapUI.getSettings().setString(valuesSettingID, dialog.getValues().toXml());
216 		}
217 		else
218 		{
219 			modelItem.getSettings().setString(valuesSettingID, dialog.getValues().toXml());
220 		}
221 	}
222 
223 	protected String getWsdlUrl(StringToStringMap values, T modelItem)
224 	{
225 		String wsdl = values.get(WSDL);
226 		boolean useCached = values.getBoolean(CACHED_WSDL);
227 
228 		if( modelItem instanceof AbstractInterface )
229 		{
230 			AbstractInterface<?> iface = (AbstractInterface<?>) modelItem;
231 		
232 			boolean hasDefinition = StringUtils.hasContent(iface.getDefinition());
233 			if (wsdl == null && !useCached && hasDefinition)
234 			{
235 				return PathUtils.expandPath( iface.getDefinition(), iface );
236 			}
237 		
238 			if (!hasDefinition || (useCached && iface.getDefinitionContext().isCached()))
239 			{
240 				try
241 				{
242 					File tempFile = File.createTempFile("tempdir", null);
243 					String path = tempFile.getAbsolutePath();
244 					tempFile.delete();
245 					wsdl = iface.getDefinitionContext().export( path );
246 					
247 	//				CachedWsdlLoader loader = (CachedWsdlLoader) iface.createWsdlLoader();
248 	//				wsdl = loader.saveDefinition(path);
249 				}
250 				catch (Exception e)
251 				{
252 					SoapUI.logError( e );
253 				}
254 			}
255 		}
256 
257 		return wsdl;
258 	}
259 
260 	protected String buildClasspath(File jarDir)
261 	{
262 		String[] jars = jarDir.list(new FilenameFilter()
263 		{
264 
265 			public boolean accept(File dir, String name)
266 			{
267 				return name.endsWith(".jar");
268 			}
269 		});
270 
271 		StringBuilder classpath = new StringBuilder();
272 
273 		for (int c = 0; c < jars.length; c++)
274 		{
275 			if (c > 0)
276 				classpath.append(File.pathSeparatorChar);
277 
278 			classpath.append(jars[c]);
279 		}
280 		return classpath.toString();
281 	}
282 
283 	protected ActionList buildDefaultActions(String helpUrl, T modelItem )
284 	{
285 		ActionList actions = new DefaultActionList("Actions");
286 
287 		if (helpUrl != null)
288 		{
289 			actions.addAction(new ShowOnlineHelpAction(helpUrl));
290 			actions.addSeparator();
291 		}
292 
293 		Action runAction = createRunOption( modelItem );
294 		actions.addAction(runAction);
295 		actions.setDefaultAction( runAction );
296 		actions.addAction(new CloseAction( modelItem ));
297 		
298 		if( toolsSettingsAction != null)
299 			actions.addAction(toolsSettingsAction);
300 
301 		return actions;
302 	}
303 	
304 	public Action getToolsSettingsAction()
305 	{
306 		return toolsSettingsAction;
307 	}
308 
309 	public void setToolsSettingsAction(Action toolsSettingsAction)
310 	{
311 		this.toolsSettingsAction = toolsSettingsAction;
312 	}
313 
314 	protected Action createRunOption( T modelItem )
315 	{
316 		return new GenerateAction( modelItem );
317 	}
318 
319 	protected String getDefinition( T modelItem )
320 	{
321 		if (modelItem == null)
322 			return "";
323 		WsdlInterface iface = (WsdlInterface) modelItem;
324 		String definition = PathUtils.expandPath(iface.getDefinition(), iface );
325 		if (definition.startsWith("file:"))
326 			definition = definition.substring(5);
327 
328 		return definition;
329 	}
330 
331 	protected void addJavaArgs(StringToStringMap values, ArgumentBuilder builder)
332 	{
333 		String[] javaArgs = Tools.tokenizeArgs(values.get(JAVA_ARGS));
334 		if (javaArgs != null)
335 			builder.addArgs(javaArgs);
336 	}
337 
338 	protected void addToolArgs(StringToStringMap values, ArgumentBuilder builder)
339 	{
340 		String[] toolArgs = Tools.tokenizeArgs(values.get(TOOL_ARGS));
341 		if (toolArgs != null)
342 			builder.addArgs(toolArgs);
343 	}
344 
345 	protected XForm buildArgsForm(XFormDialogBuilder builder, boolean addJavaArgs, String toolName)
346 	{
347 		XForm argsForm = builder.createForm("Custom Args");
348 		if (addJavaArgs)
349 			argsForm.addTextField(JAVA_ARGS, "additional arguments to java", XForm.FieldType.TEXT);
350 
351 		argsForm.addTextField(TOOL_ARGS, "additional arguments to " + toolName, XForm.FieldType.TEXT);
352 		return argsForm;
353 	}
354 
355 	public static final class ShowIntegratedToolsSettingsAction extends AbstractAction
356 	{
357 		public ShowIntegratedToolsSettingsAction()
358 		{
359 			super("Tools");
360 		}
361 
362 		public void actionPerformed(ActionEvent e)
363 		{
364 			SoapUIPreferencesAction.getInstance().show(SoapUIPreferencesAction.INTEGRATED_TOOLS);
365 		}
366 	}
367 
368 	protected final class CloseAction extends AbstractAction
369 	{
370 		private final T modelItem;
371 
372 		public CloseAction( T modelItem )
373 		{
374 			super("Close");
375 			this.modelItem = modelItem;
376 		}
377 
378 		public void actionPerformed(ActionEvent e)
379 		{
380 			closeDialog( modelItem );
381 		}
382 	}
383 
384 	public void closeDialog( T modelItem )
385 	{
386 		onClose( modelItem );
387 		if (dialog != null)
388 			dialog.setVisible(false);
389 	}
390 
391 	protected final class GenerateAction extends AbstractAction
392 	{
393 		private final T modelItem;
394 
395 		public GenerateAction(T modelItem)
396 		{
397 			super("Generate");
398 			this.modelItem = modelItem;
399 		}
400 
401 		public void actionPerformed(ActionEvent e)
402 		{
403 			try
404 			{
405 				if (dialog.validate())
406 				{
407 					generate(dialog.getValues(), UISupport.getToolHost(), modelItem );
408 				}
409 			}
410 			catch (Exception e1)
411 			{
412 				UISupport.showErrorMessage(e1);
413 			}
414 		}
415 	}
416 }