View Javadoc
1   package ca.uhn.hl7v2.util;
2   
3   import java.io.IOException;
4   import java.net.ServerSocket;
5   import java.net.Socket;
6   import java.net.SocketException;
7   
8   public class StandardSocketFactory implements SocketFactory {
9   
10  	/**
11  	 * Default value for {@link #getAcceptedSocketTimeout()}
12  	 */
13  	public static final int DEFAULT_ACCEPTED_SOCKET_TIMEOUT = 500;
14  
15  	private int myAcceptedSocketTimeout = DEFAULT_ACCEPTED_SOCKET_TIMEOUT;
16  
17  	/**
18  	 * Gets the value for {@link Socket#setSoTimeout(int)} to be set on newly accepted sockets in server applications. Default is {@link #DEFAULT_ACCEPTED_SOCKET_TIMEOUT}
19  	 */
20  	public int getAcceptedSocketTimeout() {
21  		return myAcceptedSocketTimeout;
22  	}
23  
24  	/**
25  	 * Sets the value for {@link Socket#setSoTimeout(int)} to be set on newly accepted sockets in server applications
26  	 */
27  	public void setAcceptedSocketTimeout(int theAcceptedSocketTimeout) {
28  		if (theAcceptedSocketTimeout < 0) {
29  			throw new IllegalArgumentException("Timeout can't be negative");
30  		}
31  		myAcceptedSocketTimeout = theAcceptedSocketTimeout;
32  	}
33  
34  	/**
35  	 * {@inheritDoc}
36  	 */
37  	public Socket createSocket() throws IOException {
38  		Socket retVal = javax.net.SocketFactory.getDefault().createSocket();
39  		retVal.setKeepAlive(true);
40  		retVal.setTcpNoDelay(true);
41  		return retVal;
42  	}
43  
44  	/**
45  	 * {@inheritDoc}
46  	 */
47  	public Socket createTlsSocket() throws IOException {
48  		Socket retVal = javax.net.ssl.SSLSocketFactory.getDefault().createSocket();
49  		retVal.setKeepAlive(true);
50  		retVal.setTcpNoDelay(true);
51  		return retVal;
52  	}
53  
54  	/**
55  	 * {@inheritDoc}
56  	 */
57  	public ServerSocket createServerSocket() throws IOException {
58  		return javax.net.ServerSocketFactory.getDefault().createServerSocket();
59  	}
60  
61  	/**
62  	 * {@inheritDoc}
63  	 */
64  	public ServerSocket createTlsServerSocket() throws IOException {
65  		return javax.net.ssl.SSLServerSocketFactory.getDefault().createServerSocket();
66  	}
67  
68  	public void configureNewAcceptedSocket(Socket theSocket) throws SocketException {
69  		theSocket.setSoTimeout(myAcceptedSocketTimeout);
70  	}
71  
72  }