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