View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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  	
73  	public Axis2WSDL2CodeAction()
74     {
75        super( "Axis 2 Artifacts", "Generates Axis 2 artifacts using wsdl2java");
76     }
77  
78  	protected StringToStringMap initValues( Interface modelItem, Object param )
79  	{
80  		StringToStringMap values = super.initValues( modelItem, param );
81  
82  		if( !values.hasValue( PORT_NAME ) || !values.hasValue( SERVICE_NAME ))
83  			initServiceAndPort( values, (WsdlInterface) modelItem );		
84  		
85  		return values;
86  	}
87  
88  	@SuppressWarnings("unchecked")
89  	private void initServiceAndPort(StringToStringMap values, WsdlInterface modelItem )
90  	{
91  		if( modelItem == null )
92  			return;
93  		
94  		try
95  		{
96  			QName bindingName = modelItem.getBindingName();
97  			Definition definition = modelItem.getWsdlContext().getDefinition();
98  			Map<QName, Service> services = definition.getAllServices();
99  			
100 			for( QName serviceName : services.keySet() )
101 			{
102 				Service service = services.get( serviceName );
103 				
104 				Map<String,Port> portMap = service.getPorts();
105 				for( String portName : portMap.keySet() )
106 				{
107 					Port port = portMap.get( portName );
108 					if( port.getBinding().getQName().equals( bindingName ))
109 					{
110 						values.put( SERVICE_NAME, serviceName.getLocalPart() );
111 						values.put( PORT_NAME, portName );
112 						
113 						break;
114 					}
115 				}
116 				
117 				if( values.containsKey( PORT_NAME ))
118 					break;
119 			}
120 		}
121 		catch (Exception e)
122 		{
123 			SoapUI.logError( e );
124 		}
125 	}
126 
127 	protected XFormDialog buildDialog( Interface modelItem)
128 	{
129       XFormDialogBuilder builder = XFormFactory.createDialogBuilder("Axis2 artifacts");
130       XForm mainForm = builder.createForm( "Basic" );
131 		
132       addWSDLFields(mainForm, modelItem);
133 		
134 		mainForm.addTextField( OUTPUT, "root directory for generated files.", XForm.FieldType.PROJECT_FOLDER );
135 		mainForm.addTextField( PACKAGE, "target package nam", XForm.FieldType.JAVA_PACKAGE );
136 
137 		XFormField dbComboBox = mainForm.addComboBox( DATABINDING, new String[] { "xmlbeans", "adb", "jibx", "jaxme" }, "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, "(Sets the packing flag. if true the classes will be packed.)");
161 		XFormField adbWriteCB = advForm.addCheckBox( ADB_WRITE, "(Sets the write flag. If set to true the classes will be written by ADB)");
162 		XFormTextField jibxCB = advForm.addTextField( JIBX_BINDING_FILE, "The JIBX binding file to use", XForm.FieldType.PROJECT_FILE );
163       dbComboBox.addComponentEnabler(adbWrapCB, "adb");
164       dbComboBox.addComponentEnabler(adbWriteCB, "adb");
165       dbComboBox.addComponentEnabler(jibxCB, "jibx");
166       
167       buildArgsForm( builder, false, "WSDL2Java" );
168       
169 		return builder.buildDialog( buildDefaultActions( HelpUrls.AXIS2X_HELP_URL, modelItem ),
170       		"Specify arguments for Axis 2.X Wsdl2Java", UISupport.TOOL_ICON );
171 	}
172 
173 	protected void generate(StringToStringMap values, ToolHost toolHost, Interface modelItem ) throws Exception
174 	{
175 		String axis2Dir = SoapUI.getSettings().getString( ToolsSettings.AXIS_2_LOCATION, null );
176 		if( Tools.isEmpty( axis2Dir ))
177 		{
178 			UISupport.showErrorMessage( "Axis 2 wsdl2java directory must be set in global preferences" );
179 			return;
180 		}
181 		
182 		ProcessBuilder builder = new ProcessBuilder();
183 		ArgumentBuilder args = buildArgs( values, (WsdlInterface) modelItem  );
184 		builder.command(args.getArgs());
185 		builder.directory(new File(axis2Dir + File.separatorChar + "bin" ));
186 		
187 		toolHost.run( new ProcessToolRunner( builder, "Axis2 wsdl2java", modelItem ));
188 	}
189 
190 	private ArgumentBuilder buildArgs(StringToStringMap values, WsdlInterface modelItem )
191 	{
192 		values.put( OUTPUT, Tools.ensureDir( values.get( OUTPUT ), "" ));
193 		
194 		ArgumentBuilder builder = new ArgumentBuilder( values );
195 		builder.startScript( WSDL2JAVA_SCRIPT_NAME);
196 		builder.addArgs( "-uri", getWsdlUrl( values, modelItem ) );
197 		builder.addString( OUTPUT, "-o" );
198 		builder.addString( PACKAGE, "p" );
199 		builder.addString( DATABINDING, "-d" );
200 		builder.addBoolean( ASYNC, "-a" );
201 		builder.addBoolean( SYNC, "-s" );
202 		builder.addBoolean( TESTCASE, "-t" );
203 		builder.addBoolean( SERVERSIDE, "-ss" );
204 		builder.addBoolean( SERVERSIDEINTERFACE, "-ssi" );
205 		builder.addBoolean( SERICEDESCRIPTOR, "-sd" );
206 		builder.addBoolean( GENERATEALL, "-g" );
207 		builder.addBoolean( UNPACK, "-u" );
208 		
209 		builder.addString( SERVICE_NAME, "-sn" );
210 		builder.addString( PORT_NAME, "-pn" );
211 		
212 		if( "adb".equals( values.get( DATABINDING )))
213 		{
214 			builder.addBoolean( ADB_WRAP, "-Ew", "true", "false" );
215 			builder.addBoolean( ADB_WRITE, "-Er" );
216 		}
217 		
218 		if( "jibx".equals( values.get( DATABINDING )))
219 		{
220 			builder.addString( JIBX_BINDING_FILE, "-E", "" );
221 		}
222 		
223 		try
224 		{
225 			StringBuilder nsMapArg = new StringBuilder();
226 			StringToStringMap nsMappings = StringToStringMap.fromXml(values.get(NAMESPACE_MAPPING));
227 			for (String namespace : nsMappings.keySet())
228 			{
229 				if( nsMapArg.length() > 0 )
230 					nsMapArg.append( ',' );
231 				
232 			   nsMapArg.append( namespace ).append( '=').append( nsMappings.get( namespace ));	
233 			}
234 			
235 			builder.addArgs( "-ns2p", nsMapArg.toString());
236 		}
237 		catch (Exception e)
238 		{
239 			SoapUI.logError( e );
240 		}		
241 		
242 		addToolArgs( values, builder );
243 		
244 		return builder;
245 	}
246 }