Skip to content

Commit d2ff8e1

Browse files
joankaradimovrozza
authored andcommitted
Support Unix Domain Sockets via jnr-unixsocket
JAVA-163
1 parent 2b12bbe commit d2ff8e1

File tree

7 files changed

+61
-8
lines changed

7 files changed

+61
-8
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ apply plugin: 'eclipse'
2020
apply plugin: 'idea'
2121

2222
def configDir = new File(rootDir, 'config')
23+
ext.jnrUnixsocketVersion = '0.18'
2324
ext.nettyVersion = '4.1.17.Final'
2425
ext.snappyVersion = '1.1.4'
2526

driver-core/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ clirr {
2929
dependencies {
3030
compile project(':bson')
3131

32+
compile "com.github.jnr:jnr-unixsocket:$jnrUnixsocketVersion", optional
3233
compile "io.netty:netty-buffer:$nettyVersion", optional
3334
compile "io.netty:netty-transport:$nettyVersion", optional
3435
compile "io.netty:netty-handler:$nettyVersion", optional

driver-core/src/main/com/mongodb/ServerAddress.java

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@
1919
import com.mongodb.annotations.Immutable;
2020
import com.mongodb.lang.Nullable;
2121

22+
import jnr.unixsocket.UnixSocketAddress;
23+
24+
import java.io.File;
2225
import java.io.Serializable;
2326
import java.net.InetAddress;
2427
import java.net.InetSocketAddress;
28+
import java.net.SocketAddress;
2529
import java.net.UnknownHostException;
2630

2731
/**
@@ -33,6 +37,7 @@ public class ServerAddress implements Serializable {
3337

3438
private final String host;
3539
private final int port;
40+
private final SocketAddress address;
3641

3742
/**
3843
* Creates a ServerAddress with default host and port
@@ -56,7 +61,7 @@ public ServerAddress(@Nullable final String host) {
5661
* @param inetAddress host address
5762
*/
5863
public ServerAddress(final InetAddress inetAddress) {
59-
this(inetAddress.getHostName(), defaultPort());
64+
this(inetAddress.getHostName(), defaultPort(), new InetSocketAddress(inetAddress, defaultPort()));
6065
}
6166

6267
/**
@@ -66,7 +71,7 @@ public ServerAddress(final InetAddress inetAddress) {
6671
* @param port mongod port
6772
*/
6873
public ServerAddress(final InetAddress inetAddress, final int port) {
69-
this(inetAddress.getHostName(), port);
74+
this(inetAddress.getHostName(), port, new InetSocketAddress(inetAddress, port));
7075
}
7176

7277
/**
@@ -75,7 +80,38 @@ public ServerAddress(final InetAddress inetAddress, final int port) {
7580
* @param inetSocketAddress inet socket address containing hostname and port
7681
*/
7782
public ServerAddress(final InetSocketAddress inetSocketAddress) {
78-
this(inetSocketAddress.getAddress(), inetSocketAddress.getPort());
83+
this(inetSocketAddress.getHostName(), inetSocketAddress.getPort(), inetSocketAddress);
84+
}
85+
86+
/**
87+
* Creates a ServerAddress
88+
*
89+
* @param serverAddress an instance to be shallow-copied
90+
*/
91+
public ServerAddress(final ServerAddress serverAddress) {
92+
this(serverAddress.host, serverAddress.port, serverAddress.address);
93+
}
94+
95+
/**
96+
* Creates a ServerAddress
97+
*
98+
* @param path the file used for the Unix domain socket
99+
*/
100+
public ServerAddress(final File path) {
101+
this(path.toString(), 0, new UnixSocketAddress(path));
102+
}
103+
104+
/**
105+
* Creates a ServerAddress - intended for internal usage
106+
*
107+
* @param host hostname
108+
* @param port mongod port
109+
* @param address an instance of socket address or `null`
110+
*/
111+
protected ServerAddress(final String host, final int port, final SocketAddress address) {
112+
this.host = host;
113+
this.port = port;
114+
this.address = address;
79115
}
80116

81117
/**
@@ -125,6 +161,7 @@ public ServerAddress(@Nullable final String host, final int port) {
125161
}
126162
this.host = hostToUse.toLowerCase();
127163
this.port = portToUse;
164+
this.address = null;
128165
}
129166

130167
@Override
@@ -179,7 +216,10 @@ public int getPort() {
179216
*
180217
* @return socket address
181218
*/
182-
public InetSocketAddress getSocketAddress() {
219+
public SocketAddress getSocketAddress() {
220+
if (address != null) {
221+
return address;
222+
}
183223
try {
184224
return new InetSocketAddress(InetAddress.getByName(host), port);
185225
} catch (UnknownHostException e) {

driver-core/src/main/com/mongodb/connection/ClusterSettings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public Builder hosts(final List<ServerAddress> hosts) {
144144
Set<ServerAddress> hostsSet = new LinkedHashSet<ServerAddress>(hosts.size());
145145
for (ServerAddress host : hosts) {
146146
notNull("host", host);
147-
hostsSet.add(new ServerAddress(host.getHost(), host.getPort()));
147+
hostsSet.add(new ServerAddress(host));
148148
}
149149
this.hosts = unmodifiableList(new ArrayList<ServerAddress>(hostsSet));
150150
return this;

driver-core/src/main/com/mongodb/connection/SocketChannelStream.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import com.mongodb.MongoSocketOpenException;
2020
import com.mongodb.MongoSocketReadException;
2121
import com.mongodb.ServerAddress;
22+
import jnr.unixsocket.UnixSocketAddress;
23+
import jnr.unixsocket.UnixSocketChannel;
2224
import org.bson.ByteBuf;
2325

2426
import java.io.IOException;
@@ -48,8 +50,12 @@ class SocketChannelStream implements Stream {
4850
@Override
4951
public void open() throws IOException {
5052
try {
51-
socketChannel = SocketChannel.open();
52-
SocketStreamHelper.initialize(socketChannel.socket(), address, settings, sslSettings);
53+
if (address.getSocketAddress() instanceof UnixSocketAddress) {
54+
socketChannel = UnixSocketChannel.open((UnixSocketAddress) address.getSocketAddress());
55+
} else {
56+
socketChannel = SocketChannel.open();
57+
SocketStreamHelper.initialize(socketChannel.socket(), address, settings, sslSettings);
58+
}
5359
} catch (IOException e) {
5460
close();
5561
throw new MongoSocketOpenException("Exception opening socket", getAddress(), e);

driver-core/src/main/com/mongodb/connection/SocketStreamFactory.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import javax.net.ssl.SSLContext;
2525
import java.security.NoSuchAlgorithmException;
2626

27+
import jnr.unixsocket.UnixSocketAddress;
28+
2729
import static com.mongodb.assertions.Assertions.notNull;
2830

2931
/**
@@ -63,7 +65,9 @@ public SocketStreamFactory(final SocketSettings settings, final SslSettings sslS
6365
@Override
6466
public Stream create(final ServerAddress serverAddress) {
6567
Stream stream;
66-
if (socketFactory != null) {
68+
if (serverAddress.getSocketAddress() instanceof UnixSocketAddress) {
69+
stream = new SocketChannelStream(serverAddress, settings, sslSettings, bufferProvider);
70+
} else if (socketFactory != null) {
6771
stream = new SocketStream(serverAddress, settings, sslSettings, socketFactory, bufferProvider);
6872
} else if (sslSettings.isEnabled()) {
6973
stream = new SocketStream(serverAddress, settings, sslSettings, getSslContext().getSocketFactory(), bufferProvider);

mongo-java-driver/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ archivesBaseName = 'mongo-java-driver'
2121

2222
// dependencies copied from driver-core
2323
dependencies {
24+
compile "com.github.jnr:jnr-unixsocket:$jnrUnixsocketVersion", optional
2425
compile "io.netty:netty-buffer:$nettyVersion", optional
2526
compile "io.netty:netty-transport:$nettyVersion", optional
2627
compile "io.netty:netty-handler:$nettyVersion", optional

0 commit comments

Comments
 (0)