001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.tools;
014:
015: import java.io.File;
016:
017: import org.apache.commons.cli.CommandLine;
018:
019: import com.eviware.soapui.SoapUI;
020: import com.eviware.soapui.impl.wsdl.WsdlProject;
021: import com.eviware.soapui.impl.wsdl.actions.iface.tools.axis1.Axis1XWSDL2JavaAction;
022: import com.eviware.soapui.impl.wsdl.actions.iface.tools.axis2.Axis2WSDL2CodeAction;
023: import com.eviware.soapui.impl.wsdl.actions.iface.tools.dotnet.DotNetWsdlAction;
024: import com.eviware.soapui.impl.wsdl.actions.iface.tools.gsoap.GSoapAction;
025: import com.eviware.soapui.impl.wsdl.actions.iface.tools.jaxb.JaxbXjcAction;
026: import com.eviware.soapui.impl.wsdl.actions.iface.tools.jbossws.JBossWSConsumeAction;
027: import com.eviware.soapui.impl.wsdl.actions.iface.tools.jbossws.WSToolsWsdl2JavaAction;
028: import com.eviware.soapui.impl.wsdl.actions.iface.tools.oracle.OracleWsaGenProxyAction;
029: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.AbstractToolsAction;
030: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.RunnerContext;
031: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
032: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolRunner;
033: import com.eviware.soapui.impl.wsdl.actions.iface.tools.wscompile.WSCompileAction;
034: import com.eviware.soapui.impl.wsdl.actions.iface.tools.wsi.WSIAnalyzeAction;
035: import com.eviware.soapui.impl.wsdl.actions.iface.tools.wsimport.WSImportAction;
036: import com.eviware.soapui.impl.wsdl.actions.iface.tools.xfire.XFireAction;
037: import com.eviware.soapui.impl.wsdl.actions.iface.tools.xmlbeans.XmlBeans2Action;
038: import com.eviware.soapui.model.iface.Interface;
039:
040: /**
041: * Standalone tool-runner used from maven-plugin, can also be used from command-line (see xdocs) or
042: * directly from other classes.
043: * <p>
044: * For standalone usage, set the project file (with setProjectFile) and other desired properties before
045: * calling run</p>
046: *
047: * @author Ole.Matzura
048: */
049:
050: public class SoapUIToolRunner extends AbstractSoapUIRunner implements
051: ToolHost, RunnerContext {
052: private String iface;
053: private String tool;
054:
055: private RunnerStatus status;
056: public static String TITLE = "soapUI " + SoapUI.SOAPUI_VERSION
057: + " Tool Runner";
058:
059: /**
060: * Runs the specified tool in the specified soapUI project file, see soapUI xdocs for details.
061: *
062: * @param args
063: * @throws Exception
064: */
065:
066: @SuppressWarnings("static-access")
067: public static void main(String[] args) throws Exception {
068: new SoapUIToolRunner().runFromCommandLine(args);
069: }
070:
071: /**
072: * Sets the tool(s) to run, can be a comma-seperated list
073: *
074: * @param tool the tools to run
075: */
076:
077: public void setTool(String tool) {
078: this .tool = tool;
079: }
080:
081: public void setInterface(String iface) {
082: this .iface = iface;
083: }
084:
085: public SoapUIToolRunner() {
086: super (TITLE);
087: }
088:
089: public SoapUIToolRunner(String title) {
090: super (title);
091: }
092:
093: public void run() throws Exception {
094: String projectFile = getProjectFile();
095:
096: if (!new File(projectFile).exists())
097: throw new Exception("soapUI project file [" + projectFile
098: + "] not found");
099:
100: WsdlProject project = new WsdlProject(projectFile, null);
101: log.info("Running tools [" + tool + "] for interface [" + iface
102: + "] in project [" + project.getName() + "]");
103:
104: long startTime = System.nanoTime();
105:
106: for (int c = 0; c < project.getInterfaceCount(); c++) {
107: Interface i = project.getInterfaceAt(c);
108: if (iface == null || i.getName().equals(iface)) {
109: runTool(i);
110: }
111: }
112:
113: long timeTaken = (System.nanoTime() - startTime) / 1000000;
114: log.info("time taken: " + timeTaken + "ms");
115: }
116:
117: /**
118: * Runs the configured tool(s) for the specified interface.. needs to be refactored to use
119: * some kind of registry/factory pattern for tools
120: *
121: * @param iface
122: */
123:
124: public void runTool(Interface iface) {
125: AbstractToolsAction<Interface> action = null;
126:
127: String[] tools = tool.split(",");
128: for (String tool : tools) {
129: if (tool == null || tool.trim().length() == 0)
130: continue;
131:
132: if (tool.equals("axis1")) {
133: action = new Axis1XWSDL2JavaAction();
134: } else if (tool.equals("axis2")) {
135: action = new Axis2WSDL2CodeAction();
136: } else if (tool.equals("dotnet")) {
137: action = new DotNetWsdlAction();
138: } else if (tool.equals("gsoap")) {
139: action = new GSoapAction();
140: } else if (tool.equals("jaxb")) {
141: action = new JaxbXjcAction();
142: } else if (tool.equals("wstools")) {
143: action = new WSToolsWsdl2JavaAction();
144: } else if (tool.equals("wscompile")) {
145: action = new WSCompileAction();
146: } else if (tool.equals("wsimport")) {
147: action = new WSImportAction();
148: } else if (tool.equals("wsconsume")) {
149: action = new JBossWSConsumeAction();
150: } else if (tool.equals("xfire")) {
151: action = new XFireAction();
152: } else if (tool.equals("xmlbeans")) {
153: action = new XmlBeans2Action();
154: } else if (tool.equals("ora")) {
155: action = new OracleWsaGenProxyAction();
156: } else if (tool.equals("wsi")) {
157: action = new WSIAnalyzeAction();
158: }
159:
160: try {
161: log.info("Running tool [" + tool + "] for Interface ["
162: + iface.getName() + "]");
163: action.perform(iface, null);
164: } catch (Exception e) {
165: SoapUI.logError(e);
166: }
167: }
168: }
169:
170: public void run(ToolRunner runner) throws Exception {
171: status = RunnerStatus.RUNNING;
172: runner.setContext(this );
173: runner.run();
174: }
175:
176: public RunnerStatus getStatus() {
177: return status;
178: }
179:
180: public String getTitle() {
181: return getClass().getSimpleName();
182: }
183:
184: public void log(String msg) {
185: System.out.print(msg);
186: }
187:
188: public void logError(String msg) {
189: System.err.println(msg);
190: }
191:
192: public void setStatus(RunnerStatus status) {
193: this .status = status;
194: }
195:
196: public void disposeContext() {
197: }
198:
199: @Override
200: protected SoapUIOptions initCommandLineOptions() {
201: SoapUIOptions options = new SoapUIOptions("toolrunner");
202: options.addOption("i", true, "Sets the interface");
203: options.addOption("t", true, "Sets the tool to run");
204: options.addOption("s", false,
205: "Sets the soapui-settings.xml file to use");
206: return options;
207: }
208:
209: @Override
210: protected boolean processCommandLine(CommandLine cmd) {
211: setTool(cmd.getOptionValue("t"));
212:
213: if (cmd.hasOption("i"))
214: setInterface(cmd.getOptionValue("i"));
215:
216: if (cmd.hasOption("s"))
217: SoapUI.initSettings(cmd.getOptionValue("s"));
218:
219: return true;
220:
221: }
222: }
|