forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerClientConfig.java
More file actions
177 lines (156 loc) · 6.35 KB
/
Copy pathDockerClientConfig.java
File metadata and controls
177 lines (156 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.github.dockerjava.core;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;
import java.util.Properties;
import com.google.common.base.Preconditions;
public class DockerClientConfig {
private final URI uri;
private final String version, username, password, email;
private final Integer readTimeout;
private final boolean loggingFilterEnabled;
private DockerClientConfig(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;
}
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(DockerClientConfig.class.getResourceAsStream("/docker.io.properties"));
return p;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 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);
final File usersDockerPropertiesFile = new File(System.getProperty("user.home"), ".docker.io.properties");
if (usersDockerPropertiesFile.isFile()) {
try {
final FileInputStream in = new FileInputStream(usersDockerPropertiesFile);
try {
overriddenProperties.load(in);
} finally {
in.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return overriddenProperties;
}
/**
* 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", "readTimeout", "enableLoggingFilter"}) {
final String key = "docker.io." + s;
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);
}
public static class DockerClientConfigBuilder {
private URI uri;
private String version, username, password, email;
private Integer readTimeout;
private boolean loggingFilterEnabled;
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", "0")))
.withLoggingFilter(Boolean.valueOf(p.getProperty("docker.io.enableLoggingFilter", "true")));
}
public final DockerClientConfigBuilder withUri(String uri) {
Preconditions.checkNotNull(uri, "uri was not specified");
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(Integer readTimeout) {
this.readTimeout = readTimeout;
return this;
}
public final DockerClientConfigBuilder withLoggingFilter(boolean loggingFilterEnabled) {
this.loggingFilterEnabled = loggingFilterEnabled;
return this;
}
public DockerClientConfig build() {
return new DockerClientConfig(this);
}
}
}