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.axis2;
    14
    15  import java.io.File;
    16  import java.util.Map;
    17
    18  import javax.wsdl.Definition;
    19  import javax.wsdl.Port;
    20  import javax.wsdl.Service;
    21  import javax.xml.namespace.QName;
    22
    23  import com.eviware.soapui.SoapUI;
    24  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.AbstractToolsAction;
    25  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ArgumentBuilder;
    26  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.NamespaceTable;
    27  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ProcessToolRunner;
    28  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
    29  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
    30  import com.eviware.soapui.model.iface.Interface;
    31  import com.eviware.soapui.settings.ToolsSettings;
    32  import com.eviware.soapui.support.Tools;
    33  import com.eviware.soapui.support.UISupport;
    34  import com.eviware.soapui.support.types.StringToStringMap;
    35  import com.eviware.x.form.XForm;
    36  import com.eviware.x.form.XFormDialog;
    37  import com.eviware.x.form.XFormDialogBuilder;
    38  import com.eviware.x.form.XFormFactory;
    39  import com.eviware.x.form.XFormField;
    40  import com.eviware.x.form.XFormTextField;
    41
    42  /***
    43   * Invokes axis 2 WSDL2Code
    44   *
    45   * @author Ole.Matzura
    46   */
    47
    48  public class Axis2WSDL2CodeAction extends AbstractToolsAction<Interface>
    49  {
    50  	private static final String WSDL2JAVA_SCRIPT_NAME = "wsdl2java";
    51  	private static final String PACKAGE = "Package";
    52  	private static final String OUTPUT = "Output Directory";
    53  	private static final String SERVICE_NAME = "service name";
    54  	private static final String PORT_NAME = "port name";
    55  	private static final String ASYNC = "async";
    56  	private static final String SYNC = "sync";
    57  	private static final String TESTCASE = "test-case";
    58  	private static final String SERVERSIDE = "server-side";
    59  	private static final String SERICEDESCRIPTOR = "service descriptor";
    60  	private static final String DATABINDING = "databinding method";
    61  	private static final String GENERATEALL = "generate all";
    62  	private static final String UNPACK = "unpack classes";
    63  	private static final String SERVERSIDEINTERFACE = "serverside-interface";
    64
    65  	private static final String ADB_WRITE = "adb writeClasses";
    66  	private static final String ADB_WRAP = "adb wrapClasses";
    67  	private static final String NAMESPACE_MAPPING = "namespace mapping";
    68  	private static final String JIBX_BINDING_FILE = "JIBX bindingfile";
    69  	public static final String SOAPUI_ACTION_ID = "Axis2WSDL2CodeAction";
    70
    71
    72  	public Axis2WSDL2CodeAction()
    73     {
    74        super( "Axis 2 Artifacts", "Generates Axis 2 artifacts using wsdl2java");
    75     }
    76
    77  	protected StringToStringMap initValues( Interface modelItem )
    78  	{
    79  		StringToStringMap values = super.initValues( modelItem );
    80
    81  		if( !values.hasValue( PORT_NAME ) || !values.hasValue( SERVICE_NAME ))
    82  			initServiceAndPort( values, modelItem );
    83
    84  		return values;
    85  	}
    86
    87  	@SuppressWarnings("unchecked")
    88  	private void initServiceAndPort(StringToStringMap values, Interface modelItem )
    89  	{
    90  		if( modelItem == null )
    91  			return;
    92
    93  		try
    94  		{
    95  			QName bindingName = modelItem.getBindingName();
    96  			Definition definition = modelItem.getWsdlContext().getDefinition();
    97  			Map<QName, Service> services = definition.getAllServices();
    98
    99  			for( QName serviceName : services.keySet() )
    100 			{
    101 				Service service = services.get( serviceName );
    102
    103 				Map<String,Port> portMap = service.getPorts();
    104 				for( String portName : portMap.keySet() )
    105 				{
    106 					Port port = portMap.get( portName );
    107 					if( port.getBinding().getQName().equals( bindingName ))
    108 					{
    109 						values.put( SERVICE_NAME, serviceName.getLocalPart() );
    110 						values.put( PORT_NAME, portName );
    111
    112 						break;
    113 					}
    114 				}
    115
    116 				if( values.containsKey( PORT_NAME ))
    117 					break;
    118 			}
    119 		}
    120 		catch (Exception e)
    121 		{
    122 			SoapUI.logError( e );
    123 		}
    124 	}
    125
    126 	protected XFormDialog buildDialog( Interface modelItem)
    127 	{
    128       XFormDialogBuilder builder = XFormFactory.createDialogBuilder("Axis2 artifacts");
    129       XForm mainForm = builder.createForm( "Basic" );
    130
    131       addWSDLFields(mainForm, modelItem);
    132
    133 		mainForm.addTextField( OUTPUT, "root directory for generated files.", XForm.FieldType.PROJECT_FOLDER );
    134 		mainForm.addTextField( PACKAGE, "target package nam", XForm.FieldType.JAVA_PACKAGE );
    135
    136 		XFormField dbComboBox = mainForm.addComboBox( DATABINDING, new String[] { "xmlbeans", "adb", "jibx", "jaxme" }, "Specifies the Databinding framework.");
    137
    138 		mainForm.addCheckBox( ASYNC, "(generate code only for async style)" );
    139 		mainForm.addCheckBox( SYNC, "(generate code only for sync style)" );
    140 		mainForm.addCheckBox( TESTCASE, "(Generate a test case)" );
    141
    142 		XFormField serverSideCB = mainForm.addCheckBox( SERVERSIDE, "(Generate server side code (i.e. skeletons))" );
    143
    144 		XFormField ssiCB = mainForm.addCheckBox( SERVERSIDEINTERFACE, "(Generate interface for server side)" );
    145 		XFormField sdCB = mainForm.addCheckBox( SERICEDESCRIPTOR, "(Generate the service descriptor (i.e. server.xml).)" );
    146       serverSideCB.addComponentEnabler(ssiCB, "true");
    147       serverSideCB.addComponentEnabler(sdCB, "true");
    148
    149 		XForm advForm = builder.createForm( "Advanced" );
    150
    151 		advForm.addCheckBox( GENERATEALL, "(Genrates all the classes)" );
    152 		advForm.addCheckBox( UNPACK, "(Unpacks the databinding classes)" );
    153
    154 		advForm.addTextField( SERVICE_NAME, "the service name to be code generated", XForm.FieldType.TEXT );
    155 		advForm.addTextField( PORT_NAME, "the port name to be code generated", XForm.FieldType.TEXT );
    156
    157 		advForm.addComponent( NAMESPACE_MAPPING, new NamespaceTable( modelItem ) );
    158
    159 		XFormField adbWrapCB = advForm.addCheckBox( ADB_WRAP, "(Sets the packing flag. if true the classes will be packed.)");
    160 		XFormField adbWriteCB = advForm.addCheckBox( ADB_WRITE, "(Sets the write flag. If set to true the classes will be written by ADB)");
    161 		XFormTextField jibxCB = advForm.addTextField( JIBX_BINDING_FILE, "The JIBX binding file to use", XForm.FieldType.PROJECT_FILE );
    162       dbComboBox.addComponentEnabler(adbWrapCB, "adb");
    163       dbComboBox.addComponentEnabler(adbWriteCB, "adb");
    164       dbComboBox.addComponentEnabler(jibxCB, "jibx");
    165
    166       buildArgsForm( builder, false, "WSDL2Java" );
    167
    168 		return builder.buildDialog( buildDefaultActions( HelpUrls.AXIS2X_HELP_URL, modelItem ),
    169       		"Specify arguments for Axis 2.X Wsdl2Java", UISupport.TOOL_ICON );
    170 	}
    171
    172 	protected void generate(StringToStringMap values, ToolHost toolHost, Interface modelItem ) throws Exception
    173 	{
    174 		String axis2Dir = SoapUI.getSettings().getString( ToolsSettings.AXIS_2_LOCATION, null );
    175 		if( Tools.isEmpty( axis2Dir ))
    176 		{
    177 			UISupport.showErrorMessage( "Axis 2 wsdl2java directory must be set in global preferences" );
    178 			return;
    179 		}
    180
    181 		ProcessBuilder builder = new ProcessBuilder();
    182 		ArgumentBuilder args = buildArgs( values, modelItem  );
    183 		builder.command(args.getArgs());
    184 		builder.directory(new File(axis2Dir + File.separatorChar + "bin" ));
    185
    186 		toolHost.run( new ProcessToolRunner( builder, "Axis2 wsdl2java", modelItem ));
    187 	}
    188
    189 	private ArgumentBuilder buildArgs(StringToStringMap values, Interface modelItem )
    190 	{
    191 		values.put( OUTPUT, Tools.ensureDir( values.get( OUTPUT ), "" ));
    192
    193 		ArgumentBuilder builder = new ArgumentBuilder( values );
    194 		builder.startScript( WSDL2JAVA_SCRIPT_NAME);
    195 		builder.addArgs( "-uri", getWsdlUrl( values, modelItem ) );
    196 		builder.addString( OUTPUT, "-o" );
    197 		builder.addString( PACKAGE, "p" );
    198 		builder.addString( DATABINDING, "-d" );
    199 		builder.addBoolean( ASYNC, "-a" );
    200 		builder.addBoolean( SYNC, "-s" );
    201 		builder.addBoolean( TESTCASE, "-t" );
    202 		builder.addBoolean( SERVERSIDE, "-ss" );
    203 		builder.addBoolean( SERVERSIDEINTERFACE, "-ssi" );
    204 		builder.addBoolean( SERICEDESCRIPTOR, "-sd" );
    205 		builder.addBoolean( GENERATEALL, "-g" );
    206 		builder.addBoolean( UNPACK, "-u" );
    207
    208 		builder.addString( SERVICE_NAME, "-sn" );
    209 		builder.addString( PORT_NAME, "-pn" );
    210
    211 		if( "adb".equals( values.get( DATABINDING )))
    212 		{
    213 			builder.addBoolean( ADB_WRAP, "-Ew", "true", "false" );
    214 			builder.addBoolean( ADB_WRITE, "-Er" );
    215 		}
    216
    217 		if( "jibx".equals( values.get( DATABINDING )))
    218 		{
    219 			builder.addString( JIBX_BINDING_FILE, "-E", "" );
    220 		}
    221
    222 		try
    223 		{
    224 			StringBuilder nsMapArg = new StringBuilder();
    225 			StringToStringMap nsMappings = StringToStringMap.fromXml(values.get(NAMESPACE_MAPPING));
    226 			for (String namespace : nsMappings.keySet())
    227 			{
    228 				if( nsMapArg.length() > 0 )
    229 					nsMapArg.append( ',' );
    230
    231 			   nsMapArg.append( namespace ).append( '=').append( nsMappings.get( namespace ));
    232 			}
    233
    234 			builder.addArgs( "-ns2p", nsMapArg.toString());
    235 		}
    236 		catch (Exception e)
    237 		{
    238 			SoapUI.logError( e );
    239 		}
    240
    241 		addToolArgs( values, builder );
    242
    243 		return builder;
    244 	}
    245 }