Skip to content

Commit

Permalink
Merge pull request #10 from Piszmog/develop
Browse files Browse the repository at this point in the history
Update Spring Versions
  • Loading branch information
Piszmog authored Dec 17, 2019
2 parents ceb96b6 + 8051e3f commit 8010657
Show file tree
Hide file tree
Showing 14 changed files with 370 additions and 456 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@
## Description
Spring Auto-configuration library for [Cloud Config Client](https://github.com/Piszmog/cloud-config-client).

__Cloud Config Client AutoConfig 3.x__ is compatible with

| Dependency | Version |
| :---: | :---: |
| Spring Boot | 2.2.x |
| Spring Cloud Services | 3.x.x.RELEASE |

__Cloud Config Client AutoConfig 2.x__ is compatible with

| Dependency | Version |
| :---: | :---: |
| Spring Boot | 2.x.x |
| Spring Boot | 2.1.x |
| Spring Cloud Services | 2.x.x.RELEASE |

__Cloud Config Client AutoConfig 1.x__ is compatible with
Expand Down
24 changes: 16 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<groupId>io.github.piszmog</groupId>
<artifactId>cloud-config-client-autoconfig</artifactId>
<version>2.4</version>
<version>3.0-SNAPSHOT</version>
<packaging>jar</packaging>

<!--====================================-->
Expand All @@ -30,22 +30,22 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!--Plugins-->
<gmavenplus-plugin.version>1.7.1</gmavenplus-plugin.version>
<gmavenplus-plugin.version>1.8.1</gmavenplus-plugin.version>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<maven-plugin.version>3.1.0</maven-plugin.version>
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
<!--Profiles-->
<maven-gpg-plugin.version>1.6</maven-gpg-plugin.version>
<nexus-staging-maven-plugin.version>1.6.8</nexus-staging-maven-plugin.version>
<!--Dependency Management-->
<spring-cloud-services-dependencies.version>2.1.4.RELEASE</spring-cloud-services-dependencies.version>
<spring-boot-dependencies.version>2.1.6.RELEASE</spring-boot-dependencies.version>
<spring-cloud-services-dependencies.version>3.1.0.RELEASE</spring-cloud-services-dependencies.version>
<spring-boot-dependencies.version>2.2.2.RELEASE</spring-boot-dependencies.version>
<!--Dependency-->
<cloud-config-client.version>2.5</cloud-config-client.version>
<cloud-config-client.version>3.0-SNAPSHOT</cloud-config-client.version>
<!--Tests-->
<cglib.version>3.2.12</cglib.version>
<groovy-all.version>2.5.7</groovy-all.version>
<objenesis.version>3.0.1</objenesis.version>
<cglib.version>3.3.0</cglib.version>
<groovy-all.version>2.5.8</groovy-all.version>
<objenesis.version>3.1</objenesis.version>
<spock-core.version>1.3-groovy-2.5</spock-core.version>
</properties>

Expand Down Expand Up @@ -77,6 +77,11 @@
<artifactId>spring-cloud-services-starter-config-client</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.pivotal.spring.cloud</groupId>
<artifactId>spring-cloud-services-config-client-autoconfigure</artifactId>
<scope>provided</scope>
</dependency>
<!-- Tests -->
<dependency>
<groupId>org.spockframework</groupId>
Expand Down Expand Up @@ -172,6 +177,9 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven-plugin.version}</version>
<configuration>
<source>1.8</source>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
* Created by Piszmog on 5/5/2018
*/
@Configuration
@ConditionalOnBean( ConfigTemplate.class )
public class ConfigClientAutoConfiguration
{
@ConditionalOnBean(ConfigTemplate.class)
public class ConfigClientAutoConfiguration {
// ============================================================
// Beans:
// ============================================================
Expand All @@ -33,10 +32,9 @@ public class ConfigClientAutoConfiguration
* @return The decryption client.
*/
@Bean
@ConditionalOnProperty( prefix = "cloud.config.client", name = "decrypt.enabled", matchIfMissing = true )
public DecryptConfigClient decryptConfigClient( final ConfigTemplate configTemplate )
{
return new DecryptConfigClient( configTemplate );
@ConditionalOnProperty(prefix = "cloud.config.client", name = "decrypt.enabled", matchIfMissing = true)
public DecryptConfigClient decryptConfigClient(final ConfigTemplate configTemplate) {
return new DecryptConfigClient(configTemplate);
}

/**
Expand All @@ -46,10 +44,9 @@ public DecryptConfigClient decryptConfigClient( final ConfigTemplate configTempl
* @return The encryption client.
*/
@Bean
@ConditionalOnProperty( prefix = "cloud.config.client", name = "encrypt.enabled", matchIfMissing = true )
public EncryptConfigClient encryptConfigClient( final ConfigTemplate configTemplate )
{
return new EncryptConfigClient( configTemplate );
@ConditionalOnProperty(prefix = "cloud.config.client", name = "encrypt.enabled", matchIfMissing = true)
public EncryptConfigClient encryptConfigClient(final ConfigTemplate configTemplate) {
return new EncryptConfigClient(configTemplate);
}

/**
Expand All @@ -59,9 +56,8 @@ public EncryptConfigClient encryptConfigClient( final ConfigTemplate configTempl
* @return The public key client.
*/
@Bean
@ConditionalOnProperty( prefix = "cloud.config.client", name = "publickey.enabled", matchIfMissing = true )
public PublicKeyClient publicKeyClient( final ConfigTemplate configTemplate )
{
return new PublicKeyClient( configTemplate );
@ConditionalOnProperty(prefix = "cloud.config.client", name = "publickey.enabled", matchIfMissing = true)
public PublicKeyClient publicKeyClient(final ConfigTemplate configTemplate) {
return new PublicKeyClient(configTemplate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,28 @@
* <p>
* Created by Piszmog on 7/21/2018
*/
public class ConfigPropertySourceLocator
{
private static final transient Log logger = LogFactory.getLog( ConfigPropertySourceLocator.class );
public class ConfigPropertySourceLocator {
private static final transient Log logger = LogFactory.getLog(ConfigPropertySourceLocator.class);

// ============================================================
// Class Constants:
// ============================================================

private static final String PROPERTY_SOURCE_NAME = "jsonResource";
private static final String THREAD_NAME = "ConfigLocator";
private static final ObjectMapper YAML_MAPPER = new ObjectMapper( new YAMLFactory() );
private static final ObjectMapper YAML_MAPPER = new ObjectMapper(new YAMLFactory());
private static final JavaPropsMapper PROPERTIES_MAPPER = new JavaPropsMapper();

// ============================================================
// Class Attributes:
// ============================================================

private final FileConfigClient fileConfigClient;
private final ExecutorService executorService = Executors.newFixedThreadPool( 10,
new BasicThreadFactory.Builder().namingPattern( THREAD_NAME + "-%d" )
.daemon( true )
.priority( Thread.MAX_PRIORITY )
.build() );
private final ExecutorService executorService = Executors.newFixedThreadPool(10,
new BasicThreadFactory.Builder().namingPattern(THREAD_NAME + "-%d")
.daemon(true)
.priority(Thread.MAX_PRIORITY)
.build());

// ============================================================
// Constructors:
Expand All @@ -64,8 +63,7 @@ public class ConfigPropertySourceLocator
*
* @param fileConfigClient the file config client to load files from the config server.
*/
public ConfigPropertySourceLocator( final FileConfigClient fileConfigClient )
{
public ConfigPropertySourceLocator(final FileConfigClient fileConfigClient) {
this.fileConfigClient = fileConfigClient;
}

Expand All @@ -79,31 +77,25 @@ public ConfigPropertySourceLocator( final FileConfigClient fileConfigClient )
* @param resources the resources to load
* @return The property source with the JSON resources loaded.
*/
public PropertySource<?> locateConfigResources( final List<Resource> resources )
{
CompositePropertySource composite = new CompositePropertySource( PROPERTY_SOURCE_NAME );
public PropertySource<?> locateConfigResources(final List<Resource> resources) {
CompositePropertySource composite = new CompositePropertySource(PROPERTY_SOURCE_NAME);
//
// Load files asynchronously
//
List<CompletableFuture<Void>> futures = new ArrayList<>();
resources.stream().filter( Objects::nonNull ).map( resource ->
getJsonConfiguration( composite, resource.getDirectory(), resource.getFiles() ) ).forEach( futures::addAll );
final CompletableFuture<List<Void>> listCompletableFuture = CompletableFuture.allOf( futures.toArray( new CompletableFuture[ 0 ] ) )
.thenApply( future -> futures.stream().map( CompletableFuture::join ).collect( Collectors.toList() ) );
try
{
resources.stream().filter(Objects::nonNull).map(resource ->
getJsonConfiguration(composite, resource.getDirectory(), resource.getFiles())).forEach(futures::addAll);
final CompletableFuture<List<Void>> listCompletableFuture = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
.thenApply(future -> futures.stream().map(CompletableFuture::join).collect(Collectors.toList()));
try {
//
// Wait for files to complete -- don't want Spring to start setting up Beans without all property sources
//
listCompletableFuture.get();
logger.info( "Successfully loaded all external configuration files." );
}
catch ( InterruptedException | ExecutionException e )
{
throw new ConfigResourceException( "Failed to retrieve files.", e );
}
finally
{
logger.info("Successfully loaded all external configuration files.");
} catch (InterruptedException | ExecutionException e) {
throw new ConfigResourceException("Failed to retrieve files.", e);
} finally {
executorService.shutdownNow();
}
return composite;
Expand All @@ -113,101 +105,76 @@ public PropertySource<?> locateConfigResources( final List<Resource> resources )
// Private Methods:
// ============================================================

private List<CompletableFuture<Void>> getJsonConfiguration( final CompositePropertySource composite,
final String directoryPath,
final List<String> files )
{
return files.stream().filter( Objects::nonNull ).map( file -> CompletableFuture.supplyAsync( () ->
loadConfigurationFile( directoryPath, file ), executorService ).thenAccept( mapPropertySource -> {
if ( mapPropertySource != null )
{
composite.addPropertySource( mapPropertySource );
private List<CompletableFuture<Void>> getJsonConfiguration(final CompositePropertySource composite,
final String directoryPath,
final List<String> files) {
return files.stream().filter(Objects::nonNull).map(file -> CompletableFuture.supplyAsync(() ->
loadConfigurationFile(directoryPath, file), executorService).thenAccept(mapPropertySource -> {
if (mapPropertySource != null) {
composite.addPropertySource(mapPropertySource);
}
} ) ).collect( Collectors.toList() );
})).collect(Collectors.toList());
}

@SuppressWarnings( "unchecked" )
private MapPropertySource loadConfigurationFile( final String directoryPath, final String fileName )
{
final String filePath = getFilePath( directoryPath, fileName );
@SuppressWarnings("unchecked")
private MapPropertySource loadConfigurationFile(final String directoryPath, final String fileName) {
final String filePath = getFilePath(directoryPath, fileName);
Map file;
try
{
logger.info( "Loading configuration " + filePath + "..." );
if ( StringUtils.endsWithIgnoreCase( fileName, ".yml" ) || StringUtils.endsWithIgnoreCase( fileName, ".yaml" ) )
{
file = getYAMLFile( directoryPath, fileName );
}
else if ( StringUtils.endsWithIgnoreCase( fileName, ".properties" ) )
{
file = getPropertiesFile( directoryPath, fileName );
}
else
{
file = getJSONFile( directoryPath, fileName );
try {
logger.info("Loading configuration " + filePath + "...");
if (StringUtils.endsWithIgnoreCase(fileName, ".yml") || StringUtils.endsWithIgnoreCase(fileName, ".yaml")) {
file = getYAMLFile(directoryPath, fileName);
} else if (StringUtils.endsWithIgnoreCase(fileName, ".properties")) {
file = getPropertiesFile(directoryPath, fileName);
} else {
file = getJSONFile(directoryPath, fileName);
}
} catch (ConfigException e) {
throw new ConfigResourceException("Failed to load " + filePath, e);
}
catch ( ConfigException e )
{
throw new ConfigResourceException( "Failed to load " + filePath, e );
}
if ( !file.isEmpty() )
{
final Map flattenMap = MapFlattener.flatten( file );
return new MapPropertySource( filePath, flattenMap );
if (!file.isEmpty()) {
final Map flattenMap = MapFlattener.flatten(file);
return new MapPropertySource(filePath, flattenMap);
}
return null;
}

private String getFilePath( final String directoryPath, final String fileName )
{
private String getFilePath(final String directoryPath, final String fileName) {
return directoryPath + "/" + fileName;
}

private Map getYAMLFile( final String directoryPath, final String fileName ) throws ConfigException
{
private Map getYAMLFile(final String directoryPath, final String fileName) throws ConfigException {
final Map file;
final byte[] yamlFile = fileConfigClient.getFileFromDefaultBranch( fileName, directoryPath, byte[].class );
if ( yamlFile == null )
{
throw new ConfigResourceException( "Failed to find file " + fileName + " from the Config Server. Ensure the file exists." );
}
try
{
file = YAML_MAPPER.readValue( yamlFile, Map.class );
final byte[] yamlFile = fileConfigClient.getFileFromDefaultBranch(fileName, directoryPath, byte[].class);
if (yamlFile == null) {
throw new ConfigResourceException("Failed to find file " + fileName + " from the Config Server. Ensure the file exists.");
}
catch ( IOException e )
{
throw new ConfigResourceException( "Failed to convert " + fileName + " from YAML format to be loaded in the property sources.", e );
try {
file = YAML_MAPPER.readValue(yamlFile, Map.class);
} catch (IOException e) {
throw new ConfigResourceException("Failed to convert " + fileName + " from YAML format to be loaded in the property sources.", e);
}
return file;
}

private Map getPropertiesFile( final String directoryPath, final String fileName ) throws ConfigException
{
private Map getPropertiesFile(final String directoryPath, final String fileName) throws ConfigException {
final Map file;
final byte[] propertiesFile = fileConfigClient.getFileFromDefaultBranch( fileName, directoryPath, byte[].class );
if ( propertiesFile == null )
{
throw new ConfigResourceException( "Failed to find file " + fileName + " from the Config Server. Ensure the file exists." );
}
try
{
file = PROPERTIES_MAPPER.readValue( propertiesFile, Map.class );
final byte[] propertiesFile = fileConfigClient.getFileFromDefaultBranch(fileName, directoryPath, byte[].class);
if (propertiesFile == null) {
throw new ConfigResourceException("Failed to find file " + fileName + " from the Config Server. Ensure the file exists.");
}
catch ( IOException e )
{
throw new ConfigResourceException( "Failed to convert " + fileName + " from PROPERTIES format to be loaded in the property sources.", e );
try {
file = PROPERTIES_MAPPER.readValue(propertiesFile, Map.class);
} catch (IOException e) {
throw new ConfigResourceException("Failed to convert " + fileName + " from PROPERTIES format to be loaded in the property sources.", e);
}
return file;
}

private Map getJSONFile( final String directoryPath, final String fileName ) throws ConfigException
{
final Map jsonFile = fileConfigClient.getFileFromDefaultBranch( fileName, directoryPath, Map.class );
if ( jsonFile == null )
{
throw new ConfigResourceException( "Failed to find file " + fileName + " from the Config Server. Ensure the file exists." );
private Map getJSONFile(final String directoryPath, final String fileName) throws ConfigException {
final Map jsonFile = fileConfigClient.getFileFromDefaultBranch(fileName, directoryPath, Map.class);
if (jsonFile == null) {
throw new ConfigResourceException("Failed to find file " + fileName + " from the Config Server. Ensure the file exists.");
}
return jsonFile;
}
Expand Down
Loading

0 comments on commit 8010657

Please sign in to comment.