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