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.impl.wsdl.actions.iface.tools.support;
014:
015: import java.io.InputStream;
016: import java.util.List;
017:
018: import com.eviware.soapui.model.ModelItem;
019: import com.eviware.soapui.support.UISupport;
020:
021: /**
022: * ToolRunner for running command-line processes
023: *
024: * @author ole.matzura
025: */
026:
027: public class ProcessToolRunner implements ToolRunner {
028: private final ProcessBuilder[] builders;
029: private boolean running;
030: private Process process;
031: private RunnerContext context;
032: private final String name;
033: private final ModelItem modelItem;
034: private boolean canCancel = true;
035: private boolean showLog = true;
036:
037: public ProcessToolRunner(ProcessBuilder builder, String name,
038: ModelItem modelItem) {
039: this (new ProcessBuilder[] { builder }, name, modelItem);
040: }
041:
042: public ProcessToolRunner(ProcessBuilder[] builders, String name,
043: ModelItem modelItem) {
044: this .builders = builders;
045: this .name = name;
046: this .modelItem = modelItem;
047: }
048:
049: public ProcessBuilder[] getBuilders() {
050: return builders;
051: }
052:
053: public Process getProcess() {
054: return process;
055: }
056:
057: public boolean isRunning() {
058: return running;
059: }
060:
061: public void cancel() {
062: getProcess().destroy();
063: }
064:
065: public void run() {
066: try {
067: int exitCode = -1;
068:
069: beforeRun(context);
070:
071: for (int c = 0; c < builders.length; c++) {
072: beforeProcess(process, context);
073:
074: logRunInfo(builders[c]);
075: process = builders[c].start();
076: if (c == 0)
077: context
078: .setStatus(RunnerContext.RunnerStatus.RUNNING);
079:
080: running = true;
081:
082: InputStream in = process.getInputStream();
083: InputStream err = process.getErrorStream();
084:
085: exitCode = -1;
086:
087: while (exitCode == -1 && running) {
088: try {
089: exitCode = process.exitValue();
090: break;
091: } catch (IllegalThreadStateException e) {
092: } finally {
093: while (in.available() > 0) {
094: byte[] data = new byte[in.available()];
095: in.read(data);
096:
097: context.log(new String(data));
098: }
099:
100: while (err.available() > 0) {
101: byte[] data = new byte[err.available()];
102: err.read(data);
103:
104: context.logError(new String(data));
105: }
106: }
107:
108: Thread.sleep(25);
109: }
110:
111: afterProcess(process, context);
112: }
113:
114: context.setStatus(RunnerContext.RunnerStatus.FINISHED);
115:
116: if (running) {
117: running = false;
118: afterRun(exitCode, context);
119: }
120: } catch (Exception ex) {
121: context.setStatus(RunnerContext.RunnerStatus.ERROR);
122: UISupport.showErrorMessage(ex);
123: running = false;
124: afterRun(-1, context);
125: } finally {
126: context.disposeContext();
127: }
128: }
129:
130: protected void beforeRun(RunnerContext context) {
131: }
132:
133: protected void beforeProcess(Process process2, RunnerContext context) {
134: }
135:
136: protected void afterProcess(Process process2, RunnerContext context) {
137: }
138:
139: protected void afterRun(int exitCode, RunnerContext context) {
140: if (exitCode == 0)
141: UISupport.showInfoMessage(
142: "Execution finished successfully", context
143: .getTitle());
144: else
145: UISupport
146: .showInfoMessage(
147: "Execution finished with errorCode "
148: + exitCode
149: + ",\r\nplease check log for error messages",
150: context.getTitle());
151: }
152:
153: private void logRunInfo(ProcessBuilder builder) {
154: List<String> args = builder.command();
155: StringBuffer buf = new StringBuffer();
156: for (int c = 0; c < args.size(); c++) {
157: if (c > 0)
158: buf.append(' ');
159:
160: buf.append(args.get(c));
161: }
162:
163: context.log("directory: "
164: + builder.directory().getAbsolutePath() + "\r\n");
165: context.log("command: " + buf.toString() + "\r\n");
166: }
167:
168: public void setContext(RunnerContext context) {
169: this .context = context;
170: }
171:
172: public ModelItem getModelItem() {
173: return modelItem;
174: }
175:
176: public String getName() {
177: return name;
178: }
179:
180: public boolean canCancel() {
181: return canCancel;
182: }
183:
184: public boolean showLog() {
185: return showLog;
186: }
187:
188: public void setCanCancel(boolean canCancel) {
189: this .canCancel = canCancel;
190: }
191:
192: public void setShowLog(boolean showLog) {
193: this .showLog = showLog;
194: }
195:
196: public String getDescription() {
197: return null;
198: }
199: }
|