X Tutup
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 137 additions & 24 deletions src/main/java/com/github/dockerjava/client/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,169 @@
import java.net.URI;
import java.util.Properties;

class Config {
URI url;
String version, username, password, email;
Integer readTimeout;
boolean enableLoggingFilter;
public class Config {
private final URI uri;
private final String version, username, password, email;
private final Integer readTimeout;
private final boolean loggingFilterEnabled;

private Config() {
private Config(DockerClientConfigBuilder builder) {
this.uri = builder.uri;
this.version = builder.version;
this.username = builder.username;
this.password = builder.password;
this.email = builder.email;
this.readTimeout = builder.readTimeout;
this.loggingFilterEnabled = builder.loggingFilterEnabled;
}

static Config createConfig() throws DockerException {
final Properties p = new Properties();
public URI getUri() {
return uri;
}

public String getVersion() {
return version;
}

public String getUsername() {
return username;
}

public String getPassword() {
return password;
}

public String getEmail() {
return email;
}

public Integer getReadTimeout() {
return readTimeout;
}

public boolean isLoggingFilterEnabled() {
return loggingFilterEnabled;
}

public static Properties loadIncludedDockerProperties() {
try {
Properties p = new Properties();
p.load(Config.class.getResourceAsStream("/docker.io.properties"));
return p;
} catch (IOException e) {
throw new DockerException(e);
}
}

final File file = new File(System.getProperty("user.home"), ".docker.io.properties");
/**
* Creates a new Properties object containing values overridden from ${user.home}/.docker.io.properties
* @param p The original set of properties to override
* @return A copy of the original Properties with overridden values
*/
public static Properties overrideDockerPropertiesWithSettingsFromUserHome(Properties p) {
Properties overriddenProperties = new Properties();
overriddenProperties.putAll(p);

if (file.isFile()) {
final File usersDockerPropertiesFile = new File(System.getProperty("user.home"), ".docker.io.properties");
if (usersDockerPropertiesFile.isFile()) {
try {
final FileInputStream in = new FileInputStream(file);
final FileInputStream in = new FileInputStream(usersDockerPropertiesFile);
try {
p.load(in);
overriddenProperties.load(in);
} finally {
in.close();
}
} catch (IOException e) {
throw new DockerException(e);
}
}
return overriddenProperties;
}

for (String s : new String[]{"url", "version", "username", "password", "email"}) {
/**
* Creates a new Properties object containing values overridden from the System properties
* @param p The original set of properties to override
* @return A copy of the original Properties with overridden values
*/
public static Properties overrideDockerPropertiesWithSystemProperties(Properties p) {
Properties overriddenProperties = new Properties();
overriddenProperties.putAll(p);

// TODO Add all values from system properties that begin with docker.io.*
for (String s : new String[]{"url", "version", "username", "password", "email"}) {
final String key = "docker.io." + s;
if (System.getProperties().keySet().contains(key)) {
p.setProperty(key, System.getProperty(key));
if (System.getProperties().containsKey(key)) {
overriddenProperties.setProperty(key, System.getProperty(key));
}
}
return overriddenProperties;
}

public static DockerClientConfigBuilder createDefaultConfigBuilder() {
Properties properties = loadIncludedDockerProperties();
properties = overrideDockerPropertiesWithSettingsFromUserHome(properties);
properties = overrideDockerPropertiesWithSystemProperties(properties);
return new DockerClientConfigBuilder().withProperties(properties);
}

final Config c = new Config();
public static class DockerClientConfigBuilder {
private URI uri;
private String version, username, password, email;
private Integer readTimeout;
private boolean loggingFilterEnabled;

c.url = URI.create(p.getProperty("docker.io.url"));
c.version = p.getProperty("docker.io.version");
c.username = p.getProperty("docker.io.username");
c.password = p.getProperty("docker.io.password");
c.email = p.getProperty("docker.io.email");
c.readTimeout = Integer.valueOf(p.getProperty("docker.io.readTimeout", "1000"));
c.enableLoggingFilter = Boolean.valueOf(p.getProperty("docker.io.enableLoggingFilter", "true"));
public DockerClientConfigBuilder() {
}

/**
* This will set all fields in the builder to those contained in the Properties object. The Properties object
* should contain the following docker.io.* keys: url, version, username, password, and email. If
* docker.io.readTimeout or docker.io.enableLoggingFilter are not contained, they will be set to 1000 and true,
* respectively.
*
* @param p
* @return
*/
public DockerClientConfigBuilder withProperties(Properties p) {
return withUri(p.getProperty("docker.io.url"))
.withVersion(p.getProperty("docker.io.version"))
.withUsername(p.getProperty("docker.io.username"))
.withPassword(p.getProperty("docker.io.password"))
.withEmail(p.getProperty("docker.io.email"))
.withReadTimeout(Integer.valueOf(p.getProperty("docker.io.readTimeout", "1000")))
.withLoggingFilter(Boolean.valueOf(p.getProperty("docker.io.enableLoggingFilter", "true")));
}

return c;
public final DockerClientConfigBuilder withUri(String uri) {
this.uri = URI.create(uri);
return this;
}
public final DockerClientConfigBuilder withVersion(String version) {
this.version = version;
return this;
}
public final DockerClientConfigBuilder withUsername(String username) {
this.username = username;
return this;
}
public final DockerClientConfigBuilder withPassword(String password) {
this.password = password;
return this;
}
public final DockerClientConfigBuilder withEmail(String email) {
this.email = email;
return this;
}
public final DockerClientConfigBuilder withReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
return this;
}
public final DockerClientConfigBuilder withLoggingFilter(boolean loggingFilterEnabled) {
this.loggingFilterEnabled = loggingFilterEnabled;
return this;
}
public Config build() {
return new Config(this);
}
}
}
53 changes: 26 additions & 27 deletions src/main/java/com/github/dockerjava/client/DockerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URI;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
Expand Down Expand Up @@ -66,27 +65,25 @@ public class DockerClient {
private WebResource baseResource;
private AuthConfig authConfig;


public DockerClient() throws DockerException {
this(Config.createConfig());
public DockerClient() {
this(Config.createDefaultConfigBuilder().build());
}

public DockerClient(String serverUrl) throws DockerException {
public DockerClient(String serverUrl) {
this(configWithServerUrl(serverUrl));
}

private static Config configWithServerUrl(String serverUrl)
throws DockerException {
final Config c = Config.createConfig();
c.url = URI.create(serverUrl);
return c;

private static Config configWithServerUrl(String serverUrl) {
return Config.createDefaultConfigBuilder()
.withUri(serverUrl)
.build();
}

public DockerClient(Config config) {
ClientConfig clientConfig = new DefaultClientConfig();

SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", config.url.getPort(),
schemeRegistry.register(new Scheme("http", config.getUri().getPort(),
PlainSocketFactory.getSocketFactory()));
schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory
.getSocketFactory()));
Expand All @@ -103,17 +100,17 @@ public DockerClient(Config config) {
null, false), clientConfig);

// 1 hour
client.setReadTimeout(config.readTimeout);
client.setReadTimeout(config.getReadTimeout());

client.addFilter(new JsonClientFilter());
if (config.enableLoggingFilter)

if (config.isLoggingFilterEnabled())
client.addFilter(new SelectiveLoggingFilter());

baseResource = client.resource(config.url + "/v" + config.version);
baseResource = client.resource(config.getUri() + "/v" + config.getVersion());
}



public void setCredentials(String username, String password, String email) {
if (username == null) {
Expand Down Expand Up @@ -143,9 +140,11 @@ public AuthConfig authConfig() throws DockerException {
private static AuthConfig authConfigFromProperties() throws DockerException {
final AuthConfig a = new AuthConfig();

a.setUsername(Config.createConfig().username);
a.setPassword(Config.createConfig().password);
a.setEmail(Config.createConfig().email);
// TODO This should probably come from the Config used to create the DockerClient.
Config defaultConfig = Config.createDefaultConfigBuilder().build();
a.setUsername(defaultConfig.getUsername());
a.setPassword(defaultConfig.getPassword());
a.setEmail(defaultConfig.getEmail());

if (a.getUsername() == null) {
throw new IllegalStateException("username is null");
Expand Down Expand Up @@ -228,7 +227,7 @@ public ListContainersCmd listContainersCmd() {

public CreateContainerCmd createContainerCmd(String image) {
return new CreateContainerCmd(new CreateContainerConfig()).withImage(
image).withBaseResource(baseResource);
image).withBaseResource(baseResource);
}

public StartContainerCmd startContainerCmd(String containerId) {
Expand All @@ -253,8 +252,8 @@ public WaitContainerCmd waitContainerCmd(String containerId) {
public AttachContainerCmd attachContainerCmd(String containerId) {
return new AttachContainerCmd(containerId).withBaseResource(baseResource);
}


public LogContainerCmd logContainerCmd(String containerId) {
return new LogContainerCmd(containerId).withBaseResource(baseResource);
}
Expand Down Expand Up @@ -297,11 +296,11 @@ public BuildImgCmd buildImageCmd(InputStream tarInputStream) {
public TopContainerCmd topContainerCmd(String containerId) {
return new TopContainerCmd(containerId).withBaseResource(baseResource);
}

public TagImageCmd tagImageCmd(String imageId, String repository, String tag) {
return new TagImageCmd(imageId, repository, tag).withBaseResource(baseResource);
}


/**
* @return The output slurped into a string.
Expand Down
X Tutup