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.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.addComboBox( STYLE, new String [] {Style.DOCUMENT.toString(), Style.RPC.toString()},  "The style to use" );
82  		mainForm.addComboBox( PARAMETER_STYLE, new String [] {ParameterStyle.BARE.toString(), ParameterStyle.WRAPPED.toString()},  "The style to use" );
83  		mainForm.addTextField( CLASSPATH, "Classpath to use", XForm.FieldType.PROJECT_FOLDER );
84  		mainForm.addTextField( OUTPUT, "The root directory for all emitted files.", XForm.FieldType.PROJECT_FOLDER );
85  		mainForm.addTextField( MAPPING, "mapping file to generate", XForm.FieldType.PROJECT_FILE );
86  		mainForm.addTextField( TARGET_NAMESPACE, "The target namespace for the generated WSDL", XForm.FieldType.TEXT );
87  		mainForm.addTextField( TYPES_NAMESPACE, "The namespace for the generated types", XForm.FieldType.TEXT );
88  		mainForm.addTextField( EJB_LINK, "The name of the source EJB to link to", XForm.FieldType.TEXT );
89  		mainForm.addTextField( SERVLET_LINK, "The name of the source Servlet to link to", XForm.FieldType.TEXT );
90  
91        buildArgsForm( builder, false, "wstools" );
92        
93  		ActionList actions = buildDefaultActions(HelpUrls.WSTOOLS_HELP_URL, modelItem);
94  		actions.addAction( new ShowConfigFileAction( "JBossWS Wsdl2Java", "Contents of generated wsconfig.xml file" )
95  		{
96  			protected String getConfigFile()
97  			{
98  				ConfigurationDocument configDocument = createConfigFile(dialog.getValues());
99  				return configDocument.toString();
100 			}});
101 		
102 		return builder.buildDialog( actions,
103       		"Specify arguments for JBossWS wstools java2wsdl functionality", UISupport.TOOL_ICON );
104 	}
105    
106 	protected void generate(StringToStringMap values, ToolHost toolHost, WsdlInterface modelItem ) throws Exception
107 	{
108 		String wstoolsDir = SoapUI.getSettings().getString( ToolsSettings.JBOSSWS_WSTOOLS_LOCATION, null );
109 		if( Tools.isEmpty( wstoolsDir ))
110 		{
111 			UISupport.showErrorMessage( "wstools directory must be set in global preferences" );
112 			return;
113 		}
114 		
115 		String wsToolsExtension = UISupport.isWindows() ? ".bat" : ".sh";
116 		
117 		File wstoolsFile = new File( wstoolsDir + File.separatorChar + "wstools" + wsToolsExtension );
118 		if( !wstoolsFile.exists() )
119 		{
120 			UISupport.showErrorMessage( "Could not find wstools script at [" + wstoolsFile + "]" );
121 			return;
122 		}
123 		
124 		ProcessBuilder builder = new ProcessBuilder();
125 		ArgumentBuilder args = buildArgs( UISupport.isWindows() );
126 		builder.command(args.getArgs());
127 		builder.directory(new File(wstoolsDir));
128 		
129 		toolHost.run( new ToolRunner( builder, new File( values.get( OUTPUT )), values.get( SERVICE_NAME ), modelItem ));
130 	}
131 
132 	private ArgumentBuilder buildArgs( boolean isWindows ) throws IOException
133 	{
134 		StringToStringMap values = dialog.getValues();
135 		values.put( OUTPUT, Tools.ensureDir( values.get( OUTPUT ), "" ));
136 		
137 		ArgumentBuilder builder = new ArgumentBuilder( values );
138 		builder.startScript( "wstools" );
139 		
140 		builder.addString( CLASSPATH, "-cp" );
141 		builder.addArgs( "-config", buildConfigFile( values ) );
142 		builder.addString( OUTPUT, "-dest" );
143 		addToolArgs( values, builder );
144 		return builder;
145 	}
146 
147 	private String buildConfigFile(StringToStringMap values ) throws IOException
148 	{
149 		File file = File.createTempFile( "wstools-config", ".xml", new File( SoapUI.getSettings().getString( ToolsSettings.JBOSSWS_WSTOOLS_LOCATION, null )) );
150 		ConfigurationDocument configDocument = createConfigFile(values);
151 		configDocument.save( file );
152 		return file.getName();
153 	}
154 
155 	private ConfigurationDocument createConfigFile(StringToStringMap values)
156 	{
157 		ConfigurationDocument configDocument = ConfigurationDocument.Factory.newInstance();
158 		ConfigurationType config = configDocument.addNewConfiguration();
159 		
160 		JavaToWsdlType java2Wsdl = config.addNewJavaWsdl();
161 		ServiceType service = java2Wsdl.addNewService();
162 		service.setEndpoint( values.get( ENDPOINT ));
163 		service.setStyle( Style.Enum.forString( values.get(STYLE)));
164 		service.setParameterStyle( ParameterStyle.Enum.forString( values.get(PARAMETER_STYLE)));
165 		service.setName( values.get( SERVICE_NAME ));
166 		
167 		MappingType mapping = java2Wsdl.addNewMapping();
168 		mapping.setFile( values.get( MAPPING ));
169 		
170 		NamespacesType namespaces = java2Wsdl.addNewNamespaces();
171 		namespaces.setTargetNamespace( values.get( TARGET_NAMESPACE ));
172 		namespaces.setTypeNamespace( values.get( TYPES_NAMESPACE ));
173 		
174 		WsxmlType webservices = java2Wsdl.addNewWebservices();
175 		if( values.get( EJB_LINK ) != null && values.get( EJB_LINK ).length() > 0 )
176 			webservices.setEjbLink( values.get( EJB_LINK ));
177 		if( values.get( SERVLET_LINK ) != null && values.get( SERVLET_LINK ).length() > 0 )
178 			webservices.setServletLink( values.get( SERVLET_LINK ));
179 		return configDocument;
180 	}
181 	
182 	private class ToolRunner extends ProcessToolRunner
183 	{
184 		private final File outDir;
185 		private final String serviceName;
186 		private final WsdlInterface modelItem;
187 
188 		public ToolRunner(ProcessBuilder builder, File outDir, String serviceName, WsdlInterface modelItem  )
189 		{
190 			super(builder, "JBossWS wstools", modelItem );
191 			this.outDir = outDir;
192 			this.serviceName = serviceName;
193 			this.modelItem = modelItem;
194 		}
195 
196 		protected void afterRun( RunnerContext context )
197 		{
198 			if( context.getStatus() != RunnerContext.RunnerStatus.FINISHED )
199 				return;
200 			
201 			try
202 			{
203 				boolean ifaces = modelItem.updateDefinition( "file:" + outDir.getAbsolutePath() + 
204 						File.separatorChar + "wsdl" + File.separatorChar + serviceName + ".wsdl", true );
205 				
206 				if (ifaces)
207 				{
208 					context.log( "Updated Interface [" + modelItem.getName() + "]" );
209 					UISupport.select(modelItem);
210 				}
211 				else
212 				{
213 					UISupport.showErrorMessage( "Failed to update Interface from generated WSDL" );
214 				}
215 			}
216 			catch (Exception e)
217 			{
218 				SoapUI.logError( e );
219 			}
220 		}
221 	}
222 }