Commit b906b186 authored by Stephane Nicoll's avatar Stephane Nicoll

Initiate GitProperties

This commit polish the new info contributor infrastructure by migrating
`GitInfo` to `GitProperties`. `InfoProperties` provides an abstraction
that exposes unstructured data in an immutable way.

The `GitInfoContributor` now accepts a "mode" that determines if all data
should be exposed or only a sub-set of known keys.

Closes gh-2644
parent 474aed05
...@@ -16,17 +16,17 @@ ...@@ -16,17 +16,17 @@
package org.springframework.boot.actuate.autoconfigure; package org.springframework.boot.actuate.autoconfigure;
import java.io.IOException;
import org.springframework.boot.actuate.info.EnvironmentInfoContributor; import org.springframework.boot.actuate.info.EnvironmentInfoContributor;
import org.springframework.boot.actuate.info.GitInfoContributor;
import org.springframework.boot.actuate.info.InfoContributor; import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.boot.actuate.info.SimpleInfoContributor;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.autoconfigure.info.GitInfo;
import org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration; import org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.info.GitProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
...@@ -44,6 +44,7 @@ import org.springframework.core.env.ConfigurableEnvironment; ...@@ -44,6 +44,7 @@ import org.springframework.core.env.ConfigurableEnvironment;
@Configuration @Configuration
@AutoConfigureAfter(ProjectInfoAutoConfiguration.class) @AutoConfigureAfter(ProjectInfoAutoConfiguration.class)
@AutoConfigureBefore(EndpointAutoConfiguration.class) @AutoConfigureBefore(EndpointAutoConfiguration.class)
@EnableConfigurationProperties(InfoContributorProperties.class)
public class InfoContributorAutoConfiguration { public class InfoContributorAutoConfiguration {
/** /**
...@@ -51,6 +52,12 @@ public class InfoContributorAutoConfiguration { ...@@ -51,6 +52,12 @@ public class InfoContributorAutoConfiguration {
*/ */
public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 10; public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 10;
private final InfoContributorProperties properties;
public InfoContributorAutoConfiguration(InfoContributorProperties properties) {
this.properties = properties;
}
@Bean @Bean
@ConditionalOnEnabledInfoContributor("env") @ConditionalOnEnabledInfoContributor("env")
@Order(DEFAULT_ORDER) @Order(DEFAULT_ORDER)
...@@ -61,10 +68,11 @@ public class InfoContributorAutoConfiguration { ...@@ -61,10 +68,11 @@ public class InfoContributorAutoConfiguration {
@Bean @Bean
@ConditionalOnEnabledInfoContributor("git") @ConditionalOnEnabledInfoContributor("git")
@ConditionalOnSingleCandidate(GitInfo.class) @ConditionalOnSingleCandidate(GitProperties.class)
@ConditionalOnMissingBean
@Order(DEFAULT_ORDER) @Order(DEFAULT_ORDER)
public InfoContributor gitInfoContributor(GitInfo gitInfo) throws IOException { public GitInfoContributor gitInfoContributor(GitProperties gitProperties) {
return new SimpleInfoContributor("git", gitInfo); return new GitInfoContributor(gitProperties, this.properties.getGit().getMode());
} }
} }
...@@ -14,59 +14,39 @@ ...@@ -14,59 +14,39 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.boot.autoconfigure.info; package org.springframework.boot.actuate.autoconfigure;
import java.util.Date; import org.springframework.boot.actuate.info.GitInfoContributor;
import org.springframework.boot.context.properties.ConfigurationProperties;
/** /**
* Provide git-related information such as commit id and time. * Configuration properties for core info contributors.
* *
* @author Dave Syer
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 1.4.0 * @since 1.4.0
*/ */
public class GitInfo { @ConfigurationProperties("management.info")
public class InfoContributorProperties {
private String branch; private final Git git = new Git();
private final Commit commit = new Commit(); public Git getGit() {
return this.git;
public String getBranch() {
return this.branch;
}
public void setBranch(String branch) {
this.branch = branch;
} }
public Commit getCommit() { public static class Git {
return this.commit;
}
public static class Commit {
private String id;
private Date time; /**
* Mode to use to expose git information.
public String getId() { */
return (this.id == null ? "" : getShortId(this.id)); private GitInfoContributor.Mode mode = GitInfoContributor.Mode.SIMPLE;
}
private String getShortId(String string) {
return string.substring(0, Math.min(this.id.length(), 7));
}
public void setId(String id) {
this.id = id;
}
public Date getTime() { public GitInfoContributor.Mode getMode() {
return this.time; return this.mode;
} }
public void setTime(Date time) { public void setMode(GitInfoContributor.Mode mode) {
this.time = time; this.mode = mode;
} }
} }
......
...@@ -16,29 +16,26 @@ ...@@ -16,29 +16,26 @@
package org.springframework.boot.actuate.info; package org.springframework.boot.actuate.info;
import java.util.Map;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
/** /**
* A {@link InfoContributor} that provides all environment entries prefixed with info. * An {@link InfoContributor} that provides all environment entries prefixed with info.
* *
* @author Meang Akira Tanaka * @author Meang Akira Tanaka
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 1.4.0 * @since 1.4.0
*/ */
public class EnvironmentInfoContributor extends AbstractEnvironmentInfoContributor { public class EnvironmentInfoContributor implements InfoContributor {
private final Map<String, Object> info; private final PropertySourcesBinder binder;
public EnvironmentInfoContributor(ConfigurableEnvironment environment) { public EnvironmentInfoContributor(ConfigurableEnvironment environment) {
super(environment); this.binder = new PropertySourcesBinder(environment);
this.info = extract("info");
} }
@Override @Override
public void contribute(Info.Builder builder) { public void contribute(Info.Builder builder) {
builder.withDetails(this.info); builder.withDetails(this.binder.extractAll("info"));
} }
} }
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.info;
import java.util.Collections;
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;
import org.springframework.core.env.PropertySource;
import org.springframework.util.StringUtils;
/**
* An {@link InfoContributor} that exposes {@link GitProperties}.
*
* @author Stephane Nicoll
* @since 1.4.0
*/
public class GitInfoContributor implements InfoContributor {
private static final Log logger = LogFactory.getLog(GitInfoContributor.class);
private final GitProperties properties;
private final Mode mode;
public GitInfoContributor(GitProperties properties, Mode mode) {
this.properties = properties;
this.mode = mode;
}
public GitInfoContributor(GitProperties properties) {
this(properties, Mode.SIMPLE);
}
protected final GitProperties getProperties() {
return this.properties;
}
protected final Mode getMode() {
return this.mode;
}
@Override
public void contribute(Info.Builder builder) {
builder.withDetail("git", extractContent());
}
/**
* Extract the content to contribute to the info endpoint.
* @return the content to expose
*/
protected Map<String, Object> extractContent() {
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addFirst(toPropertySources());
Map<String, Object> content = new PropertySourcesBinder(propertySources).extractAll("");
postProcess(content);
return content;
}
/**
* Post-process the content to expose. By default, well known keys representing dates
* are converted to {@link Date} instances.
* @param content the content to expose
*/
protected void postProcess(Map<String, Object> content) {
coerceDate(getNestedMap(content, "commit"), "time");
coerceDate(getNestedMap(content, "build"), "time");
}
/**
* Coerce the specified key if the value is a proper epoch time.
* @param content the content to expose
* @param key the property to coerce
*/
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);
}
}
}
/**
* Return the {@link PropertySource} to use.
* @return the property source
*/
protected PropertySource<?> toPropertySources() {
if (this.mode.equals(Mode.FULL)) {
return this.properties.toPropertySource();
}
else {
Properties props = new Properties();
copyIfSet(this.properties, props, "branch");
String commitId = this.properties.getShortCommitId();
if (commitId != null) {
props.put("commit.id", commitId);
}
copyIfSet(this.properties, props, "commit.time");
return new PropertiesPropertySource("git", props);
}
}
private void copyIfSet(GitProperties source, Properties target, String key) {
String value = source.get(key);
if (StringUtils.hasText(value)) {
target.put(key, value);
}
}
@SuppressWarnings("unchecked")
private Map<String, Object> getNestedMap(Map<String, Object> map, String key) {
Object o = map.get(key);
if (o == null) {
return Collections.emptyMap();
}
else {
return (Map<String, Object>) o;
}
}
/**
* Defines how git properties should be exposed.
*/
public enum Mode {
/**
* Expose all available data, including custom properties.
*/
FULL,
/**
* Expose a pre-defined set of core settings only.
*/
SIMPLE
}
}
...@@ -21,38 +21,43 @@ import java.util.Map; ...@@ -21,38 +21,43 @@ import java.util.Map;
import org.springframework.boot.bind.PropertiesConfigurationFactory; import org.springframework.boot.bind.PropertiesConfigurationFactory;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment; import org.springframework.core.env.PropertySources;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindException; import org.springframework.validation.BindException;
/** /**
* A base {@link InfoContributor} implementation working on the {@link Environment}. * Helper extracting info from {@link PropertySources}.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 1.4.0 * @since 1.4.0
*/ */
public abstract class AbstractEnvironmentInfoContributor implements InfoContributor { public class PropertySourcesBinder {
private final ConfigurableEnvironment environment; private final PropertySources propertySources;
protected AbstractEnvironmentInfoContributor(ConfigurableEnvironment environment) { public PropertySourcesBinder(PropertySources propertySources) {
this.environment = environment; this.propertySources = propertySources;
} }
public final ConfigurableEnvironment getEnvironment() { public PropertySourcesBinder(ConfigurableEnvironment environment) {
return this.environment; this(environment.getPropertySources());
}
public final PropertySources getPropertySources() {
return this.propertySources;
} }
/** /**
* Extract the keys from the environment using the specified {@code prefix}. The * Extract the keys using the specified {@code prefix}. The
* prefix won't be included. * prefix won't be included.
* <p> * <p>
* Any key that starts with the {@code prefix} will be included * Any key that starts with the {@code prefix} will be included
* @param prefix the prefix to use * @param prefix the prefix to use
* @return the keys from the environment matching the prefix * @return the keys matching the prefix
*/ */
protected Map<String, Object> extract(String prefix) { public Map<String, Object> extractAll(String prefix) {
Map<String, Object> content = new LinkedHashMap<String, Object>(); Map<String, Object> content = new LinkedHashMap<String, Object>();
bindEnvironmentTo(prefix, content); bindTo(prefix, content);
return content; return content;
} }
...@@ -63,11 +68,13 @@ public abstract class AbstractEnvironmentInfoContributor implements InfoContribu ...@@ -63,11 +68,13 @@ public abstract class AbstractEnvironmentInfoContributor implements InfoContribu
* @param prefix the prefix to use * @param prefix the prefix to use
* @param target the object to bind to * @param target the object to bind to
*/ */
protected void bindEnvironmentTo(String prefix, Object target) { public void bindTo(String prefix, Object target) {
PropertiesConfigurationFactory<Object> factory = new PropertiesConfigurationFactory<Object>( PropertiesConfigurationFactory<Object> factory = new PropertiesConfigurationFactory<Object>(
target); target);
factory.setTargetName(prefix); if (StringUtils.hasText(prefix)) {
factory.setPropertySources(this.environment.getPropertySources()); factory.setTargetName(prefix);
}
factory.setPropertySources(this.propertySources);
try { try {
factory.bindPropertiesToTarget(); factory.bindPropertiesToTarget();
} }
......
...@@ -17,13 +17,15 @@ ...@@ -17,13 +17,15 @@
package org.springframework.boot.actuate.autoconfigure; package org.springframework.boot.actuate.autoconfigure;
import java.util.Map; import java.util.Map;
import java.util.Properties;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.actuate.info.GitInfoContributor;
import org.springframework.boot.actuate.info.Info; import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor; import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.boot.autoconfigure.info.GitInfo; import org.springframework.boot.info.GitProperties;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
...@@ -74,12 +76,45 @@ public class InfoContributorAutoConfigurationTests { ...@@ -74,12 +76,45 @@ public class InfoContributorAutoConfigurationTests {
.isSameAs(beans.values().iterator().next()); .isSameAs(beans.values().iterator().next());
} }
@SuppressWarnings("unchecked")
@Test @Test
public void gitInfoAvailable() { public void gitPropertiesDefaultMode() {
load(GitInfoConfiguration.class); load(GitPropertiesConfiguration.class);
Map<String, InfoContributor> beans = this.context Map<String, InfoContributor> beans = this.context
.getBeansOfType(InfoContributor.class); .getBeansOfType(InfoContributor.class);
assertThat(beans).containsKeys("gitInfoContributor"); assertThat(beans).containsKeys("gitInfoContributor");
Map<String, Object> content =
invokeContributor(this.context.getBean("gitInfoContributor", InfoContributor.class));
Object git = content.get("git");
assertThat(git).isInstanceOf(Map.class);
Map<String, Object> gitInfo = (Map<String, Object>) git;
assertThat(gitInfo).containsOnlyKeys("branch", "commit");
}
@SuppressWarnings("unchecked")
@Test
public void gitPropertiesFullMode() {
load(GitPropertiesConfiguration.class, "management.info.git.mode=full");
Map<String, Object> content =
invokeContributor(this.context.getBean("gitInfoContributor", InfoContributor.class));
Object git = content.get("git");
assertThat(git).isInstanceOf(Map.class);
Map<String, Object> gitInfo = (Map<String, Object>) git;
assertThat(gitInfo).containsOnlyKeys("branch", "commit", "foo");
assertThat(gitInfo.get("foo")).isEqualTo("bar");
}
@Test
public void customGitInfoContributor() {
load(CustomGitInfoProviderConfiguration.class);
assertThat(this.context.getBean(GitInfoContributor.class))
.isSameAs(this.context.getBean("customGitInfoContributor"));
}
private Map<String, Object> invokeContributor(InfoContributor contributor) {
Info.Builder builder = new Info.Builder();
contributor.contribute(builder);
return builder.build().getDetails();
} }
private void load(String... environment) { private void load(String... environment) {
...@@ -98,14 +133,15 @@ public class InfoContributorAutoConfigurationTests { ...@@ -98,14 +133,15 @@ public class InfoContributorAutoConfigurationTests {
} }
@Configuration @Configuration
static class GitInfoConfiguration { static class GitPropertiesConfiguration {
@Bean @Bean
public GitInfo gitInfo() { public GitProperties gitProperties() {
GitInfo gitInfo = new GitInfo(); Properties properties = new Properties();
gitInfo.setBranch("master"); properties.put("branch", "master");
gitInfo.getCommit().setId("abcdefg"); properties.put("commit.id", "abcdefg");
return gitInfo; properties.put("foo", "bar");
return new GitProperties(properties);
} }
} }
...@@ -124,4 +160,14 @@ public class InfoContributorAutoConfigurationTests { ...@@ -124,4 +160,14 @@ public class InfoContributorAutoConfigurationTests {
} }
@Configuration
static class CustomGitInfoProviderConfiguration {
@Bean
public GitInfoContributor customGitInfoContributor() {
return new GitInfoContributor(new GitProperties(new Properties()));
}
}
} }
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.info;
import java.util.Date;
import java.util.Map;
import java.util.Properties;
import org.junit.Test;
import org.springframework.boot.info.GitProperties;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link GitInfoContributor}.
*
* @author Stephane Nicoll
*/
public class GitInfoContributorTests {
@SuppressWarnings("unchecked")
@Test
public void coerceDate() {
Properties properties = new Properties();
properties.put("branch", "master");
properties.put("commit.time", "2016-03-04T14:36:33+0100");
GitInfoContributor contributor = new GitInfoContributor(new GitProperties(properties));
Map<String, Object> content = contributor.extractContent();
assertThat(content.get("commit")).isInstanceOf(Map.class);
Map<String, Object> commit = (Map<String, Object>) content.get("commit");
Object commitTime = commit.get("time");
assertThat(commitTime).isInstanceOf(Date.class);
assertThat(((Date) commitTime).getTime()).isEqualTo(1457098593000L);
}
@SuppressWarnings("unchecked")
@Test
public void shortenCommitId() {
Properties properties = new Properties();
properties.put("branch", "master");
properties.put("commit.id", "8e29a0b0d423d2665c6ee5171947c101a5c15681");
GitInfoContributor contributor = new GitInfoContributor(new GitProperties(properties));
Map<String, Object> content = contributor.extractContent();
assertThat(content.get("commit")).isInstanceOf(Map.class);
Map<String, Object> commit = (Map<String, Object>) content.get("commit");
assertThat(commit.get("id")).isEqualTo("8e29a0b");
}
}
...@@ -17,36 +17,25 @@ ...@@ -17,36 +17,25 @@
package org.springframework.boot.autoconfigure.info; package org.springframework.boot.autoconfigure.info;
import java.io.IOException; import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Properties; import java.util.Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome; import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition; import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.bind.PropertiesConfigurationFactory;
import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.info.GitProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.env.PropertyResolver; import org.springframework.core.env.PropertyResolver;
import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PropertiesLoaderUtils; import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.core.type.AnnotatedTypeMetadata; import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.validation.BindException;
/** /**
* {@link EnableAutoConfiguration Auto-configuration} for various project information. * {@link EnableAutoConfiguration Auto-configuration} for various project information.
...@@ -58,36 +47,31 @@ import org.springframework.validation.BindException; ...@@ -58,36 +47,31 @@ import org.springframework.validation.BindException;
@EnableConfigurationProperties(ProjectInfoProperties.class) @EnableConfigurationProperties(ProjectInfoProperties.class)
public class ProjectInfoAutoConfiguration { public class ProjectInfoAutoConfiguration {
@Autowired private final ProjectInfoProperties properties;
private ProjectInfoProperties properties;
private final ConversionService conversionService = createConversionService(); public ProjectInfoAutoConfiguration(ProjectInfoProperties properties) {
this.properties = properties;
}
@Conditional(GitResourceAvailableCondition.class) @Conditional(GitResourceAvailableCondition.class)
@ConditionalOnMissingBean @ConditionalOnMissingBean
@Bean @Bean
public GitInfo gitInfo() throws Exception { public GitProperties gitProperties() throws Exception {
GitInfo gitInfo = new GitInfo(); return new GitProperties(loadFrom(this.properties.getGit().getLocation(), "git"));
bindPropertiesTo(gitInfo, this.properties.getGit().getLocation(), "git");
return gitInfo;
} }
protected void bindPropertiesTo(Object target, Resource location, String prefix) protected Properties loadFrom(Resource location, String prefix) throws IOException {
throws BindException, IOException { String p = prefix.endsWith(".") ? prefix : prefix + ".";
PropertiesConfigurationFactory<Object> factory = new PropertiesConfigurationFactory<Object>( Properties source = PropertiesLoaderUtils.loadProperties(location);
target); Properties target = new Properties();
factory.setConversionService(this.conversionService); for (String key : source.stringPropertyNames()) {
factory.setTargetName(prefix); if (key.startsWith(p)) {
Properties gitInfoProperties = PropertiesLoaderUtils.loadProperties(location); target.put(key.substring(p.length(), key.length()), source.get(key));
factory.setProperties(gitInfoProperties); }
factory.bindPropertiesToTarget(); }
return target;
} }
private static ConversionService createConversionService() {
DefaultConversionService conversionService = new DefaultConversionService();
conversionService.addConverter(new StringToDateConverter());
return conversionService;
}
static class GitResourceAvailableCondition extends SpringBootCondition { static class GitResourceAvailableCondition extends SpringBootCondition {
...@@ -116,40 +100,4 @@ public class ProjectInfoAutoConfiguration { ...@@ -116,40 +100,4 @@ public class ProjectInfoAutoConfiguration {
} }
private static class StringToDateConverter implements Converter<String, Date> {
@Override
public Date convert(String s) {
Long epoch = parseEpochSecond(s);
if (epoch != null) {
return new Date(epoch);
}
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX",
Locale.US);
try {
return format.parse(s);
}
catch (ParseException ex) {
throw new ConversionFailedException(TypeDescriptor.valueOf(String.class),
TypeDescriptor.valueOf(Date.class), s, ex);
}
}
/**
* Attempt to parse a {@code Long} from the specified input, representing the
* epoch time in seconds.
* @param s the input
* @return the epoch time in msec
*/
private Long parseEpochSecond(String s) {
try {
return Long.parseLong(s) * 1000;
}
catch (NumberFormatException ex) {
return null;
}
}
}
} }
...@@ -17,11 +17,13 @@ ...@@ -17,11 +17,13 @@
package org.springframework.boot.autoconfigure.info; package org.springframework.boot.autoconfigure.info;
import java.util.Map; import java.util.Map;
import java.util.Properties;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.info.GitProperties;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
...@@ -46,9 +48,9 @@ public class ProjectInfoAutoConfigurationTests { ...@@ -46,9 +48,9 @@ public class ProjectInfoAutoConfigurationTests {
} }
@Test @Test
public void gitInfoUnavailableIfResourceNotAvailable() { public void gitPropertiesUnavailableIfResourceNotAvailable() {
load(); load();
Map<String, GitInfo> beans = this.context.getBeansOfType(GitInfo.class); Map<String, GitProperties> beans = this.context.getBeansOfType(GitProperties.class);
assertThat(beans).hasSize(0); assertThat(beans).hasSize(0);
} }
...@@ -56,34 +58,34 @@ public class ProjectInfoAutoConfigurationTests { ...@@ -56,34 +58,34 @@ public class ProjectInfoAutoConfigurationTests {
public void gitLocationTakesPrecedenceOverLegacyKey() { public void gitLocationTakesPrecedenceOverLegacyKey() {
load("spring.info.git.location=classpath:/org/springframework/boot/autoconfigure/info/git.properties", load("spring.info.git.location=classpath:/org/springframework/boot/autoconfigure/info/git.properties",
"spring.git.properties=classpath:/org/springframework/boot/autoconfigure/info/git-no-data.properties"); "spring.git.properties=classpath:/org/springframework/boot/autoconfigure/info/git-no-data.properties");
GitInfo gitInfo = this.context.getBean(GitInfo.class); GitProperties gitProperties = this.context.getBean(GitProperties.class);
assertThat(gitInfo.getBranch()).isNull(); assertThat(gitProperties.getBranch()).isNull();
assertThat(gitInfo.getCommit().getId()).isEqualTo("f95038e"); assertThat(gitProperties.getCommitId()).isEqualTo("f95038ec09e29d8f91982fd1cbcc0f3b131b1d0a");
assertThat(gitInfo.getCommit().getTime().getTime()).isEqualTo(1456995720000L); assertThat(gitProperties.getCommitTime()).isEqualTo("1456995720000");
} }
@Test @Test
public void gitLegacyKeyIsUsedAsFallback() { public void gitLegacyKeyIsUsedAsFallback() {
load("spring.git.properties=classpath:/org/springframework/boot/autoconfigure/info/git-epoch.properties"); load("spring.git.properties=classpath:/org/springframework/boot/autoconfigure/info/git-epoch.properties");
GitInfo gitInfo = this.context.getBean(GitInfo.class); GitProperties gitProperties = this.context.getBean(GitProperties.class);
assertThat(gitInfo.getBranch()).isEqualTo("master"); assertThat(gitProperties.getBranch()).isEqualTo("master");
assertThat(gitInfo.getCommit().getId()).isEqualTo("5009933"); assertThat(gitProperties.getCommitId()).isEqualTo("5009933788f5f8c687719de6a697074ff80b1b69");
assertThat(gitInfo.getCommit().getTime().getTime()).isEqualTo(1457103850000L); assertThat(gitProperties.getCommitTime()).isEqualTo("1457103850000");
} }
@Test @Test
public void gitInfoWithNoData() { public void gitPropertiesWithNoData() {
load("spring.info.git.location=classpath:/org/springframework/boot/autoconfigure/info/git-no-data.properties"); load("spring.info.git.location=classpath:/org/springframework/boot/autoconfigure/info/git-no-data.properties");
GitInfo gitInfo = this.context.getBean(GitInfo.class); GitProperties gitProperties = this.context.getBean(GitProperties.class);
assertThat(gitInfo.getBranch()).isNull(); assertThat(gitProperties.getBranch()).isNull();
} }
@Test @Test
public void gitInfoFallbackWithGitInfoBean() { public void gitPropertiesFallbackWithGitPropertiesBean() {
load(CustomGitInfoConfiguration.class, load(CustomGitPropertiesConfiguration.class,
"spring.info.git.location=classpath:/org/springframework/boot/autoconfigure/info/git.properties"); "spring.info.git.location=classpath:/org/springframework/boot/autoconfigure/info/git.properties");
GitInfo gitInfo = this.context.getBean(GitInfo.class); GitProperties gitProperties = this.context.getBean(GitProperties.class);
assertThat(gitInfo).isSameAs(this.context.getBean("customGitInfo")); assertThat(gitProperties).isSameAs(this.context.getBean("customGitProperties"));
} }
private void load(String... environment) { private void load(String... environment) {
...@@ -103,11 +105,11 @@ public class ProjectInfoAutoConfigurationTests { ...@@ -103,11 +105,11 @@ public class ProjectInfoAutoConfigurationTests {
} }
@Configuration @Configuration
static class CustomGitInfoConfiguration { static class CustomGitPropertiesConfiguration {
@Bean @Bean
public GitInfo customGitInfo() { public GitProperties customGitProperties() {
return new GitInfo(); return new GitProperties(new Properties());
} }
} }
......
...@@ -891,6 +891,7 @@ content into your application; rather pick only the properties that you need. ...@@ -891,6 +891,7 @@ content into your application; rather pick only the properties that you need.
management.info.defaults.enabled=true # Enable default health indicators. management.info.defaults.enabled=true # Enable default health indicators.
management.info.env.enabled=true # Enable environment info. management.info.env.enabled=true # Enable environment info.
management.info.git.enabled=true # Enable git info. management.info.git.enabled=true # Enable git info.
management.info.git.mode=simple # Mode to use to expose git information.
# TRACING (({sc-spring-boot-actuator}/trace/TraceProperties.{sc-ext}[TraceProperties]) # TRACING (({sc-spring-boot-actuator}/trace/TraceProperties.{sc-ext}[TraceProperties])
management.trace.include=request-headers,response-headers,errors # Items to be included in the trace. management.trace.include=request-headers,response-headers,errors # Items to be included in the trace.
......
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.info;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Properties;
/**
* Provide git-related information such as commit id and time.
*
* @author Stephane Nicoll
* @since 1.4.0
*/
public class GitProperties extends InfoProperties {
public GitProperties(Properties entries) {
super(processEntries(entries));
}
/**
* Return the name of the branch or {@code null}.
* @return the branch
*/
public String getBranch() {
return get("branch");
}
/**
* Return the full id of the commit or {@code null}.
* @return the full commit id
*/
public String getCommitId() {
return get("commit.id");
}
/**
* Return the abbreviated id of the commit oir {@code null}.
* @return the short commit id
*/
public String getShortCommitId() {
String commitId = getCommitId();
return commitId == null ? null
: (commitId.length() > 7 ? commitId.substring(0, 7) : commitId);
}
/**
* Return the timestamp of the commit, possibly as epoch time in millisecond, or {@code null}.
* @return the commit time
* @see Date#getTime()
*/
public String getCommitTime() {
return get("commit.time");
}
private static Properties processEntries(Properties properties) {
coerceDate(properties, "commit.time");
coerceDate(properties, "build.time");
return properties;
}
private static void coerceDate(Properties properties, String key) {
String value = properties.getProperty(key);
if (value != null) {
properties.setProperty(key, coerceToEpoch(value));
}
}
/**
* Attempt to convert the specified value to epoch time. Git properties
* information are known to be specified either as epoch time in seconds
* or using a specific date format.
* @param s the value to coerce to
* @return the epoch time in milliseconds or the original value if it couldn't be
* converted
*/
private static String coerceToEpoch(String s) {
Long epoch = parseEpochSecond(s);
if (epoch != null) {
return String.valueOf(epoch);
}
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX", Locale.US);
try {
return String.valueOf(format.parse(s).getTime());
}
catch (ParseException ex) {
return s;
}
}
private static Long parseEpochSecond(String s) {
try {
return Long.parseLong(s) * 1000;
}
catch (NumberFormatException e) {
return null;
}
}
}
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.info;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.util.Assert;
/**
* Base class for components exposing unstructured data with dedicated methods for
* well known keys.
*
* @author Stephane Nicoll
* @since 1.4.0
*/
public class InfoProperties implements Iterable<InfoProperties.Entry> {
private final Properties entries;
/**
* Create an instance with the specified entries.
* @param entries the information to expose
*/
public InfoProperties(Properties entries) {
Assert.notNull(entries, "Properties must not be null");
this.entries = copy(entries);
}
/**
* Return the value of the specified property or {@code null}.
* @param property the id of the property
* @return the property value
*/
public String get(String property) {
return this.entries.getProperty(property);
}
@Override
public Iterator<Entry> iterator() {
return new PropertiesIterator(this.entries);
}
/**
* Return a {@link PropertySource} of this instance.
* @return a {@link PropertySource}
*/
public PropertySource<?> toPropertySource() {
return new PropertiesPropertySource(getClass().getSimpleName(), copy(this.entries));
}
private static Properties copy(Properties properties) {
Properties copy = new Properties();
copy.putAll(properties);
return copy;
}
/**
* Property entry.
*/
public final class Entry {
private final String key;
private final String value;
private Entry(String key, String value) {
this.key = key;
this.value = value;
}
public String getKey() {
return this.key;
}
public String getValue() {
return this.value;
}
}
private final class PropertiesIterator implements Iterator<Entry> {
private final Properties properties;
private final Enumeration<Object> keys;
private PropertiesIterator(Properties properties) {
this.properties = properties;
this.keys = this.properties.keys();
}
@Override
public boolean hasNext() {
return this.keys.hasMoreElements();
}
@Override
public Entry next() {
String key = (String) this.keys.nextElement();
return new Entry(key, this.properties.getProperty(key));
}
@Override
public void remove() {
throw new UnsupportedOperationException("InfoProperties are immutable.");
}
}
}
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.info;
import java.util.Properties;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link GitProperties}.
*
* @author Stephane Nicoll
*/
public class GitPropertiesTests {
@Test
public void basicInfo() {
GitProperties properties = new GitProperties(
createProperties("master", "abcdefghijklmno", "1457527123"));
assertThat(properties.getBranch()).isEqualTo("master");
assertThat(properties.getCommitId()).isEqualTo("abcdefghijklmno");
assertThat(properties.getShortCommitId()).isEqualTo("abcdefg");
}
@Test
public void noInfo() {
GitProperties properties = new GitProperties(new Properties());
assertThat(properties.getBranch()).isNull();
assertThat(properties.getCommitId()).isNull();
assertThat(properties.getShortCommitId()).isNull();
assertThat(properties.getCommitTime()).isNull();
}
@Test
public void coerceEpochSecond() {
GitProperties properties = new GitProperties(
createProperties("master", "abcdefg", "1457527123"));
assertThat(properties.getCommitTime()).isEqualTo("1457527123000");
}
@Test
public void coerceDateString() {
GitProperties properties = new GitProperties(
createProperties("master", "abcdefg", "2016-03-04T14:36:33+0100"));
assertThat(properties.getCommitTime()).isEqualTo("1457098593000");
}
@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");
}
@Test
public void shortenCommitId() {
GitProperties properties = new GitProperties(
createProperties("master", "abc", "1457527123"));
assertThat(properties.getCommitId()).isEqualTo("abc");
assertThat(properties.getShortCommitId()).isEqualTo("abc");
}
private static Properties createProperties(String branch, String commitId, String commitTime) {
Properties properties = new Properties();
properties.put("branch", branch);
properties.put("commit.id", commitId);
properties.put("commit.time", commitTime);
return properties;
}
}
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.info;
import java.util.Properties;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.core.env.PropertySource;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link InfoProperties}.
*
* @author Stephane Nicoll
*/
public class InfoPropertiesTests {
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Test
public void inputIsImmutable() {
Properties p = new Properties();
p.put("foo", "bar");
InfoProperties infoProperties = new InfoProperties(p);
assertThat(infoProperties.get("foo")).isEqualTo("bar");
p.remove("foo");
assertThat(infoProperties.get("foo")).isEqualTo("bar");
}
@Test
public void iterator() {
Properties p = new Properties();
p.put("one", "first");
p.put("two", "second");
InfoProperties infoProperties = new InfoProperties(p);
Properties copy = new Properties();
for (InfoProperties.Entry entry : infoProperties) {
copy.put(entry.getKey(), entry.getValue());
}
assertThat(p).isEqualTo(copy);
}
@Test
public void removeNotSupported() {
Properties p = new Properties();
p.put("foo", "bar");
InfoProperties infoProperties = new InfoProperties(p);
this.thrown.expect(UnsupportedOperationException.class);
infoProperties.iterator().remove();
}
@Test
public void toPropertySources() {
Properties p = new Properties();
p.put("one", "first");
p.put("two", "second");
InfoProperties infoProperties = new MyInfoProperties(p);
PropertySource<?> source = infoProperties.toPropertySource();
assertThat(source.getProperty("one")).isEqualTo("first");
assertThat(source.getProperty("two")).isEqualTo("second");
assertThat(source.getName()).isEqualTo("MyInfoProperties");
}
private static class MyInfoProperties extends InfoProperties {
MyInfoProperties(Properties entries) {
super(entries);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment