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( 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( boolean isWindows, Interface modelItem ) throws IOException
185 	{
186 		StringToStringMap values = dialog.getValues();
187 
188 		values.put( OUTPUT, Tools.ensureDir( values.get( OUTPUT ), "" ) );
189 
190 		ArgumentBuilder builder = new ArgumentBuilder( values );
191 		builder.startScript( "wstools" );
192 
193 		builder.addArgs( "-config", buildConfigFile( values, modelItem ) );
194 		builder.addString( OUTPUT, "-dest" );
195 		addToolArgs( values, builder );
196 		return builder;
197 	}
198 
199 	private String buildConfigFile( StringToStringMap values, Interface modelItem ) throws IOException
200 	{
201 		File file = File.createTempFile( "wstools-config", ".xml" );
202 		ConfigurationDocument configDocument = createConfigFile( values, modelItem );
203 
204 		configDocument.save( file );
205 
206 		return file.getAbsolutePath();
207 	}
208 
209 	private ConfigurationDocument createConfigFile( StringToStringMap values, Interface modelItem )
210 	{
211 		ConfigurationDocument configDocument = ConfigurationDocument.Factory.newInstance();
212 		ConfigurationType config = configDocument.addNewConfiguration();
213 
214 		try
215 		{
216 			StringToStringMap nsMappings = StringToStringMap.fromXml( values.get( NAMESPACE_MAPPING ) );
217 			if( !nsMappings.isEmpty() )
218 			{
219 				GlobalType global = config.addNewGlobal();
220 
221 				for( String namespace : nsMappings.keySet() )
222 				{
223 					PkgNSType entry = global.addNewPackageNamespace();
224 					entry.setNamespace( namespace );
225 					entry.setPackage( nsMappings.get( namespace ) );
226 				}
227 			}
228 		}
229 		catch( Exception e )
230 		{
231 			SoapUI.logError( e );
232 		}
233 
234 		WsdlToJavaType wsdl2Java = config.addNewWsdlJava();
235 
236 		String wsdlUrl = getWsdlUrl( values, modelItem );
237 		try
238 		{
239 			new URL( wsdlUrl );
240 			wsdl2Java.setLocation( wsdlUrl );
241 		}
242 		catch( MalformedURLException e )
243 		{
244 			( ( Element )wsdl2Java.getDomNode() ).setAttribute( "file", wsdlUrl );
245 		}
246 
247 		if( values.getBoolean( UNWRAP ) )
248 			wsdl2Java.setParameterStyle( ParameterStyle.BARE );
249 		else
250 			wsdl2Java.setParameterStyle( ParameterStyle.WRAPPED );
251 
252 		if( values.get( EJB_LINK ) != null && values.get( EJB_LINK ).length() > 0 )
253 		{
254 			WsxmlType webservices = wsdl2Java.addNewWebservices();
255 			webservices.setEjbLink( values.get( EJB_LINK ) );
256 			webservices.setAppend( values.getBoolean( APPEND ) );
257 		}
258 		else if( values.get( SERVLET_LINK ) != null && values.get( SERVLET_LINK ).length() > 0 )
259 		{
260 			WsxmlType webservices = wsdl2Java.addNewWebservices();
261 			webservices.setServletLink( values.get( SERVLET_LINK ) );
262 			webservices.setAppend( values.getBoolean( APPEND ) );
263 		}
264 
265 		String mappingFile = values.get( MAPPING ).toString().trim();
266 		if( mappingFile.length() > 0 )
267 		{
268 			wsdl2Java.addNewMapping().setFile( mappingFile );
269 		}
270 		return configDocument;
271 	}
272 
273 	private final class JBossWSShowConfigFileAction extends ShowConfigFileAction
274 	{
275 		private final Interface modelItem;
276 
277 		private JBossWSShowConfigFileAction( String title, String description, Interface modelItem )
278 		{
279 			super( title, description );
280 			this.modelItem = modelItem;
281 		}
282 
283 		protected String getConfigFile()
284 		{
285 			ConfigurationDocument configDocument = createConfigFile( dialog.getValues(), modelItem );
286 			return configDocument.toString();
287 		}
288 	}
289 
290 }