Global Guard

Previously, whenever the library was included on a classpath it would attempt
to auto-configure Spring Boot.  Since part of the API can be used as a
straight library, and in that usage, auto-configuration would not be
desirable, the auto-configuration needed a global off switch.  This change
adds a global guard, disabled by default, in the
org.springframework.cloud.bindings.boot.enable System Property.

[resolves #25]

Signed-off-by: Ben Hale <bhale@vmware.com>
This commit is contained in:
Ben Hale
2020-05-11 10:41:00 -07:00
parent effd3c4c86
commit aa6f50e5c3
15 changed files with 195 additions and 124 deletions

View File

@@ -36,6 +36,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.springframework.cloud.bindings.boot.Guards.isGlobalEnabled;
import static org.springframework.core.env.CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME;
/**
@@ -43,6 +44,8 @@ import static org.springframework.core.env.CommandLinePropertySource.COMMAND_LIN
* This implementation generates a single instance of {@code Bindings} and then calls all implementations of
* {@link BindingsPropertiesProcessor} registered with {@link SpringFactoriesLoader} allowing them to generate any
* properties from the contents of the {@code Bindings}.
* <p>
* Must be enabled by setting the {@code org.springframework.cloud.bindings.boot.enable} System Property to {@code true}.
*/
public final class BindingsEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered {
@@ -79,6 +82,10 @@ public final class BindingsEnvironmentPostProcessor implements EnvironmentPostPr
public void postProcessEnvironment(@NotNull ConfigurableEnvironment environment,
@NotNull SpringApplication application) {
if (!isGlobalEnabled()) {
return;
}
if (bindings.getBindings().isEmpty()) {
log.debug("No CNB Bindings found. Skipping Environment post-processing.");
return;

View File

@@ -23,7 +23,7 @@ import org.springframework.lang.NonNull;
import java.util.Map;
import static org.springframework.cloud.bindings.boot.KindGuard.isKindEnabled;
import static org.springframework.cloud.bindings.boot.Guards.isKindEnabled;
/**
* An implementation of {@link BindingsPropertiesProcessor} that detects {@link Binding}s of kind: {@value KIND}.

View File

@@ -23,7 +23,7 @@ import org.springframework.lang.NonNull;
import java.util.Map;
import static org.springframework.cloud.bindings.boot.KindGuard.isKindEnabled;
import static org.springframework.cloud.bindings.boot.Guards.isKindEnabled;
/**
* An implementation of {@link BindingsPropertiesProcessor} that detects {@link Binding}s of kind: {@value KIND}.

View File

@@ -18,7 +18,12 @@ package org.springframework.cloud.bindings.boot;
import org.jetbrains.annotations.NotNull;
final class KindGuard {
final class Guards {
static boolean isGlobalEnabled() {
String value = System.getProperty("org.springframework.cloud.bindings.boot.enable", "false");
return Boolean.parseBoolean(value);
}
static boolean isKindEnabled(@NotNull String kind) {
String property = String.format("org.springframework.cloud.bindings.boot.%s.enable", kind.toLowerCase());

View File

@@ -22,7 +22,7 @@ import org.springframework.lang.NonNull;
import java.util.Map;
import static org.springframework.cloud.bindings.boot.KindGuard.isKindEnabled;
import static org.springframework.cloud.bindings.boot.Guards.isKindEnabled;
/**
* An implementation of {@link BindingsPropertiesProcessor} that detects {@link Binding}s of kind: {@value KIND}.

View File

@@ -23,7 +23,7 @@ import org.springframework.lang.NonNull;
import java.util.Map;
import static org.springframework.cloud.bindings.boot.KindGuard.isKindEnabled;
import static org.springframework.cloud.bindings.boot.Guards.isKindEnabled;
/**
* An implementation of {@link BindingsPropertiesProcessor} that detects {@link Binding}s of kind: {@value KIND}.

View File

@@ -23,7 +23,7 @@ import org.springframework.lang.NonNull;
import java.util.Map;
import static org.springframework.cloud.bindings.boot.KindGuard.isKindEnabled;
import static org.springframework.cloud.bindings.boot.Guards.isKindEnabled;
/**
* An implementation of {@link BindingsPropertiesProcessor} that detects {@link Binding}s of kind: {@value KIND}.

View File

@@ -23,7 +23,7 @@ import org.springframework.lang.NonNull;
import java.util.Map;
import static org.springframework.cloud.bindings.boot.KindGuard.isKindEnabled;
import static org.springframework.cloud.bindings.boot.Guards.isKindEnabled;
/**
* An implementation of {@link BindingsPropertiesProcessor} that detects {@link Binding}s of kind: {@value KIND}.

View File

@@ -23,7 +23,7 @@ import org.springframework.lang.NonNull;
import java.util.Map;
import static org.springframework.cloud.bindings.boot.KindGuard.isKindEnabled;
import static org.springframework.cloud.bindings.boot.Guards.isKindEnabled;
/**
* An implementation of {@link BindingsPropertiesProcessor} that detects {@link Binding}s of kind: {@value KIND}.

View File

@@ -23,7 +23,7 @@ import org.springframework.lang.NonNull;
import java.util.Map;
import static org.springframework.cloud.bindings.boot.KindGuard.isKindEnabled;
import static org.springframework.cloud.bindings.boot.Guards.isKindEnabled;
/**
* An implementation of {@link BindingsPropertiesProcessor} that detects {@link Binding}s of kind: {@value KIND}.