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