001package ca.uhn.hl7v2.util; 002 003import java.io.IOException; 004import java.net.ServerSocket; 005import java.net.Socket; 006import java.net.SocketException; 007 008public class StandardSocketFactory implements SocketFactory { 009 010 /** 011 * Default value for {@link #getAcceptedSocketTimeout()} 012 */ 013 public static final int DEFAULT_ACCEPTED_SOCKET_TIMEOUT = 500; 014 015 private int myAcceptedSocketTimeout = DEFAULT_ACCEPTED_SOCKET_TIMEOUT; 016 017 /** 018 * 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} 019 */ 020 public int getAcceptedSocketTimeout() { 021 return myAcceptedSocketTimeout; 022 } 023 024 /** 025 * Sets the value for {@link Socket#setSoTimeout(int)} to be set on newly accepted sockets in server applications 026 */ 027 public void setAcceptedSocketTimeout(int theAcceptedSocketTimeout) { 028 if (theAcceptedSocketTimeout < 0) { 029 throw new IllegalArgumentException("Timeout can't be negative"); 030 } 031 myAcceptedSocketTimeout = theAcceptedSocketTimeout; 032 } 033 034 /** 035 * {@inheritDoc} 036 */ 037 public Socket createSocket() throws IOException { 038 Socket retVal = javax.net.SocketFactory.getDefault().createSocket(); 039 retVal.setKeepAlive(true); 040 retVal.setTcpNoDelay(true); 041 return retVal; 042 } 043 044 /** 045 * {@inheritDoc} 046 */ 047 public Socket createTlsSocket() throws IOException { 048 Socket retVal = javax.net.ssl.SSLSocketFactory.getDefault().createSocket(); 049 retVal.setKeepAlive(true); 050 retVal.setTcpNoDelay(true); 051 return retVal; 052 } 053 054 /** 055 * {@inheritDoc} 056 */ 057 public ServerSocket createServerSocket() throws IOException { 058 return javax.net.ServerSocketFactory.getDefault().createServerSocket(); 059 } 060 061 /** 062 * {@inheritDoc} 063 */ 064 public ServerSocket createTlsServerSocket() throws IOException { 065 return javax.net.ssl.SSLServerSocketFactory.getDefault().createServerSocket(); 066 } 067 068 public void configureNewAcceptedSocket(Socket theSocket) throws SocketException { 069 theSocket.setSoTimeout(myAcceptedSocketTimeout); 070 } 071 072}