44import org .apache .commons .lang .builder .HashCodeBuilder ;
55
66/**
7- * Represents a host path being bind mounted as a {@link Volume} in a Docker container. The Bind can be in read only or read write access
8- * mode.
7+ * Represents a host path being bind mounted as a {@link Volume} in a Docker container.
8+ * The Bind can be in read only or read write access mode.
99 */
1010public class Bind {
1111
@@ -15,14 +15,24 @@ public class Bind {
1515
1616 private AccessMode accessMode ;
1717
18+ /**
19+ * @since {@link com.github.dockerjava.core.RemoteApiVersion#VERSION_1_17}
20+ */
21+ private SELContext secMode ;
22+
1823 public Bind (String path , Volume volume ) {
19- this (path , volume , AccessMode .DEFAULT );
24+ this (path , volume , AccessMode .DEFAULT , SELContext . DEFAULT );
2025 }
2126
2227 public Bind (String path , Volume volume , AccessMode accessMode ) {
28+ this (path , volume , accessMode , SELContext .DEFAULT );
29+ }
30+
31+ public Bind (String path , Volume volume , AccessMode accessMode , SELContext secMode ) {
2332 this .path = path ;
2433 this .volume = volume ;
2534 this .accessMode = accessMode ;
35+ this .secMode = secMode ;
2636 }
2737
2838 public String getPath () {
@@ -37,6 +47,10 @@ public AccessMode getAccessMode() {
3747 return accessMode ;
3848 }
3949
50+ public SELContext getSecMode () {
51+ return secMode ;
52+ }
53+
4054 /**
4155 * Parses a bind mount specification to a {@link Bind}.
4256 *
@@ -50,47 +64,70 @@ public static Bind parse(String serialized) {
5064 try {
5165 String [] parts = serialized .split (":" );
5266 switch (parts .length ) {
53- case 2 : {
54- return new Bind (parts [0 ], new Volume (parts [1 ]));
55- }
56- case 3 : {
57- AccessMode accessMode = AccessMode .valueOf (parts [2 ].toLowerCase ());
58- return new Bind (parts [0 ], new Volume (parts [1 ]), accessMode );
59- }
60- default : {
61- throw new IllegalArgumentException ();
67+ case 2 : {
68+ return new Bind (parts [0 ], new Volume (parts [1 ]));
69+ }
70+ case 3 : {
71+ String [] flags = parts [2 ].split ("," );
72+ AccessMode accessMode = AccessMode .DEFAULT ;
73+ SELContext seMode = SELContext .DEFAULT ;
74+ for (String p : flags ) {
75+ if (p .length () == 2 ) {
76+ accessMode = AccessMode .valueOf (p .toLowerCase ());
77+ } else {
78+ seMode = SELContext .fromString (p );
79+ }
6280 }
81+
82+ return new Bind (parts [0 ], new Volume (parts [1 ]), accessMode , seMode );
83+ }
84+ default : {
85+ throw new IllegalArgumentException ();
86+ }
6387 }
6488 } catch (Exception e ) {
65- throw new IllegalArgumentException ("Error parsing Bind '" + serialized + "'" );
89+ throw new IllegalArgumentException ("Error parsing Bind '" + serialized + "'" , e );
6690 }
6791 }
6892
6993 @ Override
7094 public boolean equals (Object obj ) {
7195 if (obj instanceof Bind ) {
7296 Bind other = (Bind ) obj ;
73- return new EqualsBuilder ().append (path , other .getPath ()).append (volume , other .getVolume ())
74- .append (accessMode , other .getAccessMode ()).isEquals ();
97+ return new EqualsBuilder ()
98+ .append (path , other .getPath ())
99+ .append (volume , other .getVolume ())
100+ .append (accessMode , other .getAccessMode ())
101+ .append (secMode , other .getSecMode ())
102+ .isEquals ();
75103 } else {
76104 return super .equals (obj );
77105 }
78106 }
79107
80108 @ Override
81109 public int hashCode () {
82- return new HashCodeBuilder ().append (path ).append (volume ).append (accessMode ).toHashCode ();
110+ return new HashCodeBuilder ()
111+ .append (path )
112+ .append (volume )
113+ .append (accessMode )
114+ .append (secMode )
115+ .toHashCode ();
83116 }
84117
85118 /**
86- * Returns a string representation of this {@link Bind} suitable for inclusion in a JSON message. The format is
87- * <code><host path>:<container path>:<access mode></code>, like the argument in {@link #parse(String)}.
119+ * Returns a string representation of this {@link Bind} suitable for inclusion in a JSON message.
120+ * The format is <code><host path>:<container path>:<access mode></code>,
121+ * like the argument in {@link #parse(String)}.
88122 *
89123 * @return a string representation of this {@link Bind}
90124 */
91125 @ Override
92126 public String toString () {
93- return path + ":" + volume .getPath () + ":" + accessMode .toString ();
127+ return String .format ("%s:%s:%s%s" ,
128+ path ,
129+ volume .getPath (),
130+ accessMode .toString (),
131+ secMode != SELContext .none ? "," + secMode .toString () : "" );
94132 }
95-
96133}
0 commit comments