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  
35  	public SocketWaiter( String name, SoapMonitor l, int p )
36  	{
37  		super( name );
38  		listener = l;
39  		port = p;
40  		start();
41  	}
42  
43  	/***
44  	 * Method run
45  	 */
46  	public void run()
47  	{
48  		try
49  		{
50  			sSocket = new ServerSocket( port );
51  			for( ;; )
52  			{
53  				Socket inSocket = sSocket.accept();
54  				if( pleaseStop )
55  				{
56  					break;
57  				}
58  				new Connection( getName() + " connection from " + inSocket.getRemoteSocketAddress(), listener, inSocket );
59  				inSocket = null;
60  			}
61  		}
62  		catch( Exception exp )
63  		{
64  			if( !"socket closed".equals( exp.getMessage() ) )
65  			{
66  				listener.stop();
67  			}
68  			
69  			SoapUI.log.info( "Error stopping SocketWaiter: " + exp.toString() );
70  		}
71  	}
72  
73  	/***
74  	 * force a halt by connecting to self and then closing the server socket
75  	 */
76  	public void halt()
77  	{
78  		try
79  		{
80  			pleaseStop = true;
81  			new Socket( "127.0.0.1", port );
82  			if( sSocket != null )
83  			{
84  				sSocket.close();
85  			}
86  		}
87  		catch( Exception e )
88  		{
89  			e.printStackTrace();
90  		}
91  	}
92  }