Commit 83e81e13 authored by Stephane Nicoll's avatar Stephane Nicoll

Add support of metadata generation for Endpoints

This commit improves the configuration metadata annotation processor to
explicitly handle `@Endpoint` annotated class. Adding a new endpoint on
a project potentially creates the following keys:

* `endpoints.<id>.enabled`
* `endpoints.<id>.cache.time-to-live`
* `endpoints.<id>.jmx.enabled`
* `endpoints.<id>.web.enabled`

Default values are extracted from the annotation type. If an endpoint
is restricted to a given tech, properties from unrelated techs are not
generated.

Closes gh-9692
parent 25d2d553
......@@ -19,7 +19,10 @@ 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.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
......@@ -70,6 +73,9 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
static final String DEPRECATED_CONFIGURATION_PROPERTY_ANNOTATION = "org.springframework.boot."
+ "context.properties.DeprecatedConfigurationProperty";
static final String ENDPOINT_ANNOTATION = "org.springframework.boot."
+ "endpoint.Endpoint";
static final String LOMBOK_DATA_ANNOTATION = "lombok.Data";
static final String LOMBOK_GETTER_ANNOTATION = "lombok.Getter";
......@@ -98,6 +104,10 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
return DEPRECATED_CONFIGURATION_PROPERTY_ANNOTATION;
}
protected String endpointAnnotation() {
return ENDPOINT_ANNOTATION;
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
......@@ -132,6 +142,13 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
processElement(element);
}
}
TypeElement endpointType = elementUtils.getTypeElement(endpointAnnotation());
if (endpointType != null) { // Is @Endpoint available
for (Element element : roundEnv.getElementsAnnotatedWith(endpointType)) {
processEndpoint(element);
}
}
if (roundEnv.processingOver()) {
try {
writeMetaData();
......@@ -332,6 +349,59 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
}
}
private void processEndpoint(Element element) {
try {
AnnotationMirror annotation = getAnnotation(element, endpointAnnotation());
if (element instanceof TypeElement) {
processEndpoint(annotation, (TypeElement) element);
}
}
catch (Exception ex) {
throw new IllegalStateException(
"Error processing configuration meta-data on " + element, ex);
}
}
private void processEndpoint(AnnotationMirror annotation, TypeElement element) {
Map<String, Object> elementValues = getAnnotationElementValues(annotation);
String endpointId = (String) elementValues.get("id");
if (endpointId == null || "".equals(endpointId)) {
return; // Can't process that endpoint
}
Boolean enabledByDefault = (Boolean) elementValues.get("enabledByDefault");
if (enabledByDefault == null) {
enabledByDefault = Boolean.TRUE;
}
String type = this.typeUtils.getQualifiedName(element);
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));
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"));
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));
}
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), enabledByDefault, null));
}
}
private String endpointKey(String suffix) {
return "endpoints." + suffix;
}
private boolean isNested(Element returnType, VariableElement field,
TypeElement element) {
if (hasAnnotation(field, nestedConfigurationPropertyAnnotation())) {
......@@ -454,4 +524,41 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
this.processingEnv.getMessager().printMessage(kind, msg);
}
private static class EndpointTypes {
private static final List<String> ALL_TYPES = Arrays.asList("JMX", "WEB");
private final List<String> types;
EndpointTypes(List<String> types) {
this.types = types;
}
static EndpointTypes parse(Object typesAttribute) {
if (typesAttribute == null || !(typesAttribute instanceof List)) {
return new EndpointTypes(ALL_TYPES);
}
List<AnnotationValue> values = (List<AnnotationValue>) typesAttribute;
if (values.isEmpty()) {
return new EndpointTypes(ALL_TYPES);
}
List<String> types = new ArrayList<>();
for (AnnotationValue value : values) {
types.add(((VariableElement) value.getValue()).getSimpleName().toString());
}
return new EndpointTypes(types);
}
public boolean hasJmx() {
return this.types.contains("JMX");
}
public boolean hasWeb() {
return this.types.contains("WEB");
}
}
}
......@@ -209,4 +209,14 @@ public class ConfigurationMetadata {
return content;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(String.format("items: %n"));
this.items.values().forEach(itemMetadata -> {
sb.append("\t").append(String.format("%s%n", itemMetadata));
});
return sb.toString();
}
}
......@@ -36,6 +36,13 @@ import org.springframework.boot.configurationprocessor.metadata.ItemHint;
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata;
import org.springframework.boot.configurationprocessor.metadata.Metadata;
import org.springframework.boot.configurationprocessor.metadata.TestJsonConverter;
import org.springframework.boot.configurationsample.endpoint.CustomPropertiesEndpoint;
import org.springframework.boot.configurationsample.endpoint.DisabledEndpoint;
import org.springframework.boot.configurationsample.endpoint.OnlyJmxEndpoint;
import org.springframework.boot.configurationsample.endpoint.OnlyWebEndpoint;
import org.springframework.boot.configurationsample.endpoint.SimpleEndpoint;
import org.springframework.boot.configurationsample.endpoint.incremental.IncrementalEndpoint;
import org.springframework.boot.configurationsample.endpoint.incremental.IncrementalJmxEndpoint;
import org.springframework.boot.configurationsample.incremental.BarProperties;
import org.springframework.boot.configurationsample.incremental.FooProperties;
import org.springframework.boot.configurationsample.incremental.RenamedBarProperties;
......@@ -517,6 +524,164 @@ 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(enabledFlag("simple", true));
assertThat(metadata).has(jmxEnabledFlag("simple", true));
assertThat(metadata).has(webEnabledFlag("simple", true));
assertThat(metadata).has(cacheTtl("simple"));
assertThat(metadata.getItems()).hasSize(5);
}
@Test
public void disableEndpoint() throws IOException {
ConfigurationMetadata metadata = compile(DisabledEndpoint.class);
assertThat(metadata).has(Metadata.withGroup("endpoints.disabled")
.fromSource(DisabledEndpoint.class));
assertThat(metadata).has(enabledFlag("disabled", false));
assertThat(metadata).has(jmxEnabledFlag("disabled", false));
assertThat(metadata).has(webEnabledFlag("disabled", false));
assertThat(metadata).has(cacheTtl("disabled"));
assertThat(metadata.getItems()).hasSize(5);
}
@Test
public void customPropertiesEndpoint() throws IOException {
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(enabledFlag("customprops", true));
assertThat(metadata).has(jmxEnabledFlag("customprops", true));
assertThat(metadata).has(webEnabledFlag("customprops", true));
assertThat(metadata).has(cacheTtl("customprops"));
assertThat(metadata.getItems()).hasSize(6);
}
@Test
public void jmxOnlyEndpoint() throws IOException {
ConfigurationMetadata metadata = compile(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"));
assertThat(metadata.getItems()).hasSize(4);
}
@Test
public void webOnlyEndpoint() throws IOException {
ConfigurationMetadata metadata = compile(OnlyWebEndpoint.class);
assertThat(metadata).has(Metadata.withGroup("endpoints.web")
.fromSource(OnlyWebEndpoint.class));
assertThat(metadata).has(enabledFlag("web", true));
assertThat(metadata).has(webEnabledFlag("web", true));
assertThat(metadata).has(cacheTtl("web"));
assertThat(metadata.getItems()).hasSize(4);
}
@Test
public void incrementalEndpointBuildChangeGeneralEnabledFlag() throws Exception {
TestProject project = new TestProject(this.temporaryFolder,
IncrementalEndpoint.class);
ConfigurationMetadata metadata = project.fullBuild();
assertThat(metadata).has(Metadata.withGroup("endpoints.incremental")
.fromSource(IncrementalEndpoint.class));
assertThat(metadata).has(enabledFlag("incremental", true));
assertThat(metadata).has(jmxEnabledFlag("incremental", true));
assertThat(metadata).has(webEnabledFlag("incremental", true));
assertThat(metadata).has(cacheTtl("incremental"));
assertThat(metadata.getItems()).hasSize(5);
project.replaceText(IncrementalEndpoint.class, "id = \"incremental\"",
"id = \"incremental\", enabledByDefault = false");
metadata = project.incrementalBuild(IncrementalEndpoint.class);
assertThat(metadata).has(Metadata.withGroup("endpoints.incremental")
.fromSource(IncrementalEndpoint.class));
assertThat(metadata).has(enabledFlag("incremental", false));
assertThat(metadata).has(jmxEnabledFlag("incremental", false));
assertThat(metadata).has(webEnabledFlag("incremental", false));
assertThat(metadata).has(cacheTtl("incremental"));
assertThat(metadata.getItems()).hasSize(5);
}
@Test
public void incrementalEndpointBuildDisableJmxEndpoint() throws Exception {
TestProject project = new TestProject(this.temporaryFolder,
IncrementalEndpoint.class);
ConfigurationMetadata metadata = project.fullBuild();
assertThat(metadata).has(Metadata.withGroup("endpoints.incremental")
.fromSource(IncrementalEndpoint.class));
assertThat(metadata).has(enabledFlag("incremental", true));
assertThat(metadata).has(jmxEnabledFlag("incremental", true));
assertThat(metadata).has(webEnabledFlag("incremental", true));
assertThat(metadata).has(cacheTtl("incremental"));
assertThat(metadata.getItems()).hasSize(5);
project.replaceText(IncrementalEndpoint.class, "id = \"incremental\"",
"id = \"incremental\", types = Endpoint.Type.WEB");
metadata = project.incrementalBuild(IncrementalEndpoint.class);
assertThat(metadata).has(Metadata.withGroup("endpoints.incremental")
.fromSource(IncrementalEndpoint.class));
assertThat(metadata).has(enabledFlag("incremental", true));
assertThat(metadata).has(webEnabledFlag("incremental", true));
assertThat(metadata).has(cacheTtl("incremental"));
assertThat(metadata.getItems()).hasSize(4);
}
@Test
public void incrementalEndpointBuildEnableJmxEndpoint() throws Exception {
TestProject project = new TestProject(this.temporaryFolder,
IncrementalJmxEndpoint.class);
ConfigurationMetadata metadata = project.fullBuild();
assertThat(metadata).has(Metadata.withGroup("endpoints.incremental")
.fromSource(IncrementalJmxEndpoint.class));
assertThat(metadata).has(enabledFlag("incremental", true));
assertThat(metadata).has(jmxEnabledFlag("incremental", true));
assertThat(metadata).has(cacheTtl("incremental"));
assertThat(metadata.getItems()).hasSize(4);
project.replaceText(IncrementalJmxEndpoint.class, ", types = Endpoint.Type.JMX",
"");
metadata = project.incrementalBuild(IncrementalJmxEndpoint.class);
assertThat(metadata).has(Metadata.withGroup("endpoints.incremental")
.fromSource(IncrementalJmxEndpoint.class));
assertThat(metadata).has(enabledFlag("incremental", true));
assertThat(metadata).has(jmxEnabledFlag("incremental", true));
assertThat(metadata).has(webEnabledFlag("incremental", true));
assertThat(metadata).has(cacheTtl("incremental"));
assertThat(metadata.getItems()).hasSize(5);
}
private Metadata.MetadataItemCondition enabledFlag(String endpointId,
boolean defaultValue) {
return Metadata.withEnabledFlag("endpoints." + endpointId + ".enabled")
.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));
}
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));
}
private Metadata.MetadataItemCondition cacheTtl(String endpointId) {
return Metadata.withProperty("endpoints." + endpointId + ".cache.time-to-live")
.ofType(Long.class).withDefaultValue(0).withDescription(
"Maximum time in milliseconds that a response can be cached.");
}
@Test
public void mergingOfAdditionalProperty() throws Exception {
ItemMetadata property = ItemMetadata.newProperty(null, "foo", "java.lang.String",
......
......@@ -46,6 +46,8 @@ public class TestConfigurationMetadataAnnotationProcessor
static final String DEPRECATED_CONFIGURATION_PROPERTY_ANNOTATION = "org.springframework.boot.configurationsample.DeprecatedConfigurationProperty";
static final String ENDPOINT_ANNOTATION = "org.springframework.boot.configurationsample.Endpoint";
private ConfigurationMetadata metadata;
private final File outputLocation;
......@@ -69,6 +71,11 @@ public class TestConfigurationMetadataAnnotationProcessor
return DEPRECATED_CONFIGURATION_PROPERTY_ANNOTATION;
}
@Override
protected String endpointAnnotation() {
return ENDPOINT_ANNOTATION;
}
@Override
protected ConfigurationMetadata writeMetaData() throws Exception {
super.writeMetaData();
......
......@@ -62,6 +62,10 @@ public final class Metadata {
return new MetadataItemCondition(ItemType.PROPERTY, name).ofType(type);
}
public static Metadata.MetadataItemCondition withEnabledFlag(String key) {
return withProperty(key).ofType(Boolean.class);
}
public static MetadataHintCondition withHint(String name) {
return new MetadataHintCondition(name);
}
......
/*
* 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;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Alternative to Spring Boot's {@code @Endpoint} for testing (removes the need for a
* dependency on the real annotation).
*
* @author Stephane Nicoll
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Endpoint {
String id();
boolean enabledByDefault() default true;
Type[] types() default {};
enum Type {
JMX,
WEB
}
}
/*
* 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.endpoint;
import org.springframework.boot.configurationsample.ConfigurationProperties;
import org.springframework.boot.configurationsample.Endpoint;
/**
* An endpoint with additional custom properties.
*
* @author Stephane Nicoll
*/
@Endpoint(id = "customprops")
@ConfigurationProperties("endpoints.customprops")
public class CustomPropertiesEndpoint {
private String name = "test";
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
/*
* 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.endpoint;
import org.springframework.boot.configurationsample.Endpoint;
/**
* An endpoint that is disabled by default.
*
* @author Stephane Nicoll
*/
@Endpoint(id = "disabled", enabledByDefault = false)
public class DisabledEndpoint {
}
/*
* 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.endpoint;
import org.springframework.boot.configurationsample.Endpoint;
/**
* An endpoint that only exposes a JMX MBean.
*
* @author Stephane Nicoll
*/
@Endpoint(id = "jmx", types = Endpoint.Type.JMX)
public class OnlyJmxEndpoint {
}
/*
* 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.endpoint;
import org.springframework.boot.configurationsample.Endpoint;
/**
* An endpoints that only exposes a web endpoint.
*
* @author Stephane Nicoll
*/
@Endpoint(id = "web", types = Endpoint.Type.WEB)
public class OnlyWebEndpoint {
}
/*
* 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.endpoint;
import org.springframework.boot.configurationsample.Endpoint;
/**
* A simple endpoint with no default override.
*
* @author Stephane Nicoll
*/
@Endpoint(id = "simple")
public class SimpleEndpoint {
}
/*
* 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.endpoint.incremental;
import org.springframework.boot.configurationsample.Endpoint;
/**
* An endpoint that is enabled by default.
*
* @author Stephane Nicoll
*/
@Endpoint(id = "incremental")
public class IncrementalEndpoint {
}
/*
* 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.endpoint.incremental;
import org.springframework.boot.configurationsample.Endpoint;
/**
* An endpoint that only exposes a JMX MBean.
*
* @author Stephane Nicoll
*/
@Endpoint(id = "incremental", types = Endpoint.Type.JMX)
public class IncrementalJmxEndpoint {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment