View Javadoc

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