|
| 1 | +package com.github.dockerjava.httpclient5; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.URI; |
| 5 | +import java.time.Duration; |
| 6 | + |
| 7 | +import org.osgi.service.component.annotations.Activate; |
| 8 | +import org.osgi.service.component.annotations.Component; |
| 9 | +import org.osgi.service.component.annotations.Deactivate; |
| 10 | +import org.osgi.service.metatype.annotations.ObjectClassDefinition; |
| 11 | + |
| 12 | +import com.github.dockerjava.transport.DockerHttpClient; |
| 13 | +import com.github.dockerjava.transport.DockerHttpClientType; |
| 14 | + |
| 15 | +@DockerHttpClientType(value = OSGiApacheDockerHttpClientService.APACHE_HTTPCLIENT_5, transports = { DockerHttpClientType.TRANSPORT_STDIN_ATTACHMENT, |
| 16 | + DockerHttpClientType.TRANSPORT_WINDOWS_NPIPE, DockerHttpClientType.TRANSPORT_UNIX_SOCKETS }) |
| 17 | +@Component(service = DockerHttpClient.class, immediate = true, name = OSGiApacheDockerHttpClientService.PID) |
| 18 | +public class OSGiApacheDockerHttpClientService extends ApacheDockerHttpClientImpl { |
| 19 | + |
| 20 | + static final String APACHE_HTTPCLIENT_5 = "apache.httpclient.5"; |
| 21 | + public static final String PID = "com.github.dockerjava.httpclient5.basic"; |
| 22 | + |
| 23 | + @ObjectClassDefinition(pid = PID) |
| 24 | + @interface Config { |
| 25 | + String dockerHost() default "localhost"; |
| 26 | + |
| 27 | + int maxConnections() default Integer.MAX_VALUE;; |
| 28 | + |
| 29 | + long connectionTimeoutNanos() default -1; |
| 30 | + |
| 31 | + long responseTimeoutNanos() default -1; |
| 32 | + } |
| 33 | + |
| 34 | + @Activate |
| 35 | + protected OSGiApacheDockerHttpClientService(Config config) { |
| 36 | + super(toDockerHost(config.dockerHost()), null, config.maxConnections(), |
| 37 | + toDurationNanos(config.connectionTimeoutNanos()), toDurationNanos(config.responseTimeoutNanos())); |
| 38 | + } |
| 39 | + |
| 40 | + private static Duration toDurationNanos(long value) { |
| 41 | + if (value < 0) { |
| 42 | + return null; |
| 43 | + } |
| 44 | + return Duration.ofNanos(value); |
| 45 | + } |
| 46 | + |
| 47 | + private static URI toDockerHost(String config) { |
| 48 | + return URI.create(config); |
| 49 | + } |
| 50 | + |
| 51 | + @Deactivate |
| 52 | + public void deactivate() throws IOException { |
| 53 | + close(); |
| 54 | + } |
| 55 | + |
| 56 | +} |
0 commit comments