001package ca.uhn.hl7v2.hoh.util;
002
003import java.io.IOException;
004import java.net.ServerSocket;
005import java.net.Socket;
006import java.net.SocketException;
007
008import javax.net.ServerSocketFactory;
009
010import ca.uhn.hl7v2.hoh.sockets.ISocketFactory;
011import ca.uhn.hl7v2.util.SocketFactory;
012import ca.uhn.hl7v2.util.StandardSocketFactory;
013
014/**
015 * Wraps an HoH {@link ISocketFactory} instance for use in HAPI. Note that the
016 * wrapped methods will only be used to create TLS socket instances.
017 */
018public class HapiSocketTlsFactoryWrapper implements SocketFactory {
019
020        private ISocketFactory mySocketFactory;
021
022        /**
023         * Constuctor
024         * 
025         * @param theSocketFactoryToWrap
026         *            The socket factory to wrap. This instance will only be used
027         *            for {@link #createTlsSocket()} and
028         *            {@link #createTlsServerSocket()}
029         */
030        public HapiSocketTlsFactoryWrapper(ISocketFactory theSocketFactoryToWrap) {
031                mySocketFactory = theSocketFactoryToWrap;
032        }
033
034        /**
035         * {@inheritDoc}
036         */
037        public Socket createSocket() throws IOException {
038                return javax.net.SocketFactory.getDefault().createSocket();
039        }
040
041        /**
042         * {@inheritDoc}
043         */
044        public Socket createTlsSocket() throws IOException {
045                return mySocketFactory.createClientSocket();
046        }
047
048        /**
049         * {@inheritDoc}
050         */
051        public ServerSocket createServerSocket() throws IOException {
052                return ServerSocketFactory.getDefault().createServerSocket();
053        }
054
055        /**
056         * {@inheritDoc}
057         */
058        public ServerSocket createTlsServerSocket() throws IOException {
059                return mySocketFactory.createServerSocket();
060        }
061
062        public void configureNewAcceptedSocket(Socket theSocket) throws SocketException {
063                theSocket.setSoTimeout(StandardSocketFactory.DEFAULT_ACCEPTED_SOCKET_TIMEOUT);
064        }
065
066}