View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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.wscompile;
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.ShowConfigFileAction;
23  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
24  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
25  import com.eviware.soapui.model.iface.Interface;
26  import com.eviware.soapui.settings.ToolsSettings;
27  import com.eviware.soapui.support.Tools;
28  import com.eviware.soapui.support.UISupport;
29  import com.eviware.soapui.support.action.ActionList;
30  import com.eviware.soapui.support.types.StringToStringMap;
31  import com.eviware.x.form.XForm;
32  import com.eviware.x.form.XFormDialog;
33  import com.eviware.x.form.XFormDialogBuilder;
34  import com.eviware.x.form.XFormFactory;
35  import com.sun.java.xml.ns.jaxRpc.ri.config.ConfigurationDocument;
36  import com.sun.java.xml.ns.jaxRpc.ri.config.NamespaceMappingRegistryType;
37  import com.sun.java.xml.ns.jaxRpc.ri.config.NamespaceMappingType;
38  import com.sun.java.xml.ns.jaxRpc.ri.config.WsdlType;
39  import com.sun.java.xml.ns.jaxRpc.ri.config.ConfigurationDocument.Configuration;
40  
41  /***
42   * Invokes JWSDP wscompile
43   * 
44   * @author Ole.Matzura
45   */
46  
47  public class WSCompileAction extends AbstractToolsAction<Interface>
48  {
49  	private static final String OUTPUT = "directory";
50  	private static final String DATAHANDLERONLY = "datahandleronly";
51  	private static final String DONOTUNWRAP = "donotunwrap";
52  	private static final String PACKAGE = "package";
53  	private static final String KEEP = "keep";
54  	private static final String MAPPING = "mapping";
55  	private static final String SOURCE = "source";
56  	private static final String OPTIMIZE = "optimize";
57  	private static final String SOURCE_VERSION = "source version";
58  	private static final String MODEL = "model";
59  	private static final String NONCLASS = "non-class";
60  	private static final String SECURITY = "security";
61  	private static final String DEBUG = "debug";
62  	private static final String EXPLICITCONTEXT = "explicitcontext";
63  	private static final String JAXBENUMTYPE = "jaxbenumtype";
64  	private static final String NODATABINDING = "nodatabinding";
65  	private static final String NOENCODEDTYPES = "noencodedtypes";
66  	private static final String NOMULTIREFS = "nomultirefs";
67  	private static final String NORPCSTRUCTURES = "norpcstructures";
68  	private static final String NOVALIDATION = "novalidation";
69  	private static final String RESOLVEIDREF = "resolveidref";
70  	private static final String SEARCHSCHEMA = "searchschema";
71  	private static final String SERIALIZEINTERFACES = "serializeinterfaces";
72  	private static final String STRICT = "strict";
73  	private static final String UNWRAP = "unwrap";
74  	private static final String WSI = "wsi";
75  	private static final String PROXY = "proxy";
76  	private static final String NAMESPACE_MAPPING = "Namespace mapping";
77  
78  	public WSCompileAction( Interface iface )
79     {
80        super( iface, "JAX-RPC Artifacts", "Generates JAX-RPC artifacts using wscompile");
81     }
82  
83  	protected XFormDialog buildDialog()
84  	{
85        XFormDialogBuilder builder = XFormFactory.createDialogBuilder("WSCompile");
86  
87  		XForm mainForm = builder.createForm( "Basic" );
88  		addWSDLFields( mainForm );
89  		
90  		mainForm.addTextField( PACKAGE, "the package of the classes generated by wscompile", XForm.FieldType.JAVA_PACKAGE );
91  		mainForm.addTextField( OUTPUT, "where to place generated output files", XForm.FieldType.PROJECT_FOLDER );
92  		mainForm.addCheckBox( KEEP, "(Keep generated files)");
93  		mainForm.addTextField( MAPPING, "Generate a J2EE mapping.xml file", XForm.FieldType.PROJECT_FILE );
94  		mainForm.addTextField( MODEL, "Write the internal model to the given file", XForm.FieldType.PROJECT_FILE );
95  		mainForm.addTextField( SOURCE, "Where to place generated source files", XForm.FieldType.PROJECT_FOLDER );
96  		mainForm.addTextField( NONCLASS, "Where to place non-class generated files", XForm.FieldType.PROJECT_FOLDER );
97  		mainForm.addCheckBox( OPTIMIZE, "(Optimize generated code)");
98  		mainForm.addCheckBox( DEBUG, "(Generate debugging info)");
99  		mainForm.addComboBox( SOURCE_VERSION, new String[] {"1.0.1", "1.0.3", "1.1", "1.1.1", "1.1.2"}, 
100 				"Generate code for the specified JAX-RPC SI version" );
101 		mainForm.addTextField( SECURITY, "Security configuration file to generate security code", XForm.FieldType.PROJECT_FILE );
102 		mainForm.addTextField( PROXY, "Specify a HTTP proxy server", XForm.FieldType.URL );
103 		
104 		XForm featuresForm = builder.createForm( "Features" );
105 		
106 		featuresForm.addCheckBox( DATAHANDLERONLY, "(Always map attachments to the DataHandler type)");
107 		featuresForm.addCheckBox( DONOTUNWRAP , "(Disable unwrapping of document/literal wrapper elements in WSI mode)");
108 		featuresForm.addCheckBox( EXPLICITCONTEXT , "(Turn on explicit service context mapping)");
109 		featuresForm.addCheckBox( JAXBENUMTYPE , "(Map anonymous enumeration to its base type)");
110 		featuresForm.addCheckBox( NODATABINDING , "(Turn off data binding for literal encoding)");
111 		featuresForm.addCheckBox( NOENCODEDTYPES , "(Turn off encoding type information)");
112 		featuresForm.addCheckBox( NOMULTIREFS , "(Turn off support for multiple references)");
113 		featuresForm.addCheckBox( NORPCSTRUCTURES , "(Do not generate RPC structures)");
114 		featuresForm.addCheckBox( NOVALIDATION , "(Turn off full validation of imported WSDL documents)");
115 		featuresForm.addCheckBox( RESOLVEIDREF , "(Resolve xsd:IDREF)");
116 		featuresForm.addCheckBox( SEARCHSCHEMA , "(Search schema aggressively for types)");
117 		featuresForm.addCheckBox( SERIALIZEINTERFACES , "(Turn on direct serialization of interface types)");
118 		featuresForm.addCheckBox( STRICT , "(Generate code strictly compliant with JAXRPC spec)");
119 		featuresForm.addCheckBox( UNWRAP , "(Enable unwrapping of document/literal wrapper elements in WSI mode)");
120 		featuresForm.addCheckBox( WSI , "(Enable WSI-Basic Profile features, to be used for document/literal and rpc/literal)");
121 		
122 		XForm advForm = builder.createForm( "Advanced" );
123 		advForm.addNameSpaceTable( NAMESPACE_MAPPING, modelItem );
124 		
125       buildArgsForm( builder, false, "wscompile");
126       
127 		ActionList actions = buildDefaultActions(HelpUrls.WSCOMPILE_HELP_URL);
128 		actions.addAction( new ShowConfigFileAction( "JAX-RPC wscompile", "Contents of generated config.xml file" )
129 		{
130 			protected String getConfigFile()
131 			{
132 				ConfigurationDocument configDocument = createConfigFile(dialog.getValues());
133 				return configDocument.toString();
134 			}});
135 		
136 		return builder.buildDialog( actions,
137       		"Specify arguments for JAX-RPC wscompile", UISupport.TOOL_ICON );
138 	}
139 
140 	protected StringToStringMap initValues()
141 	{
142 		StringToStringMap values = super.initValues();
143 		values.putIfMissing( SOURCE_VERSION, "1.1.2" );
144 		values.putIfMissing( WSI, Boolean.toString( true ));
145 		
146 		return values;
147 	}
148 	
149 	protected void generate(StringToStringMap values, ToolHost toolHost) throws Exception
150 	{
151 		String wscompileDir = SoapUI.getSettings().getString( ToolsSettings.JWSDP_WSCOMPILE_LOCATION, null );
152 		if( Tools.isEmpty( wscompileDir ))
153 		{
154 			UISupport.showErrorMessage( "wscompile directory must be set in global preferences" );
155 			return;
156 		}
157 		
158 		String wscompileExtension = UISupport.isWindows() ? ".bat" : ".sh";
159 		
160 		File wscompileFile = new File( wscompileDir + File.separatorChar + "wscompile" + wscompileExtension );
161 		if( !wscompileFile.exists() )
162 		{
163 			UISupport.showErrorMessage( "Could not find wscompile script at [" + wscompileFile + "]" );
164 			return;
165 		}
166 		
167 		ProcessBuilder builder = new ProcessBuilder();
168 		ArgumentBuilder args = buildArgs( UISupport.isWindows() );
169 		builder.command(args.getArgs());
170 		builder.directory(new File(wscompileDir));
171 		
172 		toolHost.run( new ProcessToolRunner( builder, "JAX-RPC wscompile", modelItem ));
173 	}
174 
175 	private ArgumentBuilder buildArgs( boolean isWindows ) throws IOException
176 	{
177 		StringToStringMap values = dialog.getValues();
178 		ArgumentBuilder builder = new ArgumentBuilder( values );
179 		builder.startScript( "wscompile" );
180 		
181 		values.put( OUTPUT, Tools.ensureDir( values.get( OUTPUT ), "" ));
182 		
183 		values.put( SOURCE, Tools.ensureDir( values.get( SOURCE ), values.get( OUTPUT ) ));
184 		values.put( NONCLASS, Tools.ensureDir( values.get( NONCLASS ), values.get( OUTPUT ) ));
185 
186 		values.put( MAPPING, Tools.ensureFileDir( values.get( MAPPING ), values.get( OUTPUT ) ));
187 		values.put( MODEL, Tools.ensureFileDir( values.get( MODEL ), values.get( OUTPUT ) ));
188 		
189 		builder.addString( OUTPUT, "-d" );
190 		builder.addBoolean( KEEP, "-keep" );
191 		builder.addString( MAPPING, "-mapping" );
192 		builder.addString( MODEL, "-model" );
193 		builder.addString( SOURCE, "-s" );
194 		builder.addString( NONCLASS, "-nd" );
195 		builder.addBoolean( OPTIMIZE, "-O" );
196 		builder.addBoolean( DEBUG, "-g" );
197 		builder.addString( SOURCE_VERSION, "-source" );
198 		builder.addString( SECURITY, "-security" );
199 		builder.addString( PROXY, "httpproxy", ":" );
200 		
201 		builder.addBoolean( DATAHANDLERONLY, "-f:datahandleronly" );
202 		builder.addBoolean( DONOTUNWRAP , "-f:donotunwrap" );
203 		builder.addBoolean( EXPLICITCONTEXT , "-f:explicitcontext" );
204 		builder.addBoolean( JAXBENUMTYPE , "-f:jaxbenumtype" );
205 		builder.addBoolean( NODATABINDING , "-f:nodatabinding" );
206 		builder.addBoolean( NOENCODEDTYPES , "-f:noencodedtypes" );
207 		builder.addBoolean( NOMULTIREFS , "-f:nomultirefs" );
208 		builder.addBoolean( NORPCSTRUCTURES , "-f:norpcstructures" );
209 		builder.addBoolean( NOVALIDATION , "-f:novalidation" );
210 		builder.addBoolean( RESOLVEIDREF , "-f:resolveidref" );
211 		builder.addBoolean( SEARCHSCHEMA , "-f:searchschema" );
212 		builder.addBoolean( SERIALIZEINTERFACES , "-f:serializeinterfaces");
213 		builder.addBoolean( STRICT , "-f:strict");
214 		builder.addBoolean( UNWRAP , "-f:unwrap");
215 		builder.addBoolean( WSI , "-f:wsi");
216 		
217 		builder.addArgs( "-import" );
218 		builder.addArgs( "-verbose" );
219 		addToolArgs( values, builder );
220 		builder.addArgs( buildConfigFile( values ) );
221 		return builder;
222 	}
223 
224 	private String buildConfigFile(StringToStringMap values ) throws IOException
225 	{
226 		File file = File.createTempFile( "wscompile-config", ".xml" );
227 		ConfigurationDocument configDocument = createConfigFile(values);		
228 		configDocument.save( file );
229 		return file.getAbsolutePath();
230 	}
231 
232 	private ConfigurationDocument createConfigFile(StringToStringMap values)
233 	{
234 		ConfigurationDocument configDocument = ConfigurationDocument.Factory.newInstance();
235 		Configuration config = configDocument.addNewConfiguration();
236 		
237 		WsdlType wsdl = config.addNewWsdl();
238 		wsdl.setLocation( getWsdlUrl( values ) );
239 		wsdl.setPackageName( values.get( PACKAGE ).toString() );
240 		
241 		try
242 		{
243 			StringToStringMap nsMappings = StringToStringMap.fromXml(values.get(NAMESPACE_MAPPING));
244 			if (!nsMappings.isEmpty())
245 			{
246 				NamespaceMappingRegistryType nsMappingRegistry = wsdl.addNewNamespaceMappingRegistry();
247 
248 				for (String namespace : nsMappings.keySet())
249 				{
250 					String packageName = nsMappings.get( namespace );
251 					
252 					NamespaceMappingType newMapping = nsMappingRegistry.addNewNamespaceMapping();
253 					newMapping.setNamespace(namespace);
254 					newMapping.setPackageName(packageName);
255 				}
256 			}
257 		}
258 		catch (Exception e)
259 		{
260 			e.printStackTrace();
261 		}
262 		return configDocument;
263 	}
264 }