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