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