pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruno Harbulot <bruno@distributedmatter.net>
To: pgsql-jdbc@postgresql.org
Subject: Re: Connecting over UNIX domain sockets
Date: Sat, 06 Aug 2011 21:58:03 +0100
Message-ID: <j1k9ss$p4m$1@dough.gmane.org> (raw)
In-Reply-To: <4E090211.8010502@postnewspapers.com.au>
References: <852104.71586.qm@web65506.mail.ac4.yahoo.com>
	<4E090211.8010502@postnewspapers.com.au>

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.

Attachments:

PgJdbcUnixSocketFactory.java (text/plain, 1.4K) download 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(); } }
socketfactory.patch (text/plain, 5.4K) download ? 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());

view thread (7+ messages)

Message-ID: <j1k9ss$p4m$1@dough.gmane.org>
Permalink:  ../j1k9ss$p4m$1@dough.gmane.org/
Also on:    postgresql.org/message-id/j1k9ss$p4m$1@dough.gmane.org

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: bruno@distributedmatter.net
  Subject: Re: Connecting over UNIX domain sockets
  In-Reply-To: <j1k9ss$p4m$1@dough.gmane.org>
  References: <852104.71586.qm@web65506.mail.ac4.yahoo.com> <4E090211.8010502@postnewspapers.com.au> <j1k9ss$p4m$1@dough.gmane.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox