XD-3355, XD-3582: Support options metadata

This commit is contained in:
Eric Bottard
2015-09-15 09:34:45 -04:00
committed by Patrick Peralta
parent 04b0ad7dbf
commit 9b1e418411
8 changed files with 236 additions and 30 deletions

View File

@@ -33,6 +33,7 @@
<module>spring-cloud-stream-module-launcher</module>
<module>docs</module>
<module>spring-cloud-stream-test-support</module>
<module>spring-cloud-stream-configuration-metadata</module>
</modules>
<dependencyManagement>
<dependencies>

View File

@@ -0,0 +1,26 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-parent</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-cloud-stream-configuration-metadata</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-module-launcher</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-metadata</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,116 @@
/*
* 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.cloud.stream.configuration.metadata;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.configurationmetadata.ConfigurationMetadataProperty;
import org.springframework.boot.configurationmetadata.ConfigurationMetadataRepositoryJsonBuilder;
import org.springframework.boot.loader.archive.Archive;
import org.springframework.boot.loader.archive.ExplodedArchive;
import org.springframework.boot.loader.archive.JarFileArchive;
import org.springframework.boot.loader.jar.JarFile;
import org.springframework.cloud.stream.module.launcher.ModuleJarLauncher;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
/**
* Used to retrieve metadata about the configuration properties that can alter a module behavior.
*
* @author Eric Bottard
*/
public class ModuleConfigurationMetadataResolver {
public ModuleConfigurationMetadataResolver() {
JarFile.registerUrlProtocolHandler();
}
/**
* Return metadata about configuration properties that are documented via
* <a href="http://docs.spring.io/spring-boot/docs/current/reference/html/configuration-metadata.html">
* Spring Boot configuration metadata</a> and visible in a module.
*
* @param module a Spring Cloud Stream module; typically a Boot uberjar,
* but directories are supported as well
*/
public List<ConfigurationMetadataProperty> listProperties(Resource module) {
List<ConfigurationMetadataProperty> result = new ArrayList<>();
ClassLoader classLoader = null;
try {
File moduleFile = module.getFile();
Archive archive = moduleFile.isDirectory() ? new ExplodedArchive(moduleFile) : new JarFileArchive(moduleFile);
classLoader = createClassLoader(archive);
ConfigurationMetadataRepositoryJsonBuilder builder = ConfigurationMetadataRepositoryJsonBuilder.create();
ResourcePatternResolver moduleResourceLoader = new PathMatchingResourcePatternResolver(classLoader);
for (Resource r : moduleResourceLoader.getResources("classpath*:/META-INF/*spring-configuration-metadata.json")) {
builder.withJsonResource(r.getInputStream());
}
for (ConfigurationMetadataProperty property : builder.build().getAllProperties().values()) {
result.add(property);
}
}
catch (Exception e) {
throw new RuntimeException("Exception trying to list configuration properties for module " + module, e);
}
finally {
if (classLoader instanceof Closeable) {
try {
((Closeable) classLoader).close();
}
catch (IOException e) {
// ignore
}
}
}
return result;
}
/**
* Return a {@link ClassLoader} for accessing resources in the provided
* {@link Archive}. The caller is responsible for disposing of the
* class loader.
*
* @param archive the archive for which to return a class loader
* @return class loader for the given archive
* @throws Exception if the class loader cannot be created
*/
protected ClassLoader createClassLoader(Archive archive) throws Exception {
return new ClassLoaderExposingJarLauncher(archive).createClassLoader();
}
/**
* Extension of {@link ModuleJarLauncher} used for exposing a {@link ClassLoader}
* for the provided {@link Archive}.
*/
private static class ClassLoaderExposingJarLauncher extends ModuleJarLauncher {
public ClassLoaderExposingJarLauncher(Archive archive) {
super(archive);
}
protected ClassLoader createClassLoader() throws Exception {
List<Archive> classPathArchives = getClassPathArchives();
return createClassLoader(classPathArchives);
}
}
}

View File

@@ -0,0 +1,36 @@
/*
* 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.cloud.stream.configuration.metadata;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;
/**
* Automatically exposes a ModuleConfigurationMetadataResolver if none is already registered.
*
* @author Eric Bottard
*/
@Configuration
public class ModuleConfigurationMetadataResolverAutoConfiguration {
@Bean
@ConditionalOnMissingBean(ModuleConfigurationMetadataResolver.class)
public ModuleConfigurationMetadataResolver metadataResolver() {
return new ModuleConfigurationMetadataResolver();
}
}

View File

@@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration:\
org.springframework.cloud.stream.configuration.metadata.ModuleConfigurationMetadataResolverAutoConfiguration

View File

@@ -29,8 +29,8 @@ import org.springframework.util.ReflectionUtils;
/**
* A {@link JarLauncher} that supports launching module archive.
*
* Also, it restricts the classloader of the launched application to the contents of the uber-jar and the
* extension classloader of the JVM.
* Also, it restricts the classloader of the launched application to the contents
* of the uber-jar and the extension classloader of the JVM.
*
* @author Mark Fisher
* @author Marius Bogoevici

View File

@@ -16,17 +16,11 @@
package org.springframework.cloud.stream.module.launcher;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.stream.module.resolver.AetherModuleResolver;
import org.springframework.cloud.stream.module.resolver.ModuleResolver;
import org.springframework.cloud.stream.module.resolver.ModuleResolverProperties;
import org.springframework.cloud.stream.module.resolver.ModuleResolverConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* Configuration class that has the beans required for module launcher.
@@ -36,28 +30,9 @@ import org.springframework.context.annotation.Configuration;
* @author Eric Bottard
*/
@Configuration
@EnableConfigurationProperties(ModuleResolverProperties.class)
@Import(ModuleResolverConfiguration.class)
public class ModuleLauncherConfiguration {
@Autowired
private ModuleResolverProperties properties;
/**
* Sets up the default Aether-based module resolver, unless overridden.
*/
@Bean
@ConditionalOnMissingBean(ModuleResolver.class)
public ModuleResolver moduleResolver() {
int i = 1;
Map<String, String> repositoriesMap = new HashMap<>();
for (String repository: properties.getRemoteRepositories()) {
repositoriesMap.put("repository " + i++, repository);
}
AetherModuleResolver aetherModuleResolver = new AetherModuleResolver(properties.getLocalRepository(), repositoriesMap);
aetherModuleResolver.setOffline(properties.isOffline());
return aetherModuleResolver;
}
@Bean
public ModuleLauncher moduleLauncher(ModuleResolver moduleResolver) {
return new ModuleLauncher(moduleResolver);

View File

@@ -0,0 +1,50 @@
/*
* 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.cloud.stream.module.resolver;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
/**
* Sets up the default Aether-based module resolver, unless overridden.
*
* @author Eric Bottard
*/
@EnableConfigurationProperties(ModuleResolverProperties.class)
public class ModuleResolverConfiguration {
@Autowired
private ModuleResolverProperties properties;
@Bean
@ConditionalOnMissingBean(ModuleResolver.class)
public ModuleResolver moduleResolver() {
int i = 1;
Map<String, String> repositoriesMap = new HashMap<>();
for (String repository: properties.getRemoteRepositories()) {
repositoriesMap.put("repository " + i++, repository);
}
AetherModuleResolver aetherModuleResolver = new AetherModuleResolver(properties.getLocalRepository(), repositoriesMap);
aetherModuleResolver.setOffline(properties.isOffline());
return aetherModuleResolver;
}
}