Skip to content

Commit dd17358

Browse files
SineaggiClayton Walkerbsideup
authored
Add java16 unix socket support (docker-java#1627)
* Add java16 unix socket support * Update docker-java-transport/src/main/java/com/github/dockerjava/transport/UnixSocket.java * Update ApacheDockerHttpClientImpl.java * Use UnixSocket in OkHttp as well * Update UnixSocket.java * Update DomainSocket.java * Update UnixSocket.java Co-authored-by: Clayton Walker <cwalker@sofi.org> Co-authored-by: Sergei Egorov <bsideup@gmail.com>
1 parent b1430f3 commit dd17358

4 files changed

Lines changed: 94 additions & 4 deletions

File tree

docker-java-transport-httpclient5/src/main/java/com/github/dockerjava/httpclient5/ApacheDockerHttpClientImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.github.dockerjava.httpclient5;
22

33
import com.github.dockerjava.transport.DockerHttpClient;
4-
import com.github.dockerjava.transport.DomainSocket;
54
import com.github.dockerjava.transport.NamedPipeSocket;
65
import com.github.dockerjava.transport.SSLConfig;
6+
import com.github.dockerjava.transport.UnixSocket;
77
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
88
import org.apache.hc.client5.http.config.RequestConfig;
99
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
@@ -134,7 +134,7 @@ private Registry<ConnectionSocketFactory> createConnectionSocketFactoryRegistry(
134134
.register("unix", new PlainConnectionSocketFactory() {
135135
@Override
136136
public Socket createSocket(HttpContext context) throws IOException {
137-
return DomainSocket.get(dockerHost.getPath());
137+
return UnixSocket.get(dockerHost.getPath());
138138
}
139139
})
140140
.register("npipe", new PlainConnectionSocketFactory() {

docker-java-transport-okhttp/src/main/java/com/github/dockerjava/okhttp/UnixSocketFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.github.dockerjava.okhttp;
22

3-
import com.github.dockerjava.transport.DomainSocket;
3+
import com.github.dockerjava.transport.UnixSocket;
44

55
import javax.net.SocketFactory;
66
import java.io.IOException;
@@ -18,7 +18,7 @@ class UnixSocketFactory extends SocketFactory {
1818
@Override
1919
public Socket createSocket() {
2020
try {
21-
return DomainSocket.get(socketPath);
21+
return UnixSocket.get(socketPath);
2222
} catch (IOException e) {
2323
throw new RuntimeException(e);
2424
}

docker-java-transport/src/main/java/com/github/dockerjava/transport/DomainSocket.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ public void close() throws IOException {
130130
* @param path the path to the domain socket
131131
* @return a {@link DomainSocket} instance
132132
* @throws IOException if the socket cannot be opened
133+
* @deprecated use {@link UnixSocket#get(String)}
133134
*/
135+
@Deprecated
134136
public static DomainSocket get(String path) throws IOException {
135137
if (Platform.isMac() || isBsdPlatform()) {
136138
return new BsdDomainSocket(path);
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.github.dockerjava.transport;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.io.OutputStream;
6+
import java.net.Socket;
7+
import java.net.SocketAddress;
8+
import java.net.SocketException;
9+
import java.nio.channels.Channels;
10+
import java.nio.channels.SocketChannel;
11+
12+
public class UnixSocket extends AbstractSocket {
13+
14+
/**
15+
* Return a new {@link Socket} for the given path. Will use JDK's {@link java.net.UnixDomainSocketAddress}
16+
* if available and fallback to {@link DomainSocket} otherwise.
17+
*
18+
* @param path the path to the domain socket
19+
* @return a {@link Socket} instance
20+
* @throws IOException if the socket cannot be opened
21+
*/
22+
public static Socket get(String path) throws IOException {
23+
try {
24+
return new UnixSocket(path);
25+
} catch (Exception e) {
26+
//noinspection deprecation
27+
return DomainSocket.get(path);
28+
}
29+
}
30+
31+
private final SocketAddress socketAddress;
32+
33+
private final SocketChannel socketChannel;
34+
35+
private UnixSocket(String path) throws Exception {
36+
Class<?> unixDomainSocketAddress = Class.forName("java.net.UnixDomainSocketAddress");
37+
this.socketAddress =
38+
(SocketAddress) unixDomainSocketAddress.getMethod("of", String.class)
39+
.invoke(null, path);
40+
this.socketChannel = SocketChannel.open(this.socketAddress);
41+
}
42+
43+
@Override
44+
public InputStream getInputStream() throws IOException {
45+
if (isClosed()) {
46+
throw new SocketException("Socket is closed");
47+
}
48+
if (!isConnected()) {
49+
throw new SocketException("Socket is not connected");
50+
}
51+
if (isInputShutdown()) {
52+
throw new SocketException("Socket input is shutdown");
53+
}
54+
55+
return Channels.newInputStream(socketChannel);
56+
}
57+
58+
@Override
59+
public OutputStream getOutputStream() throws IOException {
60+
if (isClosed()) {
61+
throw new SocketException("Socket is closed");
62+
}
63+
if (!isConnected()) {
64+
throw new SocketException("Socket is not connected");
65+
}
66+
if (isOutputShutdown()) {
67+
throw new SocketException("Socket output is shutdown");
68+
}
69+
70+
return Channels.newOutputStream(socketChannel);
71+
}
72+
73+
@Override
74+
public SocketAddress getLocalSocketAddress() {
75+
return socketAddress;
76+
}
77+
78+
@Override
79+
public SocketAddress getRemoteSocketAddress() {
80+
return socketAddress;
81+
}
82+
83+
@Override
84+
public void close() throws IOException {
85+
super.close();
86+
this.socketChannel.close();
87+
}
88+
}

0 commit comments

Comments
 (0)