View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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", XForm.FieldType.TEXT );
126 		ejbLinkField.addFormFieldListener( new XFormFieldListener() 
127 		{
128 			public void valueChanged( XFormField sourceField, String newValue, String oldValue )
129 			{
130 				servletLinkField.setEnabled( newValue.length() == 0 );
131 				appendField.setEnabled( newValue.length() > 0 );
132 			}} );
133 		
134 		servletLinkField = mainForm.addTextField( SERVLET_LINK, "The web.xml servlet-link that is used by Java Service Endpoints (WAR)", XForm.FieldType.TEXT );
135 		servletLinkField.addFormFieldListener( new XFormFieldListener() 
136 		{
137 			public void valueChanged( XFormField sourceField, String newValue, String oldValue )
138 			{
139 				ejbLinkField.setEnabled( newValue.length() == 0 );
140 				appendField.setEnabled( newValue.length() > 0 );
141 			}} );
142 
143 		appendField = mainForm.addCheckBox( APPEND, "append to existing file" );
144 		appendField.setEnabled( false );
145 		buildArgsForm( builder, false, "wstools" );
146       
147 		ActionList actions = buildDefaultActions(HelpUrls.WSTOOLS_HELP_URL, modelItem);
148 		actions.addAction( new JBossWSShowConfigFileAction( "JBossWS Wsdl2Java", "Contents of generated wsconfig.xml file", modelItem ));
149 		return builder.buildDialog( actions,
150       		"Specify arguments for JBossWS wstools wsdl2java functionality", UISupport.TOOL_ICON );
151 	}
152    
153 	protected void generate(StringToStringMap values, ToolHost toolHost, Interface modelItem ) throws Exception
154 	{
155 		String wstoolsDir = SoapUI.getSettings().getString( ToolsSettings.JBOSSWS_WSTOOLS_LOCATION, null );
156 		if( Tools.isEmpty( wstoolsDir ))
157 		{
158 			UISupport.showErrorMessage( "wstools directory must be set in global preferences" );
159 			return;
160 		}
161 		
162 		String wsToolsExtension = UISupport.isWindows() ? ".bat" : ".sh";
163 		
164 		File wstoolsFile = new File( wstoolsDir + File.separatorChar + "wstools" + wsToolsExtension );
165 		if( !wstoolsFile.exists() )
166 		{
167 			UISupport.showErrorMessage( "Could not find wstools script at [" + wstoolsFile + "]" );
168 			return;
169 		}
170 		
171 		ProcessBuilder builder = new ProcessBuilder();
172 		ArgumentBuilder args = buildArgs( UISupport.isWindows(), modelItem );
173 		builder.command(args.getArgs());
174 		builder.directory(new File(wstoolsDir));
175 		
176 		toolHost.run( new ProcessToolRunner( builder, "JBossWS wstools", modelItem ));
177 	}
178 
179 	private ArgumentBuilder buildArgs( boolean isWindows, Interface modelItem  ) throws IOException
180 	{
181 		StringToStringMap values = dialog.getValues();
182 		
183 		values.put( OUTPUT, Tools.ensureDir( values.get( OUTPUT ), "" ));
184 		
185 		ArgumentBuilder builder = new ArgumentBuilder( values );
186 		builder.startScript( "wstools" );
187 		
188 		builder.addArgs( "-config", buildConfigFile( values, modelItem ) );
189 		builder.addString( OUTPUT, "-dest" );
190 		addToolArgs( values, builder );
191 		return builder;
192 	}
193 
194 	private String buildConfigFile(StringToStringMap values, Interface modelItem  ) throws IOException
195 	{
196 		File file = File.createTempFile( "wstools-config", ".xml" );
197 		ConfigurationDocument configDocument = createConfigFile(values, modelItem);
198 		
199 		configDocument.save( file );
200 		
201 		return file.getAbsolutePath();
202 	}
203 
204 	private ConfigurationDocument createConfigFile(StringToStringMap values, Interface modelItem )
205 	{
206 		ConfigurationDocument configDocument = ConfigurationDocument.Factory.newInstance();
207 		ConfigurationType config = configDocument.addNewConfiguration();
208 		
209 		try
210 		{
211 			StringToStringMap nsMappings = StringToStringMap.fromXml(values.get(NAMESPACE_MAPPING));
212 			if (!nsMappings.isEmpty())
213 			{
214 				GlobalType global = config.addNewGlobal();
215 				
216 				for (String namespace : nsMappings.keySet())
217 				{
218 					PkgNSType entry = global.addNewPackageNamespace();
219 					entry.setNamespace( namespace );
220 					entry.setPackage( nsMappings.get( namespace ));
221 				}
222 			}
223 		}
224 		catch (Exception e)
225 		{
226 			SoapUI.logError( e );
227 		}		
228 		
229 		WsdlToJavaType wsdl2Java = config.addNewWsdlJava();
230 		
231 		String wsdlUrl = getWsdlUrl( values, modelItem );
232 		try
233 		{
234 			new URL( wsdlUrl );
235 			wsdl2Java.setLocation( wsdlUrl );
236 		}
237 		catch( MalformedURLException e )
238 		{
239 			((Element)wsdl2Java.getDomNode()).setAttribute( "file", wsdlUrl );
240 		}
241 		
242 		if( values.getBoolean( UNWRAP ))
243 			wsdl2Java.setParameterStyle( ParameterStyle.BARE );
244 		else
245 			wsdl2Java.setParameterStyle( ParameterStyle.WRAPPED );
246 
247 		if( values.get( EJB_LINK ) != null && values.get( EJB_LINK ).length() > 0 )
248 		{
249 			WsxmlType webservices = wsdl2Java.addNewWebservices();
250 			webservices.setEjbLink( values.get( EJB_LINK ) );
251 			webservices.setAppend( values.getBoolean( APPEND ) );
252 		}
253 		else if( values.get( SERVLET_LINK ) != null && values.get( SERVLET_LINK ).length() > 0)
254 		{
255 			WsxmlType webservices = wsdl2Java.addNewWebservices();
256 			webservices.setServletLink( values.get( SERVLET_LINK ) );
257 			webservices.setAppend( values.getBoolean( APPEND ) );
258 		}
259 		
260 		String mappingFile = values.get( MAPPING ).toString().trim();
261 		if( mappingFile.length() > 0 )
262 		{
263 			wsdl2Java.addNewMapping().setFile( mappingFile );
264 		}
265 		return configDocument;
266 	}
267 
268 	private final class JBossWSShowConfigFileAction extends ShowConfigFileAction
269 	{
270 		private final Interface modelItem;
271 
272 		private JBossWSShowConfigFileAction( String title, String description, Interface modelItem )
273 		{
274 			super( title, description );
275 			this.modelItem = modelItem;
276 		}
277 
278 		protected String getConfigFile()
279 		{
280 			ConfigurationDocument configDocument = createConfigFile(dialog.getValues(), modelItem);
281 			return configDocument.toString();
282 		}
283 	}
284 
285 }