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 java.io.File;
16 import java.io.IOException;
17
18 import org.jboss.jbosswsTools.ConfigurationDocument;
19 import org.jboss.jbosswsTools.ConfigurationType;
20 import org.jboss.jbosswsTools.JavaToWsdlType;
21 import org.jboss.jbosswsTools.MappingType;
22 import org.jboss.jbosswsTools.NamespacesType;
23 import org.jboss.jbosswsTools.ServiceType;
24 import org.jboss.jbosswsTools.WsxmlType;
25 import org.jboss.jbosswsTools.ServiceType.ParameterStyle;
26 import org.jboss.jbosswsTools.ServiceType.Style;
27
28 import com.eviware.soapui.SoapUI;
29 import com.eviware.soapui.impl.wsdl.WsdlInterface;
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.RunnerContext;
34 import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ShowConfigFileAction;
35 import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
36 import com.eviware.soapui.impl.wsdl.support.HelpUrls;
37 import com.eviware.soapui.model.settings.SettingsListener;
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.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
48 /***
49 * Invokes jbossws wsdl2java tools
50 *
51 * @author Ole.Matzura
52 */
53
54 public class WSToolsRegenerateJava2WsdlAction extends AbstractToolsAction<WsdlInterface>
55 {
56 private static final String CLASSPATH = "Classpath";
57 private static final String OUTPUT = "Output Directory";
58 private static final String ENDPOINT = "Endpoint";
59 private static final String MAPPING = "Mapping file";
60 private static final String SERVICE_NAME = "Service Name";
61 private static final String STYLE = "Style";
62 private static final String PARAMETER_STYLE = "Parameter Style";
63 private static final String TARGET_NAMESPACE = "Target NS";
64 private static final String TYPES_NAMESPACE = "Types NS";
65 private static final String EJB_LINK = "ejb-link";
66 private static final String SERVLET_LINK = "servlet-link";
67 private InternalSettingsListener internalSettingsListener;
68
69 public WSToolsRegenerateJava2WsdlAction( WsdlInterface iface )
70 {
71 super( iface, "Regenerate with JBossWS", "Regenerates WSDL with the jbossws wstools utility");
72
73 setEnabled( iface.getSettings().getString( valuesSettingID, null ) != null );
74
75 internalSettingsListener = new InternalSettingsListener();
76 iface.getSettings().addSettingsListener( internalSettingsListener);
77 }
78
79 protected XFormDialog buildDialog()
80 {
81 XFormDialogBuilder builder = XFormFactory.createDialogBuilder("Regenerate JBossWS WSDL Artifacts");
82
83 XForm mainForm = builder.createForm( "Basic" );
84
85 mainForm.addTextField( ENDPOINT, "Serice Endpoint Interface", XForm.FieldType.JAVA_CLASS );
86 mainForm.addTextField( SERVICE_NAME, "The name of the generated Service", XForm.FieldType.TEXT );
87 mainForm.addComboBox( STYLE, new String [] {Style.DOCUMENT.toString(), Style.RPC.toString()}, "The style to use" );
88 mainForm.addComboBox( PARAMETER_STYLE, new String [] {ParameterStyle.BARE.toString(), ParameterStyle.WRAPPED.toString()}, "The style to use" );
89 mainForm.addTextField( CLASSPATH, "Classpath to use", XForm.FieldType.PROJECT_FOLDER );
90 mainForm.addTextField( OUTPUT, "The root directory for all emitted files.", XForm.FieldType.PROJECT_FOLDER );
91 mainForm.addTextField( MAPPING, "mapping file to generate", XForm.FieldType.PROJECT_FILE );
92 mainForm.addTextField( TARGET_NAMESPACE, "The target namespace for the generated WSDL", XForm.FieldType.TEXT );
93 mainForm.addTextField( TYPES_NAMESPACE, "The namespace for the generated types", XForm.FieldType.TEXT );
94 mainForm.addTextField( EJB_LINK, "The name of the source EJB to link to", XForm.FieldType.TEXT );
95 mainForm.addTextField( SERVLET_LINK, "The name of the source Servlet to link to", XForm.FieldType.TEXT );
96
97 buildArgsForm( builder, false, "wstools" );
98
99 ActionList actions = buildDefaultActions(HelpUrls.WSTOOLS_HELP_URL);
100 actions.addAction( new ShowConfigFileAction( "JBossWS Wsdl2Java", "Contents of generated wsconfig.xml file" )
101 {
102 protected String getConfigFile()
103 {
104 ConfigurationDocument configDocument = createConfigFile(dialog.getValues());
105 return configDocument.toString();
106 }});
107
108 return builder.buildDialog( actions,
109 "Specify arguments for JBossWS wstools java2wsdl functionality", UISupport.TOOL_ICON );
110 }
111
112 protected void generate(StringToStringMap values, ToolHost toolHost) throws Exception
113 {
114 String wstoolsDir = SoapUI.getSettings().getString( ToolsSettings.JBOSSWS_WSTOOLS_LOCATION, null );
115 if( Tools.isEmpty( wstoolsDir ))
116 {
117 UISupport.showErrorMessage( "wstools directory must be set in global preferences" );
118 return;
119 }
120
121 String wsToolsExtension = UISupport.isWindows() ? ".bat" : ".sh";
122
123 File wstoolsFile = new File( wstoolsDir + File.separatorChar + "wstools" + wsToolsExtension );
124 if( !wstoolsFile.exists() )
125 {
126 UISupport.showErrorMessage( "Could not find wstools script at [" + wstoolsFile + "]" );
127 return;
128 }
129
130 ProcessBuilder builder = new ProcessBuilder();
131 ArgumentBuilder args = buildArgs( UISupport.isWindows() );
132 builder.command(args.getArgs());
133 builder.directory(new File(wstoolsDir));
134
135 toolHost.run( new ToolRunner( builder, new File( values.get( OUTPUT )), values.get( SERVICE_NAME ) ));
136 }
137
138 private ArgumentBuilder buildArgs( boolean isWindows ) throws IOException
139 {
140 StringToStringMap values = dialog.getValues();
141 values.put( OUTPUT, Tools.ensureDir( values.get( OUTPUT ), "" ));
142
143 ArgumentBuilder builder = new ArgumentBuilder( values );
144 builder.startScript( "wstools" );
145
146 builder.addString( CLASSPATH, "-cp" );
147 builder.addArgs( "-config", buildConfigFile( values ) );
148 builder.addString( OUTPUT, "-dest" );
149 addToolArgs( values, builder );
150 return builder;
151 }
152
153 private String buildConfigFile(StringToStringMap values ) throws IOException
154 {
155 File file = File.createTempFile( "wstools-config", ".xml", new File( SoapUI.getSettings().getString( ToolsSettings.JBOSSWS_WSTOOLS_LOCATION, null )) );
156 ConfigurationDocument configDocument = createConfigFile(values);
157 configDocument.save( file );
158 return file.getName();
159 }
160
161 private ConfigurationDocument createConfigFile(StringToStringMap values)
162 {
163 ConfigurationDocument configDocument = ConfigurationDocument.Factory.newInstance();
164 ConfigurationType config = configDocument.addNewConfiguration();
165
166 JavaToWsdlType java2Wsdl = config.addNewJavaWsdl();
167 ServiceType service = java2Wsdl.addNewService();
168 service.setEndpoint( values.get( ENDPOINT ));
169 service.setStyle( Style.Enum.forString( values.get(STYLE)));
170 service.setParameterStyle( ParameterStyle.Enum.forString( values.get(PARAMETER_STYLE)));
171 service.setName( values.get( SERVICE_NAME ));
172
173 MappingType mapping = java2Wsdl.addNewMapping();
174 mapping.setFile( values.get( MAPPING ));
175
176 NamespacesType namespaces = java2Wsdl.addNewNamespaces();
177 namespaces.setTargetNamespace( values.get( TARGET_NAMESPACE ));
178 namespaces.setTypeNamespace( values.get( TYPES_NAMESPACE ));
179
180 WsxmlType webservices = java2Wsdl.addNewWebservices();
181 webservices.setEjbLink( values.get( EJB_LINK ));
182 webservices.setServletLink( values.get( SERVLET_LINK ));
183 return configDocument;
184 }
185
186 private final class InternalSettingsListener implements SettingsListener
187 {
188 public void settingChanged(String name, String newValue, String oldValue)
189 {
190 if( name.equals( valuesSettingID ))
191 {
192 setEnabled( newValue != null );
193 }
194 }
195 }
196
197 private class ToolRunner extends ProcessToolRunner
198 {
199 private final File outDir;
200 private final String serviceName;
201
202 public ToolRunner(ProcessBuilder builder, File outDir, String serviceName )
203 {
204 super(builder, "JBossWS wstools", modelItem );
205 this.outDir = outDir;
206 this.serviceName = serviceName;
207 }
208
209 protected void afterRun( RunnerContext context )
210 {
211 if( context.getStatus() != RunnerContext.RunnerStatus.FINISHED )
212 return;
213
214 try
215 {
216 boolean ifaces = modelItem.updateDefinition( "file:" + outDir.getAbsolutePath() +
217 File.separatorChar + "wsdl" + File.separatorChar + serviceName + ".wsdl", true );
218
219 if (ifaces)
220 {
221 context.log( "Updated Interface [" + modelItem.getName() + "]" );
222 SoapUI.selectModelItem(modelItem);
223 }
224 }
225 catch (Exception e)
226 {
227 e.printStackTrace();
228 }
229 }
230 }
231 }