1
2
3
4
5
6
7
8
9
10
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 SoapUIToolRunner runner = new SoapUIToolRunner();
80
81 Options options = new Options();
82 options.addOption( "i", true, "Sets the interface" );
83 options.addOption( "t", true, "Sets the tool to run" );
84 options.addOption( "s", false, "Sets the soapui-settings.xml file to use" );
85
86 CommandLineParser parser = new PosixParser();
87 CommandLine cmd = parser.parse( options, args);
88
89 String[] args2 = cmd.getArgs();
90 if( args2.length != 1 || !cmd.hasOption( "t") )
91 {
92 HelpFormatter formatter = new HelpFormatter();
93 formatter.printHelp( "toolrunner [options] <soapui-project-file>", options );
94
95 System.err.println( "Missing soapUI project file.." );
96 return;
97 }
98
99 runner.setProjectFile( args2[0] );
100 runner.setTool( cmd.getOptionValue( "t") );
101
102 if( cmd.hasOption( "i"))
103 runner.setInterface( cmd.getOptionValue( "i" ) );
104
105 if( cmd.hasOption( "s"))
106 SoapUI.initSettings( cmd.getOptionValue( "s" ));
107
108 try
109 {
110 SoapUI.loadExtLibs();
111
112 runner.run();
113 System.exit( 0 );
114 }
115 catch (Exception e)
116 {
117 log.error( e.toString() );
118 e.printStackTrace();
119 System.exit( 1 );
120 }
121 }
122
123 public void setProjectFile(String projectFile)
124 {
125 this.projectFile = projectFile;
126 }
127
128 /***
129 * Sets the tool(s) to run, can be a comma-seperated list
130 *
131 * @param tool the tools to run
132 */
133
134 public void setTool(String tool)
135 {
136 this.tool = tool;
137 }
138
139 public void setInterface(String iface)
140 {
141 this.iface = iface;
142 }
143
144 public SoapUIToolRunner()
145 {
146 }
147
148 public void run() throws Exception
149 {
150 if( !new File( projectFile ).exists() )
151 throw new Exception( "soapUI project file [" + projectFile + "] not found" );
152
153 WsdlProject project = new WsdlProject( projectFile, null );
154 log.info( "Running tools [" + tool + "] for interface [" + iface + "] in project [" + project.getName() + "]" );
155
156 long startTime = System.nanoTime();
157
158 for( int c = 0; c < project.getInterfaceCount(); c++ )
159 {
160 Interface i = project.getInterfaceAt( c );
161 if( iface == null || i.getName().equals( iface ))
162 {
163 runTool( i );
164 }
165 }
166
167 long timeTaken = (System.nanoTime()-startTime)/1000000;
168 log.info( "time taken: " + timeTaken + "ms" );
169 }
170
171 /***
172 * Runs the configured tool(s) for the specified interface.. needs to be refactored to use
173 * some kind of registry/factory pattern for tools
174 *
175 * @param iface
176 */
177
178 public void runTool( Interface iface )
179 {
180 AbstractToolsAction<Interface> action = null;
181
182 String [] tools = tool.split( "," );
183 for( String tool : tools )
184 {
185 if( tool == null || tool.trim().length() == 0 )
186 continue;
187
188 if( tool.equals( "axis1" ))
189 {
190 action = new Axis1XWSDL2JavaAction( iface );
191 }
192 else if( tool.equals( "axis2" ))
193 {
194 action = new Axis2WSDL2CodeAction( iface );
195 }
196 else if( tool.equals( "dotnet" ))
197 {
198 action = new DotNetWsdlAction( iface );
199 }
200 else if( tool.equals( "gsoap" ))
201 {
202 action = new GSoapAction( iface );
203 }
204 else if( tool.equals( "jaxb" ))
205 {
206 action = new JaxbXjcAction( iface );
207 }
208 else if( tool.equals( "wstools" ))
209 {
210 action = new WSToolsWsdl2JavaAction( iface );
211 }
212 else if( tool.equals( "wscompile" ))
213 {
214 action = new WSCompileAction( iface );
215 }
216 else if( tool.equals( "wsimport" ))
217 {
218 action = new WSImportAction( iface );
219 }
220 else if( tool.equals( "wsconsume" ))
221 {
222 action = new JBossWSConsumeAction( iface );
223 }
224 else if( tool.equals( "xfire" ))
225 {
226 action = new XFireAction( iface );
227 }
228 else if( tool.equals( "xmlbeans" ))
229 {
230 action = new XmlBeans2Action( iface );
231 }
232 else if( tool.equals( "ora" ))
233 {
234 action = new OracleWsaGenProxyAction( iface );
235 }
236 else if( tool.equals( "wsi" ))
237 {
238 action = new WSIAnalyzeAction( iface );
239 }
240
241 try
242 {
243 log.info( "Running tool [" + action.getValue( Action.NAME ) +
244 "] for Interface [" + iface.getName() + "]" );
245 action.run( this );
246 }
247 catch (Exception e)
248 {
249 e.printStackTrace();
250 }
251 }
252 }
253
254 public void run(ToolRunner runner) throws Exception
255 {
256 status = RunnerStatus.RUNNING;
257 runner.setContext( this );
258 runner.run();
259 }
260
261 public RunnerStatus getStatus()
262 {
263 return status;
264 }
265
266 public String getTitle()
267 {
268 return getClass().getSimpleName();
269 }
270
271 public void log(String msg)
272 {
273 System.out.print( msg );
274 }
275
276 public void logError( String msg )
277 {
278 System.err.println( msg );
279 }
280
281 public void setStatus(RunnerStatus status)
282 {
283 this.status = status;
284 }
285
286 public void disposeContext()
287 {
288 }
289 }