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.tools;
14  
15  import java.io.File;
16  
17  import javax.swing.Action;
18  
19  import org.apache.commons.cli.CommandLine;
20  import org.apache.commons.cli.CommandLineParser;
21  import org.apache.commons.cli.HelpFormatter;
22  import org.apache.commons.cli.Options;
23  import org.apache.commons.cli.PosixParser;
24  import org.apache.log4j.Logger;
25  
26  import com.eviware.soapui.SoapUI;
27  import com.eviware.soapui.impl.wsdl.WsdlProject;
28  import com.eviware.soapui.impl.wsdl.actions.iface.tools.axis1.Axis1XWSDL2JavaAction;
29  import com.eviware.soapui.impl.wsdl.actions.iface.tools.axis2.Axis2WSDL2CodeAction;
30  import com.eviware.soapui.impl.wsdl.actions.iface.tools.dotnet.DotNetWsdlAction;
31  import com.eviware.soapui.impl.wsdl.actions.iface.tools.gsoap.GSoapAction;
32  import com.eviware.soapui.impl.wsdl.actions.iface.tools.jaxb.JaxbXjcAction;
33  import com.eviware.soapui.impl.wsdl.actions.iface.tools.jbossws.JBossWSConsumeAction;
34  import com.eviware.soapui.impl.wsdl.actions.iface.tools.jbossws.WSToolsWsdl2JavaAction;
35  import com.eviware.soapui.impl.wsdl.actions.iface.tools.oracle.OracleWsaGenProxyAction;
36  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.AbstractToolsAction;
37  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.RunnerContext;
38  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
39  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolRunner;
40  import com.eviware.soapui.impl.wsdl.actions.iface.tools.wscompile.WSCompileAction;
41  import com.eviware.soapui.impl.wsdl.actions.iface.tools.wsi.WSIAnalyzeAction;
42  import com.eviware.soapui.impl.wsdl.actions.iface.tools.wsimport.WSImportAction;
43  import com.eviware.soapui.impl.wsdl.actions.iface.tools.xfire.XFireAction;
44  import com.eviware.soapui.impl.wsdl.actions.iface.tools.xmlbeans.XmlBeans2Action;
45  import com.eviware.soapui.model.iface.Interface;
46  
47  /***
48   * Standalone tool-runner used from maven-plugin, can also be used from command-line (see xdocs) or
49   * directly from other classes.
50   * <p>
51   * For standalone usage, set the project file (with setProjectFile) and other desired properties before
52   * calling run</p> 
53   * 
54   * @author Ole.Matzura
55   */
56  
57  public class SoapUIToolRunner implements ToolHost, RunnerContext 
58  {
59  	private final static Logger log = Logger.getLogger( SoapUIToolRunner.class );
60  	
61  	private String projectFile;
62  	private String iface;
63  	private String tool;
64  
65  	private RunnerStatus status;
66  	public static String TITLE = "soapUI " + SoapUI.SOAPUI_VERSION + " Tool Runner";
67  	
68  	/***
69  	 * Runs the specified tool in the specified soapUI project file, see soapUI xdocs for details.
70  	 * 
71  	 * @param args
72  	 * @throws Exception
73  	 */
74  
75  	@SuppressWarnings("static-access")
76  	public static void main( String [] args) throws Exception
77  	{
78  		System.out.println( TITLE );
79  		SoapUI.initSoapUILog();
80  		SoapUIToolRunner runner = new SoapUIToolRunner();
81  		
82  		Options options = new Options();
83  		options.addOption( "i", true, "Sets the interface" );
84  		options.addOption( "t", true, "Sets the tool to run" );
85  		options.addOption( "s", false, "Sets the soapui-settings.xml file to use" );
86  		
87  		CommandLineParser parser = new PosixParser();
88  		CommandLine cmd = parser.parse( options, args);
89  		
90  		String[] args2 = cmd.getArgs();
91  		if( args2.length != 1 || !cmd.hasOption( "t") )
92  		{
93  			HelpFormatter formatter = new HelpFormatter();
94  			formatter.printHelp( "toolrunner [options] <soapui-project-file>", options );
95  
96  			System.err.println( "Missing soapUI project file.." );
97  			return;
98  		}
99  		
100 		runner.setProjectFile( args2[0] );
101 		runner.setTool( cmd.getOptionValue( "t") );
102 		
103 		if( cmd.hasOption( "i"))
104 			runner.setInterface( cmd.getOptionValue( "i" ) );
105 		
106 		if( cmd.hasOption( "s"))
107 			SoapUI.initSettings( cmd.getOptionValue( "s" ));
108 
109 		try
110 		{
111 			SoapUI.loadExtLibs();
112 			
113 			runner.run();
114 			System.exit( 0 );
115 		}
116 		catch (Exception e)
117 		{
118 			log.error( e.toString() );
119 			e.printStackTrace();
120 			System.exit( 1 );
121 		}		
122 	}
123 
124 	public void setProjectFile(String projectFile)
125 	{
126 		this.projectFile = projectFile;
127 	}
128 	
129 	/***
130 	 * Sets the tool(s) to run, can be a comma-seperated list
131 	 * 
132 	 * @param tool the tools to run
133 	 */
134 
135 	public void setTool(String tool)
136 	{
137 		this.tool = tool;
138 	}
139 
140 	public void setInterface(String iface)
141 	{
142 		this.iface = iface;
143 	}
144 
145 	public SoapUIToolRunner()
146 	{
147 	}
148 	
149 	public void run() throws Exception
150 	{
151 		if( !new File( projectFile ).exists() )
152 			throw new Exception( "soapUI project file [" + projectFile + "] not found" );
153 		
154 		WsdlProject project = new WsdlProject( projectFile, null );
155 		log.info( "Running tools [" + tool + "] for interface [" + iface + "] in project [" + project.getName() + "]" );
156 
157 		long startTime = System.nanoTime();
158 		
159 		for( int c = 0; c < project.getInterfaceCount(); c++ )
160 		{
161 			Interface i = project.getInterfaceAt( c );
162 			if( iface == null || i.getName().equals( iface ))
163 			{
164 				runTool( i );
165 			}
166 		}
167 		
168 		long timeTaken = (System.nanoTime()-startTime)/1000000;
169 		log.info( "time taken: " + timeTaken + "ms" );
170 	}
171 	
172 	/***
173 	 * Runs the configured tool(s) for the specified interface.. needs to be refactored to use
174 	 * some kind of registry/factory pattern for tools
175 	 * 
176 	 * @param iface
177 	 */
178 	
179 	public void runTool( Interface iface )
180 	{
181 		AbstractToolsAction<Interface> action = null;
182 		
183 		String [] tools = tool.split( "," );
184 		for( String tool : tools )
185 		{
186 			if( tool == null || tool.trim().length() == 0 )
187 				continue;
188 			
189 			if( tool.equals( "axis1" ))
190 			{
191 				action = new Axis1XWSDL2JavaAction( iface );
192 			}
193 			else if( tool.equals( "axis2" ))
194 			{
195 				action = new Axis2WSDL2CodeAction( iface );
196 			}
197 			else if( tool.equals( "dotnet" ))
198 			{
199 				action = new DotNetWsdlAction( iface );
200 			}
201 			else if( tool.equals( "gsoap" ))
202 			{
203 				action = new GSoapAction( iface );
204 			}
205 			else if( tool.equals( "jaxb" ))
206 			{
207 				action = new JaxbXjcAction( iface );
208 			}
209 			else if( tool.equals( "wstools" ))
210 			{
211 				action = new WSToolsWsdl2JavaAction( iface );
212 			}
213 			else if( tool.equals( "wscompile" ))
214 			{
215 				action = new WSCompileAction( iface );
216 			}
217 			else if( tool.equals( "wsimport" ))
218 			{
219 				action = new WSImportAction( iface );
220 			}
221 			else if( tool.equals( "wsconsume" ))
222 			{
223 				action = new JBossWSConsumeAction( iface );
224 			}
225 			else if( tool.equals( "xfire" ))
226 			{
227 				action = new XFireAction( iface );
228 			}
229 			else if( tool.equals( "xmlbeans" ))
230 			{
231 				action = new XmlBeans2Action( iface );
232 			}
233 			else if( tool.equals( "ora" ))
234 			{
235 				action = new OracleWsaGenProxyAction( iface );
236 			}
237 			else if( tool.equals( "wsi" ))
238 			{
239 				action = new WSIAnalyzeAction( iface );
240 			}
241 			
242 			try
243 			{
244 				log.info( "Running tool [" + action.getValue( Action.NAME ) + 
245 							"] for Interface [" + iface.getName() + "]" );
246 				action.run( this );
247 			}
248 			catch (Exception e)
249 			{
250 				e.printStackTrace();
251 			}
252 		}
253 	}
254 
255 	public void run(ToolRunner runner) throws Exception
256 	{
257 		status = RunnerStatus.RUNNING;
258 		runner.setContext( this );
259 		runner.run();
260 	}
261 
262 	public RunnerStatus getStatus()
263 	{
264 		return status;
265 	}
266 
267 	public String getTitle()
268 	{
269 		return getClass().getSimpleName();
270 	}
271 
272 	public void log(String msg)
273 	{
274 		System.out.print( msg );
275 	}
276 
277    public void logError( String msg )
278    {
279       System.err.println( msg );
280    }
281 
282 	public void setStatus(RunnerStatus status)
283 	{
284 		this.status = status;
285 	}
286 
287    public void disposeContext()
288    {
289    }
290 }