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.wsimport;
    14
    15  import java.io.File;
    16  import java.io.IOException;
    17
    18  import com.eviware.soapui.SoapUI;
    19  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.AbstractToolsAction;
    20  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ArgumentBuilder;
    21  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ProcessToolRunner;
    22  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
    23  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
    24  import com.eviware.soapui.model.iface.Interface;
    25  import com.eviware.soapui.settings.ToolsSettings;
    26  import com.eviware.soapui.support.Tools;
    27  import com.eviware.soapui.support.UISupport;
    28  import com.eviware.soapui.support.types.StringToStringMap;
    29  import com.eviware.x.form.XForm;
    30  import com.eviware.x.form.XFormDialog;
    31  import com.eviware.x.form.XFormDialogBuilder;
    32  import com.eviware.x.form.XFormFactory;
    33
    34  /***
    35   * Invokes JAX-WS WSImport
    36   *
    37   * @author Ole.Matzura
    38   */
    39
    40  public class WSImportAction extends AbstractToolsAction<Interface>
    41  {
    42  	private static final String PACKAGE = "Package";
    43  	private static final String OUTPUT = "Target Directory";
    44  	private static final String SOURCE_OUTPUT = "Source Directory";
    45  	private static final String WSDLLOCATION = "wsdlLocation";
    46  	private static final String BINDING_FILES = "Binding files";
    47  	private static final String HTTPPROXY = "HTTP Proxy";
    48  	private static final String KEEP = "Keep";
    49  	private static final String CATALOG = "Catalog";
    50  	public static final String SOAPUI_ACTION_ID = "WSImportAction";
    51
    52  	public WSImportAction()
    53     {
    54        super( "JAX-WS Artifacts", "Generates JAX-WS artifacts using wsimport");
    55     }
    56
    57  	protected XFormDialog buildDialog(Interface modelItem)
    58  	{
    59        XFormDialogBuilder builder = XFormFactory.createDialogBuilder("JAX-WS Artifacts");
    60
    61  		XForm mainForm = builder.createForm( "Basic" );
    62  		addWSDLFields( mainForm, modelItem );
    63
    64  		mainForm.addTextField( OUTPUT, "target directory for generated files.", XForm.FieldType.PROJECT_FOLDER );
    65  		mainForm.addTextField( PACKAGE, "target package nam", XForm.FieldType.JAVA_PACKAGE );
    66  		mainForm.addTextField( SOURCE_OUTPUT, "target directory for generated source files", XForm.FieldType.PROJECT_FOLDER );
    67  		mainForm.addTextField( HTTPPROXY, "HTTP Proxy-server", XForm.FieldType.TEXT );
    68  		mainForm.addTextField( CATALOG, "catalog file to resolve external entity references", XForm.FieldType.PROJECT_FILE );
    69  		mainForm.addTextField( BINDING_FILES, "comma-separated list of external JAX-WS or JAXB binding files", XForm.FieldType.TEXT );
    70  		mainForm.addTextField( WSDLLOCATION, "@WebService.wsdlLocation and @WebServiceClient.wsdlLocation value", XForm.FieldType.TEXT );
    71
    72  		mainForm.addCheckBox( KEEP, "(keep generated files)" );
    73
    74        buildArgsForm( builder, false, "WSImport" );
    75
    76  		return builder.buildDialog(  buildDefaultActions(HelpUrls.WSIMPORT_HELP_URL, modelItem),
    77        		"Specify arguments for JWSDP/JAX-WS wsimport", UISupport.TOOL_ICON );
    78  	}
    79
    80  	protected void generate(StringToStringMap values, ToolHost toolHost ,Interface modelItem) throws Exception
    81  	{
    82  		String wsimportDir = SoapUI.getSettings().getString( ToolsSettings.JWSDP_WSIMPORT_LOCATION, null );
    83  		if( Tools.isEmpty( wsimportDir ))
    84  		{
    85  			UISupport.showErrorMessage( "wsimport directory must be set in global preferences" );
    86  			return;
    87  		}
    88
    89  		String wsimportExtension = UISupport.isWindows() ? ".bat" : ".sh";
    90
    91  		File wscompileFile = new File( wsimportDir + File.separatorChar + "wsimport" + wsimportExtension );
    92  		if( !wscompileFile.exists() )
    93  		{
    94  			UISupport.showErrorMessage( "Could not find wsimport script at [" + wscompileFile + "]" );
    95  			return;
    96  		}
    97
    98  		ProcessBuilder builder = new ProcessBuilder();
    99  		ArgumentBuilder args = buildArgs( UISupport.isWindows(), modelItem );
    100 		builder.command(args.getArgs());
    101 		builder.directory(new File(wsimportDir));
    102
    103 		toolHost.run( new ProcessToolRunner( builder, "JAX-WS wsimport", modelItem ));
    104 	}
    105
    106 	private ArgumentBuilder buildArgs( boolean isWindows, Interface modelItem ) throws IOException
    107 	{
    108 		StringToStringMap values = dialog.getValues();
    109 		values.put( OUTPUT, Tools.ensureDir( values.get( OUTPUT ), "" ));
    110 		values.put( SOURCE_OUTPUT, Tools.ensureDir( values.get( SOURCE_OUTPUT ), values.get( OUTPUT )));
    111
    112 		ArgumentBuilder builder = new ArgumentBuilder( values );
    113 		builder.startScript( "wsimport" );
    114
    115 		builder.addString( OUTPUT, "-d" );
    116 		builder.addString( CATALOG, "-catalog" );
    117 		builder.addString( HTTPPROXY, "-httpproxy", ":" );
    118 		builder.addString( PACKAGE, "-p" );
    119 		builder.addString( SOURCE_OUTPUT, "-s" );
    120 		builder.addString( WSDLLOCATION, "-wsdllocation" );
    121 		builder.addBoolean( KEEP, "-keep" );
    122
    123 		String [] bindingFiles = values.get( BINDING_FILES ).split( "," );
    124 		for( String file : bindingFiles )
    125 		{
    126 			if( file.trim().length() > 0 )
    127 				builder.addArgs( "-b", file.trim() );
    128 		}
    129
    130 		builder.addArgs( "-verbose" );
    131 		addToolArgs( values, builder );
    132 		builder.addArgs( getWsdlUrl( values, modelItem ));
    133
    134 		return builder;
    135 	}
    136 }