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