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.cxf;
    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 Apache CXF wsdl2java utility
    36   *
    37   * @author Ole.Matzura
    38   */
    39
    40  public class CXFAction extends AbstractToolsAction<Interface>
    41  {
    42  	private static final String PACKAGE = "Package";
    43  	private static final String OUTPUT = "Output Directory";
    44  	private static final String NAMESPACE_MAPPING = "Output Packages";
    45  	private static final String CATALOG_FILE = "Catalog";
    46  	private static final String SERVER_STUBS = "Server";
    47  	private static final String CLIENT_STUBS = "Client";
    48  	private static final String IMPL_STUBS = "Implementation";
    49  	private static final String ANT_FILE = "build.xml";
    50  	private static final String GENERATE_ALL = "All Code";
    51  	private static final String COMPILE = "Compile";
    52  	private static final String CLASSDIR = "Class Folder";
    53  	private static final String VALIDATE = "Validate WSDL";
    54  	private static final String EXCLUDE_NAMESPACES = "Exclude namespaces";
    55  	private static final String EXSH = "EXSH";
    56  	private static final String DNS = "DNS";
    57  	private static final String DEX = "DEX";
    58
    59  	public static final String SOAPUI_ACTION_ID = "CXFAction";
    60  	private static final String BINDING_FILES = "Bindings";
    61
    62     public CXFAction()
    63     {
    64        super("Apache CXF", "Generates Apache CXF code using the wsdl2java utility");
    65     }
    66
    67     protected XFormDialog buildDialog(Interface modelItem)
    68  	{
    69        XFormDialogBuilder builder = XFormFactory.createDialogBuilder("Apache CXF Stubs");
    70
    71  		XForm mainForm = builder.createForm( "Basic" );
    72  		addWSDLFields(mainForm, modelItem);
    73
    74  		mainForm.addTextField( OUTPUT, "Root directory for all emitted files.", XForm.FieldType.PROJECT_FOLDER );
    75  		mainForm.addTextField( PACKAGE, "Default Package for generated classes", XForm.FieldType.JAVA_PACKAGE );
    76  		mainForm.addNameSpaceTable( NAMESPACE_MAPPING, modelItem );
    77
    78  		mainForm.addCheckBox( CLIENT_STUBS, "Generates starting point code for a client mainline" );
    79  		mainForm.addCheckBox( SERVER_STUBS, "Generates starting point code for a server mainline" );
    80  		mainForm.addCheckBox( IMPL_STUBS, "Generates starting point code for an implementation object" );
    81  		mainForm.addCheckBox( ANT_FILE, "Generates the Ant build.xml file" );
    82  		mainForm.addCheckBox( GENERATE_ALL, "<html>Generates all starting point code: types, <br>service proxy, service interface, server mainline, " +
    83  										"<br>client mainline, implementation object, and an Ant build.xml file</html>" );
    84
    85  		XForm advForm = builder.createForm( "Advanced" );
    86
    87  		advForm.addTextField( BINDING_FILES, "Space-separated list of JAXWS or JAXB binding files", XForm.FieldType.TEXT );
    88  		advForm.addCheckBox( COMPILE, "Compiles generated Java files" );
    89  		advForm.addTextField( CLASSDIR, "The directory into which the compiled class files are written", XForm.FieldType.FOLDER );
    90
    91  		advForm.addTextField( CATALOG_FILE, "The catalog file to map the imported wsdl/schema", XForm.FieldType.FILE );
    92
    93  		advForm.addNameSpaceTable( EXCLUDE_NAMESPACES, modelItem );
    94  		advForm.addCheckBox( EXSH, "Enables processing of extended soap header message binding" );
    95  		advForm.addCheckBox( DNS, "Enables loading of the default namespace package name mapping" );
    96  		advForm.addCheckBox( DEX, "Enables loading of the default excludes namespace mapping" );
    97  		advForm.addCheckBox( VALIDATE, "Enables validating the WSDL before generating the code" );
    98
    99  		buildArgsForm( builder, true, "wsdl2java");
    100
    101 		return builder.buildDialog( buildDefaultActions(HelpUrls.CXFWSDL2JAVA_HELP_URL, modelItem),
    102       		"Specify arguments for Apache CXF wsdl2java", UISupport.TOOL_ICON );
    103 	}
    104
    105 	protected StringToStringMap initValues(Interface modelItem, Object param)
    106 	{
    107 		StringToStringMap values = super.initValues(modelItem, param);
    108 		values.putIfMissing( DNS, "true" );
    109 		values.putIfMissing( DEX, "true" );
    110 		return values;
    111 	}
    112
    113 	protected void generate(StringToStringMap values, ToolHost toolHost, Interface modelItem) throws Exception
    114 	{
    115 		String xfireDir = SoapUI.getSettings().getString( ToolsSettings.CXF_LOCATION, null );
    116 		if( Tools.isEmpty( xfireDir ))
    117 		{
    118 			UISupport.showErrorMessage( "CXF directory must be set in global preferences" );
    119 			return;
    120 		}
    121
    122 		ProcessBuilder builder = new ProcessBuilder();
    123 		ArgumentBuilder args = buildArgs( modelItem );
    124 		builder.command(args.getArgs());
    125 		builder.directory(new File(xfireDir));
    126
    127 		toolHost.run( new ProcessToolRunner( builder, "Apache CXF wsdl2java", modelItem ));
    128 	}
    129
    130 	private ArgumentBuilder buildArgs( Interface modelItem ) throws IOException
    131 	{
    132 		StringToStringMap values = dialog.getValues();
    133 		values.put( OUTPUT, Tools.ensureDir( values.get( OUTPUT ), "" ));
    134
    135 		ArgumentBuilder builder = new ArgumentBuilder( values );
    136
    137 		builder.startScript( "wsdl2java" );
    138 		StringToStringMap nsMappings = StringToStringMap.fromXml(values.get(NAMESPACE_MAPPING));
    139 		String packages = values.get( PACKAGE ).trim();
    140 		for( String key : nsMappings.keySet() )
    141 		{
    142 			packages += " " + key + "=" + nsMappings.get( key );
    143 		}
    144
    145 		if>( packages.length() > 0 )
    146 			builder.addBoolean( NAMESPACE_MAPPING, "-p", null, packages );
    147
    148 		builder.addString( BINDING_FILES, "-b" );
    149 		builder.addString( CATALOG_FILE, "-catalog" );
    150 		builder.addString( OUTPUT, "-d" );
    151
    152 		builder.addBoolean( COMPILE, "-compile" );
    153 		builder.addString( CLASSDIR, "-classdir" );
    154
    155 		builder.addBoolean( CLIENT_STUBS, "-client" );
    156 		builder.addBoolean( SERVER_STUBS, "-server" );
    157 		builder.addBoolean( IMPL_STUBS, "-impl" );
    158 		builder.addBoolean( GENERATE_ALL, "-all" );
    159 		builder.addBoolean( ANT_FILE, "-ant" );
    160
    161 		StringToStringMap excludes = StringToStringMap.fromXml(values.get(EXCLUDE_NAMESPACES));
    162 		for( String key : excludes.keySet() )
    163 		{
    164 			String value = excludes.get( key );
    165 			if( value.equals( "-" ))
    166 				builder.addArgs( "-b", key );
    167 			else
    168 				builder.addArgs( "-b", key + "=" + value );
    169 		}
    170
    171 		builder.addBoolean( EXSH, "-exsh", "true", "false" );
    172 		builder.addBoolean( DNS, "-dns", "true", "false" );
    173 		builder.addBoolean( DEX, "-dex", "true", "false" );
    174 		builder.addBoolean( VALIDATE, "-validate" );
    175
    176 		builder.addArgs( "-verbose" );
    177 		addToolArgs( values, builder );
    178 		builder.addArgs( getWsdlUrl( values, modelItem ) );
    179 		return builder;
    180 	}
    181 }