Skip to content

Commit 2f84dfe

Browse files
authored
Merge pull request docker-java#862 from xuchenCN/OS-Compatibility
Make NettyDockerCmdExecFactory has compatibility both Linux and OSX automatically
2 parents fbd1ce1 + 7e1532f commit 2f84dfe

File tree

2 files changed

+52
-20
lines changed

2 files changed

+52
-20
lines changed

pom.xml

+7-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
<!-- test dependencies -->
7474
<logback.version>1.1.7</logback.version>
7575
<testng.version>6.9.10</testng.version>
76-
<netty.version>4.1.3.Final</netty.version>
76+
<netty.version>4.1.11.Final</netty.version>
7777
<hamcrest.library.version>1.3</hamcrest.library.version>
7878
<hamcrest.jpa-matchers>1.8</hamcrest.jpa-matchers>
7979
<lambdaj.version>2.3.3</lambdaj.version>
@@ -258,6 +258,12 @@
258258
<version>${netty.version}</version>
259259
<classifier>linux-x86_64</classifier>
260260
</dependency>
261+
<dependency>
262+
<groupId>io.netty</groupId>
263+
<artifactId>netty-transport-native-kqueue</artifactId>
264+
<version>${netty.version}</version>
265+
<classifier>osx-x86_64</classifier>
266+
</dependency>
261267
<dependency>
262268
<groupId>junit</groupId>
263269
<artifactId>junit</artifactId>

src/main/java/com/github/dockerjava/netty/NettyDockerCmdExecFactory.java

+45-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
package com.github.dockerjava.netty;
22

3+
import static com.google.common.base.Preconditions.checkNotNull;
4+
5+
import java.io.IOException;
6+
import java.net.InetAddress;
7+
import java.net.InetSocketAddress;
8+
import java.net.SocketAddress;
9+
import java.security.Security;
10+
11+
import javax.net.ssl.SSLEngine;
12+
import javax.net.ssl.SSLParameters;
13+
14+
import org.apache.commons.lang.SystemUtils;
15+
import org.bouncycastle.jce.provider.BouncyCastleProvider;
16+
317
import com.github.dockerjava.api.command.AttachContainerCmd;
418
import com.github.dockerjava.api.command.AuthCmd;
519
import com.github.dockerjava.api.command.BuildImageCmd;
@@ -39,6 +53,7 @@
3953
import com.github.dockerjava.api.command.RemoveImageCmd;
4054
import com.github.dockerjava.api.command.RemoveNetworkCmd;
4155
import com.github.dockerjava.api.command.RemoveVolumeCmd;
56+
import com.github.dockerjava.api.command.RenameContainerCmd;
4257
import com.github.dockerjava.api.command.RestartContainerCmd;
4358
import com.github.dockerjava.api.command.SaveImageCmd;
4459
import com.github.dockerjava.api.command.SearchImagesCmd;
@@ -51,7 +66,6 @@
5166
import com.github.dockerjava.api.command.UpdateContainerCmd;
5267
import com.github.dockerjava.api.command.VersionCmd;
5368
import com.github.dockerjava.api.command.WaitContainerCmd;
54-
import com.github.dockerjava.api.command.RenameContainerCmd;
5569
import com.github.dockerjava.core.DockerClientConfig;
5670
import com.github.dockerjava.core.DockerClientImpl;
5771
import com.github.dockerjava.core.SSLConfig;
@@ -93,6 +107,7 @@
93107
import com.github.dockerjava.netty.exec.RemoveImageCmdExec;
94108
import com.github.dockerjava.netty.exec.RemoveNetworkCmdExec;
95109
import com.github.dockerjava.netty.exec.RemoveVolumeCmdExec;
110+
import com.github.dockerjava.netty.exec.RenameContainerCmdExec;
96111
import com.github.dockerjava.netty.exec.RestartContainerCmdExec;
97112
import com.github.dockerjava.netty.exec.SaveImageCmdExec;
98113
import com.github.dockerjava.netty.exec.SearchImagesCmdExec;
@@ -105,7 +120,6 @@
105120
import com.github.dockerjava.netty.exec.UpdateContainerCmdExec;
106121
import com.github.dockerjava.netty.exec.VersionCmdExec;
107122
import com.github.dockerjava.netty.exec.WaitContainerCmdExec;
108-
import com.github.dockerjava.netty.exec.RenameContainerCmdExec;
109123

