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