View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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.IOException;
18  
19  import javax.swing.Action;
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.impl.wsdl.actions.iface.tools.support.AbstractToolsAction;
39  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ArgumentBuilder;
40  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ProcessToolRunner;
41  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.RunnerContext;
42  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
43  import com.eviware.soapui.model.iface.Interface;
44  import com.eviware.soapui.model.settings.Settings;
45  import com.eviware.soapui.settings.WSISettings;
46  import com.eviware.soapui.support.Tools;
47  import com.eviware.soapui.support.UISupport;
48  import com.eviware.soapui.support.types.StringToStringMap;
49  import com.eviware.soapui.ui.support.DefaultDesktopPanel;
50  
51  /***
52   * Invokes jbossws wsdl2java tools
53   * 
54   * @author Ole.Matzura
55   */
56  
57  public class WSIAnalyzeAction extends AbstractToolsAction<Interface>
58  {
59  	public final static Logger log = Logger.getLogger( WSIAnalyzeAction.class );
60  	
61  	private String configFile;
62  	
63     public WSIAnalyzeAction( Interface iface )
64     {
65        super( iface, "Check WSI Compliance", "Validate this WSDL for WSI Basic Profile compliance");
66        putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( "menu W" ));
67     }
68  
69  	protected void generate(StringToStringMap values, ToolHost toolHost) throws Exception
70  	{
71  		String wsiDir = SoapUI.getSettings().getString( WSISettings.WSI_LOCATION, null );
72  		if( Tools.isEmpty( wsiDir ) )
73  		{
74  			UISupport.showErrorMessage( "WSI Test Tools directory must be set in global preferences" );
75  			return;
76  		}
77  		
78  		ProcessBuilder builder = new ProcessBuilder();
79  		
80  		File reportFile = File.createTempFile( "wsi-report", ".xml" );
81  		
82  		ArgumentBuilder args = buildArgs( reportFile );
83  		builder.command(args.getArgs());
84  		File wsiToolDir = new File(wsiDir + File.separatorChar + "cs" + File.separatorChar + "bin" );
85  		if( !wsiToolDir.exists() )
86  			wsiToolDir = new File(wsiDir + File.separatorChar + "java" + File.separatorChar + "bin" );
87  		
88  		builder.directory(wsiToolDir);
89  		
90  		toolHost.run( new WSIProcessToolRunner( builder, reportFile ));
91  	}
92  
93  	private ArgumentBuilder buildArgs(File reportFile) throws IOException
94  	{
95  		Settings settings = modelItem.getSettings();
96  		
97  		ConfigurationDocument configDoc = createConfigFile(reportFile, settings);
98  		configFile = configDoc.toString();
99  		
100 		File file = File.createTempFile( "wsi-analyzer-config", ".xml" );
101 		
102 		configDoc.save( file );
103 		
104 		ArgumentBuilder builder = new ArgumentBuilder( new StringToStringMap() );
105 		builder.startScript( "Analyzer", "", ".sh" );
106 			
107 		builder.addArgs( "-config", file.getAbsolutePath() );
108 		
109 		// add this to command-line due to bug in wsi-tools (?)
110 		if( settings.getBoolean( WSISettings.ASSERTION_DESCRIPTION ))
111 			builder.addArgs( "-assertionDescription", "true" );
112 		
113 		return builder;
114 	}
115 
116 	private ConfigurationDocument createConfigFile(File reportFile, Settings settings)
117 	{
118 		ConfigurationDocument configDoc = ConfigurationDocument.Factory.newInstance();
119 		Configuration config = configDoc.addNewConfiguration();
120 		
121 		config.setVerbose( settings.getBoolean( WSISettings.VERBOSE ) );
122 		AssertionResults results = config.addNewAssertionResults();
123 		results.setType( 
124 				AssertionResults.Type.Enum.forString( 
125 						settings.getString( WSISettings.RESULTS_TYPE, AssertionResults.Type.ONLY_FAILED.toString() )));
126 		
127 		results.setMessageEntry( settings.getBoolean( WSISettings.MESSAGE_ENTRY ) );
128 		results.setFailureMessage( settings.getBoolean( WSISettings.FAILURE_MESSAGE ) );
129 		results.setAssertionDescription( settings.getBoolean( WSISettings.ASSERTION_DESCRIPTION ) );
130 
131 		ReportFile report = config.addNewReportFile();
132 		report.setLocation( reportFile.getAbsolutePath() );
133 		report.setReplace( true );
134 		AddStyleSheet stylesheet = report.addNewAddStyleSheet();
135 		stylesheet.setHref( ".//..//common//Profiles//SSBP10_BP11_TAD.xml" );
136 		stylesheet.setType( "text/xsl" );
137 		stylesheet.setAlternate( false );
138 		
139 		config.setTestAssertionsFile( "../../common/profiles/SSBP10_BP11_TAD.xml" );
140 		
141 		WsdlReference wsdlRef = config.addNewWsdlReference();
142 		
143 		StringToStringMap values = new StringToStringMap();
144 		values.put( WSDL, modelItem.getDefinition() );
145 		values.put( CACHED_WSDL, Boolean.toString( modelItem.isCached() ));
146 		
147 		wsdlRef.setWsdlURI( getWsdlUrl( values ) );
148 		WsdlElementReference wsdlElement = wsdlRef.addNewWsdlElement();
149 		wsdlElement.setType( WsdlElementType.BINDING );
150 		wsdlElement.setStringValue( modelItem.getBindingName().getLocalPart() );
151 		wsdlElement.setNamespace( modelItem.getBindingName().getNamespaceURI() );
152 		return configDoc;
153 	}
154 	
155 	public static File transformReport( File reportFile ) throws Exception
156 	{
157 		String dir = SoapUI.getSettings().getString( WSISettings.WSI_LOCATION, null );
158       File xsltFile = new File( dir + File.separatorChar + "common" + File.separatorChar + "xsl" + 
159       		File.separatorChar + "report.xsl" );
160 
161       Source xmlSource = new StreamSource(reportFile);
162       Source xsltSource = new StreamSource(xsltFile);
163 
164 		TransformerFactory transFact = TransformerFactory.newInstance();
165 		Transformer trans = transFact.newTransformer(xsltSource);
166 
167 		String outputFolder = SoapUI.getSettings().getString( WSISettings.OUTPUT_FOLDER, null );
168 		File output = outputFolder == null || outputFolder.trim().length() == 0 ? null : new File( outputFolder );
169 		File tempFile = File.createTempFile( "wsi-report", ".html", output );
170 		trans.transform(xmlSource, new StreamResult( tempFile ));
171 		
172 		log.info( "WSI Report created at [" + tempFile.getAbsolutePath() + "]" );
173 		
174 		return tempFile;
175 	}
176 	
177 	private class WSIProcessToolRunner extends ProcessToolRunner
178 	{
179 		private File reportFile;
180 
181 		public WSIProcessToolRunner(ProcessBuilder builder, File reportFile )
182 		{
183 			super(builder, "WSI Analyzer", modelItem);
184 			this.reportFile = reportFile;
185 		}
186 		
187 		public String getDescription()
188 		{
189 			return "Running WSI Analysis tools..";
190 		}
191 
192 		protected void afterRun( int exitCode, RunnerContext context )
193 		{
194 			if( exitCode == 0 && context.getStatus() == RunnerContext.RunnerStatus.FINISHED )
195 			{
196 				try
197 				{
198 					reportFile = transformReport( reportFile );
199 				}
200 				catch( Exception e1 )
201 				{
202 					e1.printStackTrace();
203 				}
204 				
205 				SwingUtilities.invokeLater( new Runnable() 
206 				{
207 					public void run()
208 					{
209 						try
210 						{
211 							WSIReportPanel panel = new WSIReportPanel( reportFile, configFile, null );
212 							panel.setPreferredSize( new Dimension( 600, 400 ));
213 							
214 		               UISupport.showDesktopPanel( 
215 									new DefaultDesktopPanel( "WS-I Report", "WS-I Report for Interface [" + modelItem.getName() + "]", panel ));
216 						}
217 	      			catch( Exception e )
218 	      			{
219 	      				e.printStackTrace();
220 	      				UISupport.showErrorMessage( e );
221 	      			}
222 					}
223 				} );
224 			}
225 			
226 			closeDialog();
227 		}
228 		
229 		public boolean showLog()
230 		{
231 			return modelItem.getSettings().getBoolean( WSISettings.SHOW_LOG );
232 		}
233 	}
234 }