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