View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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.WsdlInterface;
25  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.AbstractToolsAction;
26  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ArgumentBuilder;
27  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.NamespaceTable;
28  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ProcessToolRunner;
29  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
30  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
31  import com.eviware.soapui.model.iface.Interface;
32  import com.eviware.soapui.settings.ToolsSettings;
33  import com.eviware.soapui.support.Tools;
34  import com.eviware.soapui.support.UISupport;
35  import com.eviware.soapui.support.types.StringToStringMap;
36  import com.eviware.x.form.XForm;
37  import com.eviware.x.form.XFormDialog;
38  import com.eviware.x.form.XFormDialogBuilder;
39  import com.eviware.x.form.XFormFactory;
40  import com.eviware.x.form.XFormField;
41  import com.eviware.x.form.XFormTextField;
42  
43  /***
44   * Invokes axis 2 WSDL2Code
45   * 
46   * @author Ole.Matzura
47   */
48  
49  public class Axis2WSDL2CodeAction extends AbstractToolsAction<Interface>
50  {
51  	private static final String WSDL2JAVA_SCRIPT_NAME = "wsdl2java";
52  	private static final String PACKAGE = "Package";
53  	private static final String OUTPUT = "Output Directory";
54  	private static final String SERVICE_NAME = "service name";
55  	private static final String PORT_NAME = "port name";
56  	private static final String ASYNC = "async";
57  	private static final String SYNC = "sync";
58  	private static final String TESTCASE = "test-case";
59  	private static final String SERVERSIDE = "server-side";
60  	private static final String SERICEDESCRIPTOR = "service descriptor";
61  	private static final String DATABINDING = "databinding method";
62  	private static final String GENERATEALL = "generate all";
63  	private static final String UNPACK = "unpack classes";
64  	private static final String SERVERSIDEINTERFACE = "serverside-interface";
65  
66  	private static final String ADB_WRITE = "adb writeClasses";
67  	private static final String ADB_WRAP = "adb wrapClasses";
68  	private static final String NAMESPACE_MAPPING = "namespace mapping";
69  	private static final String JIBX_BINDING_FILE = "JIBX bindingfile";
70  	public static final String SOAPUI_ACTION_ID = "Axis2WSDL2CodeAction";
71  
72  	public Axis2WSDL2CodeAction()
73  	{
74  		super( "Axis 2 Artifacts", "Generates Axis 2 artifacts using wsdl2java" );
75  	}
76  
77  	protected StringToStringMap initValues( Interface modelItem, Object param )
78  	{
79  		StringToStringMap values = super.initValues( modelItem, param );
80  
81  		if( !values.hasValue( PORT_NAME ) || !values.hasValue( SERVICE_NAME ) )
82  			initServiceAndPort( values, ( WsdlInterface )modelItem );
83  
84  		return values;
85  	}
86  
87  	@SuppressWarnings( "unchecked" )
88  	private void initServiceAndPort( StringToStringMap values, WsdlInterface 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" },
137 				"Specifies the Databinding framework." );
138 
139 		mainForm.addCheckBox( ASYNC, "(generate code only for async style)" );
140 		mainForm.addCheckBox( SYNC, "(generate code only for sync style)" );
141 		mainForm.addCheckBox( TESTCASE, "(Generate a test case)" );
142 
143 		XFormField serverSideCB = mainForm.addCheckBox( SERVERSIDE, "(Generate server side code (i.e. skeletons))" );
144 
145 		XFormField ssiCB = mainForm.addCheckBox( SERVERSIDEINTERFACE, "(Generate interface for server side)" );
146 		XFormField sdCB = mainForm.addCheckBox( SERICEDESCRIPTOR, "(Generate the service descriptor (i.e. server.xml).)" );
147 		serverSideCB.addComponentEnabler( ssiCB, "true" );
148 		serverSideCB.addComponentEnabler( sdCB, "true" );
149 
150 		XForm advForm = builder.createForm( "Advanced" );
151 
152 		advForm.addCheckBox( GENERATEALL, "(Genrates all the classes)" );
153 		advForm.addCheckBox( UNPACK, "(Unpacks the databinding classes)" );
154 
155 		advForm.addTextField( SERVICE_NAME, "the service name to be code generated", XForm.FieldType.TEXT );
156 		advForm.addTextField( PORT_NAME, "the port name to be code generated", XForm.FieldType.TEXT );
157 
158 		advForm.addComponent( NAMESPACE_MAPPING, new NamespaceTable( ( WsdlInterface )modelItem ) );
159 
160 		XFormField adbWrapCB = advForm.addCheckBox( ADB_WRAP,
161 				"(Sets the packing flag. if true the classes will be packed.)" );
162 		XFormField adbWriteCB = advForm.addCheckBox( ADB_WRITE,
163 				"(Sets the write flag. If set to true the classes will be written by ADB)" );
164 		XFormTextField jibxCB = advForm.addTextField( JIBX_BINDING_FILE, "The JIBX binding file to use",
165 				XForm.FieldType.PROJECT_FILE );
166 		dbComboBox.addComponentEnabler( adbWrapCB, "adb" );
167 		dbComboBox.addComponentEnabler( adbWriteCB, "adb" );
168 		dbComboBox.addComponentEnabler( jibxCB, "jibx" );
169 
170 		buildArgsForm( builder, false, "WSDL2Java" );
171 
172 		return builder.buildDialog( buildDefaultActions( HelpUrls.AXIS2X_HELP_URL, modelItem ),
173 				"Specify arguments for Axis 2.X Wsdl2Java", UISupport.TOOL_ICON );
174 	}
175 
176 	protected void generate( StringToStringMap values, ToolHost toolHost, Interface modelItem ) throws Exception
177 	{
178 		String axis2Dir = SoapUI.getSettings().getString( ToolsSettings.AXIS_2_LOCATION, null );
179 		if( Tools.isEmpty( axis2Dir ) )
180 		{
181 			UISupport.showErrorMessage( "Axis 2 wsdl2java directory must be set in global preferences" );
182 			return;
183 		}
184 
185 		ProcessBuilder builder = new ProcessBuilder();
186 		ArgumentBuilder args = buildArgs( values, ( WsdlInterface )modelItem );
187 		builder.command( args.getArgs() );
188 		builder.directory( new File( axis2Dir + File.separatorChar + "bin" ) );
189 
190 		toolHost.run( new ProcessToolRunner( builder, "Axis2 wsdl2java", modelItem ) );
191 	}
192 
193 	private ArgumentBuilder buildArgs( StringToStringMap values, WsdlInterface modelItem )
194 	{
195 		values.put( OUTPUT, Tools.ensureDir( values.get( OUTPUT ), "" ) );
196 
197 		ArgumentBuilder builder = new ArgumentBuilder( values );
198 		builder.startScript( WSDL2JAVA_SCRIPT_NAME );
199 		builder.addArgs( "-uri", getWsdlUrl( values, modelItem ) );
200 		builder.addString( OUTPUT, "-o" );
201 		builder.addString( PACKAGE, "p" );
202 		builder.addString( DATABINDING, "-d" );
203 		builder.addBoolean( ASYNC, "-a" );
204 		builder.addBoolean( SYNC, "-s" );
205 		builder.addBoolean( TESTCASE, "-t" );
206 		builder.addBoolean( SERVERSIDE, "-ss" );
207 		builder.addBoolean( SERVERSIDEINTERFACE, "-ssi" );
208 		builder.addBoolean( SERICEDESCRIPTOR, "-sd" );
209 		builder.addBoolean( GENERATEALL, "-g" );
210 		builder.addBoolean( UNPACK, "-u" );
211 
212 		builder.addString( SERVICE_NAME, "-sn" );
213 		builder.addString( PORT_NAME, "-pn" );
214 
215 		if( "adb".equals( values.get( DATABINDING ) ) )
216 		{
217 			builder.addBoolean( ADB_WRAP, "-Ew", "true", "false" );
218 			builder.addBoolean( ADB_WRITE, "-Er" );
219 		}
220 
221 		if( "jibx".equals( values.get( DATABINDING ) ) )
222 		{
223 			builder.addString( JIBX_BINDING_FILE, "-E", "" );
224 		}
225 
226 		try
227 		{
228 			StringBuilder nsMapArg = new StringBuilder();
229 			StringToStringMap nsMappings = StringToStringMap.fromXml( values.get( NAMESPACE_MAPPING ) );
230 			for( String namespace : nsMappings.keySet() )
231 			{
232 				if( nsMapArg.length() > 0 )
233 					nsMapArg.append( ',' );
234 
235 				nsMapArg.append( namespace ).append( '=' ).append( nsMappings.get( namespace ) );
236 			}
237 
238 			builder.addArgs( "-ns2p", nsMapArg.toString() );
239 		}
240 		catch( Exception e )
241 		{
242 			SoapUI.logError( e );
243 		}
244 
245 		addToolArgs( values, builder );
246 
247 		return builder;
248 	}
249 }