View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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.wsi;
14  
15  import java.awt.Dimension;
16  import java.io.File;
17  import java.io.FileWriter;
18  import java.io.IOException;
19  
20  import javax.swing.SwingUtilities;
21  import javax.xml.transform.Source;
22  import javax.xml.transform.Transformer;
23  import javax.xml.transform.TransformerFactory;
24  import javax.xml.transform.stream.StreamResult;
25  import javax.xml.transform.stream.StreamSource;
26  
27  import org.apache.log4j.Logger;
28  import org.wsI.testing.x2003.x03.common.AddStyleSheet;
29  import org.wsI.testing.x2004.x07.analyzerConfig.AssertionResults;
30  import org.wsI.testing.x2004.x07.analyzerConfig.Configuration;
31  import org.wsI.testing.x2004.x07.analyzerConfig.ConfigurationDocument;
32  import org.wsI.testing.x2004.x07.analyzerConfig.ReportFile;
33  import org.wsI.testing.x2004.x07.analyzerConfig.WsdlElementReference;
34  import org.wsI.testing.x2004.x07.analyzerConfig.WsdlElementType;
35  import org.wsI.testing.x2004.x07.analyzerConfig.WsdlReference;
36  
37  import com.eviware.soapui.SoapUI;
38  import com.eviware.soapui.actions.SoapUIPreferencesAction;
39  import com.eviware.soapui.impl.wsdl.WsdlInterface;
40  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.AbstractToolsAction;
41  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ArgumentBuilder;
42  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ProcessToolRunner;
43  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.RunnerContext;
44  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
45  import com.eviware.soapui.model.iface.Interface;
46  import com.eviware.soapui.model.settings.Settings;
47  import com.eviware.soapui.settings.WSISettings;
48  import com.eviware.soapui.support.Tools;
49  import com.eviware.soapui.support.UISupport;
50  import com.eviware.soapui.support.types.StringToStringMap;
51  import com.eviware.soapui.ui.support.DefaultDesktopPanel;
52  
53  /***
54   * Invokes WS-I Analyzer Tool
55   * 
56   * @author Ole.Matzura
57   */
58  
59  public class WSIAnalyzeAction extends AbstractToolsAction<Interface>
60  {
61  	public final static String SOAPUI_ACTION_ID = "WSIAnalyzeAction";
62  	public final static Logger log = Logger.getLogger( WSIAnalyzeAction.class );
63  
64  	private String configFile;
65  	private String wsiDir;
66  
67  	public WSIAnalyzeAction()
68  	{
69  		super( "Check WSI Compliance", "Validate this WSDL for WSI Basic Profile compliance" );
70  	}
71  
72  	protected void generate( StringToStringMap values, ToolHost toolHost, Interface modelItem ) throws Exception
73  	{
74  		wsiDir = SoapUI.getSettings().getString( WSISettings.WSI_LOCATION,
75  				System.getProperty( "wsi.dir", System.getenv( "WSI_HOME" ) ) );
76  		if( Tools.isEmpty( wsiDir ) )
77  		{
78  			UISupport.showErrorMessage( "WSI Test Tools directory must be set in global preferences" );
79  
80  			if( UISupport.getMainFrame() != null )
81  			{
82  				if( SoapUIPreferencesAction.getInstance().show( SoapUIPreferencesAction.WS_I_SETTINGS ) )
83  				{
84  					wsiDir = SoapUI.getSettings().getString( WSISettings.WSI_LOCATION, null );
85  				}
86  			}
87  		}
88  
89  		if( Tools.isEmpty( wsiDir ) )
90  			return;
91  
92  		ProcessBuilder builder = new ProcessBuilder();
93  
94  		File reportFile = File.createTempFile( "wsi-report", ".xml" );
95  
96  		File wsiToolDir = new File( wsiDir + File.separatorChar + "cs" + File.separatorChar + "bin" );
97  		if( !wsiToolDir.exists() )
98  			wsiToolDir = new File( wsiDir + File.separatorChar + "java" + File.separatorChar + "bin" );
99  
100 		ArgumentBuilder args = buildArgs( wsiToolDir, reportFile, modelItem );
101 		builder.command( args.getArgs() );
102 		builder.directory( wsiToolDir );
103 
104 		toolHost.run( new WSIProcessToolRunner( builder, reportFile, modelItem ) );
105 	}
106 
107 	private ArgumentBuilder buildArgs( File wsiToolDir, File reportFile, Interface modelItem ) throws IOException
108 	{
109 		Settings settings = modelItem.getSettings();
110 
111 		ConfigurationDocument configDoc = createConfigFile( reportFile, settings, ( WsdlInterface )modelItem );
112 		configFile = configDoc.toString();
113 
114 		File file = File.createTempFile( "wsi-analyzer-config", ".xml" );
115 
116 		configDoc.save( file );
117 
118 		ArgumentBuilder builder = new ArgumentBuilder( new StringToStringMap() );
119 		builder.startScript( wsiToolDir.getAbsolutePath() + File.separator + "Analyzer", ".bat", ".sh" );
120 
121 		builder.addArgs( "-config", file.getAbsolutePath() );
122 
123 		// add this to command-line due to bug in wsi-tools (?)
124 		if( settings.getBoolean( WSISettings.ASSERTION_DESCRIPTION ) )
125 			builder.addArgs( "-assertionDescription", "true" );
126 
127 		return builder;
128 	}
129 
130 	private ConfigurationDocument createConfigFile( File reportFile, Settings settings, WsdlInterface iface )
131 	{
132 		ConfigurationDocument configDoc = ConfigurationDocument.Factory.newInstance();
133 		Configuration config = configDoc.addNewConfiguration();
134 
135 		config.setVerbose( settings.getBoolean( WSISettings.VERBOSE ) );
136 		AssertionResults results = config.addNewAssertionResults();
137 		results.setType( AssertionResults.Type.Enum.forString( settings.getString( WSISettings.RESULTS_TYPE,
138 				AssertionResults.Type.ONLY_FAILED.toString() ) ) );
139 
140 		results.setMessageEntry( settings.getBoolean( WSISettings.MESSAGE_ENTRY ) );
141 		results.setFailureMessage( settings.getBoolean( WSISettings.FAILURE_MESSAGE ) );
142 		results.setAssertionDescription( settings.getBoolean( WSISettings.ASSERTION_DESCRIPTION ) );
143 
144 		ReportFile report = config.addNewReportFile();
145 		report.setLocation( reportFile.getAbsolutePath() );
146 		report.setReplace( true );
147 		AddStyleSheet stylesheet = report.addNewAddStyleSheet();
148 		stylesheet.setHref( "./../common/Profiles/SSBP10_BP11_TAD.xml" );
149 		stylesheet.setType( "text/xsl" );
150 		stylesheet.setAlternate( false );
151 
152 		config.setTestAssertionsFile( "../../common/profiles/SSBP10_BP11_TAD.xml" );
153 
154 		WsdlReference wsdlRef = config.addNewWsdlReference();
155 
156 		StringToStringMap values = new StringToStringMap();
157 		values.put( WSDL, iface.getDefinition() );
158 		values.put( CACHED_WSDL, Boolean.toString( iface.isCached() ) );
159 
160 		wsdlRef.setWsdlURI( getWsdlUrl( values, iface ) );
161 		WsdlElementReference wsdlElement = wsdlRef.addNewWsdlElement();
162 		wsdlElement.setType( WsdlElementType.BINDING );
163 		wsdlElement.setStringValue( iface.getBindingName().getLocalPart() );
164 		wsdlElement.setNamespace( iface.getBindingName().getNamespaceURI() );
165 		return configDoc;
166 	}
167 
168 	protected void showReport( File reportFile, String configFile ) throws Exception
169 	{
170 		WSIReportPanel panel = new WSIReportPanel( reportFile, configFile, null, true );
171 		panel.setPreferredSize( new Dimension( 600, 400 ) );
172 
173 		UISupport.showDesktopPanel( new DefaultDesktopPanel( "WS-I Report", "WS-I Report for Interface ["
174 				+ getModelItem().getName() + "]", panel ) );
175 	}
176 
177 	public static File transformReport( File reportFile ) throws Exception
178 	{
179 		String dir = SoapUI.getSettings().getString( WSISettings.WSI_LOCATION, null );
180 		File xsltFile = new File( dir + File.separatorChar + "common" + File.separatorChar + "xsl" + File.separatorChar
181 				+ "report.xsl" );
182 
183 		Source xmlSource = new StreamSource( reportFile );
184 		Source xsltSource = new StreamSource( xsltFile );
185 
186 		TransformerFactory transFact = TransformerFactory.newInstance();
187 		Transformer trans = transFact.newTransformer( xsltSource );
188 
189 		String outputFolder = SoapUI.getSettings().getString( WSISettings.OUTPUT_FOLDER, null );
190 		File output = outputFolder == null || outputFolder.trim().length() == 0 ? null : new File( outputFolder );
191 
192 		File tempFile = File.createTempFile( "wsi-report", ".html", output );
193 		trans.transform( xmlSource, new StreamResult( new FileWriter( tempFile ) ) );
194 
195 		log.info( "WSI Report created at [" + tempFile.getAbsolutePath() + "]" );
196 
197 		return tempFile;
198 	}
199 
200 	private class WSIProcessToolRunner extends ProcessToolRunner
201 	{
202 		private File reportFile;
203 		private final Interface modelItem;
204 
205 		public WSIProcessToolRunner( ProcessBuilder builder, File reportFile, Interface modelItem )
206 		{
207 			super( builder, "WSI Analyzer", modelItem );
208 			this.reportFile = reportFile;
209 			this.modelItem = modelItem;
210 		}
211 
212 		public String getDescription()
213 		{
214 			return "Running WSI Analysis tools..";
215 		}
216 
217 		protected void afterRun( int exitCode, RunnerContext context )
218 		{
219 			if( exitCode == 0 && context.getStatus() == RunnerContext.RunnerStatus.FINISHED )
220 			{
221 				try
222 				{
223 					reportFile = transformReport( reportFile );
224 				}
225 				catch( Exception e1 )
226 				{
227 					SoapUI.logError( e1 );
228 				}
229 
230 				SwingUtilities.invokeLater( new Runnable()
231 				{
232 					public void run()
233 					{
234 						try
235 						{
236 							showReport( reportFile, configFile );
237 						}
238 						catch( Exception e )
239 						{
240 							UISupport.showErrorMessage( e );
241 						}
242 					}
243 				} );
244 			}
245 
246 			closeDialog( modelItem );
247 		}
248 
249 		public boolean showLog()
250 		{
251 			return modelItem.getSettings().getBoolean( WSISettings.SHOW_LOG );
252 		}
253 
254 		@Override
255 		protected void beforeProcess( ProcessBuilder processBuilder, RunnerContext context )
256 		{
257 			processBuilder.environment().put( "WSI_HOME", wsiDir );
258 		}
259 	}
260 }