Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 33A1DB5DBDF for ; Sat, 6 Aug 2011 17:58:27 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 53303-07 for ; Sat, 6 Aug 2011 20:58:20 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id 2A03FB5DBC4 for ; Sat, 6 Aug 2011 17:58:19 -0300 (ADT) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1Qpnwo-0004ai-Ir for pgsql-jdbc@postgresql.org; Sat, 06 Aug 2011 22:58:18 +0200 Received: from cpc10-with5-2-0-cust74.1-4.cable.virginmedia.com ([86.26.85.75]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 06 Aug 2011 22:58:18 +0200 Received: from bruno by cpc10-with5-2-0-cust74.1-4.cable.virginmedia.com with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 06 Aug 2011 22:58:18 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-jdbc@postgresql.org From: Bruno Harbulot Subject: Re: Connecting over UNIX domain sockets Date: Sat, 06 Aug 2011 21:58:03 +0100 Lines: 307 Message-ID: References: <852104.71586.qm@web65506.mail.ac4.yahoo.com> <4E090211.8010502@postnewspapers.com.au> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------010407000405090807060801" X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: cpc10-with5-2-0-cust74.1-4.cable.virginmedia.com User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:5.0) Gecko/20110624 Thunderbird/5.0 In-Reply-To: <4E090211.8010502@postnewspapers.com.au> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.683 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-0.783 X-Spam-Level: X-Archive-Number: 201108/25 X-Sequence-Number: 22057 This is a multi-part message in MIME format. --------------010407000405090807060801 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 27/06/2011 23:20, Craig Ringer wrote: > On 06/28/2011 03:02 AM, Phil Clay wrote: > >> However, contrary to mysql's jdbc driver, postgres' jdbc driver does >> not appear >> to be designed in a way that a different Socket implementation could >> be easily >> "plugged in". > > There's already support for pluggable SSLSocketFactories to wrap > sockets. Adding pluggable SocketFactory support wouldn't seem to be > overly hard, I just suspect it's a combination of nobody having needed > it and nobody having wanted it enough to take the time to write a patch. Hello, I'm not sure if anyone had done any further work on this thread, but here is a tentative patch (I've written it against the 8.4 code base) to be able to use a custom SocketFactory (in particular for junixsocket). This relies on two new properties: - socketfactory, for the class name of the SocketFactory. - socketfactoryarg, for the String argument of this SocketFactory constructor. I've changed this constructor into two: - public PGStream(String host, int port) + public PGStream(String host, int port, SocketFactory socketFactory) + public PGStream(String host, int port, Properties info) The one that uses Properties reads the properties to instantiate the SocketFactory (via a new method PGStream.createSocketFactory(Properties info), which is a very similar to the way the SSLSocketFactory is instantiated). The one that uses a SocketFactory can be used directly, but the main reason I've introduced it was for PGStreams creates from other PGStreams (the ones called "cancelledStream"). The only glitch was in PGStream.changeSocket(...), since AFUNIXSocket (from junixsocket) doesn't support setTcpNoDelay (and throws an exception). I made it catch any SocketException: although it could catch AFUNIXSocketException (which extends SocketException), this would make the driver depend on junixsocket. - connection.setTcpNoDelay(true); + try { + connection.setTcpNoDelay(true); + } catch (SocketException e) { + // Ignore if the operation wasn't supported. + } I'm also attaching a sample PgJdbcUnixSocketFactory to use this with junixsocket (which needs to be on the classpath and have 'java.library.path' configured to point to its native libraries). The following works, using a unix socket. Properties props = new Properties(); props.setProperty("user", "name"); props.setProperty("password", "xxxxxx"); props.setProperty("socketfactory", "jdbctest.PgJdbcUnixSocketFactory"); props.setProperty("socketfactoryarg", "/tmp/.s.PGSQL.5432"); Connection jdbcConnection = DriverManager.getConnection("jdbc:postgresql://", props) To specify which database is to be used, it's possible to use something like this: DriverManager.getConnection("jdbc:postgresql://.../dbname", props) (What's in "..." doesn't really matter, but "jdbc:postgresql:///dbname" doesn't work.) I'm not sure how these changes would fit with the rest of the design of the driver, as I must admit I don't know this code very well. I suppose there are multiple ways of implementing this feature, but I hope this patch may be a sufficient starting point. Best wishes, Bruno. --------------010407000405090807060801 Content-Type: text/plain; x-mac-type="0"; x-mac-creator="0"; name="PgJdbcUnixSocketFactory.java" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="PgJdbcUnixSocketFactory.java" 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(); } } --------------010407000405090807060801 Content-Type: text/plain; x-mac-type="0"; x-mac-creator="0"; name="socketfactory.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="socketfactory.patch" ? socketfactory.patch Index: org/postgresql/core/PGStream.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/core/PGStream.java,v retrieving revision 1.22.4.1 diff -r1.22.4.1 PGStream.java 18a19 > import java.lang.reflect.Constructor; 19a21 > import java.net.SocketException; 20a23,25 > import java.util.Properties; > > import javax.net.SocketFactory; 36a42 > private final SocketFactory socketFactory; 54a61 > * @param socketFactory {@link SocketFactory} used for creating the sockets 57c64 < public PGStream(String host, int port) throws IOException --- > public PGStream(String host, int port, SocketFactory socketFactory) throws IOException 61,62c68,73 < < changeSocket(new Socket(host, port)); --- > > if (socketFactory == null) { > socketFactory = SocketFactory.getDefault(); > } > this.socketFactory = socketFactory; > changeSocket(this.socketFactory.createSocket(host, port)); 67a79,135 > > /** > * Constructor: Connect to the PostgreSQL back end and return > * a stream connection. > * > * @param host the hostname to connect to > * @param port the port number that the postmaster is sitting on > * @param info properties used to create the socket factory > * @exception IOException if an IOException occurs below it. > */ > public PGStream(String host, int port, Properties info) throws IOException > { > this(host, port, createSocketFactory(info)); > } > > public static SocketFactory createSocketFactory(Properties info) { > SocketFactory factory; > > // Use the default factory if no specific factory is requested > // > String classname = info.getProperty("socketfactory"); > if (classname == null) > { > factory = SocketFactory.getDefault(); > } > else > { > Object[] args = {info.getProperty("socketfactoryarg")}; > Constructor ctor; > Class factoryClass; > > try > { > factoryClass = Class.forName(classname); > try > { > ctor = factoryClass.getConstructor(new Class[]{String.class}); > } > catch (NoSuchMethodException nsme) > { > ctor = factoryClass.getConstructor((Class[])null); > args = null; > } > factory = (SocketFactory)ctor.newInstance(args); > } > catch (Exception e) > { > throw new RuntimeException(new PSQLException(GT.tr("The SocketFactory class provided {0} could not be instantiated.", classname), PSQLState.CONNECTION_FAILURE, e)); > } > } > > return factory; > } > > public SocketFactory getSocketFactory() throws PSQLException { > return socketFactory; > } 108c176,180 < connection.setTcpNoDelay(true); --- > try { > connection.setTcpNoDelay(true); > } catch (SocketException e) { > // Ignore if the operation wasn't supported. > } Index: org/postgresql/core/v2/ConnectionFactoryImpl.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/core/v2/ConnectionFactoryImpl.java,v retrieving revision 1.18.2.1 diff -r1.18.2.1 ConnectionFactoryImpl.java 62c62 < newStream = new PGStream(host, port); --- > newStream = new PGStream(host, port, info); 163c163 < return new PGStream(pgStream.getHost(), pgStream.getPort()); --- > return new PGStream(pgStream.getHost(), pgStream.getPort(), pgStream.getSocketFactory()); Index: org/postgresql/core/v2/ProtocolConnectionImpl.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/core/v2/ProtocolConnectionImpl.java,v retrieving revision 1.12 diff -r1.12 ProtocolConnectionImpl.java 93c93 < cancelStream = new PGStream(pgStream.getHost(), pgStream.getPort()); --- > cancelStream = new PGStream(pgStream.getHost(), pgStream.getPort(), pgStream.getSocketFactory()); Index: org/postgresql/core/v3/ConnectionFactoryImpl.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/core/v3/ConnectionFactoryImpl.java,v retrieving revision 1.20.2.1 diff -r1.20.2.1 ConnectionFactoryImpl.java 16a17 > import java.lang.reflect.Constructor; 76c77,78 < newStream = new PGStream(host, port); --- > > newStream = new PGStream(host, port, info); 192c194 < return new PGStream(pgStream.getHost(), pgStream.getPort()); --- > return new PGStream(pgStream.getHost(), pgStream.getPort(), info); Index: org/postgresql/core/v3/ProtocolConnectionImpl.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/core/v3/ProtocolConnectionImpl.java,v retrieving revision 1.13 diff -r1.13 ProtocolConnectionImpl.java 93c93 < cancelStream = new PGStream(pgStream.getHost(), pgStream.getPort()); --- > cancelStream = new PGStream(pgStream.getHost(), pgStream.getPort(), pgStream.getSocketFactory()); --------------010407000405090807060801--