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;
014:
015: import java.util.concurrent.ExecutorService;
016: import java.util.concurrent.Executors;
017: import java.util.concurrent.Future;
018:
019: import org.apache.log4j.Logger;
020:
021: import com.eviware.soapui.SoapUI;
022: import com.eviware.soapui.impl.wsdl.submit.RequestTransport;
023: import com.eviware.soapui.model.iface.Request;
024: import com.eviware.soapui.model.iface.Response;
025: import com.eviware.soapui.model.iface.Submit;
026: import com.eviware.soapui.model.iface.SubmitContext;
027: import com.eviware.soapui.model.iface.SubmitListener;
028:
029: /**
030: * Submit implementation for submitting a WsdlRequest
031: *
032: * @author Ole.Matzura
033: */
034:
035: public final class WsdlSubmit implements Runnable, Submit {
036: private final static Logger logger = Logger
037: .getLogger(WsdlSubmit.class);
038: private WsdlRequest wsdlRequest;
039: private SubmitListener[] listeners;
040: private Status status;
041: private Exception error;
042: private Response response;
043: private volatile Future future;
044: private SubmitContext submitContext;
045: private final static ExecutorService threadPool = Executors
046: .newCachedThreadPool();
047: private RequestTransport transport;
048:
049: public WsdlSubmit(WsdlRequest wsdlRequest,
050: SubmitListener[] listeners, RequestTransport transport) {
051: this .wsdlRequest = wsdlRequest;
052: this .transport = transport;
053:
054: this .listeners = new SubmitListener[listeners.length];
055: for (int c = 0; c < listeners.length; c++)
056: this .listeners[c] = listeners[c];
057:
058: error = null;
059: status = Status.INITIALIZED;
060: future = null;
061: }
062:
063: public void submitRequest(SubmitContext submitContext, boolean async) {
064: this .submitContext = submitContext;
065:
066: if (async && future != null)
067: throw new RuntimeException("Submit already running");
068:
069: if (async)
070: future = threadPool.submit(this );
071: else
072: run();
073: }
074:
075: public void cancel() {
076: if (status == Status.CANCELED)
077: return;
078:
079: logger.info("Canceling request..");
080: if (status == Status.RUNNING) {
081: transport.abortRequest(submitContext);
082: }
083:
084: status = Status.CANCELED;
085:
086: for (int i = 0; i < listeners.length; i++) {
087: listeners[i].afterSubmit(this , submitContext);
088: }
089: }
090:
091: public void run() {
092: try {
093: submitContext.setProperty(
094: RequestTransport.REQUEST_TRANSPORT, transport);
095: submitContext.setProperty(RequestTransport.WSDL_REQUEST,
096: wsdlRequest);
097:
098: for (int i = 0; i < listeners.length; i++) {
099: if (!listeners[i].beforeSubmit(this , submitContext)) {
100: status = Status.CANCELED;
101: System.err.println("listener cancelled submit..");
102: return;
103: }
104: }
105:
106: status = Status.RUNNING;
107: response = transport
108: .sendRequest(submitContext, wsdlRequest);
109:
110: if (status != Status.CANCELED) {
111: status = Status.FINISHED;
112: }
113:
114: if (response.getTimeTaken() == 0) {
115: logger.warn("Request took 0 in thread "
116: + Thread.currentThread().getId()
117: + ", response length = "
118: + response.getContentLength());
119: }
120: } catch (Exception e1) {
121: error = e1;
122: status = Status.ERROR;
123: logger.error("Exception in request: " + e1);
124: SoapUI.logError(e1);
125: } finally {
126: if (status != Status.CANCELED) {
127: for (int i = 0; i < listeners.length; i++) {
128: listeners[i].afterSubmit(this , submitContext);
129: }
130: }
131: }
132: }
133:
134: public Request getRequest() {
135: return wsdlRequest;
136: }
137:
138: public Status getStatus() {
139: return status;
140: }
141:
142: public Exception getError() {
143: return error;
144: }
145:
146: public synchronized Status waitUntilFinished() {
147: if (future != null) {
148: if (!future.isDone()) {
149: try {
150: future.get();
151: } catch (Exception e) {
152: SoapUI.logError(e);
153: }
154: }
155: } else
156: throw new RuntimeException("cannot wait on null future");
157:
158: return getStatus();
159: }
160:
161: public Response getResponse() {
162: return response;
163: }
164: }
|