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