@ -16,8 +16,14 @@
package org.springframework.boot.autoconfigure.info ;
package org.springframework.boot.autoconfigure.info ;
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 ;
@ -29,11 +35,18 @@ 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.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 .
@ -45,24 +58,38 @@ import org.springframework.core.type.AnnotatedTypeMetadata;
@EnableConfigurationProperties ( ProjectInfoProperties . class )
@EnableConfigurationProperties ( ProjectInfoProperties . class )
public class ProjectInfoAutoConfiguration {
public class ProjectInfoAutoConfiguration {
@Configuration
@Autowired
private ProjectInfoProperties properties ;
private final ConversionService conversionService = createConversionService ( ) ;
@Conditional ( GitResourceAvailableCondition . class )
@Conditional ( GitResourceAvailableCondition . class )
protected static class GitInfoAutoConfiguration {
@ConditionalOnMissingBean
@Bean
@ConditionalOnMissingBean
public GitInfo gitInfo ( ) throws Exception {
@Bean
GitInfo gitInfo = new GitInfo ( ) ;
public GitInfo gitInfo ( ProjectInfoProperties properties ) throws Exception {
bindPropertiesTo ( gitInfo , this . properties . getGit ( ) . getLocation ( ) , "git" ) ;
PropertiesConfigurationFactory < GitInfo > factory = new PropertiesConfigurationFactory < GitInfo > (
return gitInfo ;
new GitInfo ( ) ) ;
}
factory . setTargetName ( "git" ) ;
Properties gitInfoProperties = PropertiesLoaderUtils
protected void bindPropertiesTo ( Object target , Resource location , String prefix )
. loadProperties ( properties . getGit ( ) . getLocation ( ) ) ;
throws BindException , IOException {
factory . setProperties ( gitInfoProperties ) ;
PropertiesConfigurationFactory < Object > factory =
return factory . getObject ( ) ;
new PropertiesConfigurationFactory < Object > ( target ) ;
}
factory . setConversionService ( this . conversionService ) ;
factory . setTargetName ( prefix ) ;
Properties gitInfoProperties = PropertiesLoaderUtils . loadProperties ( location ) ;
factory . setProperties ( gitInfoProperties ) ;
factory . bindPropertiesToTarget ( ) ;
}
private static ConversionService createConversionService ( ) {
DefaultConversionService conversionService = new DefaultConversionService ( ) ;
conversionService . addConverter ( new StringToDateConverter ( ) ) ;
return conversionService ;
}
}
static class GitResourceAvailableCondition extends SpringBootCondition {
static class GitResourceAvailableCondition extends SpringBootCondition {
private final ResourceLoader defaultResourceLoader = new DefaultResourceLoader ( ) ;
private final ResourceLoader defaultResourceLoader = new DefaultResourceLoader ( ) ;
@ -90,4 +117,38 @@ 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 e ) {
return null ;
}
}
}
}
}