View Javadoc

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