View Javadoc

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