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.
This commit is contained in:
parent
a508864d6f
commit
3e6b584953
|
@ -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<String, Object> 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<String, Object> 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<String, Object> content, String key, Object value) {
|
||||
if (content.containsKey(key) && value != null) {
|
||||
content.put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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}.
|
||||
* <p>
|
||||
* 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) {
|
||||
|
|
|
@ -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<InfoProperties.Entry> {
|
|||
return this.entries.getProperty(property);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the value of the specified property as a {@link Date} or {@code null}
|
||||
* <p>
|
||||
* 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<Entry> iterator() {
|
||||
return new PropertiesIterator(this.entries);
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue