From 21e0f4d28d37d0069c5b1b49646a971c25acfe82 Mon Sep 17 00:00:00 2001 From: Max Kalika Date: Sat, 2 Jul 2016 23:36:27 -0700 Subject: [PATCH] Issue #125: Allow app to run with default (no) config. Add Duration deserializer, remove not null restriction, update test. --- .../github/mk23/jmxproxy/conf/AppConfig.java | 4 +++ .../jmxproxy/conf/DurationDeserializer.java | 36 +++++++++++++++++++ .../jmxproxy/conf/DurationSerializer.java | 4 +-- .../github/mk23/jmxproxy/conf/MainConfig.java | 4 +-- src/test/resources/main_config.yaml | 2 +- 5 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/github/mk23/jmxproxy/conf/DurationDeserializer.java diff --git a/src/main/java/com/github/mk23/jmxproxy/conf/AppConfig.java b/src/main/java/com/github/mk23/jmxproxy/conf/AppConfig.java index 6baac58..8785921 100644 --- a/src/main/java/com/github/mk23/jmxproxy/conf/AppConfig.java +++ b/src/main/java/com/github/mk23/jmxproxy/conf/AppConfig.java @@ -1,6 +1,7 @@ package com.github.mk23.jmxproxy.conf; import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import io.dropwizard.jackson.JsonSnakeCase; @@ -50,6 +51,7 @@ public class AppConfig { */ @Valid @JsonProperty + @JsonDeserialize(using = DurationDeserializer.class) @JsonSerialize(using = DurationSerializer.class) private Duration cleanInterval = Duration.minutes(DEFAULT_CLEAN_INTERVAL); @@ -59,6 +61,7 @@ public class AppConfig { */ @Valid @JsonProperty + @JsonDeserialize(using = DurationDeserializer.class) @JsonSerialize(using = DurationSerializer.class) private Duration cacheDuration = Duration.minutes(DEFAULT_CACHE_DURATION); @@ -68,6 +71,7 @@ public class AppConfig { */ @Valid @JsonProperty + @JsonDeserialize(using = DurationDeserializer.class) @JsonSerialize(using = DurationSerializer.class) private Duration accessDuration = Duration.minutes(DEFAULT_ACCESS_DURATION); diff --git a/src/main/java/com/github/mk23/jmxproxy/conf/DurationDeserializer.java b/src/main/java/com/github/mk23/jmxproxy/conf/DurationDeserializer.java new file mode 100644 index 0000000..366a4df --- /dev/null +++ b/src/main/java/com/github/mk23/jmxproxy/conf/DurationDeserializer.java @@ -0,0 +1,36 @@ +package com.github.mk23.jmxproxy.conf; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.JsonToken; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; + +import io.dropwizard.util.Duration; + +import java.io.IOException; + +/** + *

Custom deserializer for configuration Duration fields.

+ * + * Converts a String or Long into io.dropwizard.util.Duration types to milliseconds. + * + * @see io.dropwizard.util.Duration + * @see com.fasterxml.jackson.databind.JsonDeserializer + * + * @since 2016-07-02 + * @author mk23 + * @version 3.3.3 + */ +public class DurationDeserializer extends JsonDeserializer { + /** {@inheritDoc} */ + @Override + public final Duration deserialize( + final JsonParser jp, + final DeserializationContext ctxt + ) throws IOException, JsonProcessingException { + return jp.getCurrentToken() == JsonToken.VALUE_NUMBER_INT + ? Duration.milliseconds(jp.getValueAsLong()) + : Duration.parse(jp.getValueAsString()); + } +} diff --git a/src/main/java/com/github/mk23/jmxproxy/conf/DurationSerializer.java b/src/main/java/com/github/mk23/jmxproxy/conf/DurationSerializer.java index 47484c5..6bbbc71 100644 --- a/src/main/java/com/github/mk23/jmxproxy/conf/DurationSerializer.java +++ b/src/main/java/com/github/mk23/jmxproxy/conf/DurationSerializer.java @@ -1,9 +1,9 @@ package com.github.mk23.jmxproxy.conf; -import com.fasterxml.jackson.databind.JsonSerializer; -import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; import io.dropwizard.util.Duration; diff --git a/src/main/java/com/github/mk23/jmxproxy/conf/MainConfig.java b/src/main/java/com/github/mk23/jmxproxy/conf/MainConfig.java index 834c4fd..140e58f 100644 --- a/src/main/java/com/github/mk23/jmxproxy/conf/MainConfig.java +++ b/src/main/java/com/github/mk23/jmxproxy/conf/MainConfig.java @@ -5,7 +5,6 @@ import io.dropwizard.Configuration; import javax.validation.Valid; -import javax.validation.constraints.NotNull; /** *

Main top-level configuration.

@@ -23,9 +22,8 @@ public class MainConfig extends Configuration { * Application service config. */ @Valid - @NotNull @JsonProperty("jmxproxy") - private AppConfig appConfig; + private AppConfig appConfig = new AppConfig(); /** *

Getter for appConfig.

diff --git a/src/test/resources/main_config.yaml b/src/test/resources/main_config.yaml index 1830605..5d76203 100644 --- a/src/test/resources/main_config.yaml +++ b/src/test/resources/main_config.yaml @@ -1,5 +1,5 @@ jmxproxy: - access_duration: '5m' + access_duration: 300000 allowed_endpoints: - 'localhost:1100' - 'remotehost:2211'