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