|
1 | 1 | package com.github.dockerjava.api.model; |
2 | 2 |
|
| 3 | +import java.io.IOException; |
3 | 4 | import java.util.Map; |
4 | 5 |
|
5 | 6 | import com.fasterxml.jackson.annotation.JsonIgnore; |
6 | 7 | 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; |
7 | 19 |
|
8 | 20 | /** |
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: |
16 | 22 | * |
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. |
23 | 28 | */ |
24 | 29 | public class LogConfig { |
25 | | - |
| 30 | + |
26 | 31 | @JsonProperty("Type") |
27 | | - public String type; |
28 | | - |
| 32 | + public LoggingType type = null; |
| 33 | + |
29 | 34 | @JsonProperty("Config") |
30 | 35 | public Map<String, String> config; |
31 | 36 |
|
32 | | - public LogConfig(String type, Map<String, String> config) { |
| 37 | + public LogConfig(LoggingType type, Map<String, String> config) { |
33 | 38 | this.type = type; |
34 | 39 | this.config = config; |
35 | 40 | } |
36 | 41 |
|
| 42 | + public LogConfig(LoggingType type) { |
| 43 | + this(type, null); |
| 44 | + } |
| 45 | + |
37 | 46 | public LogConfig() { |
38 | 47 | } |
39 | 48 |
|
40 | | - public String getType() { |
| 49 | + public LoggingType getType() { |
41 | 50 | return type; |
42 | 51 | } |
43 | 52 |
|
44 | | - public LogConfig setType(String type) { |
| 53 | + public LogConfig setType(LoggingType type) { |
45 | 54 | this.type = type; |
46 | 55 | return this; |
47 | 56 | } |
48 | 57 |
|
49 | 58 | @JsonIgnore |
50 | 59 | public Map<String, String> getConfig() { |
51 | | - return config; |
| 60 | + return config; |
52 | 61 | } |
53 | | - |
| 62 | + |
54 | 63 | @JsonIgnore |
55 | 64 | 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 | + } |
58 | 108 | } |
59 | 109 | } |
0 commit comments