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  
61     public static String TITLE = "soapUI " + SoapUI.SOAPUI_VERSION + " Tool Runner";
62  
63     /***
64      * Runs the specified tool in the specified soapUI project file, see soapUI xdocs for details.
65      *
66      * @param args
67      * @throws Exception
68      */
69  
70     public static void main( String[] args ) throws Exception
71     {
72        new SoapUIToolRunner().runFromCommandLine( args );
73     }
74  
75     /***
76      * Sets the tool(s) to run, can be a comma-seperated list
77      *
78      * @param tool the tools to run
79      */
80  
81     public void setTool( String tool )
82     {
83        this.tool = tool;
84     }
85  
86     public void setInterface( String iface )
87     {
88        this.iface = iface;
89     }
90  
91     public SoapUIToolRunner()
92     {
93        super( TITLE );
94     }
95  
96     public SoapUIToolRunner( String title )
97     {
98        super( title );
99     }
100 
101    public boolean runRunner() throws Exception
102    {
103       UISupport.setToolHost( this );
104       String projectFile = getProjectFile();
105 
106       if( !new File( projectFile ).exists() )
107          throw new Exception( "soapUI project file [" + projectFile + "] not found" );
108 
109 //		WsdlProject project = new WsdlProject( projectFile, getProjectPassword() );
110       WsdlProject project = (WsdlProject) ProjectFactoryRegistry.getProjectFactory( "wsdl" ).createNew( projectFile, getProjectPassword() );
111 
112       log.info( "Running tools [" + tool + "] for interface [" + iface + "] in project [" + project.getName() + "]" );
113 
114       long startTime = System.nanoTime();
115 
116       for( int c = 0; c < project.getInterfaceCount(); c++ )
117       {
118          Interface i = project.getInterfaceAt( c );
119          if( iface == null || i.getName().equals( iface ) )
120          {
121             runTool( i );
122          }
123       }
124 
125       long timeTaken = ( System.nanoTime() - startTime ) / 1000000;
126       log.info( "time taken: " + timeTaken + "ms" );
127 
128       return true;
129    }
130 
131    /***
132     * Runs the configured tool(s) for the specified interface.. needs to be refactored to use
133     * some kind of registry/factory pattern for tools
134     *
135     * @param iface
136     */
137 
138    public void runTool( Interface iface )
139    {
140       AbstractToolsAction<Interface> action = null;
141 
142       String[] tools = tool.split( "," );
143       for( String tool : tools )
144       {
145          if( tool == null || tool.trim().length() == 0 )
146             continue;
147 
148          if( tool.equals( "axis1" ) )
149          {
150             action = new Axis1XWSDL2JavaAction();
151          }
152          else if( tool.equals( "axis2" ) )
153          {
154             action = new Axis2WSDL2CodeAction();
155          }
156          else if( tool.equals( "dotnet" ) )
157          {
158             action = new DotNetWsdlAction();
159          }
160          else if( tool.equals( "gsoap" ) )
161          {
162             action = new GSoapAction();
163          }
164          else if( tool.equals( "jaxb" ) )
165          {
166             action = new JaxbXjcAction();
167          }
168          else if( tool.equals( "wstools" ) )
169          {
170             action = new WSToolsWsdl2JavaAction();
171          }
172          else if( tool.equals( "wscompile" ) )
173          {
174             action = new WSCompileAction();
175          }
176          else if( tool.equals( "wsimport" ) )
177          {
178             action = new WSImportAction();
179          }
180          else if( tool.equals( "wsconsume" ) )
181          {
182             action = new JBossWSConsumeAction();
183          }
184          else if( tool.equals( "xfire" ) )
185          {
186             action = new XFireAction();
187          }
188          else if( tool.equals( "cxf" ) )
189          {
190             action = new CXFAction();
191          }
192          else if( tool.equals( "xmlbeans" ) )
193          {
194             action = new XmlBeans2Action();
195          }
196          else if( tool.equals( "ora" ) )
197          {
198             action = new OracleWsaGenProxyAction();
199          }
200          else if( tool.equals( "wsi" ) )
201          {
202             action = new WSIAnalyzeAction();
203          }
204 
205          try
206          {
207             log.info( "Running tool [" + tool +
208                     "] for Interface [" + iface.getName() + "]" );
209             action.perform( iface, null );
210          }
211          catch( Exception e )
212          {
213             SoapUI.logError( e );
214          }
215       }
216    }
217 
218    public void run( ToolRunner runner ) throws Exception
219    {
220       status = RunnerStatus.RUNNING;
221       runner.setContext( this );
222       runner.run();
223    }
224 
225    public RunnerStatus getStatus()
226    {
227       return status;
228    }
229 
230    public String getTitle()
231    {
232       return getClass().getSimpleName();
233    }
234 
235    public String getProjectPassword()
236    {
237       return projectPassword;
238    }
239 
240    public void log( String msg )
241    {
242       System.out.print( msg );
243    }
244 
245    public void logError( String msg )
246    {
247       System.err.println( msg );
248    }
249 
250    public void setStatus( RunnerStatus status )
251    {
252       this.status = status;
253    }
254 
255    public void disposeContext()
256    {
257    }
258 
259    @Override
260    protected SoapUIOptions initCommandLineOptions()
261    {
262       SoapUIOptions options = new SoapUIOptions( "toolrunner" );
263       options.addOption( "i", true, "Sets the interface" );
264       options.addOption( "t", true, "Sets the tool to run" );
265       options.addOption( "s", true, "Sets the soapui-settings.xml file to use" );
266       options.addOption( "x", true, "Sets project password for decryption if project is encrypted" );
267       options.addOption( "v", true, "Sets password for soapui-settings.xml file" );
268       return options;
269    }
270 
271    @Override
272    protected boolean processCommandLine( CommandLine cmd )
273    {
274       setTool( cmd.getOptionValue( "t" ) );
275 
276       if( cmd.hasOption( "i" ) )
277          setInterface( cmd.getOptionValue( "i" ) );
278 
279       if( cmd.hasOption( "s" ) )
280          setSettingsFile( getCommandLineOptionSubstSpace( cmd, "s" ) );
281 
282       if( cmd.hasOption( "x" ) )
283       {
284          setProjectPassword( cmd.getOptionValue( "x" ) );
285       }
286 
287       if( cmd.hasOption( "v" ) )
288       {
289          setSoapUISettingsPassword( cmd.getOptionValue( "v" ) );
290       }
291 
292       return true;
293 
294    }
295 
296    public void setProjectPassword( String projectPassword )
297    {
298       this.projectPassword = projectPassword;
299 	}
300 }