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,
61  						slowLink );
62  				inSocket = null;
63  			}
64  		}
65  		catch( Exception exp )
66  		{
67  			if( !"socket closed".equals( exp.getMessage() ) )
68  			{
69  				listener.stop();
70  			}
71  
72  			SoapUI.log.info( "Error stopping SocketWaiter: " + exp.toString() );
73  		}
74  	}
75  
76  	/***
77  	 * force a halt by connecting to self and then closing the server socket
78  	 */
79  	public void halt()
80  	{
81  		try
82  		{
83  			pleaseStop = true;
84  			new Socket( "127.0.0.1", port );
85  			if( sSocket != null )
86  			{
87  				sSocket.close();
88  			}
89  		}
90  		catch( Exception e )
91  		{
92  			e.printStackTrace();
93  		}
94  	}
95  }