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