|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2023 the original author or authors. |
| 2 | + * Copyright 2012-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
59 | 59 | * @author Kris De Volder
|
60 | 60 | * @author Jonas Keßler
|
61 | 61 | * @author Scott Frederick
|
| 62 | + * @author Moritz Halbritter |
62 | 63 | * @since 1.2.0
|
63 | 64 | */
|
64 | 65 | @SupportedAnnotationTypes({ ConfigurationMetadataAnnotationProcessor.AUTO_CONFIGURATION_ANNOTATION,
|
@@ -291,18 +292,30 @@ private void processEndpoint(AnnotationMirror annotation, TypeElement element) {
|
291 | 292 | return; // Can't process that endpoint
|
292 | 293 | }
|
293 | 294 | String endpointKey = ItemMetadata.newItemMetadataPrefix("management.endpoint.", endpointId);
|
294 |
| - Boolean enabledByDefault = (Boolean) elementValues.get("enableByDefault"); |
| 295 | + boolean enabledByDefault = (boolean) elementValues.getOrDefault("enableByDefault", true); |
295 | 296 | String type = this.metadataEnv.getTypeUtils().getQualifiedName(element);
|
296 |
| - this.metadataCollector.add(ItemMetadata.newGroup(endpointKey, type, type, null)); |
297 |
| - this.metadataCollector.add(ItemMetadata.newProperty(endpointKey, "enabled", Boolean.class.getName(), type, null, |
298 |
| - String.format("Whether to enable the %s endpoint.", endpointId), |
299 |
| - (enabledByDefault != null) ? enabledByDefault : true, null)); |
| 297 | + this.metadataCollector.addIfAbsent(ItemMetadata.newGroup(endpointKey, type, type, null)); |
| 298 | + this.metadataCollector.add( |
| 299 | + ItemMetadata.newProperty(endpointKey, "enabled", Boolean.class.getName(), type, null, |
| 300 | + "Whether to enable the %s endpoint.".formatted(endpointId), enabledByDefault, null), |
| 301 | + (existing) -> checkEnabledValueMatchesExisting(existing, enabledByDefault, type)); |
300 | 302 | if (hasMainReadOperation(element)) {
|
301 |
| - this.metadataCollector.add(ItemMetadata.newProperty(endpointKey, "cache.time-to-live", |
| 303 | + this.metadataCollector.addIfAbsent(ItemMetadata.newProperty(endpointKey, "cache.time-to-live", |
302 | 304 | Duration.class.getName(), type, null, "Maximum time that a response can be cached.", "0ms", null));
|
303 | 305 | }
|
304 | 306 | }
|
305 | 307 |
|
| 308 | + private void checkEnabledValueMatchesExisting(ItemMetadata existing, boolean enabledByDefault, String sourceType) { |
| 309 | + boolean existingDefaultValue = (boolean) existing.getDefaultValue(); |
| 310 | + if (enabledByDefault == existingDefaultValue) { |
| 311 | + return; |
| 312 | + } |
| 313 | + throw new IllegalStateException( |
| 314 | + "Existing property '%s' from type %s has a conflicting value. Existing value: %b, new value from type %s: %b" |
| 315 | + .formatted(existing.getName(), existing.getSourceType(), existingDefaultValue, sourceType, |
| 316 | + enabledByDefault)); |
| 317 | + } |
| 318 | + |
306 | 319 | private boolean hasMainReadOperation(TypeElement element) {
|
307 | 320 | for (ExecutableElement method : ElementFilter.methodsIn(element.getEnclosedElements())) {
|
308 | 321 | if (this.metadataEnv.getReadOperationAnnotation(method) != null
|
|
0 commit comments