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.net.ServerSocket;
20  import java.net.Socket;
21  
22  import com.eviware.soapui.SoapUI;
23  
24  /***
25   * wait for incoming connections, spawn a connection thread when stuff comes in.
26   */
27  
28  class SocketWaiter extends Thread
29  {
30  	ServerSocket sSocket = null;
31  	SoapMonitor listener;
32  	int port;
33  	boolean pleaseStop = false;
34  	private SlowLinkSimulator slowLink;
35  
36  	public SocketWaiter( String name, SoapMonitor l, int p )
37  	{
38  		super( name );
39  		listener = l;
40  		port = p;
41  		this.slowLink = new SlowLinkSimulator( 0, 0 );
42  		start();
43  	}
44  
45  	/***
46  	 * Method run
47  	 */
48  	public void run()
49  	{
50  		try
51  		{
52  			sSocket = new ServerSocket( port );
53  			for( ;; )
54  			{
55  				Socket inSocket = sSocket.accept();
56  				if( pleaseStop )
57  				{
58  					break;
59  				}
60  				new Connection( getName() + " connection from " + inSocket.getRemoteSocketAddress(), listener, inSocket, slowLink );
61  				inSocket = null;
62  			}
63  		}
64  		catch( Exception exp )
65  		{
66  			if( !"socket closed".equals( exp.getMessage() ) )
67  			{
68  				listener.stop();
69  			}
70  			
71  			SoapUI.log.info( "Error stopping SocketWaiter: " + exp.toString() );
72  		}
73  	}
74  
75  	/***
76  	 * force a halt by connecting to self and then closing the server socket
77  	 */
78  	public void halt()
79  	{
80  		try
81  		{
82  			pleaseStop = true;
83  			new Socket( "127.0.0.1", port );
84  			if( sSocket != null )
85  			{
86  				sSocket.close();
87  			}
88  		}
89  		catch( Exception e )
90  		{
91  			e.printStackTrace();
92  		}
93  	}
94  }