This commit is contained in:
Phillip Webb
2017-08-28 15:29:36 -07:00
parent b02edd2e81
commit 2c97d3a5e9
143 changed files with 689 additions and 655 deletions

View File

@@ -19,12 +19,13 @@ package org.springframework.boot.configurationprocessor;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
@@ -148,7 +149,6 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
processEndpoint(element);
}
}
if (roundEnv.processingOver()) {
try {
writeMetaData();
@@ -372,27 +372,30 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
enabledByDefault = Boolean.TRUE;
}
String type = this.typeUtils.getQualifiedName(element);
this.metadataCollector.add(ItemMetadata.newGroup(endpointKey(endpointId),
type, type, null));
this.metadataCollector
.add(ItemMetadata.newGroup(endpointKey(endpointId), type, type, null));
this.metadataCollector.add(ItemMetadata.newProperty(endpointKey(endpointId),
"enabled", Boolean.class.getName(), type, null, String.format(
"Enable the %s endpoint.", endpointId), enabledByDefault, null));
"enabled", Boolean.class.getName(), type, null,
String.format("Enable the %s endpoint.", endpointId), enabledByDefault,
null));
this.metadataCollector.add(ItemMetadata.newProperty(endpointKey(endpointId),
"cache.time-to-live", Long.class.getName(), type, null,
"Maximum time in milliseconds that a response can be cached.", 0, null));
EndpointTypes endpointTypes = EndpointTypes.parse(elementValues.get("types"));
EndpointExposure endpointTypes = EndpointExposure
.parse(elementValues.get("exposure"));
if (endpointTypes.hasJmx()) {
this.metadataCollector.add(ItemMetadata.newProperty(
endpointKey(endpointId + ".jmx"), "enabled", Boolean.class.getName(),
type, null, String.format("Expose the %s endpoint as a JMX MBean.",
endpointId), enabledByDefault, null));
type, null,
String.format("Expose the %s endpoint as a JMX MBean.", endpointId),
enabledByDefault, null));
}
if (endpointTypes.hasWeb()) {
this.metadataCollector.add(ItemMetadata.newProperty(
endpointKey(endpointId + ".web"), "enabled", Boolean.class.getName(),
type, null, String.format("Expose the %s endpoint as a Web endpoint.",
endpointId), false, null));
endpointId),
false, null));
}
}
@@ -400,7 +403,6 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
return "endpoints." + suffix;
}
private boolean isNested(Element returnType, VariableElement field,
TypeElement element) {
if (hasAnnotation(field, nestedConfigurationPropertyAnnotation())) {
@@ -521,39 +523,44 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
this.processingEnv.getMessager().printMessage(kind, msg);
}
private static class EndpointTypes {
private static class EndpointExposure {
private static final List<String> ALL_TYPES = Arrays.asList("JMX", "WEB");
private static final List<String> ALL = Arrays.asList("JMX", "WEB");
private final List<String> types;
EndpointTypes(List<String> types) {
EndpointExposure(List<String> types) {
this.types = types;
}
static EndpointTypes parse(Object typesAttribute) {
if (!(typesAttribute instanceof List)) {
return new EndpointTypes(ALL_TYPES);
}
List<AnnotationValue> values = (List<AnnotationValue>) typesAttribute;
static EndpointExposure parse(Object exposureAttribute) {
List<AnnotationValue> values = asAnnotationValues(exposureAttribute);
if (values.isEmpty()) {
return new EndpointTypes(ALL_TYPES);
return new EndpointExposure(ALL);
}
List<String> types = new ArrayList<>();
for (AnnotationValue value : values) {
types.add(((VariableElement) value.getValue()).getSimpleName().toString());
}
return new EndpointTypes(types);
return new EndpointExposure(
values.stream().map(EndpointExposure::getValueAttribute)
.collect(Collectors.toList()));
}
@SuppressWarnings("unchecked")
private static List<AnnotationValue> asAnnotationValues(Object typesAttribute) {
if (!(typesAttribute instanceof List)) {
return Collections.emptyList();
}
return (List<AnnotationValue>) typesAttribute;
}
private static String getValueAttribute(AnnotationValue value) {
return ((VariableElement) value.getValue()).getSimpleName().toString();
}
public boolean hasJmx() {
return this.types.contains("JMX");
return this.types.contains("JMX");
}
public boolean hasWeb() {
return this.types.contains("WEB");
return this.types.contains("WEB");
}
}

View File

@@ -211,12 +211,12 @@ public class ConfigurationMetadata {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(String.format("items: %n"));
StringBuilder result = new StringBuilder();
result.append(String.format("items: %n"));
this.items.values().forEach(itemMetadata -> {
sb.append("\t").append(String.format("%s%n", itemMetadata));
result.append("\t").append(String.format("%s%n", itemMetadata));
});
return sb.toString();
return result.toString();
}
}

View File

@@ -524,12 +524,11 @@ public class ConfigurationMetadataAnnotationProcessorTests {
assertThat(metadata.getItems()).hasSize(3);
}
@Test
public void simpleEndpoint() throws IOException {
ConfigurationMetadata metadata = compile(SimpleEndpoint.class);
assertThat(metadata).has(Metadata.withGroup("endpoints.simple")
.fromSource(SimpleEndpoint.class));
assertThat(metadata).has(
Metadata.withGroup("endpoints.simple").fromSource(SimpleEndpoint.class));
assertThat(metadata).has(enabledFlag("simple", true));
assertThat(metadata).has(jmxEnabledFlag("simple", true));
assertThat(metadata).has(webEnabledFlag("simple", false));
@@ -554,8 +553,8 @@ public class ConfigurationMetadataAnnotationProcessorTests {
ConfigurationMetadata metadata = compile(CustomPropertiesEndpoint.class);
assertThat(metadata).has(Metadata.withGroup("endpoints.customprops")
.fromSource(CustomPropertiesEndpoint.class));
assertThat(metadata).has(Metadata.withProperty("endpoints.customprops.name").
ofType(String.class).withDefaultValue("test"));
assertThat(metadata).has(Metadata.withProperty("endpoints.customprops.name")
.ofType(String.class).withDefaultValue("test"));
assertThat(metadata).has(enabledFlag("customprops", true));
assertThat(metadata).has(jmxEnabledFlag("customprops", true));
assertThat(metadata).has(webEnabledFlag("customprops", false));
@@ -566,8 +565,8 @@ public class ConfigurationMetadataAnnotationProcessorTests {
@Test
public void jmxOnlyEndpoint() throws IOException {
ConfigurationMetadata metadata = compile(OnlyJmxEndpoint.class);
assertThat(metadata).has(Metadata.withGroup("endpoints.jmx")
.fromSource(OnlyJmxEndpoint.class));
assertThat(metadata).has(
Metadata.withGroup("endpoints.jmx").fromSource(OnlyJmxEndpoint.class));
assertThat(metadata).has(enabledFlag("jmx", true));
assertThat(metadata).has(jmxEnabledFlag("jmx", true));
assertThat(metadata).has(cacheTtl("jmx"));
@@ -577,8 +576,8 @@ public class ConfigurationMetadataAnnotationProcessorTests {
@Test
public void webOnlyEndpoint() throws IOException {
ConfigurationMetadata metadata = compile(OnlyWebEndpoint.class);
assertThat(metadata).has(Metadata.withGroup("endpoints.web")
.fromSource(OnlyWebEndpoint.class));
assertThat(metadata).has(
Metadata.withGroup("endpoints.web").fromSource(OnlyWebEndpoint.class));
assertThat(metadata).has(enabledFlag("web", true));
assertThat(metadata).has(webEnabledFlag("web", false));
assertThat(metadata).has(cacheTtl("web"));
@@ -622,7 +621,8 @@ public class ConfigurationMetadataAnnotationProcessorTests {
assertThat(metadata).has(cacheTtl("incremental"));
assertThat(metadata.getItems()).hasSize(5);
project.replaceText(IncrementalEndpoint.class, "id = \"incremental\"",
"id = \"incremental\", types = Endpoint.Type.WEB");
"id = \"incremental\", exposure = org.springframework.boot."
+ "configurationsample.EndpointExposure.WEB");
metadata = project.incrementalBuild(IncrementalEndpoint.class);
assertThat(metadata).has(Metadata.withGroup("endpoints.incremental")
.fromSource(IncrementalEndpoint.class));
@@ -643,8 +643,8 @@ public class ConfigurationMetadataAnnotationProcessorTests {
assertThat(metadata).has(jmxEnabledFlag("incremental", true));
assertThat(metadata).has(cacheTtl("incremental"));
assertThat(metadata.getItems()).hasSize(4);
project.replaceText(IncrementalJmxEndpoint.class, ", types = Endpoint.Type.JMX",
"");
project.replaceText(IncrementalJmxEndpoint.class,
", exposure = EndpointExposure.JMX", "");
metadata = project.incrementalBuild(IncrementalJmxEndpoint.class);
assertThat(metadata).has(Metadata.withGroup("endpoints.incremental")
.fromSource(IncrementalJmxEndpoint.class));
@@ -658,22 +658,22 @@ public class ConfigurationMetadataAnnotationProcessorTests {
private Metadata.MetadataItemCondition enabledFlag(String endpointId,
boolean defaultValue) {
return Metadata.withEnabledFlag("endpoints." + endpointId + ".enabled")
.withDefaultValue(defaultValue).withDescription(
String.format("Enable the %s endpoint.", endpointId));
.withDefaultValue(defaultValue)
.withDescription(String.format("Enable the %s endpoint.", endpointId));
}
private Metadata.MetadataItemCondition jmxEnabledFlag(String endpointId,
boolean defaultValue) {
return Metadata.withEnabledFlag("endpoints." + endpointId + ".jmx.enabled")
.withDefaultValue(defaultValue).withDescription(String.format(
"Expose the %s endpoint as a JMX MBean.", endpointId));
.withDefaultValue(defaultValue).withDescription(String
.format("Expose the %s endpoint as a JMX MBean.", endpointId));
}
private Metadata.MetadataItemCondition webEnabledFlag(String endpointId,
boolean defaultValue) {
return Metadata.withEnabledFlag("endpoints." + endpointId + ".web.enabled")
.withDefaultValue(defaultValue).withDescription(String.format(
"Expose the %s endpoint as a Web endpoint.", endpointId));
.withDefaultValue(defaultValue).withDescription(String
.format("Expose the %s endpoint as a Web endpoint.", endpointId));
}
private Metadata.MetadataItemCondition cacheTtl(String endpointId) {

View File

@@ -37,14 +37,6 @@ public @interface Endpoint {
boolean enabledByDefault() default true;
Type[] types() default {};
enum Type {
JMX,
WEB
}
EndpointExposure[] exposure() default {};
}

View File

@@ -0,0 +1,25 @@
/*
* 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.configurationsample;
public enum EndpointExposure {
JMX,
WEB
}

View File

@@ -17,13 +17,14 @@
package org.springframework.boot.configurationsample.endpoint;
import org.springframework.boot.configurationsample.Endpoint;
import org.springframework.boot.configurationsample.EndpointExposure;
/**
* An endpoint that only exposes a JMX MBean.
*
* @author Stephane Nicoll
*/
@Endpoint(id = "jmx", types = Endpoint.Type.JMX)
@Endpoint(id = "jmx", exposure = EndpointExposure.JMX)
public class OnlyJmxEndpoint {
}

View File

@@ -17,13 +17,14 @@
package org.springframework.boot.configurationsample.endpoint;
import org.springframework.boot.configurationsample.Endpoint;
import org.springframework.boot.configurationsample.EndpointExposure;
/**
* An endpoints that only exposes a web endpoint.
*
* @author Stephane Nicoll
*/
@Endpoint(id = "web", types = Endpoint.Type.WEB)
@Endpoint(id = "web", exposure = EndpointExposure.WEB)
public class OnlyWebEndpoint {
}

View File

@@ -17,13 +17,14 @@
package org.springframework.boot.configurationsample.endpoint.incremental;
import org.springframework.boot.configurationsample.Endpoint;
import org.springframework.boot.configurationsample.EndpointExposure;
/**
* An endpoint that only exposes a JMX MBean.
*
* @author Stephane Nicoll
*/
@Endpoint(id = "incremental", types = Endpoint.Type.JMX)
@Endpoint(id = "incremental", exposure = EndpointExposure.JMX)
public class IncrementalJmxEndpoint {
}