Skip to content

Commit

Permalink
Issue #125: Allow app to run with default (no) config. Add Duration d…
Browse files Browse the repository at this point in the history
…eserializer, remove not null restriction, update test.
  • Loading branch information
mk23 committed Jul 3, 2016
1 parent 68bf7bc commit 21e0f4d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/github/mk23/jmxproxy/conf/AppConfig.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/**
* <p>Custom deserializer for configuration Duration fields.</p>
*
* Converts a String or Long into <code>io.dropwizard.util.Duration</code> types to milliseconds.
*
* @see <a href="http://dropwizard.github.io/dropwizard/0.9.3/dropwizard-util/apidocs/io/dropwizard/util/Duration.html">io.dropwizard.util.Duration</a>
* @see <a href="https://fasterxml.github.io/jackson-databind/javadoc/2.6/com/fasterxml/jackson/databind/JsonDeserializer.html">com.fasterxml.jackson.databind.JsonDeserializer</a>
*
* @since 2016-07-02
* @author mk23
* @version 3.3.3
*/
public class DurationDeserializer extends JsonDeserializer<Duration> {
/** {@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());
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/github/mk23/jmxproxy/conf/MainConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import io.dropwizard.Configuration;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
* <p>Main top-level configuration.</p>
Expand All @@ -23,9 +22,8 @@ public class MainConfig extends Configuration {
* Application service config.
*/
@Valid
@NotNull
@JsonProperty("jmxproxy")
private AppConfig appConfig;
private AppConfig appConfig = new AppConfig();

/**
* <p>Getter for appConfig.</p>
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/main_config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
jmxproxy:
access_duration: '5m'
access_duration: 300000
allowed_endpoints:
- 'localhost:1100'
- 'remotehost:2211'
Expand Down

0 comments on commit 21e0f4d

Please sign in to comment.