View Javadoc

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