View Javadoc

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