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.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
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
112
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 /***
136 * Perform an
137 *
138 * @param target
139 * @param param
140 */
141 public void performHeadless( T target, Object param )
142 {
143 this.valuesSettingID = this.getClass().getName() + "@values";
144 if( target == null )
145 this.valuesSettingID += "-global";
146 else
147 this.valuesSettingID += "-local";
148
149 modelItem = target;
150
151 try
152 {
153 generate( initValues( ( T )target, param ), UISupport.getToolHost(), ( T )target );
154 }
155 catch( Exception e1 )
156 {
157 UISupport.showErrorMessage( e1 );
158 }
159 }
160
161
162 protected StringToStringMap initValues( T modelItem, Object param )
163 {
164 String settingValues = modelItem == null ? SoapUI.getSettings().getString( valuesSettingID, null ) : modelItem
165 .getSettings().getString( valuesSettingID, null );
166
167 StringToStringMap result = settingValues == null ? new StringToStringMap() : StringToStringMap
168 .fromXml( settingValues );
169
170 if( modelItem instanceof WsdlInterface )
171 {
172 initWSDL( result, ( WsdlInterface )modelItem );
173 }
174
175 if( dialog != null && modelItem != null )
176 {
177 String projectRoot = modelItem.getSettings().getString( ProjectSettings.PROJECT_ROOT, null );
178 if( projectRoot != null )
179 dialog.setFormFieldProperty( ProjectSettings.PROJECT_ROOT, projectRoot );
180 }
181
182 return result;
183 }
184
185 protected XFormDialog buildDialog( T modelItem )
186 {
187 return null;
188 }
189
190 protected void addWSDLFields( XForm mainForm, T modelItem )
191 {
192 if( !fixedWSDL )
193 {
194 XFormTextField tf = mainForm.addTextField( WSDL, "url to wsdl", XForm.FieldType.URL );
195
196 if( modelItem instanceof Interface )
197 {
198 useCached = mainForm.addCheckBox( CACHED_WSDL, null );
199 useCached.addComponentEnabler( tf, "false" );
200 }
201 }
202 else
203 {
204 if( modelItem instanceof Interface )
205 {
206 useCached = mainForm.addCheckBox( CACHED_WSDL, null );
207 }
208 }
209 }
210
211 protected void initWSDL( StringToStringMap values, WsdlInterface iface )
212 {
213 boolean cached = iface.isCached();
214 if( useCached != null )
215 useCached.setEnabled( cached );
216
217 if( !values.containsKey( CACHED_WSDL ) )
218 values.put( CACHED_WSDL, Boolean.toString( cached ) );
219
220 if( values.getBoolean( CACHED_WSDL ) || !values.hasValue( WSDL ) )
221 values.put( WSDL, PathUtils.expandPath( iface.getDefinition(), iface ) );
222 }
223
224 protected abstract void generate( StringToStringMap values, ToolHost toolHost, T modelItem ) throws Exception;
225
226 public void run( ToolHost toolHost, T modelItem, Object param ) throws Exception
227 {
228 generate( initValues( modelItem, param ), toolHost, modelItem );
229 }
230
231 /***
232 * To be overridden..
233 */
234
235 public void onClose( T modelItem )
236 {
237 if( dialog == null )
238 return;
239
240 if( modelItem == null )
241 {
242 SoapUI.getSettings().setString( valuesSettingID, dialog.getValues().toXml() );
243 }
244 else
245 {
246 modelItem.getSettings().setString( valuesSettingID, dialog.getValues().toXml() );
247 }
248 }
249
250 protected String getWsdlUrl( StringToStringMap values, T modelItem )
251 {
252 String wsdl = values.get( WSDL );
253 boolean useCached = values.getBoolean( CACHED_WSDL );
254
255 if( modelItem instanceof AbstractInterface )
256 {
257 AbstractInterface<?> iface = ( AbstractInterface<?> )modelItem;
258
259 boolean hasDefinition = StringUtils.hasContent( iface.getDefinition() );
260 if( wsdl == null && !useCached && hasDefinition )
261 {
262 return PathUtils.expandPath( iface.getDefinition(), iface );
263 }
264
265 if( !hasDefinition || ( useCached && iface.getDefinitionContext().isCached() ) )
266 {
267 try
268 {
269 File tempFile = File.createTempFile( "tempdir", null );
270 String path = tempFile.getAbsolutePath();
271 tempFile.delete();
272 wsdl = iface.getDefinitionContext().export( path );
273
274
275
276
277 }
278 catch( Exception e )
279 {
280 SoapUI.logError( e );
281 }
282 }
283 }
284
285 return wsdl;
286 }
287
288 protected String buildClasspath( File jarDir )
289 {
290 String[] jars = jarDir.list( new FilenameFilter()
291 {
292
293 public boolean accept( File dir, String name )
294 {
295 return name.endsWith( ".jar" );
296 }
297 } );
298
299 StringBuilder classpath = new StringBuilder();
300
301 for( int c = 0; c < jars.length; c++ )
302 {
303 if( c > 0 )
304 classpath.append( File.pathSeparatorChar );
305
306 classpath.append( jars[c] );
307 }
308 return classpath.toString();
309 }
310
311 protected ActionList buildDefaultActions( String helpUrl, T modelItem )
312 {
313 ActionList actions = new DefaultActionList( "Actions" );
314
315 if( helpUrl != null )
316 {
317 actions.addAction( new ShowOnlineHelpAction( helpUrl ) );
318 actions.addSeparator();
319 }
320
321 Action runAction = createRunOption( modelItem );
322 actions.addAction( runAction );
323 actions.setDefaultAction( runAction );
324 actions.addAction( new CloseAction( modelItem ) );
325
326 if( toolsSettingsAction != null )
327 actions.addAction( toolsSettingsAction );
328
329 return actions;
330 }
331
332 public Action getToolsSettingsAction()
333 {
334 return toolsSettingsAction;
335 }
336
337 public void setToolsSettingsAction( Action toolsSettingsAction )
338 {
339 this.toolsSettingsAction = toolsSettingsAction;
340 }
341
342 protected Action createRunOption( T modelItem )
343 {
344 return new GenerateAction( modelItem );
345 }
346
347 protected String getDefinition( T modelItem )
348 {
349 if( modelItem == null )
350 return "";
351 WsdlInterface iface = ( WsdlInterface )modelItem;
352 String definition = PathUtils.expandPath( iface.getDefinition(), iface );
353 if( definition.startsWith( "file:" ) )
354 definition = definition.substring( 5 );
355
356 return definition;
357 }
358
359 protected void addJavaArgs( StringToStringMap values, ArgumentBuilder builder )
360 {
361 String[] javaArgs = Tools.tokenizeArgs( values.get( JAVA_ARGS ) );
362 if( javaArgs != null )
363 builder.addArgs( javaArgs );
364 }
365
366 protected void addToolArgs( StringToStringMap values, ArgumentBuilder builder )
367 {
368 String[] toolArgs = Tools.tokenizeArgs( values.get( TOOL_ARGS ) );
369 if( toolArgs != null )
370 builder.addArgs( toolArgs );
371 }
372
373 protected XForm buildArgsForm( XFormDialogBuilder builder, boolean addJavaArgs, String toolName )
374 {
375 XForm argsForm = builder.createForm( "Custom Args" );
376 if( addJavaArgs )
377 argsForm.addTextField( JAVA_ARGS, "additional arguments to java", XForm.FieldType.TEXT );
378
379 argsForm.addTextField( TOOL_ARGS, "additional arguments to " + toolName, XForm.FieldType.TEXT );
380 return argsForm;
381 }
382
383 public static final class ShowIntegratedToolsSettingsAction extends AbstractAction
384 {
385 public ShowIntegratedToolsSettingsAction()
386 {
387 super( "Tools" );
388 }
389
390 public void actionPerformed( ActionEvent e )
391 {
392 SoapUIPreferencesAction.getInstance().show( SoapUIPreferencesAction.INTEGRATED_TOOLS );
393 }
394 }
395
396 protected final class CloseAction extends AbstractAction
397 {
398 private final T modelItem;
399
400 public CloseAction( T modelItem )
401 {
402 super( "Close" );
403 this.modelItem = modelItem;
404 }
405
406 public void actionPerformed( ActionEvent e )
407 {
408 closeDialog( modelItem );
409 }
410 }
411
412 public void closeDialog( T modelItem )
413 {
414 onClose( modelItem );
415 if( dialog != null )
416 dialog.setVisible( false );
417 }
418
419 protected final class GenerateAction extends AbstractAction
420 {
421 private final T modelItem;
422
423 public GenerateAction( T modelItem )
424 {
425 super( "Generate" );
426 this.modelItem = modelItem;
427 }
428
429 public void actionPerformed( ActionEvent e )
430 {
431 try
432 {
433 if( dialog.validate() )
434 {
435 generate( dialog.getValues(), UISupport.getToolHost(), modelItem );
436 }
437 }
438 catch( Exception e1 )
439 {
440 UISupport.showErrorMessage( e1 );
441 }
442 }
443 }
444 }