44
55import java .io .*;
66
7+ import com .github .dockerjava .client .command .*;
8+
79import org .apache .commons .io .IOUtils ;
810import org .apache .commons .io .LineIterator ;
911import org .apache .http .client .HttpClient ;
1416import org .apache .http .impl .client .DefaultHttpClient ;
1517import org .apache .http .impl .conn .PoolingClientConnectionManager ;
1618
17- import com .github .dockerjava .client .command .AbstrDockerCmd ;
18- import com .github .dockerjava .client .command .AttachContainerCmd ;
19- import com .github .dockerjava .client .command .AuthCmd ;
20- import com .github .dockerjava .client .command .BuildImgCmd ;
21- import com .github .dockerjava .client .command .CommitCmd ;
22- import com .github .dockerjava .client .command .ContainerDiffCmd ;
23- import com .github .dockerjava .client .command .CopyFileFromContainerCmd ;
24- import com .github .dockerjava .client .command .CreateContainerCmd ;
25- import com .github .dockerjava .client .command .ImportImageCmd ;
26- import com .github .dockerjava .client .command .InfoCmd ;
27- import com .github .dockerjava .client .command .InspectContainerCmd ;
28- import com .github .dockerjava .client .command .InspectImageCmd ;
29- import com .github .dockerjava .client .command .KillContainerCmd ;
30- import com .github .dockerjava .client .command .ListContainersCmd ;
31- import com .github .dockerjava .client .command .ListImagesCmd ;
32- import com .github .dockerjava .client .command .LogContainerCmd ;
33- import com .github .dockerjava .client .command .PullImageCmd ;
34- import com .github .dockerjava .client .command .PushImageCmd ;
35- import com .github .dockerjava .client .command .RemoveContainerCmd ;
36- import com .github .dockerjava .client .command .RemoveImageCmd ;
37- import com .github .dockerjava .client .command .RestartContainerCmd ;
38- import com .github .dockerjava .client .command .SearchImagesCmd ;
39- import com .github .dockerjava .client .command .StartContainerCmd ;
40- import com .github .dockerjava .client .command .StopContainerCmd ;
41- import com .github .dockerjava .client .command .TagImageCmd ;
42- import com .github .dockerjava .client .command .TopContainerCmd ;
43- import com .github .dockerjava .client .command .VersionCmd ;
44- import com .github .dockerjava .client .command .WaitContainerCmd ;
4519import com .github .dockerjava .client .model .AuthConfig ;
4620import com .github .dockerjava .client .model .CreateContainerConfig ;
4721import com .github .dockerjava .client .utils .JsonClientFilter ;
5832 */
5933public class DockerClient implements Closeable {
6034
61- private Client client ;
62- private WebResource baseResource ;
35+ private Client client ;
36+
37+ private final CommandFactory cmdFactory ;
38+ private final WebResource baseResource ;
6339 private AuthConfig authConfig ;
6440
41+
6542 public DockerClient () {
6643 this (Config .createDefaultConfigBuilder ().build ());
6744 }
@@ -70,32 +47,26 @@ public DockerClient(String serverUrl) {
7047 this (configWithServerUrl (serverUrl ));
7148 }
7249
50+
7351 private static Config configWithServerUrl (String serverUrl ) {
7452 return Config .createDefaultConfigBuilder ()
7553 .withUri (serverUrl )
7654 .build ();
7755 }
7856
79- public DockerClient (Config config ) {
80- ClientConfig clientConfig = new DefaultClientConfig ();
8157
82- SchemeRegistry schemeRegistry = new SchemeRegistry ();
83- schemeRegistry .register (new Scheme ("http" , config .getUri ().getPort (),
84- PlainSocketFactory .getSocketFactory ()));
85- schemeRegistry .register (new Scheme ("https" , 443 , SSLSocketFactory
86- .getSocketFactory ()));
58+ public DockerClient (Config config ) {
59+ this (config , new DefaultCommandFactory ());
60+ }
8761
88- PoolingClientConnectionManager cm = new PoolingClientConnectionManager (
89- schemeRegistry );
90- // Increase max total connection
91- cm .setMaxTotal (1000 );
92- // Increase default max connection per route
93- cm .setDefaultMaxPerRoute (1000 );
62+ public DockerClient (Config config , CommandFactory cmdFactory ) {
63+ this .cmdFactory = cmdFactory ;
9464
95- HttpClient httpClient = new DefaultHttpClient (cm );
65+ HttpClient httpClient = getPoolingHttpClient (config );
66+ ClientConfig clientConfig = new DefaultClientConfig ();
9667 client = new ApacheHttpClient4 (new ApacheHttpClient4Handler (httpClient ,
9768 null , false ), clientConfig );
98-
69+
9970 // 1 hour
10071 client .setReadTimeout (config .getReadTimeout ());
10172
@@ -108,8 +79,25 @@ public DockerClient(Config config) {
10879 }
10980
11081
82+ private HttpClient getPoolingHttpClient (Config config ) {
83+ SchemeRegistry schemeRegistry = new SchemeRegistry ();
84+ schemeRegistry .register (new Scheme ("http" , config .getUri ().getPort (),
85+ PlainSocketFactory .getSocketFactory ()));
86+ schemeRegistry .register (new Scheme ("https" , 443 , SSLSocketFactory
87+ .getSocketFactory ()));
88+
89+ PoolingClientConnectionManager cm = new PoolingClientConnectionManager (schemeRegistry );
90+ // Increase max total connection
91+ cm .setMaxTotal (1000 );
92+ // Increase default max connection per route
93+ cm .setDefaultMaxPerRoute (1000 );
94+
95+
96+ return new DefaultHttpClient (cm );
97+ }
98+
11199
112- public void setCredentials (String username , String password , String email ) {
100+ public void setCredentials (String username , String password , String email ) {
113101 if (username == null ) {
114102 throw new IllegalArgumentException ("username is null" );
115103 }
@@ -164,27 +152,27 @@ private static AuthConfig authConfigFromProperties() throws DockerException {
164152 * Authenticate with the server, useful for checking authentication.
165153 */
166154 public AuthCmd authCmd () {
167- return new AuthCmd (authConfig ()).withBaseResource (baseResource );
155+ return cmdFactory . authCmd (authConfig ()).withBaseResource (baseResource );
168156 }
169157
170158 public InfoCmd infoCmd () throws DockerException {
171- return new InfoCmd ().withBaseResource (baseResource );
159+ return cmdFactory . infoCmd ().withBaseResource (baseResource );
172160 }
173161
174162 public VersionCmd versionCmd () throws DockerException {
175- return new VersionCmd ().withBaseResource (baseResource );
163+ return cmdFactory . versionCmd ().withBaseResource (baseResource );
176164 }
177165
178166 /**
179167 * * IMAGE API *
180168 */
181169
182170 public PullImageCmd pullImageCmd (String repository ) {
183- return new PullImageCmd (repository ).withBaseResource (baseResource );
171+ return cmdFactory . pullImageCmd (repository ).withBaseResource (baseResource );
184172 }
185173
186174 public PushImageCmd pushImageCmd (String name ) {
187- return new PushImageCmd (name ).withAuthConfig (authConfig ())
175+ return cmdFactory . pushImageCmd (name ).withAuthConfig (authConfig ())
188176 .withBaseResource (baseResource );
189177 }
190178
@@ -194,108 +182,107 @@ public PushImageCmd pushImageCmd(String name) {
194182
195183 public ImportImageCmd importImageCmd (String repository ,
196184 InputStream imageStream ) {
197- return new ImportImageCmd (repository , imageStream )
185+ return cmdFactory . importImageCmd (repository , imageStream )
198186 .withBaseResource (baseResource );
199187 }
200188
201189 public SearchImagesCmd searchImagesCmd (String term ) {
202- return new SearchImagesCmd (term ).withBaseResource (baseResource );
190+ return cmdFactory . searchImagesCmd (term ).withBaseResource (baseResource );
203191 }
204192
205193 public RemoveImageCmd removeImageCmd (String imageId ) {
206- return new RemoveImageCmd (imageId ).withBaseResource (baseResource );
194+ return cmdFactory . removeImageCmd (imageId ).withBaseResource (baseResource );
207195 }
208196
209197 public ListImagesCmd listImagesCmd () {
210- return new ListImagesCmd ().withBaseResource (baseResource );
198+ return cmdFactory . listImagesCmd ().withBaseResource (baseResource );
211199 }
212200
213201 public InspectImageCmd inspectImageCmd (String imageId ) {
214- return new InspectImageCmd (imageId ).withBaseResource (baseResource );
202+ return cmdFactory . inspectImageCmd (imageId ).withBaseResource (baseResource );
215203 }
216204
217205 /**
218206 * * CONTAINER API *
219207 */
220208
221209 public ListContainersCmd listContainersCmd () {
222- return new ListContainersCmd ().withBaseResource (baseResource );
210+ return cmdFactory . listContainersCmd ().withBaseResource (baseResource );
223211 }
224212
225213 public CreateContainerCmd createContainerCmd (String image ) {
226- return new CreateContainerCmd (new CreateContainerConfig ()).withImage (
227- image ).withBaseResource (baseResource );
214+ return cmdFactory .createContainerCmd (image ).withBaseResource (baseResource );
228215 }
229216
230217 public StartContainerCmd startContainerCmd (String containerId ) {
231- return new StartContainerCmd (containerId )
218+ return cmdFactory . startContainerCmd (containerId )
232219 .withBaseResource (baseResource );
233220 }
234221
235222 public InspectContainerCmd inspectContainerCmd (String containerId ) {
236- return new InspectContainerCmd (containerId )
223+ return cmdFactory . inspectContainerCmd (containerId )
237224 .withBaseResource (baseResource );
238225 }
239226
240227 public RemoveContainerCmd removeContainerCmd (String containerId ) {
241- return new RemoveContainerCmd (containerId )
228+ return cmdFactory . removeContainerCmd (containerId )
242229 .withBaseResource (baseResource );
243230 }
244231
245232 public WaitContainerCmd waitContainerCmd (String containerId ) {
246- return new WaitContainerCmd (containerId ).withBaseResource (baseResource );
233+ return cmdFactory . waitContainerCmd (containerId ).withBaseResource (baseResource );
247234 }
248235
249236 public AttachContainerCmd attachContainerCmd (String containerId ) {
250- return new AttachContainerCmd (containerId ).withBaseResource (baseResource );
237+ return cmdFactory . attachContainerCmd (containerId ).withBaseResource (baseResource );
251238 }
252239
253240
254241 public LogContainerCmd logContainerCmd (String containerId ) {
255- return new LogContainerCmd (containerId ).withBaseResource (baseResource );
242+ return cmdFactory . logContainerCmd (containerId ).withBaseResource (baseResource );
256243 }
257244
258245 public CopyFileFromContainerCmd copyFileFromContainerCmd (
259246 String containerId , String resource ) {
260- return new CopyFileFromContainerCmd (containerId , resource )
247+ return cmdFactory . copyFileFromContainerCmd (containerId , resource )
261248 .withBaseResource (baseResource );
262249 }
263250
264251 public ContainerDiffCmd containerDiffCmd (String containerId ) {
265- return new ContainerDiffCmd (containerId ).withBaseResource (baseResource );
252+ return cmdFactory . containerDiffCmd (containerId ).withBaseResource (baseResource );
266253 }
267254
268255 public StopContainerCmd stopContainerCmd (String containerId ) {
269- return new StopContainerCmd (containerId ).withBaseResource (baseResource );
256+ return cmdFactory . stopContainerCmd (containerId ).withBaseResource (baseResource );
270257 }
271258
272259 public KillContainerCmd killContainerCmd (String containerId ) {
273- return new KillContainerCmd (containerId ).withBaseResource (baseResource );
260+ return cmdFactory . killContainerCmd (containerId ).withBaseResource (baseResource );
274261 }
275262
276263 public RestartContainerCmd restartContainerCmd (String containerId ) {
277- return new RestartContainerCmd (containerId )
264+ return cmdFactory . restartContainerCmd (containerId )
278265 .withBaseResource (baseResource );
279266 }
280267
281268 public CommitCmd commitCmd (String containerId ) {
282- return new CommitCmd (containerId ).withBaseResource (baseResource );
269+ return cmdFactory . commitCmd (containerId ).withBaseResource (baseResource );
283270 }
284271
285272 public BuildImgCmd buildImageCmd (File dockerFolder ) {
286- return new BuildImgCmd (dockerFolder ).withBaseResource (baseResource );
273+ return cmdFactory . buildImgCmd (dockerFolder ).withBaseResource (baseResource );
287274 }
288275
289276 public BuildImgCmd buildImageCmd (InputStream tarInputStream ) {
290- return new BuildImgCmd (tarInputStream ).withBaseResource (baseResource );
277+ return cmdFactory . buildImgCmd (tarInputStream ).withBaseResource (baseResource );
291278 }
292279
293280 public TopContainerCmd topContainerCmd (String containerId ) {
294- return new TopContainerCmd (containerId ).withBaseResource (baseResource );
281+ return cmdFactory . topContainerCmd (containerId ).withBaseResource (baseResource );
295282 }
296283
297284 public TagImageCmd tagImageCmd (String imageId , String repository , String tag ) {
298- return new TagImageCmd (imageId , repository , tag ).withBaseResource (baseResource );
285+ return cmdFactory . tagImageCmd (imageId , repository , tag ).withBaseResource (baseResource );
299286 }
300287
301288
0 commit comments