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