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 .ImportImageCmd ;
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 ;
66-
36+ private final Client client ;
37+
6738 private final CommandFactory cmdFactory ;
6839 private final WebResource baseResource ;
69- private AuthConfig authConfig ;
70-
40+ private final DockerClientConfig dockerClientConfig ;
7141
7242 public DockerClient () {
73- this (Config .createDefaultConfigBuilder ().build ());
43+ this (DockerClientConfig .createDefaultConfigBuilder ().build ());
7444 }
7545
7646 public DockerClient (String serverUrl ) {
7747 this (configWithServerUrl (serverUrl ));
7848 }
7949
80-
81- private static Config configWithServerUrl (String serverUrl ) {
82- return Config .createDefaultConfigBuilder ()
50+ private static DockerClientConfig configWithServerUrl (String serverUrl ) {
51+ return DockerClientConfig .createDefaultConfigBuilder ()
8352 .withUri (serverUrl )
8453 .build ();
8554 }
8655
87-
88- public DockerClient (Config config ) {
89- this (config , new DefaultCommandFactory ());
56+ public DockerClient (DockerClientConfig dockerClientConfig ) {
57+ this (dockerClientConfig , new DefaultCommandFactory ());
9058 }
9159
92- public DockerClient (Config config , CommandFactory cmdFactory ) {
60+ public DockerClient (DockerClientConfig dockerClientConfig , CommandFactory cmdFactory ) {
9361 this .cmdFactory = cmdFactory ;
62+ this .dockerClientConfig = dockerClientConfig ;
9463
95- HttpClient httpClient = getPoolingHttpClient (config );
64+ HttpClient httpClient = getPoolingHttpClient (dockerClientConfig );
9665 ClientConfig clientConfig = new DefaultClientConfig ();
9766 client = new ApacheHttpClient4 (new ApacheHttpClient4Handler (httpClient ,
9867 null , false ), clientConfig );
99-
100- if (config .getReadTimeout () != null ) {
101- client .setReadTimeout (config .getReadTimeout ());
68+
69+ if (dockerClientConfig .getReadTimeout () != null ) {
70+ client .setReadTimeout (dockerClientConfig .getReadTimeout ());
10271 }
10372
10473 client .addFilter (new JsonClientFilter ());
10574
106- if (config .isLoggingFilterEnabled ())
75+ if (dockerClientConfig .isLoggingFilterEnabled ())
10776 client .addFilter (new SelectiveLoggingFilter ());
108-
109- WebResource webResource = client .resource (config .getUri ());
110-
111- if (config .getVersion () != null ) {
112- baseResource = webResource .path ("v" + config .getVersion ());
77+
78+ WebResource webResource = client .resource (dockerClientConfig .getUri ());
79+
80+ if (dockerClientConfig .getVersion () != null ) {
81+ baseResource = webResource .path ("v" + dockerClientConfig .getVersion ());
11382 } else {
11483 baseResource = webResource ;
11584 }
11685 }
11786
118-
119- private HttpClient getPoolingHttpClient (Config config ) {
87+ private HttpClient getPoolingHttpClient (DockerClientConfig dockerClientConfig ) {
12088 SchemeRegistry schemeRegistry = new SchemeRegistry ();
121- schemeRegistry .register (new Scheme ("http" , config .getUri ().getPort (),
89+ schemeRegistry .register (new Scheme ("http" , dockerClientConfig .getUri ().getPort (),
12290 PlainSocketFactory .getSocketFactory ()));
12391 schemeRegistry .register (new Scheme ("https" , 443 , SSLSocketFactory
12492 .getSocketFactory ()));
@@ -129,56 +97,25 @@ private HttpClient getPoolingHttpClient(Config config) {
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 Config used to create the DockerClient.
166- Config defaultConfig = Config .createDefaultConfigBuilder ().build ();
167- a .setUsername (defaultConfig .getUsername ());
168- a .setPassword (defaultConfig .getPassword ());
169- a .setEmail (defaultConfig .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 /**
@@ -195,7 +132,7 @@ public AuthCmd authCmd() {
195132 public InfoCmd infoCmd () throws DockerException {
196133 return cmdFactory .infoCmd ().withBaseResource (baseResource );
197134 }
198-
135+
199136 public PingCmd pingCmd () {
200137 return cmdFactory .pingCmd ().withBaseResource (baseResource );
201138 }
@@ -221,9 +158,8 @@ public PushImageCmd pushImageCmd(String name) {
221158// return execute(pushImageCmd(name));
222159// }
223160
224- public ImportImageCmd importImageCmd (String repository ,
225- InputStream imageStream ) {
226- return cmdFactory .importImageCmd (repository , imageStream )
161+ public CreateImageCmd createImageCmd (String repository , InputStream imageStream ) {
162+ return cmdFactory .createImageCmd (repository , imageStream )
227163 .withBaseResource (baseResource );
228164 }
229165
@@ -326,7 +262,7 @@ public TagImageCmd tagImageCmd(String imageId, String repository, String tag) {
326262 return cmdFactory .tagImageCmd (imageId , repository , tag ).withBaseResource (baseResource );
327263 }
328264
329-
265+ // 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.
332268 */
0 commit comments