Parser and toString for RestartPolicy · docker-java/docker-java@fc6429b
@@ -9,10 +9,17 @@
99/**
1010 * Container restart policy
1111 *
12- * no – Do not restart the container if it dies. (default)
13- * on-failure – Restart the container if it exits with a non-zero exit code.
14- * Can also accept an optional maximum restart count (e.g. on-failure:5).
15- * always – Always restart the container no matter what exit code is returned.
12+ * <dl>
13+ * <dt>no</dt>
14+ * <dd>Do not restart the container if it dies. (default)</dd>
15+ *
16+ * <dt>on-failure</dt>
17+ * <dd>Restart the container if it exits with a non-zero exit code.
18+ * Can also accept an optional maximum restart count (e.g. on-failure:5).<dd>
19+ *
20+ * <dt>always</dt>
21+ * <dd>Always restart the container no matter what exit code is returned.<dd>
22+ * </dl>
1623 *
1724 * @author marcus
1825 *
@@ -33,15 +40,27 @@ private RestartPolicy(int maximumRetryCount, String name) {
3340this.maximumRetryCount = maximumRetryCount;
3441this.name = name;
3542 }
36-43+44+/**
45+ * Do not restart the container if it dies. (default)
46+ */
3747public static RestartPolicy noRestart() {
3848return new RestartPolicy();
3949 }
40-50+51+/**
52+ * Always restart the container no matter what exit code is returned.
53+ */
4154public static RestartPolicy alwaysRestart() {
4255return new RestartPolicy(0, "always");
4356 }
44-57+58+/**
59+ * Restart the container if it exits with a non-zero exit code.
60+ *
61+ * @param maximumRetryCount the maximum number of restarts.
62+ * Set to <code>0</code> for unlimited retries.
63+ */
4564public static RestartPolicy onFailureRestart(int maximumRetryCount) {
4665return new RestartPolicy(maximumRetryCount, "on-failure");
4766 }
@@ -54,6 +73,47 @@ public String getName() {
5473return name;
5574 }
567576+/**
77+ * Parses a textual restart polixy specification (as used by the Docker CLI)
78+ * to a {@link RestartPolicy}.
79+ *
80+ * @param serialized the specification, e.g. <code>on-failure:2</code>
81+ * @return a {@link RestartPolicy} matching the specification
82+ * @throws IllegalArgumentException if the specification cannot be parsed
83+ */
84+public static RestartPolicy parse(String serialized) throws IllegalArgumentException {
85+try {
86+String[] parts = serialized.split(":");
87+String name = parts[0];
88+if ("no".equals(name))
89+return noRestart();
90+if ("always".equals(name))
91+return alwaysRestart();
92+if ("on-failure".equals(name)) {
93+int count = 0;
94+if (parts.length == 2) {
95+count = Integer.parseInt(parts[1]);
96+ }
97+return onFailureRestart(count);
98+ }
99+throw new IllegalArgumentException();
100+ } catch (Exception e) {
101+throw new IllegalArgumentException("Error parsing RestartPolicy '" + serialized + "'");
102+ }
103+ }
104+105+/**
106+ * Returns a string representation of this {@link RestartPolicy}.
107+ * The format is <code>name[:count]</code>, like the argument in {@link #parse(String)}.
108+ *
109+ * @return a string representation of this {@link RestartPolicy}
110+ */
111+@Override
112+public String toString() {
113+String result = name.isEmpty() ? "no" : name;
114+return maximumRetryCount > 0 ? result + ":" + maximumRetryCount : result;
115+ }
116+57117@Override
58118public boolean equals(Object obj) {
59119if (obj instanceof RestartPolicy) {