Skip to content
This repository was archived by the owner on Feb 1, 2018. It is now read-only.

Commit 3340bf0

Browse files
committed
Makes Config a public, immutable class with a builder
This makes Config a public class, so it can be passed into the DockerClient constructor from code in different packages. It also makes Config an immutable object and includes a builder for convenience. Fixes issue kpelykh#24.
1 parent cb9b3f2 commit 3340bf0

2 files changed

Lines changed: 163 additions & 51 deletions

File tree

src/main/java/com/github/dockerjava/client/Config.java

Lines changed: 137 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,169 @@
66
import java.net.URI;
77
import java.util.Properties;
88

9-
class Config {
10-
URI url;
11-
String version, username, password, email;
12-
Integer readTimeout;
13-
boolean enableLoggingFilter;
9+
public class Config {
10+
private final URI uri;
11+
private final String version, username, password, email;
12+
private final Integer readTimeout;
13+
private final boolean loggingFilterEnabled;
1414

15-
private Config() {
15+
private Config(DockerClientConfigBuilder builder) {
16+
this.uri = builder.uri;
17+
this.version = builder.version;
18+
this.username = builder.username;
19+
this.password = builder.password;
20+
this.email = builder.email;
21+
this.readTimeout = builder.readTimeout;
22+
this.loggingFilterEnabled = builder.loggingFilterEnabled;
1623
}
1724

18-
static Config createConfig() throws DockerException {
19-
final Properties p = new Properties();
25+
public URI getUri() {
26+
return uri;
27+
}
28+
29+
public String getVersion() {
30+
return version;
31+
}
32+
33+
public String getUsername() {
34+
return username;
35+
}
36+
37+
public String getPassword() {
38+
return password;
39+
}
2040

41+
public String getEmail() {
42+
return email;
43+
}
44+
45+
public Integer getReadTimeout() {
46+
return readTimeout;
47+
}
48+
49+
public boolean isLoggingFilterEnabled() {
50+
return loggingFilterEnabled;
51+
}
52+
53+
public static Properties loadIncludedDockerProperties() {
2154
try {
55+
Properties p = new Properties();
2256
p.load(Config.class.getResourceAsStream("/docker.io.properties"));
57+
return p;
2358
} catch (IOException e) {
2459
throw new DockerException(e);
2560
}
61+
}
2662

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

29-
if (file.isFile()) {
72+
final File usersDockerPropertiesFile = new File(System.getProperty("user.home"), ".docker.io.properties");
73+
if (usersDockerPropertiesFile.isFile()) {
3074
try {
31-
final FileInputStream in = new FileInputStream(file);
75+
final FileInputStream in = new FileInputStream(usersDockerPropertiesFile);
3276
try {
33-
p.load(in);
77+
overriddenProperties.load(in);
3478
} finally {
3579
in.close();
3680
}
3781
} catch (IOException e) {
3882
throw new DockerException(e);
3983
}
4084
}
85+
return overriddenProperties;
86+
}
4187

42-
for (String s : new String[]{"url", "version", "username", "password", "email"}) {
88+
/**
89+
* Creates a new Properties object containing values overridden from the System properties
90+
* @param p The original set of properties to override
91+
* @return A copy of the original Properties with overridden values
92+
*/
93+
public static Properties overrideDockerPropertiesWithSystemProperties(Properties p) {
94+
Properties overriddenProperties = new Properties();
95+
overriddenProperties.putAll(p);
96+
97+
// TODO Add all values from system properties that begin with docker.io.*
98+
for (String s : new String[]{"url", "version", "username", "password", "email"}) {
4399
final String key = "docker.io." + s;
44-
if (System.getProperties().keySet().contains(key)) {
45-
p.setProperty(key, System.getProperty(key));
100+
if (System.getProperties().containsKey(key)) {
101+
overriddenProperties.setProperty(key, System.getProperty(key));
46102
}
47103
}
104+
return overriddenProperties;
105+
}
106+
107+
public static DockerClientConfigBuilder createDefaultConfigBuilder() {
108+
Properties properties = loadIncludedDockerProperties();
109+
properties = overrideDockerPropertiesWithSettingsFromUserHome(properties);
110+
properties = overrideDockerPropertiesWithSystemProperties(properties);
111+
return new DockerClientConfigBuilder().withProperties(properties);
112+
}
48113

49-
final Config c = new Config();
114+
public static class DockerClientConfigBuilder {
115+
private URI uri;
116+
private String version, username, password, email;
117+
private Integer readTimeout;
118+
private boolean loggingFilterEnabled;
50119

51-
c.url = URI.create(p.getProperty("docker.io.url"));
52-
c.version = p.getProperty("docker.io.version");
53-
c.username = p.getProperty("docker.io.username");
54-
c.password = p.getProperty("docker.io.password");
55-
c.email = p.getProperty("docker.io.email");
56-
c.readTimeout = Integer.valueOf(p.getProperty("docker.io.readTimeout", "1000"));
57-
c.enableLoggingFilter = Boolean.valueOf(p.getProperty("docker.io.enableLoggingFilter", "true"));
120+
public DockerClientConfigBuilder() {
121+
}
122+
123+
/**
124+
* This will set all fields in the builder to those contained in the Properties object. The Properties object
125+
* should contain the following docker.io.* keys: url, version, username, password, and email. If
126+
* docker.io.readTimeout or docker.io.enableLoggingFilter are not contained, they will be set to 1000 and true,
127+
* respectively.
128+
*
129+
* @param p
130+
* @return
131+
*/
132+
public DockerClientConfigBuilder withProperties(Properties p) {
133+
return withUri(p.getProperty("docker.io.url"))
134+
.withVersion(p.getProperty("docker.io.version"))
135+
.withUsername(p.getProperty("docker.io.username"))
136+
.withPassword(p.getProperty("docker.io.password"))
137+
.withEmail(p.getProperty("docker.io.email"))
138+
.withReadTimeout(Integer.valueOf(p.getProperty("docker.io.readTimeout", "1000")))
139+
.withLoggingFilter(Boolean.valueOf(p.getProperty("docker.io.enableLoggingFilter", "true")));
140+
}
58141

59-
return c;
142+
public final DockerClientConfigBuilder withUri(String uri) {
143+
this.uri = URI.create(uri);
144+
return this;
145+
}
146+
public final DockerClientConfigBuilder withVersion(String version) {
147+
this.version = version;
148+
return this;
149+
}
150+
public final DockerClientConfigBuilder withUsername(String username) {
151+
this.username = username;
152+
return this;
153+
}
154+
public final DockerClientConfigBuilder withPassword(String password) {
155+
this.password = password;
156+
return this;
157+
}
158+
public final DockerClientConfigBuilder withEmail(String email) {
159+
this.email = email;
160+
return this;
161+
}
162+
public final DockerClientConfigBuilder withReadTimeout(int readTimeout) {
163+
this.readTimeout = readTimeout;
164+
return this;
165+
}
166+
public final DockerClientConfigBuilder withLoggingFilter(boolean loggingFilterEnabled) {
167+
this.loggingFilterEnabled = loggingFilterEnabled;
168+
return this;
169+
}
170+
public Config build() {
171+
return new Config(this);
172+
}
60173
}
61174
}

src/main/java/com/github/dockerjava/client/DockerClient.java

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.io.IOException;
77
import java.io.InputStream;
88
import java.io.StringWriter;
9-
import java.net.URI;
109

1110
import org.apache.commons.io.IOUtils;
1211
import org.apache.commons.io.LineIterator;
@@ -66,27 +65,25 @@ public class DockerClient {
6665
private WebResource baseResource;
6766
private AuthConfig authConfig;
6867

69-
70-
public DockerClient() throws DockerException {
71-
this(Config.createConfig());
68+
public DockerClient() {
69+
this(Config.createDefaultConfigBuilder().build());
7270
}
7371

74-
public DockerClient(String serverUrl) throws DockerException {
72+
public DockerClient(String serverUrl) {
7573
this(configWithServerUrl(serverUrl));
7674
}
77-
78-
private static Config configWithServerUrl(String serverUrl)
79-
throws DockerException {
80-
final Config c = Config.createConfig();
81-
c.url = URI.create(serverUrl);
82-
return c;
75+
76+
private static Config configWithServerUrl(String serverUrl) {
77+
return Config.createDefaultConfigBuilder()
78+
.withUri(serverUrl)
79+
.build();
8380
}
8481

8582
public DockerClient(Config config) {
8683
ClientConfig clientConfig = new DefaultClientConfig();
87-
84+
8885
SchemeRegistry schemeRegistry = new SchemeRegistry();
89-
schemeRegistry.register(new Scheme("http", config.url.getPort(),
86+
schemeRegistry.register(new Scheme("http", config.getUri().getPort(),
9087
PlainSocketFactory.getSocketFactory()));
9188
schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory
9289
.getSocketFactory()));
@@ -103,17 +100,17 @@ public DockerClient(Config config) {
103100
null, false), clientConfig);
104101

105102
// 1 hour
106-
client.setReadTimeout(config.readTimeout);
107-
103+
client.setReadTimeout(config.getReadTimeout());
104+
108105
client.addFilter(new JsonClientFilter());
109-
110-
if (config.enableLoggingFilter)
106+
107+
if (config.isLoggingFilterEnabled())
111108
client.addFilter(new SelectiveLoggingFilter());
112109

113-
baseResource = client.resource(config.url + "/v" + config.version);
110+
baseResource = client.resource(config.getUri() + "/v" + config.getVersion());
114111
}
115112

116-
113+
117114

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

146-
a.setUsername(Config.createConfig().username);
147-
a.setPassword(Config.createConfig().password);
148-
a.setEmail(Config.createConfig().email);
143+
// TODO This should probably come from the Config used to create the DockerClient.
144+
Config defaultConfig = Config.createDefaultConfigBuilder().build();
145+
a.setUsername(defaultConfig.getUsername());
146+
a.setPassword(defaultConfig.getPassword());
147+
a.setEmail(defaultConfig.getEmail());
149148

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

229228
public CreateContainerCmd createContainerCmd(String image) {
230229
return new CreateContainerCmd(new CreateContainerConfig()).withImage(
231-
image).withBaseResource(baseResource);
230+
image).withBaseResource(baseResource);
232231
}
233232

234233
public StartContainerCmd startContainerCmd(String containerId) {
@@ -253,8 +252,8 @@ public WaitContainerCmd waitContainerCmd(String containerId) {
253252
public AttachContainerCmd attachContainerCmd(String containerId) {
254253
return new AttachContainerCmd(containerId).withBaseResource(baseResource);
255254
}
256-
257-
255+
256+
258257
public LogContainerCmd logContainerCmd(String containerId) {
259258
return new LogContainerCmd(containerId).withBaseResource(baseResource);
260259
}
@@ -297,11 +296,11 @@ public BuildImgCmd buildImageCmd(InputStream tarInputStream) {
297296
public TopContainerCmd topContainerCmd(String containerId) {
298297
return new TopContainerCmd(containerId).withBaseResource(baseResource);
299298
}
300-
299+
301300
public TagImageCmd tagImageCmd(String imageId, String repository, String tag) {
302301
return new TagImageCmd(imageId, repository, tag).withBaseResource(baseResource);
303302
}
304-
303+
305304

306305
/**
307306
* @return The output slurped into a string.

0 commit comments

Comments
 (0)