Example usage for org.springframework.boot.context.properties.bind Binder get

List of usage examples for org.springframework.boot.context.properties.bind Binder get

Introduction

In this page you can find the example usage for org.springframework.boot.context.properties.bind Binder get.

Prototype

public static Binder get(Environment environment) 

Source Link

Document

Create a new Binder instance from the specified environment.

Usage

From source file:io.spring.initializr.web.autoconfigure.InitializrAutoConfiguration.java

@Bean
@ConditionalOnMissingBean//from  w w  w . j av  a 2 s .  co  m
public TemplateRenderer templateRenderer(Environment environment) {
    Binder binder = Binder.get(environment);
    boolean cache = binder.bind("spring.mustache.cache", Boolean.class).orElse(true);
    TemplateRenderer templateRenderer = new TemplateRenderer();
    templateRenderer.setCache(cache);
    return templateRenderer;
}

From source file:com.netflix.genie.common.internal.aws.s3.S3ClientFactory.java

/**
 * Constructor./*from  w w  w  . j a  v a2  s. com*/
 *
 * @param awsCredentialsProvider The base AWS credentials provider to use for the generated S3 clients
 * @param regionProvider         How this factory should determine the default {@link Regions}
 * @param environment            The Spring application {@link Environment}
 */
public S3ClientFactory(final AWSCredentialsProvider awsCredentialsProvider,
        final AwsRegionProvider regionProvider, final Environment environment) {
    this.awsCredentialsProvider = awsCredentialsProvider;

    /*
     * Use the Spring property binder to dynamically map properties under a common root into a map of key to object.
     *
     * In this case we're trying to get bucketName -> BucketProperties
     *
     * So if there were properties like:
     * genie.aws.s3.buckets.someBucket1.roleARN = blah
     * genie.aws.s3.buckets.someBucket2.region = us-east-1
     * genie.aws.s3.buckets.someBucket2.roleARN = blah
     *
     * The result of this should be two entries in the map "bucket1" and "bucket2" mapping to property binding
     * object instances of BucketProperties with the correct property set or null if option wasn't specified.
     */
    this.bucketProperties = Binder.get(environment)
            .bind(BUCKET_PROPERTIES_ROOT_KEY, Bindable.mapOf(String.class, BucketProperties.class))
            .orElse(Collections.emptyMap());

    // Set the initial size to the number of special cases defined in properties + 1 for the default client
    // NOTE: Should we proactively create all necessary clients or be lazy about it? For now, lazy.
    final int initialCapacity = this.bucketProperties.size() + 1;
    this.clientCache = new ConcurrentHashMap<>(initialCapacity);
    this.transferManagerCache = new ConcurrentHashMap<>(initialCapacity);

    String tmpRegion;
    try {
        tmpRegion = regionProvider.getRegion();
    } catch (final SdkClientException e) {
        tmpRegion = Regions.getCurrentRegion() != null ? Regions.getCurrentRegion().getName()
                : Regions.US_EAST_1.getName();
        log.warn("Couldn't determine the AWS region from the provider ({}) supplied. Defaulting to {}",
                regionProvider.toString(), tmpRegion);
    }
    this.defaultRegion = Regions.fromName(tmpRegion);

    // Create a token service client to use if we ever need to assume a role
    // TODO: Perhaps this should be just set to null if the bucket properties are empty as we'll never need it?
    this.stsClient = AWSSecurityTokenServiceClientBuilder.standard().withRegion(this.defaultRegion)
            .withCredentials(this.awsCredentialsProvider).build();

    this.bucketToClientKey = new ConcurrentHashMap<>();
}

From source file:org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.java

private List<String> getExcludeAutoConfigurationsProperty() {
    if (getEnvironment() instanceof ConfigurableEnvironment) {
        Binder binder = Binder.get(getEnvironment());
        return binder.bind(PROPERTY_NAME_AUTOCONFIGURE_EXCLUDE, String[].class).map(Arrays::asList)
                .orElse(Collections.emptyList());
    }/*from   w  w w. j  a v a2  s. c  om*/
    String[] excludes = getEnvironment().getProperty(PROPERTY_NAME_AUTOCONFIGURE_EXCLUDE, String[].class);
    return (excludes != null) ? Arrays.asList(excludes) : Collections.emptyList();
}

From source file:org.springframework.boot.context.logging.LoggingApplicationListener.java

protected void setLogLevels(LoggingSystem system, Environment environment) {
    if (!(environment instanceof ConfigurableEnvironment)) {
        return;/*from   www .  j a  va2s  .c o  m*/
    }
    Binder binder = Binder.get(environment);
    Map<String, String[]> groups = getGroups();
    binder.bind("logging.group", STRING_STRINGS_MAP.withExistingValue(groups));
    Map<String, String> levels = binder.bind("logging.level", STRING_STRING_MAP)
            .orElseGet(Collections::emptyMap);
    levels.forEach((name, level) -> {
        String[] groupedNames = groups.get(name);
        if (ObjectUtils.isEmpty(groupedNames)) {
            setLogLevel(system, name, level);
        } else {
            setLogLevel(system, groupedNames, level);
        }
    });
}