11package com .github .dockerjava .api .model ;
22
3+ import static com .github .dockerjava .api .model .AccessMode .ro ;
4+ import static com .github .dockerjava .api .model .AccessMode .rw ;
5+
36import org .apache .commons .lang .builder .EqualsBuilder ;
47import org .apache .commons .lang .builder .HashCodeBuilder ;
58
9+ /**
10+ * Represents a host path being bind mounted as a {@link Volume}
11+ * in a Docker container.
12+ * The Bind can be in read only or read write access mode.
13+ */
614public class Bind {
715
816 private String path ;
917
1018 private Volume volume ;
1119
12- private boolean readOnly = false ;
20+ private AccessMode accessMode ;
1321
1422 public Bind (String path , Volume volume ) {
15- this (path , volume , false );
23+ this (path , volume , AccessMode . DEFAULT );
1624 }
1725
18- public Bind (String path , Volume volume , boolean readOnly ) {
26+ public Bind (String path , Volume volume , AccessMode accessMode ) {
1927 this .path = path ;
2028 this .volume = volume ;
21- this .readOnly = readOnly ;
29+ this .accessMode = accessMode ;
30+ }
31+
32+ /**
33+ * @deprecated use {@link #Bind(String, Volume, AccessMode)}
34+ */
35+ @ Deprecated
36+ public Bind (String path , Volume volume , boolean readOnly ) {
37+ this (path , volume , readOnly ? ro : rw );
2238 }
2339
2440 public String getPath () {
@@ -28,9 +44,17 @@ public String getPath() {
2844 public Volume getVolume () {
2945 return volume ;
3046 }
47+
48+ public AccessMode getAccessMode () {
49+ return accessMode ;
50+ }
3151
52+ /**
53+ * @deprecated use {@link #getAccessMode()}
54+ */
55+ @ Deprecated
3256 public boolean isReadOnly () {
33- return readOnly ;
57+ return ro . equals ( accessMode ) ;
3458 }
3559
3660 /**
@@ -48,12 +72,8 @@ public static Bind parse(String serialized) {
4872 return new Bind (parts [0 ], Volume .parse (parts [1 ]));
4973 }
5074 case 3 : {
51- if ("rw" .equals (parts [2 ].toLowerCase ()))
52- return new Bind (parts [0 ], Volume .parse (parts [1 ]), false );
53- else if ("ro" .equals (parts [2 ].toLowerCase ()))
54- return new Bind (parts [0 ], Volume .parse (parts [1 ]), true );
55- else
56- throw new IllegalArgumentException ();
75+ AccessMode accessMode = AccessMode .valueOf (parts [2 ].toLowerCase ());
76+ return new Bind (parts [0 ], Volume .parse (parts [1 ]), accessMode );
5777 }
5878 default : {
5979 throw new IllegalArgumentException ();
@@ -71,14 +91,28 @@ public boolean equals(Object obj) {
7191 Bind other = (Bind ) obj ;
7292 return new EqualsBuilder ().append (path , other .getPath ())
7393 .append (volume , other .getVolume ())
74- .append (readOnly , other .isReadOnly ()).isEquals ();
94+ .append (accessMode , other .getAccessMode ()).isEquals ();
7595 } else
7696 return super .equals (obj );
7797 }
7898
7999 @ Override
80100 public int hashCode () {
81101 return new HashCodeBuilder ().append (path ).append (volume )
82- .append (readOnly ).toHashCode ();
102+ .append (accessMode ).toHashCode ();
103+ }
104+
105+ /**
106+ * Returns a string representation of this {@link Bind} suitable
107+ * for inclusion in a JSON message.
108+ * The format is <code><host path>:<container path>:<access mode></code>,
109+ * like the argument in {@link #parse(String)}.
110+ *
111+ * @return a string representation of this {@link Bind}
112+ */
113+ @ Override
114+ public String toString () {
115+ return path + ":" + volume .toString () + ":" + accessMode .toString ();
83116 }
117+
84118}
0 commit comments