This repository was archived by the owner on Apr 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 288
XD-3251: Add a boot @ConfigurationProperties compatible MOMResolver. #1747
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...rc/main/java/org/springframework/xd/module/options/BootModuleOptionsMetadataResolver.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /* | ||
| * Copyright 2015 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.xd.module.options; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Map; | ||
|
|
||
| import org.springframework.boot.configurationmetadata.ConfigurationMetadataProperty; | ||
| import org.springframework.boot.configurationmetadata.ConfigurationMetadataRepository; | ||
| import org.springframework.boot.configurationmetadata.ConfigurationMetadataRepositoryJsonBuilder; | ||
| import org.springframework.core.io.Resource; | ||
| import org.springframework.core.io.support.PathMatchingResourcePatternResolver; | ||
| import org.springframework.core.io.support.ResourcePatternResolver; | ||
| import org.springframework.xd.module.ModuleDefinition; | ||
| import org.springframework.xd.module.SimpleModuleDefinition; | ||
|
|
||
| /** | ||
| * A ModuleOptionsMetadataResolver that knows how to read Spring Boot metadata for configuration properties | ||
| * and exposes some of the properties as module options. | ||
| * | ||
| * @author Eric Bottard | ||
| */ | ||
| public class BootModuleOptionsMetadataResolver implements ModuleOptionsMetadataResolver { | ||
|
|
||
|
|
||
| private ResourcePatternResolver resourceLoader = new PathMatchingResourcePatternResolver(); | ||
|
|
||
| /** | ||
| * The prefix with which configuration options must start to be considered module options. | ||
| * | ||
| * <p>The prefix-less form will be used for module options, while at runtime, | ||
| * the module may expect the long form.</p> | ||
| */ | ||
| private String prefix = "module."; | ||
|
|
||
|
|
||
| @Override | ||
| public ModuleOptionsMetadata resolve(ModuleDefinition moduleDefinition) { | ||
| if (moduleDefinition.isComposed()) { | ||
| return null; | ||
| } | ||
| SimpleModuleDefinition simpleModuleDefinition = (SimpleModuleDefinition) moduleDefinition; | ||
| Resource resource = resourceLoader.getResource(simpleModuleDefinition.getLocation()); | ||
| ClassLoader moduleClassLoader = ModuleUtils.createModuleDiscoveryClassLoader(resource, BootModuleOptionsMetadataResolver.class.getClassLoader()); | ||
|
|
||
| ResourcePatternResolver moduleResourceLoader = new PathMatchingResourcePatternResolver(moduleClassLoader); | ||
|
|
||
| ConfigurationMetadataRepositoryJsonBuilder builder = ConfigurationMetadataRepositoryJsonBuilder.create(); | ||
|
|
||
| try { | ||
| for (Resource r : moduleResourceLoader.getResources("classpath*:/META-INF/*spring-configuration-metadata.json")) { | ||
| builder.withJsonResource(r.getInputStream()); | ||
| } | ||
| } | ||
| catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| ModuleUtils.closeClassLoader(moduleClassLoader); | ||
|
|
||
| ConfigurationMetadataRepository repo = builder.build(); | ||
| SimpleModuleOptionsMetadata result = new SimpleModuleOptionsMetadata(); | ||
|
|
||
| for (Map.Entry<String, ConfigurationMetadataProperty> kv : repo.getAllProperties().entrySet()) { | ||
| String key = kv.getKey(); | ||
| if (!key.startsWith(prefix)) { | ||
| continue; | ||
| } | ||
| ConfigurationMetadataProperty value = kv.getValue(); | ||
| ModuleOption mo = new ModuleOption(key.substring(prefix.length()), value.getDescription()) | ||
| .withDefaultValue(value.getDefaultValue()) | ||
| .withType(value.getType()); | ||
| result.add(mo); | ||
|
|
||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting technique 😄
A couple of thoughts...
TPTSThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 to Gary's comments, and...
I'm not a fan of the word "framework" here.
Also the thread prefix should be set.