View Javadoc

1   /*
2    * Copyright 2004,2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.eviware.soapui.impl.wsdl.monitor;
18  
19  import java.io.InputStream;
20  import java.io.OutputStream;
21  import java.net.Socket;
22  
23  import com.eviware.soapui.SoapUI;
24  
25  /***
26   * this class handles the pumping of data from the incoming socket to the
27   * outgoing socket
28   */
29  class SocketRR extends Thread
30  {
31  
32  	Socket inSocket = null;
33  	Socket outSocket = null;
34  
35  	InputStream in = null;
36  	OutputStream out = null;
37  
38  	volatile boolean done = false;
39  	volatile long elapsed = 0;
40  
41  	Connection myConnection = null;
42  	SlowLinkSimulator slowLink;
43  
44  	public SocketRR( String name, Connection c, Socket inputSocket, InputStream inputStream, Socket outputSocket,
45  			OutputStream outputStream, SlowLinkSimulator slowLink )
46  	{
47  		super( name );
48  
49  		inSocket = inputSocket;
50  		in = inputStream;
51  		outSocket = outputSocket;
52  		out = outputStream;
53  		myConnection = c;
54  		this.slowLink = slowLink;
55  		start();
56  	}
57  
58  	/***
59  	 * Method isDone
60  	 * 
61  	 * @return boolean
62  	 */
63  	public boolean isDone()
64  	{
65  		return done;
66  	}
67  
68  	public long getElapsed()
69  	{
70  		return elapsed;
71  	}
72  
73  	/***
74  	 * Method run
75  	 */
76  	public void run()
77  	{
78  		try
79  		{
80  			byte[] buffer = new byte[4096];
81  			int saved = 0;
82  			int len;
83  			long start = System.currentTimeMillis();
84  			a : for( ;; )
85  			{
86  
87  				elapsed = System.currentTimeMillis() - start;
88  
89  				if( done )
90  				{
91  					break;
92  				}
93  
94  				// try{
95  				// len = in.available();
96  				// }catch(Exception e){len=0;}
97  				len = buffer.length;
98  
99  				// Used to be 1, but if we block it doesn't matter
100 				// however 1 will break with some servers, including apache
101 				if( len == 0 )
102 				{
103 					len = buffer.length;
104 				}
105 				if( saved + len > buffer.length )
106 				{
107 					len = buffer.length - saved;
108 				}
109 				int len1 = 0;
110 				while( len1 == 0 )
111 				{
112 					try
113 					{
114 						len1 = in.read( buffer, saved, len );
115 					}
116 					catch( Exception ex )
117 					{
118 						if( done && ( saved == 0 ) )
119 						{
120 							break a;
121 						}
122 						len1 = -1;
123 						break;
124 					}
125 				}
126 				len = len1;
127 				if( ( len == -1 ) && ( saved == 0 ) )
128 				{
129 					break;
130 				}
131 				if( len == -1 )
132 				{
133 					done = true;
134 				}
135 
136 				if( ( out != null ) && ( len > 0 ) )
137 				{
138 					slowLink.pump( len );
139 					out.write( buffer, saved, len );
140 				}
141 			}
142 
143 		}
144 		catch( Exception e )
145 		{
146 			e.printStackTrace();
147 		}
148 		finally
149 		{
150 			done = true;
151 			try
152 			{
153 				if( out != null )
154 				{
155 					out.flush();
156 					if( null != outSocket )
157 					{
158 						outSocket.shutdownOutput();
159 					}
160 					else
161 					{
162 						out.close();
163 					}
164 					out = null;
165 				}
166 			}
167 			catch( Exception e )
168 			{
169 			}
170 			try
171 			{
172 				if( in != null )
173 				{
174 					if( inSocket != null )
175 					{
176 						inSocket.shutdownInput();
177 					}
178 					else
179 					{
180 						in.close();
181 					}
182 					in = null;
183 				}
184 			}
185 			catch( Exception e )
186 			{
187 			}
188 			myConnection.wakeUp();
189 		}
190 	}
191 
192 	/***
193 	 * Method halt
194 	 */
195 	public void halt()
196 	{
197 		try
198 		{
199 			if( inSocket != null )
200 			{
201 				inSocket.close();
202 			}
203 			if( outSocket != null )
204 			{
205 				outSocket.close();
206 			}
207 			inSocket = null;
208 			outSocket = null;
209 			if( in != null )
210 			{
211 				in.close();
212 			}
213 			if( out != null )
214 			{
215 				out.close();
216 			}
217 			in = null;
218 			out = null;
219 			done = true;
220 		}
221 		catch( Exception e )
222 		{
223 			SoapUI.log.info( "Error halting socket: " + e.toString() );
224 		}
225 	}
226 }