View Javadoc

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