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