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 boolean 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 		return true;
123 	}
124 	
125 	/***
126 	 * Runs the configured tool(s) for the specified interface.. needs to be refactored to use
127 	 * some kind of registry/factory pattern for tools
128 	 * 
129 	 * @param iface
130 	 */
131 	
132 	public void runTool( Interface iface )
133 	{
134 		AbstractToolsAction<Interface> action = null;
135 		
136 		String [] tools = tool.split( "," );
137 		for( String tool : tools )
138 		{
139 			if( tool == null || tool.trim().length() == 0 )
140 				continue;
141 			
142 			if( tool.equals( "axis1" ))
143 			{
144 				action = new Axis1XWSDL2JavaAction();
145 			}
146 			else if( tool.equals( "axis2" ))
147 			{
148 				action = new Axis2WSDL2CodeAction();
149 			}
150 			else if( tool.equals( "dotnet" ))
151 			{
152 				action = new DotNetWsdlAction();
153 			}
154 			else if( tool.equals( "gsoap" ))
155 			{
156 				action = new GSoapAction();
157 			}
158 			else if( tool.equals( "jaxb" ))
159 			{
160 				action = new JaxbXjcAction();
161 			}
162 			else if( tool.equals( "wstools" ))
163 			{
164 				action = new WSToolsWsdl2JavaAction();
165 			}
166 			else if( tool.equals( "wscompile" ))
167 			{
168 				action = new WSCompileAction();
169 			}
170 			else if( tool.equals( "wsimport" ))
171 			{
172 				action = new WSImportAction();
173 			}
174 			else if( tool.equals( "wsconsume" ))
175 			{
176 				action = new JBossWSConsumeAction();
177 			}
178 			else if( tool.equals( "xfire" ))
179 			{
180 				action = new XFireAction();
181 			}
182 			else if( tool.equals( "cxf" ))
183 			{
184 				action = new CXFAction();
185 			}
186 			else if( tool.equals( "xmlbeans" ))
187 			{
188 				action = new XmlBeans2Action();
189 			}
190 			else if( tool.equals( "ora" ))
191 			{
192 				action = new OracleWsaGenProxyAction();
193 			}
194 			else if( tool.equals( "wsi" ))
195 			{
196 				action = new WSIAnalyzeAction();
197 			}
198 			
199 			try
200 			{
201 				log.info( "Running tool [" + tool + 
202 							"] for Interface [" + iface.getName() + "]" );
203 				action.perform( iface, null );
204 			}
205 			catch (Exception e)
206 			{
207 				SoapUI.logError( e );
208 			}
209 		}
210 	}
211 
212 	public void run(ToolRunner runner) throws Exception
213 	{
214 		status = RunnerStatus.RUNNING;
215 		runner.setContext( this );
216 		runner.run();
217 	}
218 
219 	public RunnerStatus getStatus()
220 	{
221 		return status;
222 	}
223 
224 	public String getTitle()
225 	{
226 		return getClass().getSimpleName();
227 	}
228 
229 	public void log(String msg)
230 	{
231 		System.out.print( msg );
232 	}
233 
234    public void logError( String msg )
235    {
236       System.err.println( msg );
237    }
238 
239 	public void setStatus(RunnerStatus status)
240 	{
241 		this.status = status;
242 	}
243 
244    public void disposeContext()
245    {
246    }
247 
248 	@Override
249 	protected SoapUIOptions initCommandLineOptions()
250 	{
251 		SoapUIOptions options = new SoapUIOptions( "toolrunner" );
252 		options.addOption( "i", true, "Sets the interface" );
253 		options.addOption( "t", true, "Sets the tool to run" );
254 		options.addOption( "s", false, "Sets the soapui-settings.xml file to use" );
255 		return options;
256 	}
257 
258 	@Override
259 	protected boolean processCommandLine( CommandLine cmd )
260 	{
261 		setTool( cmd.getOptionValue( "t") );
262 		
263 		if( cmd.hasOption( "i"))
264 			setInterface( cmd.getOptionValue( "i" ) );
265 		
266 		if( cmd.hasOption( "s"))
267 			setSettingsFile( cmd.getOptionValue( "s" ));
268 		
269 		return true;
270 
271 	}
272 }