Skip to content

Commit d2c942d

Browse files
author
Marcus Linke
committed
Make LogConfig.type an enum
1 parent b44a091 commit d2c942d

File tree

5 files changed

+86
-37
lines changed

5 files changed

+86
-37
lines changed

src/main/java/com/github/dockerjava/api/command/CreateContainerCmd.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static interface Exec extends DockerCmdSyncExec<CreateContainerCmd, Creat
8989
public Link[] getLinks();
9090

9191
public LxcConf[] getLxcConf();
92-
92+
9393
public LogConfig getLogConfig();
9494

9595
public String getMacAddress();

src/main/java/com/github/dockerjava/api/model/HostConfig.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class HostConfig {
1717

1818
@JsonProperty("LxcConf")
1919
private LxcConf[] lxcConf;
20-
20+
2121
@JsonProperty("LogConfig")
2222
private LogConfig logConfig;
2323

@@ -69,10 +69,10 @@ public class HostConfig {
6969
public HostConfig() {
7070
}
7171

72-
public HostConfig(Bind[] binds, Link[] links, LxcConf[] lxcConf, LogConfig logConfig, Ports portBindings, boolean publishAllPorts,
73-
boolean privileged, boolean readonlyRootfs, String[] dns, String[] dnsSearch, VolumesFrom[] volumesFrom,
74-
String containerIDFile, Capability[] capAdd, Capability[] capDrop, RestartPolicy restartPolicy,
75-
String networkMode, Device[] devices, String[] extraHosts, Ulimit[] ulimits) {
72+
public HostConfig(Bind[] binds, Link[] links, LxcConf[] lxcConf, LogConfig logConfig, Ports portBindings,
73+
boolean publishAllPorts, boolean privileged, boolean readonlyRootfs, String[] dns, String[] dnsSearch,
74+
VolumesFrom[] volumesFrom, String containerIDFile, Capability[] capAdd, Capability[] capDrop,
75+
RestartPolicy restartPolicy, String networkMode, Device[] devices, String[] extraHosts, Ulimit[] ulimits) {
7676
this.binds = new Binds(binds);
7777
this.links = new Links(links);
7878
this.lxcConf = lxcConf;
@@ -102,7 +102,7 @@ public Bind[] getBinds() {
102102
public LxcConf[] getLxcConf() {
103103
return lxcConf;
104104
}
105-
105+
106106
@JsonIgnore
107107
public LogConfig getLogConfig() {
108108
return (logConfig == null) ? new LogConfig() : logConfig;
@@ -186,7 +186,7 @@ public void setLinks(Link... links) {
186186
public void setLxcConf(LxcConf[] lxcConf) {
187187
this.lxcConf = lxcConf;
188188
}
189-
189+
190190
@JsonIgnore
191191
public void setLogConfig(LogConfig logConfig) {
192192
this.logConfig = logConfig;
Lines changed: 73 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,109 @@
11
package com.github.dockerjava.api.model;
22

3+
import java.io.IOException;
34
import java.util.Map;
45

56
import com.fasterxml.jackson.annotation.JsonIgnore;
67
import com.fasterxml.jackson.annotation.JsonProperty;
8+
import com.fasterxml.jackson.core.JsonGenerator;
9+
import com.fasterxml.jackson.core.JsonParser;
10+
import com.fasterxml.jackson.core.JsonProcessingException;
11+
import com.fasterxml.jackson.core.ObjectCodec;
12+
import com.fasterxml.jackson.databind.DeserializationContext;
13+
import com.fasterxml.jackson.databind.JsonDeserializer;
14+
import com.fasterxml.jackson.databind.JsonNode;
15+
import com.fasterxml.jackson.databind.JsonSerializer;
16+
import com.fasterxml.jackson.databind.SerializerProvider;
17+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
18+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
719

820
/**
9-
* Log driver to use for a created/running container. The
10-
* available types are:
11-
*
12-
* json-file (default)
13-
* syslog
14-
* journald
15-
* none
21+
* Log driver to use for a created/running container. The available types are:
1622
*
17-
* If a driver is specified that is NOT supported,docker
18-
* will default to null. If configs are supplied that are
19-
* not supported by the type docker will ignore them. In most
20-
* cases setting the config option to null will suffice. Consult
21-
* the docker remote API for a more detailed and up-to-date
22-
* explanation of the available types and their options.
23+
* json-file (default) syslog journald none
24+
*
25+
* If a driver is specified that is NOT supported,docker will default to null. If configs are supplied that are not
26+
* supported by the type docker will ignore them. In most cases setting the config option to null will suffice. Consult
27+
* the docker remote API for a more detailed and up-to-date explanation of the available types and their options.
2328
*/
2429
public class LogConfig {
25-
30+
2631
@JsonProperty("Type")
27-
public String type;
28-
32+
public LoggingType type = null;
33+
2934
@JsonProperty("Config")
3035
public Map<String, String> config;
3136

32-
public LogConfig(String type, Map<String, String> config) {
37+
public LogConfig(LoggingType type, Map<String, String> config) {
3338
this.type = type;
3439
this.config = config;
3540
}
3641

42+
public LogConfig(LoggingType type) {
43+
this(type, null);
44+
}
45+
3746
public LogConfig() {
3847
}
3948

40-
public String getType() {
49+
public LoggingType getType() {
4150
return type;
4251
}
4352

44-
public LogConfig setType(String type) {
53+
public LogConfig setType(LoggingType type) {
4554
this.type = type;
4655
return this;
4756
}
4857

4958
@JsonIgnore
5059
public Map<String, String> getConfig() {
51-
return config;
60+
return config;
5261
}
53-
62+
5463
@JsonIgnore
5564
public LogConfig setConfig(Map<String, String> config) {
56-
this.config = config;
57-
return this;
65+
this.config = config;
66+
return this;
67+
}
68+
69+
@JsonDeserialize(using = LoggingType.Deserializer.class)
70+
@JsonSerialize(using = LoggingType.Serializer.class)
71+
public static enum LoggingType {
72+
DEFAULT("json-file"), JSON_FILE("json-file"), NONE("none"), SYSLOG("syslog"), JOURNALD("journald");
73+
74+
private String type;
75+
76+
private LoggingType(String type) {
77+
this.type = type;
78+
}
79+
80+
public String getType() {
81+
return type;
82+
}
83+
84+
public static final class Serializer extends JsonSerializer<LoggingType> {
85+
@Override
86+
public void serialize(LoggingType value, JsonGenerator jgen, SerializerProvider provider)
87+
throws IOException, JsonProcessingException {
88+
jgen.writeString(value.getType());
89+
}
90+
}
91+
92+
public static final class Deserializer extends JsonDeserializer<LoggingType> {
93+
@Override
94+
public LoggingType deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
95+
throws IOException, JsonProcessingException {
96+
97+
ObjectCodec oc = jsonParser.getCodec();
98+
JsonNode node = oc.readTree(jsonParser);
99+
100+
for (LoggingType loggingType : values()) {
101+
if (loggingType.getType().equals(node.asText()))
102+
return loggingType;
103+
}
104+
105+
throw new IllegalArgumentException("No enum constant " + LoggingType.class + "." + node.asText());
106+
}
107+
}
58108
}
59109
}

src/main/java/com/github/dockerjava/core/command/CreateContainerCmdImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public Map<String, String> getLabels() {
270270
public LxcConf[] getLxcConf() {
271271
return hostConfig.getLxcConf();
272272
}
273-
273+
274274
@Override
275275
@JsonIgnore
276276
public LogConfig getLogConfig() {
@@ -565,7 +565,7 @@ public CreateContainerCmd withLxcConf(LxcConf... lxcConf) {
565565
this.hostConfig.setLxcConf(lxcConf);
566566
return this;
567567
}
568-
568+
569569
@Override
570570
public CreateContainerCmd withLogConfig(LogConfig logConfig) {
571571
checkNotNull(logConfig, "logConfig was not specified");

src/test/java/com/github/dockerjava/core/command/CreateContainerCmdImplTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -535,13 +535,12 @@ public void createContainerWithLabels() throws DockerException {
535535
labels.put("com.github.dockerjava.null", "");
536536
assertThat(inspectContainerResponse.getConfig().getLabels(), is(equalTo(labels)));
537537
}
538-
538+
539539
@Test(groups = "ignoreInCircleCi")
540540
public void createContainerWithLogConfig() throws DockerException {
541541

542-
LogConfig logConfig = new LogConfig("none", null);
543-
CreateContainerResponse container = dockerClient.createContainerCmd("busybox").
544-
withLogConfig(logConfig).exec();
542+
LogConfig logConfig = new LogConfig(LogConfig.LoggingType.NONE, null);
543+
CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withLogConfig(logConfig).exec();
545544

546545
LOG.info("Created container {}", container.toString());
547546

0 commit comments

Comments
 (0)