Skip to content

Commit b04b6ca

Browse files
onobcdsyer
authored andcommitted
Add ConditionalOnGrpcServerEnabled flag
This adds a coarse-grained conditional guard that will disable the server autoconfiguration if `spring.grpc.server.enabled` is set to false or `BindableService` class is not available on the classpath. Signed-off-by: Chris Bono <chris.bono@gmail.com>
1 parent 2790a8a commit b04b6ca

16 files changed

+346
-15
lines changed

spring-grpc-docs/src/main/antora/modules/ROOT/partials/_configprops.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
|spring.grpc.client.enabled | `+++true+++` | Whether to enable client autoconfiguration.
2222
|spring.grpc.client.observations.enabled | `+++true+++` | Whether to enable Observations on the client.
2323
|spring.grpc.server.address | | The address to bind to. could be a host:port combination or a pseudo URL like static://host:port. Can not be set if host or port are set independently.
24+
|spring.grpc.server.enabled | `+++true+++` | Whether to enable server autoconfiguration.
2425
|spring.grpc.server.exception-handling.enabled | `+++true+++` | Whether to enable user-defined global exception handling on the gRPC server.
2526
|spring.grpc.server.health.actuator.enabled | `+++true+++` | Whether to adapt Actuator health indicators into gRPC health checks.
2627
|spring.grpc.server.health.actuator.health-indicator-paths | | List of Actuator health indicator paths to adapt into gRPC health checks.
@@ -49,4 +50,4 @@
4950
|spring.grpc.server.ssl.enabled | | Whether to enable SSL support. Enabled automatically if "bundle" is provided unless specified otherwise.
5051
|spring.grpc.server.ssl.secure | `+++true+++` | Flag to indicate that client authentication is secure (i.e. certificates are checked). Do not set this to false in production.
5152

52-
|===
53+
|===
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2023-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.grpc.autoconfigure.server;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
25+
import org.springframework.context.annotation.Conditional;
26+
27+
import io.grpc.BindableService;
28+
29+
/**
30+
* {@link Conditional @Conditional} that only matches when the
31+
* {@code io.grpc.BindableService} class is on the classpath and the
32+
* {@code spring.grpc.server.enabled} property is not explicitly set to {@code false}.
33+
*
34+
* @author Freeman
35+
* @author Chris Bono
36+
*/
37+
@Retention(RetentionPolicy.RUNTIME)
38+
@Target({ ElementType.TYPE, ElementType.METHOD })
39+
@ConditionalOnClass(BindableService.class)
40+
@ConditionalOnProperty(prefix = "spring.grpc.server", name = "enabled", matchIfMissing = true)
41+
public @interface ConditionalOnGrpcServerEnabled {
42+
43+
}

spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfiguration.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717

1818
import org.springframework.beans.factory.ObjectProvider;
1919
import org.springframework.boot.autoconfigure.AutoConfiguration;
20-
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2120
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2221
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
23-
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2422
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2523
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2624
import org.springframework.context.ApplicationContext;
@@ -50,9 +48,8 @@
5048
* @author David Syer
5149
* @author Chris Bono
5250
*/
53-
@AutoConfiguration
54-
@AutoConfigureAfter(GrpcServerFactoryAutoConfiguration.class)
55-
@ConditionalOnClass(BindableService.class)
51+
@AutoConfiguration(after = GrpcServerFactoryAutoConfiguration.class)
52+
@ConditionalOnGrpcServerEnabled
5653
@ConditionalOnBean(BindableService.class)
5754
@EnableConfigurationProperties(GrpcServerProperties.class)
5855
@Import({ GrpcCodecConfiguration.class })

spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerFactoryAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
*/
5555
@AutoConfiguration
5656
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
57-
@ConditionalOnClass(BindableService.class)
57+
@ConditionalOnGrpcServerEnabled
5858
@ConditionalOnBean(BindableService.class)
5959
public class GrpcServerFactoryAutoConfiguration {
6060

spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerObservationAutoConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
@AutoConfiguration(
2929
afterName = "org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfiguration")
30+
@ConditionalOnGrpcServerEnabled
3031
@ConditionalOnClass({ ObservationRegistry.class, ObservationGrpcServerInterceptor.class })
3132
@ConditionalOnBean(ObservationRegistry.class)
3233
@ConditionalOnProperty(name = "spring.grpc.server.observation.enabled", havingValue = "true", matchIfMissing = true)

spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerReflectionAutoConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* @author Haris Zujo
2020
*/
2121
@AutoConfiguration(before = GrpcServerFactoryAutoConfiguration.class)
22+
@ConditionalOnGrpcServerEnabled
2223
@ConditionalOnClass(ProtoReflectionService.class)
2324
@ConditionalOnProperty(name = "spring.grpc.server.reflection.enabled", havingValue = "true", matchIfMissing = true)
2425
public class GrpcServerReflectionAutoConfiguration {

spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/exception/GrpcExceptionHandlerAutoConfiguration.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@
1515
*/
1616
package org.springframework.grpc.autoconfigure.server.exception;
1717

18-
import java.util.stream.Collectors;
19-
2018
import org.springframework.beans.factory.ObjectProvider;
2119
import org.springframework.boot.autoconfigure.AutoConfiguration;
2220
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2321
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2422
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2523
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2624
import org.springframework.context.annotation.Bean;
25+
import org.springframework.grpc.autoconfigure.server.ConditionalOnGrpcServerEnabled;
2726
import org.springframework.grpc.server.GlobalServerInterceptor;
2827
import org.springframework.grpc.server.exception.CompositeGrpcExceptionHandler;
2928
import org.springframework.grpc.server.exception.GrpcExceptionHandler;
@@ -32,6 +31,7 @@
3231
import io.grpc.Grpc;
3332

3433
@AutoConfiguration
34+
@ConditionalOnGrpcServerEnabled
3535
@ConditionalOnClass(Grpc.class)
3636
@ConditionalOnBean(GrpcExceptionHandler.class)
3737
@ConditionalOnMissingBean(GrpcExceptionHandlerInterceptor.class)
@@ -44,7 +44,7 @@ public class GrpcExceptionHandlerAutoConfiguration {
4444
public GrpcExceptionHandlerInterceptor globalExceptionHandlerInterceptor(
4545
ObjectProvider<GrpcExceptionHandler> exceptionHandler) {
4646
return new GrpcExceptionHandlerInterceptor(new CompositeGrpcExceptionHandler(
47-
exceptionHandler.orderedStream().collect(Collectors.toList()).toArray(new GrpcExceptionHandler[0])));
47+
exceptionHandler.orderedStream().toArray(GrpcExceptionHandler[]::new)));
4848
}
4949

5050
}

spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/health/GrpcServerHealthAutoConfiguration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.springframework.context.annotation.Conditional;
4444
import org.springframework.context.annotation.Configuration;
4545
import org.springframework.core.type.AnnotatedTypeMetadata;
46+
import org.springframework.grpc.autoconfigure.server.ConditionalOnGrpcServerEnabled;
4647
import org.springframework.grpc.autoconfigure.server.GrpcServerFactoryAutoConfiguration;
4748
import org.springframework.grpc.autoconfigure.server.GrpcServerProperties;
4849
import org.springframework.scheduling.annotation.EnableScheduling;
@@ -57,6 +58,7 @@
5758
* @author Chris Bono
5859
*/
5960
@AutoConfiguration(before = GrpcServerFactoryAutoConfiguration.class)
61+
@ConditionalOnGrpcServerEnabled
6062
@ConditionalOnClass(HealthStatusManager.class)
6163
@ConditionalOnProperty(name = "spring.grpc.server.health.enabled", havingValue = "true", matchIfMissing = true)
6264
public class GrpcServerHealthAutoConfiguration {

spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/security/GrpcSecurityAutoConfiguration.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.context.annotation.Bean;
2424
import org.springframework.context.annotation.Conditional;
2525
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.grpc.autoconfigure.server.ConditionalOnGrpcServerEnabled;
2627
import org.springframework.grpc.autoconfigure.server.GrpcServerFactoryAutoConfiguration;
2728
import org.springframework.grpc.autoconfigure.server.exception.GrpcExceptionHandlerAutoConfiguration;
2829
import org.springframework.grpc.server.GlobalServerInterceptor;
@@ -40,6 +41,7 @@
4041
import io.grpc.internal.GrpcUtil;
4142

4243
@ConditionalOnClass(ObjectPostProcessor.class)
44+
@ConditionalOnGrpcServerEnabled
4345
@AutoConfiguration(before = GrpcExceptionHandlerAutoConfiguration.class, after = SecurityAutoConfiguration.class)
4446
public class GrpcSecurityAutoConfiguration {
4547

@@ -84,4 +86,4 @@ public <T extends ServerBuilder<T>> ServerBuilderCustomizer<T> securityContextEx
8486

8587
}
8688

87-
}
89+
}

spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
"name": "spring.grpc.server.port",
66
"defaultValue": "9090"
77
},
8+
{
9+
"name": "spring.grpc.server.enabled",
10+
"type": "java.lang.Boolean",
11+
"description": "Whether to enable server autoconfiguration.",
12+
"defaultValue": true
13+
},
814
{
915
"name": "spring.grpc.server.reflection.enabled",
1016
"type": "java.lang.Boolean",

0 commit comments

Comments
 (0)