From 3e6b584953a4ba670a2f0b0749d392942c049c99 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 10 Mar 2016 14:42:48 +0100 Subject: [PATCH] Add Date as a support property type Rather than exposing a raw String with the epoch time, GitProperties now exposes the actual `java.util.Date`. `InfoProperties` has been improved to return such data type when the raw value is an epoch time. --- .../boot/actuate/info/GitInfoContributor.java | 27 ++++++------------- .../ProjectInfoAutoConfigurationTests.java | 4 +-- .../boot/info/GitProperties.java | 11 +++++--- .../boot/info/InfoProperties.java | 23 ++++++++++++++++ .../boot/info/GitPropertiesTests.java | 11 +++++--- 5 files changed, 48 insertions(+), 28 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/GitInfoContributor.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/GitInfoContributor.java index c798833c6aa..de71ee23309 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/GitInfoContributor.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/GitInfoContributor.java @@ -21,9 +21,6 @@ import java.util.Date; import java.util.Map; import java.util.Properties; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - import org.springframework.boot.info.GitProperties; import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertiesPropertySource; @@ -38,8 +35,6 @@ import org.springframework.util.StringUtils; */ public class GitInfoContributor implements InfoContributor { - private static final Log logger = LogFactory.getLog(GitInfoContributor.class); - private final GitProperties properties; private final Mode mode; @@ -84,25 +79,19 @@ public class GitInfoContributor implements InfoContributor { * @param content the content to expose */ protected void postProcess(Map content) { - coerceDate(getNestedMap(content, "commit"), "time"); - coerceDate(getNestedMap(content, "build"), "time"); + replaceValue(getNestedMap(content, "commit"), "time", getProperties().getDate("commit.time")); + replaceValue(getNestedMap(content, "build"), "time", getProperties().getDate("build.time")); } /** - * Coerce the specified key if the value is a proper epoch time. + * Replace the {@code value} for the specified key if the value is not {@code null}. * @param content the content to expose - * @param key the property to coerce + * @param key the property to replace + * @param value the new value */ - protected void coerceDate(Map content, String key) { - Object value = content.get(key); - if (value != null) { - try { - long epoch = Long.parseLong(value.toString()); - content.put(key, new Date(epoch)); - } - catch (NumberFormatException ex) { - logger.warn("Expected a date for '" + key + "'", ex); - } + protected void replaceValue(Map content, String key, Object value) { + if (content.containsKey(key) && value != null) { + content.put(key, value); } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/info/ProjectInfoAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/info/ProjectInfoAutoConfigurationTests.java index 45974c56216..938c663f9eb 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/info/ProjectInfoAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/info/ProjectInfoAutoConfigurationTests.java @@ -61,7 +61,7 @@ public class ProjectInfoAutoConfigurationTests { GitProperties gitProperties = this.context.getBean(GitProperties.class); assertThat(gitProperties.getBranch()).isNull(); assertThat(gitProperties.getCommitId()).isEqualTo("f95038ec09e29d8f91982fd1cbcc0f3b131b1d0a"); - assertThat(gitProperties.getCommitTime()).isEqualTo("1456995720000"); + assertThat(gitProperties.getCommitTime().getTime()).isEqualTo(1456995720000L); } @Test @@ -70,7 +70,7 @@ public class ProjectInfoAutoConfigurationTests { GitProperties gitProperties = this.context.getBean(GitProperties.class); assertThat(gitProperties.getBranch()).isEqualTo("master"); assertThat(gitProperties.getCommitId()).isEqualTo("5009933788f5f8c687719de6a697074ff80b1b69"); - assertThat(gitProperties.getCommitTime()).isEqualTo("1457103850000"); + assertThat(gitProperties.getCommitTime().getTime()).isEqualTo(1457103850000L); } @Test diff --git a/spring-boot/src/main/java/org/springframework/boot/info/GitProperties.java b/spring-boot/src/main/java/org/springframework/boot/info/GitProperties.java index 1c8941d1181..2468eaa4745 100644 --- a/spring-boot/src/main/java/org/springframework/boot/info/GitProperties.java +++ b/spring-boot/src/main/java/org/springframework/boot/info/GitProperties.java @@ -61,12 +61,15 @@ public class GitProperties extends InfoProperties { } /** - * Return the timestamp of the commit, possibly as epoch time in millisecond, or {@code null}. + * Return the timestamp of the commit or {@code null}. + *

+ * If the original value could not be parsed properly, it is still available with + * the {@code commit.time} key. * @return the commit time - * @see Date#getTime() + * @see #get(String) */ - public String getCommitTime() { - return get("commit.time"); + public Date getCommitTime() { + return getDate("commit.time"); } private static Properties processEntries(Properties properties) { diff --git a/spring-boot/src/main/java/org/springframework/boot/info/InfoProperties.java b/spring-boot/src/main/java/org/springframework/boot/info/InfoProperties.java index 13d88e454a0..84edba503a3 100644 --- a/spring-boot/src/main/java/org/springframework/boot/info/InfoProperties.java +++ b/spring-boot/src/main/java/org/springframework/boot/info/InfoProperties.java @@ -16,6 +16,7 @@ package org.springframework.boot.info; +import java.util.Date; import java.util.Enumeration; import java.util.Iterator; import java.util.Properties; @@ -53,6 +54,28 @@ public class InfoProperties implements Iterable { return this.entries.getProperty(property); } + /** + * Return the value of the specified property as a {@link Date} or {@code null} + *

+ * Return {@code null} if the value is not a valid {@link Long} representing an + * epoch time. + * @param key the id of the property + * @return the property value + */ + public Date getDate(String key) { + String s = get(key); + if (s != null) { + try { + long epoch = Long.parseLong(s); + return new Date(epoch); + } + catch (NumberFormatException e) { + // Not valid epoch time + } + } + return null; + } + @Override public Iterator iterator() { return new PropertiesIterator(this.entries); diff --git a/spring-boot/src/test/java/org/springframework/boot/info/GitPropertiesTests.java b/spring-boot/src/test/java/org/springframework/boot/info/GitPropertiesTests.java index 0492c717ec4..13bd2453015 100644 --- a/spring-boot/src/test/java/org/springframework/boot/info/GitPropertiesTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/info/GitPropertiesTests.java @@ -51,21 +51,26 @@ public class GitPropertiesTests { public void coerceEpochSecond() { GitProperties properties = new GitProperties( createProperties("master", "abcdefg", "1457527123")); - assertThat(properties.getCommitTime()).isEqualTo("1457527123000"); + assertThat(properties.getCommitTime()).isNotNull(); + assertThat(properties.get("commit.time")).isEqualTo("1457527123000"); + assertThat(properties.getCommitTime().getTime()).isEqualTo(1457527123000L); } @Test public void coerceDateString() { GitProperties properties = new GitProperties( createProperties("master", "abcdefg", "2016-03-04T14:36:33+0100")); - assertThat(properties.getCommitTime()).isEqualTo("1457098593000"); + assertThat(properties.getCommitTime()).isNotNull(); + assertThat(properties.get("commit.time")).isEqualTo("1457098593000"); + assertThat(properties.getCommitTime().getTime()).isEqualTo(1457098593000L); } @Test public void coerceUnsupportedFormat() { GitProperties properties = new GitProperties( createProperties("master", "abcdefg", "2016-03-04 15:22:24")); - assertThat(properties.getCommitTime()).isEqualTo("2016-03-04 15:22:24"); + assertThat(properties.getCommitTime()).isNull(); + assertThat(properties.get("commit.time")).isEqualTo("2016-03-04 15:22:24"); } @Test