Skip to content

Commit

Permalink
Load DockerClientProviderStrategy via ServiceLoader (testcontainers#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
srempfer committed Aug 5, 2017
1 parent 32b6551 commit 85eb03b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
16 changes: 6 additions & 10 deletions core/src/main/java/org/testcontainers/DockerClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
import java.io.InputStream;
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.function.BiFunction;
import java.util.function.Consumer;

import static java.util.Arrays.asList;

/**
* Singleton class that provides initialized Docker clients.
* <p>
Expand All @@ -46,13 +46,6 @@ public class DockerClientFactory {
// Cached client configuration
private DockerClientProviderStrategy strategy;
private boolean preconditionsChecked = false;

private static final List<DockerClientProviderStrategy> CONFIGURATION_STRATEGIES =
asList(new EnvironmentAndSystemPropertyClientProviderStrategy(),
new UnixSocketClientProviderStrategy(),
new ProxiedUnixSocketClientProviderStrategy(),
new DockerMachineClientProviderStrategy(),
new WindowsClientProviderStrategy());
private String activeApiVersion;
private String activeExecutionDriver;

Expand Down Expand Up @@ -91,7 +84,10 @@ public DockerClient client() {
return strategy.getClient();
}

strategy = DockerClientProviderStrategy.getFirstValidStrategy(CONFIGURATION_STRATEGIES);
List<DockerClientProviderStrategy> configurationStrategies = new ArrayList<DockerClientProviderStrategy>();
ServiceLoader.load(DockerClientProviderStrategy.class).forEach( cs -> configurationStrategies.add( cs ) );

strategy = DockerClientProviderStrategy.getFirstValidStrategy(configurationStrategies);

String hostIpAddress = strategy.getDockerHostIpAddress();
log.info("Docker host IP address is {}", hostIpAddress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,4 @@ protected void ping(DockerClient client, int timeoutInSeconds) {
public String getDockerHostIpAddress() {
return DockerClientConfigUtils.getDockerHostIpAddress(this.config);
}


class InvalidConfigurationException extends RuntimeException {

public InvalidConfigurationException(String s) {
super(s);
}

public InvalidConfigurationException(String message, Throwable cause) {
super(message, cause);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.testcontainers.dockerclient;

/**
* Exception to indicate that a {@link DockerClientProviderStrategy} fails.
*/
public class InvalidConfigurationException extends RuntimeException {

public InvalidConfigurationException(String s) {
super(s);
}

public InvalidConfigurationException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
org.testcontainers.dockerclient.EnvironmentAndSystemPropertyClientProviderStrategy
org.testcontainers.dockerclient.UnixSocketClientProviderStrategy
org.testcontainers.dockerclient.ProxiedUnixSocketClientProviderStrategy
org.testcontainers.dockerclient.DockerMachineClientProviderStrategy
org.testcontainers.dockerclient.WindowsClientProviderStrategy

0 comments on commit 85eb03b

Please sign in to comment.