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