View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.WsdlProjectFactory;
22  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.AbstractToolsAction;
23  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.RunnerContext;
24  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
25  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolRunner;
26  import com.eviware.soapui.model.iface.Interface;
27  import com.eviware.soapui.model.project.ProjectFactoryRegistry;
28  import com.eviware.soapui.support.UISupport;
29  
30  /***
31   * Standalone tool-runner used from maven-plugin, can also be used from
32   * command-line (see xdocs) or directly from other classes.
33   * <p>
34   * For standalone usage, set the project file (with setProjectFile) and other
35   * desired properties before calling run
36   * </p>
37   * 
38   * @author Ole.Matzura
39   * @author <a href="mailto:nenadn@eviware.com">Nenad V. Nikolic</a>
40   */
41  
42  public class SoapUIToolRunner extends AbstractSoapUIRunner implements ToolHost, RunnerContext
43  {
44  	private String iface;
45  	private String tool;
46  
47  	private RunnerStatus status;
48  	private String projectPassword;
49  
50  	public static String TITLE = "soapUI " + SoapUI.SOAPUI_VERSION + " Tool Runner";
51  
52  	/***
53  	 * Runs the specified tool in the specified soapUI project file, see soapUI
54  	 * xdocs for details.
55  	 * 
56  	 * @param args
57  	 * @throws Exception
58  	 */
59  
60  	public static void main(String[] args) throws Exception
61  	{
62  		System.exit( new SoapUIToolRunner().runFromCommandLine(args));
63  	}
64  
65  	/***
66  	 * Sets the tool(s) to run, can be a comma-seperated list
67  	 * 
68  	 * @param tool
69  	 *           the tools to run
70  	 */
71  
72  	public void setTool(String tool)
73  	{
74  		this.tool = tool;
75  	}
76  
77  	public void setInterface(String iface)
78  	{
79  		this.iface = iface;
80  	}
81  
82  	public SoapUIToolRunner()
83  	{
84  		super(TITLE);
85  	}
86  
87  	public SoapUIToolRunner(String title)
88  	{
89  		super(title);
90  	}
91  
92  	public boolean runRunner() throws Exception
93  	{
94  		UISupport.setToolHost(this);
95  		String projectFile = getProjectFile();
96  
97  		if (!new File(projectFile).exists())
98  			throw new Exception("soapUI project file [" + projectFile + "] not found");
99  
100 		// WsdlProject project = new WsdlProject( projectFile,
101 		// getProjectPassword() );
102 		WsdlProject project = (WsdlProject) ProjectFactoryRegistry.getProjectFactory(WsdlProjectFactory.WSDL_TYPE)
103 				.createNew(projectFile, getProjectPassword());
104 
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 		return true;
122 	}
123 
124 	/***
125 	 * Runs the configured tool(s) for the specified interface.
126 	 * 
127 	 * @param iface
128 	 *           an interface that exposes an invokable operation
129 	 */
130 	public void runTool(Interface iface)
131 	{
132 		AbstractToolsAction<Interface> action = null;
133 
134 		String[] tools = tool.split(",");
135 		for (String toolName : tools)
136 		{
137 			if (toolName == null || toolName.trim().length() == 0)
138 				continue;
139 
140 			action = ToolActionFactory.createToolAction(toolName);
141 			try
142 			{
143 				if (action != null)
144 				{
145 					log.info("Running tool [" + toolName + "] for Interface [" + iface.getName() + "]");
146 					action.performHeadless(iface, null);
147 				}
148 				else
149 				{
150 					log.error("Specified tool [" + toolName + "] is unknown or unsupported.");
151 				}
152 			}
153 			catch (Exception e)
154 			{
155 				SoapUI.logError(e);
156 			}
157 		}
158 	}
159 
160 	public void run(ToolRunner runner) throws Exception
161 	{
162 		status = RunnerStatus.RUNNING;
163 		runner.setContext(this);
164 		runner.run();
165 	}
166 
167 	public RunnerStatus getStatus()
168 	{
169 		return status;
170 	}
171 
172 	public String getTitle()
173 	{
174 		return getClass().getSimpleName();
175 	}
176 
177 	public String getProjectPassword()
178 	{
179 		return projectPassword;
180 	}
181 
182 	public void log(String msg)
183 	{
184 		System.out.print(msg);
185 	}
186 
187 	public void logError(String msg)
188 	{
189 		System.err.println(msg);
190 	}
191 
192 	public void setStatus(RunnerStatus status)
193 	{
194 		this.status = status;
195 	}
196 
197 	public void disposeContext()
198 	{
199 	}
200 
201 	@Override
202 	protected SoapUIOptions initCommandLineOptions()
203 	{
204 		SoapUIOptions options = new SoapUIOptions("toolrunner");
205 		options.addOption("i", true, "Sets the interface");
206 		options.addOption("t", true, "Sets the tool to run");
207 		options.addOption("s", true, "Sets the soapui-settings.xml file to use");
208 		options.addOption("x", true, "Sets project password for decryption if project is encrypted");
209 		options.addOption("v", true, "Sets password for soapui-settings.xml file");
210 		options.addOption("f", true, "Sets report output folder");
211 		options.addOption("D", true, "Sets system property with name=value");
212 		options.addOption("G", true, "Sets global property with name=value");
213 
214 		return options;
215 	}
216 
217 	@Override
218 	protected boolean processCommandLine(CommandLine cmd)
219 	{
220 		setTool(cmd.getOptionValue("t"));
221 
222 		if (cmd.hasOption("i"))
223 			setInterface(cmd.getOptionValue("i"));
224 
225 		if (cmd.hasOption("s"))
226 			setSettingsFile(getCommandLineOptionSubstSpace(cmd, "s"));
227 
228 		if (cmd.hasOption("x"))
229 		{
230 			setProjectPassword(cmd.getOptionValue("x"));
231 		}
232 
233 		if (cmd.hasOption("v"))
234 		{
235 			setSoapUISettingsPassword(cmd.getOptionValue("v"));
236 		}
237 
238 		if (cmd.hasOption("D"))
239 		{
240 			setSystemProperties(cmd.getOptionValues("D"));
241 		}
242 
243 		if (cmd.hasOption("G"))
244 		{
245 			setGlobalProperties(cmd.getOptionValues("G"));
246 		}
247 
248 		if (cmd.hasOption("f"))
249 			setOutputFolder(cmd.getOptionValue("f"));
250 
251 		return true;
252 	}
253 
254 	public void setProjectPassword(String projectPassword)
255 	{
256 		this.projectPassword = projectPassword;
257 	}
258 }