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.WsdlProject;
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.iface.Interface;
38 import com.eviware.soapui.settings.ToolsSettings;
39 import com.eviware.soapui.support.SoapUIException;
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
49 /***
50 * Invokes jbossws java2wsdl tools
51 *
52 * @author Ole.Matzura
53 */
54
55 public class WSToolsJava2WsdlAction extends AbstractToolsAction<WsdlProject>
56 {
57 public static final String SOAPUI_ACTION_ID = "WSToolsJava2WsdlAction";
58
59 private static final String CLASSPATH = "Classpath";
60 private static final String OUTPUT = "Output Directory";
61 private static final String ENDPOINT = "Endpoint";
62 private static final String MAPPING = "Mapping file";
63 private static final String SERVICE_NAME = "Service Name";
64 private static final String STYLE = "Style";
65 private static final String PARAMETER_STYLE = "Parameter Style";
66 private static final String TARGET_NAMESPACE = "Target Namespace";
67 private static final String TYPES_NAMESPACE = "Types Namespace";
68 private static final String EJB_LINK = "ejb-link";
69 private static final String SERVLET_LINK = "servlet-link";
70
71 public WSToolsJava2WsdlAction()
72 {
73 super( "Generate WSDL with JBossWS", "Generates WSDL with the jbossws wstools utility");
74 }
75
76 protected XFormDialog buildDialog( WsdlProject project )
77 {
78 XFormDialogBuilder builder = XFormFactory.createDialogBuilder("Generate JBossWS WSDL Artifacts");
79
80 XForm mainForm = builder.createForm( "Basic" );
81
82 mainForm.addTextField( ENDPOINT, "Serice Endpoint Interface", XForm.FieldType.JAVA_CLASS );
83 mainForm.addTextField( SERVICE_NAME, "The name of the generated Service", XForm.FieldType.TEXT );
84 mainForm.addComboBox( STYLE, new String [] {Style.DOCUMENT.toString(), Style.RPC.toString()}, "The style to use" );
85 mainForm.addComboBox( PARAMETER_STYLE, new String [] {ParameterStyle.BARE.toString(), ParameterStyle.WRAPPED.toString()}, "The style to use" );
86 mainForm.addTextField( CLASSPATH, "Classpath to use", XForm.FieldType.PROJECT_FOLDER );
87 mainForm.addTextField( OUTPUT, "The root directory for all emitted files.", XForm.FieldType.PROJECT_FOLDER );
88 mainForm.addTextField( MAPPING, "mapping file to generate", XForm.FieldType.PROJECT_FILE );
89 mainForm.addTextField( TARGET_NAMESPACE, "The target namespace for the generated WSDL", XForm.FieldType.TEXT );
90 mainForm.addTextField( TYPES_NAMESPACE, "The namespace for the generated types", XForm.FieldType.TEXT );
91 mainForm.addTextField( EJB_LINK, "The name of the source EJB to link to", XForm.FieldType.TEXT );
92 mainForm.addTextField( SERVLET_LINK, "The name of the source Servlet to link to", XForm.FieldType.TEXT );
93
94 buildArgsForm( builder, false, "wstools" );
95
96 ActionList actions = buildDefaultActions( HelpUrls.WSTOOLS_HELP_URL, project );
97 actions.addAction( new ShowConfigFileAction( "JBossWS Java2Wsdl", "Contents of generated wsconfig.xml file" )
98 {
99 protected String getConfigFile()
100 {
101 ConfigurationDocument configDocument = createConfigFile(dialog.getValues());
102 return configDocument.toString();
103 }});
104
105 return builder.buildDialog( actions,
106 "Specify arguments for JBossWS wstools java2wsdl functionality", UISupport.TOOL_ICON );
107 }
108
109 protected void generate(StringToStringMap values, ToolHost toolHost, WsdlProject project ) 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 ), project ));
133 }
134
135 private ArgumentBuilder buildArgs( boolean isWindows ) throws IOException
136 {
137 StringToStringMap values = dialog.getValues();
138 values.put( OUTPUT, Tools.ensureDir( values.get( OUTPUT ), "" ));
139
140 ArgumentBuilder builder = new ArgumentBuilder( values );
141 builder.startScript( "wstools" );
142
143 builder.addString( CLASSPATH, "-cp" );
144 builder.addArgs( "-config", buildConfigFile( values ) );
145 builder.addString( OUTPUT, "-dest" );
146 addToolArgs( values, builder );
147 return builder;
148 }
149
150 private String buildConfigFile(StringToStringMap values ) throws IOException
151 {
152 File file = File.createTempFile( "wstools-config", ".xml" );
153 ConfigurationDocument configDocument = createConfigFile(values);
154
155 configDocument.save( file );
156
157 return file.getAbsolutePath();
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 WsdlProject modelItem;
192
193 public ToolRunner(ProcessBuilder builder, File outDir, String serviceName, WsdlProject modelItem )
194 {
195 super(builder, "JBossWS wstools", modelItem );
196 this.outDir = outDir;
197 this.serviceName = serviceName;
198 this.modelItem = modelItem;
199 }
200
201 protected void afterRun( RunnerContext context )
202 {
203 if( context.getStatus() != RunnerContext.RunnerStatus.FINISHED )
204 return;
205
206 try
207 {
208 String wsdlUrl = "file:" + outDir.getAbsolutePath() + File.separatorChar + "wsdl" + File.separatorChar + serviceName + ".wsdl";
209 Interface[] ifaces = modelItem.importWsdl( wsdlUrl, true );
210
211 if (ifaces.length > 0)
212 {
213 context.log( "Added Interface [" + ifaces[0].getName() + "] to project" );
214 ifaces[0].getSettings().setString( WSToolsRegenerateJava2WsdlAction.class.getName() + "@values",
215 dialog.getValues().toXml() );
216 UISupport.select(ifaces[0]);
217 }
218 }
219 catch (SoapUIException e)
220 {
221 SoapUI.logError( e );
222 }
223 }
224 }
225 }