11package com .github .dockerjava .client ;
22
3+ import static com .google .common .base .Preconditions .checkNotNull ;
34import static org .apache .commons .io .IOUtils .closeQuietly ;
45
56import java .io .*;
1617import org .apache .http .impl .client .DefaultHttpClient ;
1718import org .apache .http .impl .conn .PoolingClientConnectionManager ;
1819
19- import com .github .dockerjava .client .command .AbstrDockerCmd ;
20- import com .github .dockerjava .client .command .AttachContainerCmd ;
21- import com .github .dockerjava .client .command .AuthCmd ;
22- import com .github .dockerjava .client .command .BuildImgCmd ;
23- import com .github .dockerjava .client .command .CommitCmd ;
24- import com .github .dockerjava .client .command .ContainerDiffCmd ;
25- import com .github .dockerjava .client .command .CopyFileFromContainerCmd ;
26- import com .github .dockerjava .client .command .CreateContainerCmd ;
27- import com .github .dockerjava .client .command .CreateImageCmd ;
28- import com .github .dockerjava .client .command .InfoCmd ;
29- import com .github .dockerjava .client .command .InspectContainerCmd ;
30- import com .github .dockerjava .client .command .InspectImageCmd ;
31- import com .github .dockerjava .client .command .KillContainerCmd ;
32- import com .github .dockerjava .client .command .ListContainersCmd ;
33- import com .github .dockerjava .client .command .ListImagesCmd ;
34- import com .github .dockerjava .client .command .LogContainerCmd ;
35- import com .github .dockerjava .client .command .PingCmd ;
36- import com .github .dockerjava .client .command .PullImageCmd ;
37- import com .github .dockerjava .client .command .PushImageCmd ;
38- import com .github .dockerjava .client .command .RemoveContainerCmd ;
39- import com .github .dockerjava .client .command .RemoveImageCmd ;
40- import com .github .dockerjava .client .command .RestartContainerCmd ;
41- import com .github .dockerjava .client .command .SearchImagesCmd ;
42- import com .github .dockerjava .client .command .StartContainerCmd ;
43- import com .github .dockerjava .client .command .StopContainerCmd ;
44- import com .github .dockerjava .client .command .TagImageCmd ;
45- import com .github .dockerjava .client .command .TopContainerCmd ;
46- import com .github .dockerjava .client .command .VersionCmd ;
47- import com .github .dockerjava .client .command .WaitContainerCmd ;
48-
4920import com .github .dockerjava .client .model .AuthConfig ;
5021
5122import com .github .dockerjava .client .utils .JsonClientFilter ;
6233 */
6334public class DockerClient implements Closeable {
6435
65- private Client client ;
36+ private final Client client ;
6637
6738 private final CommandFactory cmdFactory ;
6839 private final WebResource baseResource ;
69- private AuthConfig authConfig ;
70-
40+ private final DockerClientConfig dockerClientConfig ;
7141
7242 public DockerClient () {
7343 this (DockerClientConfig .createDefaultConfigBuilder ().build ());
@@ -77,20 +47,19 @@ public DockerClient(String serverUrl) {
7747 this (configWithServerUrl (serverUrl ));
7848 }
7949
80-
8150 private static DockerClientConfig configWithServerUrl (String serverUrl ) {
8251 return DockerClientConfig .createDefaultConfigBuilder ()
8352 .withUri (serverUrl )
8453 .build ();
8554 }
8655
87-
8856 public DockerClient (DockerClientConfig dockerClientConfig ) {
8957 this (dockerClientConfig , new DefaultCommandFactory ());
9058 }
9159
9260 public DockerClient (DockerClientConfig dockerClientConfig , CommandFactory cmdFactory ) {
9361 this .cmdFactory = cmdFactory ;
62+ this .dockerClientConfig = dockerClientConfig ;
9463
9564 HttpClient httpClient = getPoolingHttpClient (dockerClientConfig );
9665 ClientConfig clientConfig = new DefaultClientConfig ();
@@ -115,7 +84,6 @@ public DockerClient(DockerClientConfig dockerClientConfig, CommandFactory cmdFac
11584 }
11685 }
11786
118-
11987 private HttpClient getPoolingHttpClient (DockerClientConfig dockerClientConfig ) {
12088 SchemeRegistry schemeRegistry = new SchemeRegistry ();
12189 schemeRegistry .register (new Scheme ("http" , dockerClientConfig .getUri ().getPort (),
@@ -129,56 +97,25 @@ private HttpClient getPoolingHttpClient(DockerClientConfig dockerClientConfig) {
12997 // Increase default max connection per route
13098 cm .setDefaultMaxPerRoute (1000 );
13199
132-
133100 return new DefaultHttpClient (cm );
134101 }
135102
136-
137- public void setCredentials (String username , String password , String email ) {
138- if (username == null ) {
139- throw new IllegalArgumentException ("username is null" );
140- }
141- if (password == null ) {
142- throw new IllegalArgumentException ("password is null" );
143- }
144- if (email == null ) {
145- throw new IllegalArgumentException ("email is null" );
146- }
147- authConfig = new AuthConfig ();
148- authConfig .setUsername (username );
149- authConfig .setPassword (password );
150- authConfig .setEmail (email );
151- }
152-
153103 public <RES_T > RES_T execute (AbstrDockerCmd <?, RES_T > command )
154104 throws DockerException {
155105 return command .withBaseResource (baseResource ).exec ();
156106 }
157107
158108 public AuthConfig authConfig () throws DockerException {
159- return authConfig != null ? authConfig : authConfigFromProperties ();
160- }
161-
162- private static AuthConfig authConfigFromProperties () throws DockerException {
163- final AuthConfig a = new AuthConfig ();
164-
165- // TODO This should probably come from the DockerClientConfig used to create the DockerClient.
166- DockerClientConfig defaultDockerClientConfig = DockerClientConfig .createDefaultConfigBuilder ().build ();
167- a .setUsername (defaultDockerClientConfig .getUsername ());
168- a .setPassword (defaultDockerClientConfig .getPassword ());
169- a .setEmail (defaultDockerClientConfig .getEmail ());
109+ checkNotNull (dockerClientConfig .getUsername (), "Configured username is null." );
110+ checkNotNull (dockerClientConfig .getPassword (), "Configured password is null." );
111+ checkNotNull (dockerClientConfig .getEmail (), "Configured email is null." );
170112
171- if (a .getUsername () == null ) {
172- throw new IllegalStateException ("username is null" );
173- }
174- if (a .getPassword () == null ) {
175- throw new IllegalStateException ("password is null" );
176- }
177- if (a .getEmail () == null ) {
178- throw new IllegalStateException ("email is null" );
179- }
180-
181- return a ;
113+ AuthConfig authConfig = new AuthConfig ();
114+ authConfig .setUsername (dockerClientConfig .getUsername ());
115+ authConfig .setPassword (dockerClientConfig .getPassword ());
116+ authConfig .setEmail (dockerClientConfig .getEmail ());
117+ // TODO Make the registry address configurable
118+ return authConfig ;
182119 }
183120
184121 /**
@@ -325,7 +262,6 @@ public TagImageCmd tagImageCmd(String imageId, String repository, String tag) {
325262 return cmdFactory .tagImageCmd (imageId , repository , tag ).withBaseResource (baseResource );
326263 }
327264
328-
329265 // TODO This is only being used by the test code for logging. Is it really necessary?
330266 /**
331267 * @return The output slurped into a string.
0 commit comments