|
6 | 6 | import java.net.URI; |
7 | 7 | import java.util.Properties; |
8 | 8 |
|
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; |
14 | 14 |
|
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; |
16 | 23 | } |
17 | 24 |
|
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 | + } |
20 | 40 |
|
| 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() { |
21 | 54 | try { |
| 55 | + Properties p = new Properties(); |
22 | 56 | p.load(Config.class.getResourceAsStream("/docker.io.properties")); |
| 57 | + return p; |
23 | 58 | } catch (IOException e) { |
24 | 59 | throw new DockerException(e); |
25 | 60 | } |
| 61 | + } |
26 | 62 |
|
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); |
28 | 71 |
|
29 | | - if (file.isFile()) { |
| 72 | + final File usersDockerPropertiesFile = new File(System.getProperty("user.home"), ".docker.io.properties"); |
| 73 | + if (usersDockerPropertiesFile.isFile()) { |
30 | 74 | try { |
31 | | - final FileInputStream in = new FileInputStream(file); |
| 75 | + final FileInputStream in = new FileInputStream(usersDockerPropertiesFile); |
32 | 76 | try { |
33 | | - p.load(in); |
| 77 | + overriddenProperties.load(in); |
34 | 78 | } finally { |
35 | 79 | in.close(); |
36 | 80 | } |
37 | 81 | } catch (IOException e) { |
38 | 82 | throw new DockerException(e); |
39 | 83 | } |
40 | 84 | } |
| 85 | + return overriddenProperties; |
| 86 | + } |
41 | 87 |
|
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"}) { |
43 | 99 | 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)); |
46 | 102 | } |
47 | 103 | } |
| 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 | + } |
48 | 113 |
|
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; |
50 | 119 |
|
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 | + } |
58 | 141 |
|
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 | + } |
60 | 173 | } |
61 | 174 | } |
0 commit comments