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.jbossws;
14  
15  import java.io.File;
16  import java.io.IOException;
17  import java.net.MalformedURLException;
18  import java.net.URL;
19  
20  import org.jboss.jbosswsTools.ConfigurationDocument;
21  import org.jboss.jbosswsTools.ConfigurationType;
22  import org.jboss.jbosswsTools.GlobalType;
23  import org.jboss.jbosswsTools.PkgNSType;
24  import org.jboss.jbosswsTools.WsdlToJavaType;
25  import org.jboss.jbosswsTools.WsxmlType;
26  import org.jboss.jbosswsTools.WsdlToJavaType.ParameterStyle;
27  import org.w3c.dom.Element;
28  
29  import com.eviware.soapui.SoapUI;
30  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.AbstractToolsAction;
31  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ArgumentBuilder;
32  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ProcessToolRunner;
33  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ShowConfigFileAction;
34  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
35  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
36  import com.eviware.soapui.model.iface.Interface;
37  import com.eviware.soapui.model.project.Project;
38  import com.eviware.soapui.settings.ToolsSettings;
39  import com.eviware.soapui.support.Tools;
40  import com.eviware.soapui.support.UISupport;
41  import com.eviware.soapui.support.action.swing.ActionList;
42  import com.eviware.soapui.support.types.StringToStringMap;
43  import com.eviware.x.form.XForm;
44  import com.eviware.x.form.XFormDialog;
45  import com.eviware.x.form.XFormDialogBuilder;
46  import com.eviware.x.form.XFormFactory;
47  import com.eviware.x.form.XFormField;
48  import com.eviware.x.form.XFormFieldListener;
49  import com.eviware.x.form.XFormTextField;
50  
51  /***
52   * Invokes jbossws wsdl2java tools
53   * 
54   * @author Ole.Matzura
55   */
56  
57  public class WSToolsWsdl2JavaAction extends AbstractToolsAction<Interface>
58  {
59  	public static final String SOAPUI_ACTION_ID = "WSToolsWsdl2JavaAction";
60  
61  	private static final String NAMESPACE_MAPPING = "Namespace mapping";
62  	private static final String OUTPUT = "Output Directory";
63  	private static final String MAPPING = "Mapping file";
64  	private static final String UNWRAP = "Unwrap";
65  	private static final String APPEND = "Append";
66  	private static final String SERVLET_LINK = "Servlet Link";
67  	private static final String EJB_LINK = "EJB Link";
68  	private XFormTextField ejbLinkField;
69  	private XFormTextField servletLinkField;
70  	private XFormField appendField;
71  
72  	public WSToolsWsdl2JavaAction()
73  	{
74  		super( "JBossWS Artifacts", "Generates JBossWS artifacts using the jboss wstools utility" );
75  	}
76  
77  	@Override
78  	public boolean applies( Interface target )
79  	{
80  		Interface iface = ( Interface )target;
81  		return !iface.getProject().hasNature( Project.JBOSSWS_NATURE_ID );
82  	}
83  
84  	@Override
85  	protected StringToStringMap initValues( Interface modelItem, Object param )
86  	{
87  		StringToStringMap values = super.initValues( modelItem, param );
88  
89  		boolean hasEjbLink = values.get( EJB_LINK, "" ).length() > 0;
90  		boolean hasServletLink = values.get( SERVLET_LINK, "" ).length() > 0;
91  
92  		if( !hasEjbLink && !hasServletLink )
93  		{
94  			ejbLinkField.setEnabled( true );
95  			servletLinkField.setEnabled( true );
96  		}
97  		else
98  		{
99  			ejbLinkField.setEnabled( hasEjbLink && !hasServletLink );
100 			servletLinkField.setEnabled( hasServletLink && !hasEjbLink );
101 
102 			if( hasEjbLink && hasServletLink )
103 				values.put( SERVLET_LINK, "" );
104 		}
105 
106 		appendField.setEnabled( hasEjbLink || hasServletLink );
107 
108 		return values;
109 	}
110 
111 	protected XFormDialog buildDialog( Interface modelItem )
112 	{
113 		XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "JBossWS Artifacts" );
114 
115 		XForm mainForm = builder.createForm( "Basic" );
116 		addWSDLFields( mainForm, modelItem );
117 
118 		mainForm.addTextField( OUTPUT, "The root directory for all emitted files.", XForm.FieldType.PROJECT_FOLDER );
119 		mainForm.addTextField( MAPPING, "mapping file to generate", XForm.FieldType.PROJECT_FILE );
120 		mainForm.addCheckBox( UNWRAP, "unwrap doc-literal operations" );
121 
122 		mainForm.addNameSpaceTable( NAMESPACE_MAPPING, modelItem );
123 
124 		mainForm.addSeparator( "webservices.xml generation options" );
125 		ejbLinkField = mainForm.addTextField( EJB_LINK, "The ejb-jar.xml ejb-link for Stateless Session Bean endpoints",
126 				XForm.FieldType.TEXT );
127 		ejbLinkField.addFormFieldListener( new XFormFieldListener()
128 		{
129 			public void valueChanged( XFormField sourceField, String newValue, String oldValue )
130 			{
131 				servletLinkField.setEnabled( newValue.length() == 0 );
132 				appendField.setEnabled( newValue.length() > 0 );
133 			}
134 		} );
135 
136 		servletLinkField = mainForm.addTextField( SERVLET_LINK,
137 				"The web.xml servlet-link that is used by Java Service Endpoints (WAR)", XForm.FieldType.TEXT );
138 		servletLinkField.addFormFieldListener( new XFormFieldListener()
139 		{
140 			public void valueChanged( XFormField sourceField, String newValue, String oldValue )
141 			{
142 				ejbLinkField.setEnabled( newValue.length() == 0 );
143 				appendField.setEnabled( newValue.length() > 0 );
144 			}
145 		} );
146 
147 		appendField = mainForm.addCheckBox( APPEND, "append to existing file" );
148 		appendField.setEnabled( false );
149 		buildArgsForm( builder, false, "wstools" );
150 
151 		ActionList actions = buildDefaultActions( HelpUrls.WSTOOLS_HELP_URL, modelItem );
152 		actions.addAction( new JBossWSShowConfigFileAction( "JBossWS Wsdl2Java",
153 				"Contents of generated wsconfig.xml file", modelItem ) );
154 		return builder.buildDialog( actions, "Specify arguments for JBossWS wstools wsdl2java functionality",
155 				UISupport.TOOL_ICON );
156 	}
157 
158 	protected void generate( StringToStringMap values, ToolHost toolHost, Interface modelItem ) throws Exception
159 	{
160 		String wstoolsDir = SoapUI.getSettings().getString( ToolsSettings.JBOSSWS_WSTOOLS_LOCATION, null );
161 		if( Tools.isEmpty( wstoolsDir ) )
162 		{
163 			UISupport.showErrorMessage( "wstools directory must be set in global preferences" );
164 			return;
165 		}
166 
167 		String wsToolsExtension = UISupport.isWindows() ? ".bat" : ".sh";
168 
169 		File wstoolsFile = new File( wstoolsDir + File.separatorChar + "wstools" + wsToolsExtension );
170 		if( !wstoolsFile.exists() )
171 		{
172 			UISupport.showErrorMessage( "Could not find wstools script at [" + wstoolsFile + "]" );
173 			return;
174 		}
175 
176 		ProcessBuilder builder = new ProcessBuilder();
177 		ArgumentBuilder args = buildArgs( values, UISupport.isWindows(), modelItem );
178 		builder.command( args.getArgs() );
179 		builder.directory( new File( wstoolsDir ) );
180 
181 		toolHost.run( new ProcessToolRunner( builder, "JBossWS wstools", modelItem, args ) );
182 	}
183 
184 	private ArgumentBuilder buildArgs( StringToStringMap values, boolean isWindows, Interface modelItem )
185 			throws IOException
186 	{
187 		values.put( OUTPUT, Tools.ensureDir( values.get( OUTPUT ), "" ) );
188 
189 		ArgumentBuilder builder = new ArgumentBuilder( values );
190 		builder.startScript( "wstools" );
191 
192 		builder.addArgs( "-config", buildConfigFile( values, modelItem ) );
193 		builder.addString( OUTPUT, "-dest" );
194 		addToolArgs( values, builder );
195 		return builder;
196 	}
197 
198 	private String buildConfigFile( StringToStringMap values, Interface modelItem ) throws IOException
199 	{
200 		File file = File.createTempFile( "wstools-config", ".xml" );
201 		ConfigurationDocument configDocument = createConfigFile( values, modelItem );
202 
203 		configDocument.save( file );
204 
205 		return file.getAbsolutePath();
206 	}
207 
208 	private ConfigurationDocument createConfigFile( StringToStringMap values, Interface modelItem )
209 	{
210 		ConfigurationDocument configDocument = ConfigurationDocument.Factory.newInstance();
211 		ConfigurationType config = configDocument.addNewConfiguration();
212 
213 		try
214 		{
215 			StringToStringMap nsMappings = StringToStringMap.fromXml( values.get( NAMESPACE_MAPPING ) );
216 			if( !nsMappings.isEmpty() )
217 			{
218 				GlobalType global = config.addNewGlobal();
219 
220 				for( String namespace : nsMappings.keySet() )
221 				{
222 					PkgNSType entry = global.addNewPackageNamespace();
223 					entry.setNamespace( namespace );
224 					entry.setPackage( nsMappings.get( namespace ) );
225 				}
226 			}
227 		}
228 		catch( Exception e )
229 		{
230 			SoapUI.logError( e );
231 		}
232 
233 		WsdlToJavaType wsdl2Java = config.addNewWsdlJava();
234 
235 		String wsdlUrl = getWsdlUrl( values, modelItem );
236 		try
237 		{
238 			new URL( wsdlUrl );
239 			wsdl2Java.setLocation( wsdlUrl );
240 		}
241 		catch( MalformedURLException e )
242 		{
243 			( ( Element )wsdl2Java.getDomNode() ).setAttribute( "file", wsdlUrl );
244 		}
245 
246 		if( values.getBoolean( UNWRAP ) )
247 			wsdl2Java.setParameterStyle( ParameterStyle.BARE );
248 		else
249 			wsdl2Java.setParameterStyle( ParameterStyle.WRAPPED );
250 
251 		if( values.get( EJB_LINK ) != null && values.get( EJB_LINK ).length() > 0 )
252 		{
253 			WsxmlType webservices = wsdl2Java.addNewWebservices();
254 			webservices.setEjbLink( values.get( EJB_LINK ) );
255 			webservices.setAppend( values.getBoolean( APPEND ) );
256 		}
257 		else if( values.get( SERVLET_LINK ) != null && values.get( SERVLET_LINK ).length() > 0 )
258 		{
259 			WsxmlType webservices = wsdl2Java.addNewWebservices();
260 			webservices.setServletLink( values.get( SERVLET_LINK ) );
261 			webservices.setAppend( values.getBoolean( APPEND ) );
262 		}
263 
264 		String mappingFile = values.get( MAPPING ).toString().trim();
265 		if( mappingFile.length() > 0 )
266 		{
267 			wsdl2Java.addNewMapping().setFile( mappingFile );
268 		}
269 		return configDocument;
270 	}
271 
272 	private final class JBossWSShowConfigFileAction extends ShowConfigFileAction
273 	{
274 		private final Interface modelItem;
275 
276 		private JBossWSShowConfigFileAction( String title, String description, Interface modelItem )
277 		{
278 			super( title, description );
279 			this.modelItem = modelItem;
280 		}
281 
282 		protected String getConfigFile()
283 		{
284 			ConfigurationDocument configDocument = createConfigFile( getDialog().getValues(), modelItem );
285 			return configDocument.toString();
286 		}
287 	}
288 
289 }