Add AutoConfigurationMetadata abstraction
Add AutoConfigurationMetadata interface and a an internal loader that allows easy access to data written by `spring-boot-configure-processor`. See gh-7573pull/8038/merge
parent
ca435512c0
commit
1cbda9bd60
@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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.autoconfigure;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Provides access to meta-data written by the auto-configure annotation processor.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 1.5.0
|
||||
*/
|
||||
public interface AutoConfigurationMetadata {
|
||||
|
||||
/**
|
||||
* Return {@code true} if the specified class name was processed by the annotation
|
||||
* processor.
|
||||
* @param className the source class
|
||||
* @return if the class was processed
|
||||
*/
|
||||
boolean wasProcessed(String className);
|
||||
|
||||
/**
|
||||
* Get an {@link Integer} value from the meta-data.
|
||||
* @param className the source class
|
||||
* @param key the meta-data key
|
||||
* @return the meta-data value or {@code null}
|
||||
*/
|
||||
Integer getInteger(String className, String key);
|
||||
|
||||
/**
|
||||
* Get an {@link Integer} value from the meta-data.
|
||||
* @param className the source class
|
||||
* @param key the meta-data key
|
||||
* @param defaultValue the default value
|
||||
* @return the meta-data value or {@code defaultValue}
|
||||
*/
|
||||
Integer getInteger(String className, String key, Integer defaultValue);
|
||||
|
||||
/**
|
||||
* Get a {@link Set} value from the meta-data.
|
||||
* @param className the source class
|
||||
* @param key the meta-data key
|
||||
* @return the meta-data value or {@code null}
|
||||
*/
|
||||
Set<String> getSet(String className, String key);
|
||||
|
||||
/**
|
||||
* Get a {@link Set} value from the meta-data.
|
||||
* @param className the source class
|
||||
* @param key the meta-data key
|
||||
* @param defaultValue the default value
|
||||
* @return the meta-data value or {@code defaultValue}
|
||||
*/
|
||||
Set<String> getSet(String className, String key, Set<String> defaultValue);
|
||||
|
||||
/**
|
||||
* Get an {@link String} value from the meta-data.
|
||||
* @param className the source class
|
||||
* @param key the meta-data key
|
||||
* @return the meta-data value or {@code null}
|
||||
*/
|
||||
String get(String className, String key);
|
||||
|
||||
/**
|
||||
* Get an {@link String} value from the meta-data.
|
||||
* @param className the source class
|
||||
* @param key the meta-data key
|
||||
* @param defaultValue the default value
|
||||
* @return the meta-data value or {@code defaultValue}
|
||||
*/
|
||||
String get(String className, String key, String defaultValue);
|
||||
|
||||
}
|
@ -0,0 +1,121 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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.autoconfigure;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.core.io.support.PropertiesLoaderUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Internal utility used to load {@link AutoConfigurationMetadata}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
final class AutoConfigurationMetadataLoader {
|
||||
|
||||
protected static final String PATH = "META-INF/"
|
||||
+ "spring-autoconfigure-metadata.properties";
|
||||
|
||||
private AutoConfigurationMetadataLoader() {
|
||||
}
|
||||
|
||||
public static AutoConfigurationMetadata loadMetadata(ClassLoader classLoader) {
|
||||
return loadMetadata(classLoader, PATH);
|
||||
}
|
||||
|
||||
static AutoConfigurationMetadata loadMetadata(ClassLoader classLoader, String path) {
|
||||
try {
|
||||
Enumeration<URL> urls = (classLoader != null ? classLoader.getResources(path)
|
||||
: ClassLoader.getSystemResources(path));
|
||||
Properties properties = new Properties();
|
||||
while (urls.hasMoreElements()) {
|
||||
properties.putAll(PropertiesLoaderUtils
|
||||
.loadProperties(new UrlResource(urls.nextElement())));
|
||||
}
|
||||
return loadMetadata(properties);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new IllegalArgumentException(
|
||||
"Unable to load @ConditionalOnClass location [" + path + "]", ex);
|
||||
}
|
||||
}
|
||||
|
||||
static AutoConfigurationMetadata loadMetadata(Properties properties) {
|
||||
return new PropertiesAutoConfigurationMetadata(properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link AutoConfigurationMetadata} implementation backed by a properties file.
|
||||
*/
|
||||
private static class PropertiesAutoConfigurationMetadata
|
||||
implements AutoConfigurationMetadata {
|
||||
|
||||
private final Properties properties;
|
||||
|
||||
PropertiesAutoConfigurationMetadata(Properties properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean wasProcessed(String className) {
|
||||
return this.properties.containsKey(className);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getInteger(String className, String key) {
|
||||
return getInteger(className, key, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getInteger(String className, String key, Integer defaultValue) {
|
||||
String value = get(className, key);
|
||||
return (value != null ? Integer.valueOf(value) : defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getSet(String className, String key) {
|
||||
return getSet(className, key, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getSet(String className, String key,
|
||||
Set<String> defaultValue) {
|
||||
String value = get(className, key);
|
||||
return (value != null ? StringUtils.commaDelimitedListToSet(value)
|
||||
: defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String get(String className, String key) {
|
||||
return get(className, key, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String get(String className, String key, String defaultValue) {
|
||||
String value = this.properties.getProperty(className + "." + key);
|
||||
return (value != null ? value : defaultValue);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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.autoconfigure;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link AutoConfigurationMetadataLoader}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class AutoConfigurationMetadataLoaderTests {
|
||||
|
||||
@Test
|
||||
public void loadShouldLoadProperties() throws Exception {
|
||||
assertThat(load()).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wasProcessedWhenProcessedShouldReturnTrue() throws Exception {
|
||||
assertThat(load().wasProcessed("test")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wasProcessedWhenNotProcessedShouldReturnFalse() throws Exception {
|
||||
assertThat(load().wasProcessed("testx")).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getIntegerShouldReturnValue() throws Exception {
|
||||
assertThat(load().getInteger("test", "int")).isEqualTo(123);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getIntegerWhenMissingShouldReturnNull() throws Exception {
|
||||
assertThat(load().getInteger("test", "intx")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getIntegerWithDefaultWhenMissingShouldReturnDefault() throws Exception {
|
||||
assertThat(load().getInteger("test", "intx", 345)).isEqualTo(345);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSetShouldReturnValue() throws Exception {
|
||||
assertThat(load().getSet("test", "set")).containsExactly("a", "b", "c");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSetWhenMissingShouldReturnNull() throws Exception {
|
||||
assertThat(load().getSet("test", "setx")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSetWithDefaultWhenMissingShouldReturnDefault() throws Exception {
|
||||
assertThat(load().getSet("test", "setx", Collections.singleton("x")))
|
||||
.containsExactly("x");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getShouldReturnValue() throws Exception {
|
||||
assertThat(load().get("test", "string")).isEqualTo("abc");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getWhenMissingShouldReturnNull() throws Exception {
|
||||
assertThat(load().get("test", "stringx")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getWithDefaultWhenMissingShouldReturnDefault() throws Exception {
|
||||
assertThat(load().get("test", "stringx", "xyz")).isEqualTo("xyz");
|
||||
}
|
||||
|
||||
private AutoConfigurationMetadata load() {
|
||||
return AutoConfigurationMetadataLoader.loadMetadata(null,
|
||||
"META-INF/AutoConfigurationMetadataLoaderTests.properties");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
test=
|
||||
test.string=abc
|
||||
test.int=123
|
||||
test.set=a,b,b,c
|
Loading…
Reference in New Issue