110124
import io.netty.bootstrap.Bootstrap;
111125
import io.netty.channel.Channel;
@@ -115,6 +129,8 @@
115129
import io.netty.channel.EventLoopGroup;
116130
import io.netty.channel.epoll.EpollDomainSocketChannel;
117131
import io.netty.channel.epoll.EpollEventLoopGroup;
132+
import io.netty.channel.kqueue.KQueueDomainSocketChannel;
133+
import io.netty.channel.kqueue.KQueueEventLoopGroup;
118134
import io.netty.channel.nio.NioEventLoopGroup;
119135
import io.netty.channel.socket.DuplexChannel;
120136
import io.netty.channel.socket.SocketChannel;
@@ -126,19 +142,6 @@
126142
import io.netty.handler.ssl.SslHandler;
127143
import io.netty.util.concurrent.DefaultThreadFactory;
128144

129-
import org.bouncycastle.jce.provider.BouncyCastleProvider;
130-
131-
import javax.net.ssl.SSLEngine;
132-
import javax.net.ssl.SSLParameters;
133-
134-
import java.io.IOException;
135-
import java.net.InetAddress;
136-
import java.net.InetSocketAddress;
137-
import java.net.SocketAddress;
138-
import java.security.Security;
139-
140-
import static com.google.common.base.Preconditions.checkNotNull;
141-
142145
/**
143146
* Experimental implementation of {@link DockerCmdExecFactory} that supports http connection hijacking that is needed to pass STDIN to the
144147
* container.
@@ -226,6 +229,15 @@ private interface NettyInitializer {
226229
private class UnixDomainSocketInitializer implements NettyInitializer {
227230
@Override
228231
public EventLoopGroup init(Bootstrap bootstrap, DockerClientConfig dockerClientConfig) {
232+
if (SystemUtils.IS_OS_LINUX) {
233+
return epollGroup();
234+
} else if (SystemUtils.IS_OS_MAC_OSX) {
235+
return kqueueGroup();
236+
}
237+
throw new RuntimeException("Unspported OS");
238+
}
239+
240+
public EventLoopGroup epollGroup() {
229241
EventLoopGroup epollEventLoopGroup = new EpollEventLoopGroup(0, new DefaultThreadFactory(threadPrefix));
230242

231243
ChannelFactory<EpollDomainSocketChannel> factory = new ChannelFactory<EpollDomainSocketChannel>() {
@@ -235,14 +247,28 @@ public EpollDomainSocketChannel newChannel() {
235247
}
236248
};
237249

238-
bootstrap.group(epollEventLoopGroup).channelFactory(factory)
239-
.handler(new ChannelInitializer<UnixChannel>() {
250+
bootstrap.group(epollEventLoopGroup).channelFactory(factory).handler(new ChannelInitializer<UnixChannel>() {
251+
@Override
252+
protected void initChannel(final UnixChannel channel) throws Exception {
253+
channel.pipeline().addLast(new HttpClientCodec());
254+
}
255+
});
256+
return epollEventLoopGroup;
257+
}
258+
259+
public EventLoopGroup kqueueGroup() {
260+
EventLoopGroup nioEventLoopGroup = new KQueueEventLoopGroup(0, new DefaultThreadFactory(threadPrefix));
261+
262+
bootstrap.group(nioEventLoopGroup).channel(KQueueDomainSocketChannel.class)
263+
.handler(new ChannelInitializer<KQueueDomainSocketChannel>() {
240264
@Override
241-
protected void initChannel(final UnixChannel channel) throws Exception {
265+
protected void initChannel(final KQueueDomainSocketChannel channel) throws Exception {
266+
channel.pipeline().addLast(new LoggingHandler(getClass()));
242267
channel.pipeline().addLast(new HttpClientCodec());
243268
}
244269
});
245-
return epollEventLoopGroup;
270+
271+
return nioEventLoopGroup;
246272
}
247273

248274
@Override

0 commit comments

Comments
 (0)