1-
21package com .github .dockerjava .api .model ;
32
43import org .apache .commons .lang .builder .EqualsBuilder ;
54import org .apache .commons .lang .builder .HashCodeBuilder ;
65
6+ /**
7+ * Represents a network link between two Docker containers.
8+ * The container with the name {@link #getName()} is made available in the
9+ * target container with the aliased name {@link #getAlias()}.
10+ * This involves creating an entry in <code>/etc/hosts</code> and some environment
11+ * variables in the target container as well as creating a network bridge between
12+ * both containers.
13+ */
714public class Link
815{
916
1017 private final String name ;
1118
1219 private final String alias ;
1320
21+ /**
22+ * Creates a {@link Link} for the container with the given name and an aliased
23+ * name for use in the target container.
24+ *
25+ * @param name the name of the container that you want to link into the target
26+ * container
27+ * @param alias the aliased name under which the linked container will be available
28+ * in the target container
29+ */
1430 public Link (final String name , final String alias )
1531 {
1632 this .name = name ;
1733 this .alias = alias ;
1834 }
1935
36+ /**
37+ * @return the name of the container that is linked into the target container
38+ */
2039 public String getName ()
2140 {
2241 return name ;
2342 }
2443
44+ /**
45+ * @return the aliased name under which the linked container will be available
46+ * in the target container
47+ */
2548 public String getAlias ()
2649 {
2750 return alias ;
2851 }
2952
30- public static Link parse (final String serialized )
53+ /**
54+ * Parses a textual link specification (as used by the Docker CLI) to a {@link Link}.
55+ *
56+ * @param serialized the specification, e.g. <code>name:alias</code>
57+ * @return a {@link Link} matching the specification
58+ * @throws IllegalArgumentException if the specification cannot be parsed
59+ */
60+ public static Link parse (final String serialized ) throws IllegalArgumentException
3161 {
3262 try {
3363 final String [] parts = serialized .split (":" );
@@ -36,11 +66,11 @@ public static Link parse(final String serialized)
3666 return new Link (parts [0 ], parts [1 ]);
3767 }
3868 default : {
39- throw new RuntimeException ( "Error parsing Link '" + serialized + "'" );
69+ throw new IllegalArgumentException ( );
4070 }
4171 }
4272 } catch (final Exception e ) {
43- throw new RuntimeException ("Error parsing Link '" + serialized + "'" );
73+ throw new IllegalArgumentException ("Error parsing Link '" + serialized + "'" );
4474 }
4575 }
4676
@@ -60,4 +90,16 @@ public int hashCode()
6090 return new HashCodeBuilder ().append (name ).append (alias ).toHashCode ();
6191 }
6292
93+ /**
94+ * Returns a string representation of this {@link Link} suitable
95+ * for inclusion in a JSON message.
96+ * The format is <code>name:alias</code>, like the argument in {@link #parse(String)}.
97+ *
98+ * @return a string representation of this {@link Link}
99+ */
100+ @ Override
101+ public String toString () {
102+ return name + ":" + alias ;
103+ }
104+
63105}
0 commit comments