package jdbctest;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.net.SocketFactory;

import org.newsclub.net.unix.AFUNIXSocket;
import org.newsclub.net.unix.AFUNIXSocketAddress;

/**
 * Sample SocketFactory using junixsocket for use with the PostgreSQL JDBC driver.
 * @author Bruno Harbulot
 */
public class PgJdbcUnixSocketFactory extends SocketFactory {
    private final String path;

    public PgJdbcUnixSocketFactory(String path) {
        this.path = path;
    }

    @Override
    public Socket createSocket(String host, int port) throws IOException,
            UnknownHostException {
        AFUNIXSocket socket = AFUNIXSocket.newInstance();
        socket.connect(new AFUNIXSocketAddress(new File(path)), port);
        return socket;
    }

    @Override
    public Socket createSocket(InetAddress host, int port) throws IOException {
        throw new UnsupportedOperationException();
    }

    @Override
    public Socket createSocket(String host, int port, InetAddress localHost,
            int localPort) throws IOException, UnknownHostException {
        throw new UnsupportedOperationException();
    }

    @Override
    public Socket createSocket(InetAddress address, int port,
            InetAddress localAddress, int localPort) throws IOException {
        throw new UnsupportedOperationException();
    }
}
