View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 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.impl.wsdl;
14  
15  import java.util.concurrent.ExecutorService;
16  import java.util.concurrent.Executors;
17  import java.util.concurrent.Future;
18  
19  import org.apache.log4j.Logger;
20  
21  import com.eviware.soapui.SoapUI;
22  import com.eviware.soapui.impl.wsdl.submit.RequestTransport;
23  import com.eviware.soapui.model.iface.Request;
24  import com.eviware.soapui.model.iface.Response;
25  import com.eviware.soapui.model.iface.Submit;
26  import com.eviware.soapui.model.iface.SubmitContext;
27  import com.eviware.soapui.model.iface.SubmitListener;
28  
29  /***
30   * Submit implementation for submitting a WsdlRequest
31   * 
32   * @author Ole.Matzura
33   */
34  
35  public final class WsdlSubmit implements Runnable, Submit
36  {
37  	private final static Logger logger = Logger.getLogger(WsdlSubmit.class);
38  	private WsdlRequest wsdlRequest;
39  	private SubmitListener[] listeners;
40  	private Status status;
41  	private Exception error;
42  	private Response response;
43  	private volatile Future future;
44  	private SubmitContext submitContext;
45  	private final static ExecutorService threadPool = Executors.newCachedThreadPool();
46  	private RequestTransport transport;
47  
48  	public WsdlSubmit(WsdlRequest wsdlRequest, SubmitListener[] listeners, RequestTransport transport)
49  	{
50  		this.wsdlRequest = wsdlRequest;
51  		this.transport = transport;
52  		
53  		this.listeners = new SubmitListener[listeners.length];
54  		for( int c = 0; c < listeners.length; c++ )
55  			this.listeners[c] = listeners[c];
56  		
57  		error = null;
58  		status = Status.INITIALIZED;
59  		future = null;
60  	}
61  
62  	public void submitRequest(SubmitContext submitContext, boolean async )
63  	{
64  		this.submitContext = submitContext;
65  
66  		if( async && future != null )
67  			throw new RuntimeException( "Submit already running" );
68  		
69  		if( async )
70  			future = threadPool.submit(this);
71  		else
72  			run();
73  	}
74  
75  	public void cancel()
76  	{
77  		if (status == Status.CANCELED)
78  			return;
79  
80  		logger.info("Canceling request..");
81  		if (status == Status.RUNNING )
82  		{
83  			transport.abortRequest( submitContext );
84  		}
85  
86  		status = Status.CANCELED;
87  
88  		for (int i = 0; i < listeners.length; i++)
89  		{
90  			listeners[i].afterSubmit(this, submitContext);
91  		}
92  	}
93  
94  	public void run()
95  	{
96  		try
97  		{
98  			submitContext.setProperty( RequestTransport.REQUEST_TRANSPORT, transport );
99  	      submitContext.setProperty( RequestTransport.WSDL_REQUEST, wsdlRequest );
100 			
101 			for (int i = 0; i < listeners.length; i++)
102 			{
103 				if (!listeners[i].beforeSubmit(this, submitContext))
104 				{
105 					status = Status.CANCELED;
106 					System.err.println("listener cancelled submit..");
107 					return;
108 				}
109 			}
110 
111 			status = Status.RUNNING;
112 			response = transport.sendRequest(submitContext, wsdlRequest);
113 			
114 			if (status != Status.CANCELED)
115 			{
116 				status = Status.FINISHED;
117 			}
118 
119 			if( response.getTimeTaken() == 0 )
120 			{
121 				logger.warn( "Request took 0 in thread " + Thread.currentThread().getId() + 
122 						", response length = " + response.getContentLength() );
123 			}
124 		}
125 		catch (Exception e1)
126 		{
127 			error = e1;
128 			status = Status.ERROR;
129 			logger.error("Exception in request: " + e1);
130 			SoapUI.logError( e1 );
131 		}
132 		finally
133 		{
134 			if (status != Status.CANCELED)
135 			{
136 				for (int i = 0; i < listeners.length; i++)
137 				{
138 					listeners[i].afterSubmit(this, submitContext);
139 				}
140 			}
141 		}
142 	}
143 
144 	public Request getRequest()
145 	{
146 		return wsdlRequest;
147 	}
148 
149 	public Status getStatus()
150 	{
151 		return status;
152 	}
153 
154 	public Exception getError()
155 	{
156 		return error;
157 	}
158 
159 	public synchronized Status waitUntilFinished()
160 	{
161 		if (future != null)
162 		{
163 			if (!future.isDone())
164 			{
165 				try
166 				{
167 					future.get();
168 				}
169 				catch (Exception e)
170 				{
171 					SoapUI.logError( e );
172 				}
173 			}
174 		}
175 		else
176 			throw new RuntimeException("cannot wait on null future");
177 
178 		return getStatus();
179 	}
180 
181 	public Response getResponse()
182 	{
183 		return response;
184 	}
185 }