This commit is contained in:
Phillip Webb 2018-04-09 14:49:49 -07:00
parent d89048300a
commit ba85cefce3
8 changed files with 67 additions and 68 deletions

View File

@ -16,6 +16,8 @@
package org.springframework.boot.autoconfigure.couchbase;
import java.util.function.BiFunction;
import com.couchbase.client.core.env.KeyValueServiceConfig;
import com.couchbase.client.core.env.QueryServiceConfig;
import com.couchbase.client.core.env.ViewServiceConfig;
@ -31,6 +33,8 @@ import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.couchbase.CouchbaseProperties.Endpoints;
import org.springframework.boot.autoconfigure.couchbase.CouchbaseProperties.Endpoints.CouchbaseService;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
@ -106,21 +110,15 @@ public class CouchbaseAutoConfiguration {
if (timeouts.getConnect() != null) {
builder = builder.connectTimeout(timeouts.getConnect().toMillis());
}
builder = builder.keyValueServiceConfig(KeyValueServiceConfig.create(
endpoints.getKeyValue()));
builder = builder.keyValueServiceConfig(
KeyValueServiceConfig.create(endpoints.getKeyValue()));
if (timeouts.getKeyValue() != null) {
builder = builder.kvTimeout(timeouts.getKeyValue().toMillis());
}
CouchbaseServiceConfig queryConfig = determineCouchbaseServiceConfig(
endpoints.getQueryservice(), endpoints.getQuery());
builder = builder.queryServiceConfig(QueryServiceConfig.create(
queryConfig.minEndpoints, queryConfig.maxEndpoints));
if (timeouts.getQuery() != null) {
CouchbaseServiceConfig viewConfig = determineCouchbaseServiceConfig(
endpoints.getViewservice(), endpoints.getView());
builder = builder.queryTimeout(timeouts.getQuery().toMillis())
.viewServiceConfig(ViewServiceConfig.create(
viewConfig.minEndpoints, viewConfig.maxEndpoints));
builder = builder.queryTimeout(timeouts.getQuery().toMillis());
builder = builder.queryServiceConfig(getQueryServiceConfig(endpoints));
builder = builder.viewServiceConfig(getViewServiceConfig(endpoints));
}
if (timeouts.getSocketConnect() != null) {
builder = builder.socketConnectTimeout(
@ -131,37 +129,37 @@ public class CouchbaseAutoConfiguration {
}
CouchbaseProperties.Ssl ssl = properties.getEnv().getSsl();
if (ssl.getEnabled()) {
builder.sslEnabled(true);
builder = builder.sslEnabled(true);
if (ssl.getKeyStore() != null) {
builder.sslKeystoreFile(ssl.getKeyStore());
builder = builder.sslKeystoreFile(ssl.getKeyStore());
}
if (ssl.getKeyStorePassword() != null) {
builder.sslKeystorePassword(ssl.getKeyStorePassword());
builder = builder.sslKeystorePassword(ssl.getKeyStorePassword());
}
}
return builder;
}
private CouchbaseServiceConfig determineCouchbaseServiceConfig(
CouchbaseProperties.Endpoints.CouchbaseService couchbaseService,
Integer fallback) {
if (couchbaseService.getMinEndpoints() != 1
|| couchbaseService.getMaxEndpoints() != 1) {
return new CouchbaseServiceConfig(couchbaseService.getMinEndpoints(),
couchbaseService.getMaxEndpoints());
}
int endpoints = (fallback != null ? fallback : 1);
return new CouchbaseServiceConfig(endpoints, endpoints);
@SuppressWarnings("deprecation")
private QueryServiceConfig getQueryServiceConfig(Endpoints endpoints) {
return getServiceConfig(endpoints.getQueryservice(), endpoints.getQuery(),
QueryServiceConfig::create);
}
private static class CouchbaseServiceConfig {
private int minEndpoints;
private int maxEndpoints;
@SuppressWarnings("deprecation")
private ViewServiceConfig getViewServiceConfig(Endpoints endpoints) {
return getServiceConfig(endpoints.getViewservice(), endpoints.getView(),
ViewServiceConfig::create);
}
CouchbaseServiceConfig(int minEndpoints, int maxEndpoints) {
this.minEndpoints = minEndpoints;
this.maxEndpoints = maxEndpoints;
private <T> T getServiceConfig(CouchbaseService service, Integer fallback,
BiFunction<Integer, Integer, T> factory) {
if (service.getMinEndpoints() != 1 || service.getMaxEndpoints() != 1) {
return factory.apply(service.getMinEndpoints(),
service.getMaxEndpoints());
}
int endpoints = (fallback != null ? fallback : 1);
return factory.apply(endpoints, endpoints);
}
}

View File

@ -209,7 +209,6 @@ public class CouchbaseProperties {
}
public static class Ssl {
/**

View File

@ -80,7 +80,8 @@ public class LiquibaseAutoConfiguration {
@Configuration
@ConditionalOnMissingBean(SpringLiquibase.class)
@EnableConfigurationProperties({ DataSourceProperties.class, LiquibaseProperties.class })
@EnableConfigurationProperties({ DataSourceProperties.class,
LiquibaseProperties.class })
@Import(LiquibaseJpaDependencyConfiguration.class)
public static class LiquibaseConfiguration {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.
@ -139,7 +139,8 @@ public class QuartzAutoConfiguration {
ObjectProvider<PlatformTransactionManager> transactionManager) {
return (schedulerFactoryBean) -> {
if (properties.getJobStoreType() == JobStoreType.JDBC) {
DataSource dataSourceToUse = getDataSource(dataSource, quartzDataSource);
DataSource dataSourceToUse = getDataSource(dataSource,
quartzDataSource);
schedulerFactoryBean.setDataSource(dataSourceToUse);
PlatformTransactionManager txManager = transactionManager
.getIfUnique();
@ -159,9 +160,9 @@ public class QuartzAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public QuartzDataSourceInitializer quartzDataSourceInitializer(
DataSource dataSource, @QuartzDataSource ObjectProvider<DataSource> quartzDataSource,
ResourceLoader resourceLoader,
QuartzProperties properties) {
DataSource dataSource,
@QuartzDataSource ObjectProvider<DataSource> quartzDataSource,
ResourceLoader resourceLoader, QuartzProperties properties) {
DataSource dataSourceToUse = getDataSource(dataSource, quartzDataSource);
return new QuartzDataSourceInitializer(dataSourceToUse, resourceLoader,
properties);

View File

@ -25,8 +25,9 @@ import java.lang.annotation.Target;
import org.springframework.beans.factory.annotation.Qualifier;
/**
* Qualifier annotation for a DataSource to be injected into Quartz auto-configuration. Can be used on
* a secondary data source, if there is another one marked as {@code @Primary}.
* Qualifier annotation for a DataSource to be injected into Quartz auto-configuration.
* Can be used on a secondary data source, if there is another one marked as
* {@code @Primary}.
*
* @author Madhura Bhave
* @since 2.0.2
@ -37,4 +38,5 @@ import org.springframework.beans.factory.annotation.Qualifier;
@Documented
@Qualifier
public @interface QuartzDataSource {
}

View File

@ -86,13 +86,13 @@ public class CouchbaseAutoConfigurationTests {
@Test
public void customizeEnvEndpoints() {
testCouchbaseEnv((env) -> {
assertThat(env.kvServiceConfig().minEndpoints()).isEqualTo(2);
assertThat(env.kvServiceConfig().maxEndpoints()).isEqualTo(2);
assertThat(env.queryServiceConfig().minEndpoints()).isEqualTo(3);
assertThat(env.queryServiceConfig().maxEndpoints()).isEqualTo(5);
assertThat(env.viewServiceConfig().minEndpoints()).isEqualTo(4);
assertThat(env.viewServiceConfig().maxEndpoints()).isEqualTo(6);
}, "spring.couchbase.env.endpoints.key-value=2",
assertThat(env.kvServiceConfig().minEndpoints()).isEqualTo(2);
assertThat(env.kvServiceConfig().maxEndpoints()).isEqualTo(2);
assertThat(env.queryServiceConfig().minEndpoints()).isEqualTo(3);
assertThat(env.queryServiceConfig().maxEndpoints()).isEqualTo(5);
assertThat(env.viewServiceConfig().minEndpoints()).isEqualTo(4);
assertThat(env.viewServiceConfig().maxEndpoints()).isEqualTo(6);
}, "spring.couchbase.env.endpoints.key-value=2",
"spring.couchbase.env.endpoints.queryservice.min-endpoints=3",
"spring.couchbase.env.endpoints.queryservice.max-endpoints=5",
"spring.couchbase.env.endpoints.viewservice.min-endpoints=4",
@ -103,22 +103,22 @@ public class CouchbaseAutoConfigurationTests {
@Deprecated
public void customizeEnvEndpointsWithDeprecatedProperties() {
testCouchbaseEnv((env) -> {
assertThat(env.queryServiceConfig().minEndpoints()).isEqualTo(3);
assertThat(env.queryServiceConfig().maxEndpoints()).isEqualTo(3);
assertThat(env.viewServiceConfig().minEndpoints()).isEqualTo(4);
assertThat(env.viewServiceConfig().maxEndpoints()).isEqualTo(4);
}, "spring.couchbase.env.endpoints.query=3",
assertThat(env.queryServiceConfig().minEndpoints()).isEqualTo(3);
assertThat(env.queryServiceConfig().maxEndpoints()).isEqualTo(3);
assertThat(env.viewServiceConfig().minEndpoints()).isEqualTo(4);
assertThat(env.viewServiceConfig().maxEndpoints()).isEqualTo(4);
}, "spring.couchbase.env.endpoints.query=3",
"spring.couchbase.env.endpoints.view=4");
}
@Test
public void customizeEnvEndpointsUsesNewInfrastructure() {
testCouchbaseEnv((env) -> {
assertThat(env.queryServiceConfig().minEndpoints()).isEqualTo(3);
assertThat(env.queryServiceConfig().maxEndpoints()).isEqualTo(5);
assertThat(env.viewServiceConfig().minEndpoints()).isEqualTo(4);
assertThat(env.viewServiceConfig().maxEndpoints()).isEqualTo(6);
}, "spring.couchbase.env.endpoints.query=33",
assertThat(env.queryServiceConfig().minEndpoints()).isEqualTo(3);
assertThat(env.queryServiceConfig().maxEndpoints()).isEqualTo(5);
assertThat(env.viewServiceConfig().minEndpoints()).isEqualTo(4);
assertThat(env.viewServiceConfig().maxEndpoints()).isEqualTo(6);
}, "spring.couchbase.env.endpoints.query=33",
"spring.couchbase.env.endpoints.queryservice.min-endpoints=3",
"spring.couchbase.env.endpoints.queryservice.max-endpoints=5",
"spring.couchbase.env.endpoints.view=44",
@ -129,11 +129,11 @@ public class CouchbaseAutoConfigurationTests {
@Test
public void customizeEnvEndpointsUsesNewInfrastructureWithOnlyMax() {
testCouchbaseEnv((env) -> {
assertThat(env.queryServiceConfig().minEndpoints()).isEqualTo(1);
assertThat(env.queryServiceConfig().maxEndpoints()).isEqualTo(5);
assertThat(env.viewServiceConfig().minEndpoints()).isEqualTo(1);
assertThat(env.viewServiceConfig().maxEndpoints()).isEqualTo(6);
}, "spring.couchbase.env.endpoints.query=33",
assertThat(env.queryServiceConfig().minEndpoints()).isEqualTo(1);
assertThat(env.queryServiceConfig().maxEndpoints()).isEqualTo(5);
assertThat(env.viewServiceConfig().minEndpoints()).isEqualTo(1);
assertThat(env.viewServiceConfig().maxEndpoints()).isEqualTo(6);
}, "spring.couchbase.env.endpoints.query=33",
"spring.couchbase.env.endpoints.queryservice.max-endpoints=5",
"spring.couchbase.env.endpoints.view=44",
"spring.couchbase.env.endpoints.viewservice.max-endpoints=6");

View File

@ -250,8 +250,7 @@ public class LiquibaseAutoConfigurationTests {
@Test
public void liquibaseDataSourceWithoutDataSourceAutoConfiguration() {
this.contextRunner
.withUserConfiguration(LiquibaseDataSourceConfiguration.class)
this.contextRunner.withUserConfiguration(LiquibaseDataSourceConfiguration.class)
.run((context) -> {
SpringLiquibase liquibase = context.getBean(SpringLiquibase.class);
assertThat(liquibase.getDataSource())

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.
@ -211,8 +211,7 @@ public class QuartzAutoConfigurationTests {
@Test
public void dataSourceWithQuartzDataSourceQualifierUsedWhenMultiplePresent() {
load(MultipleDataSourceConfiguration.class,
"spring.quartz.job-store-type=jdbc");
load(MultipleDataSourceConfiguration.class, "spring.quartz.job-store-type=jdbc");
}
private void load(String... environment) {
@ -354,7 +353,8 @@ public class QuartzAutoConfigurationTests {
}
@Configuration
protected static class MultipleDataSourceConfiguration extends BaseQuartzConfiguration {
protected static class MultipleDataSourceConfiguration
extends BaseQuartzConfiguration {
@Bean
@Primary
@ -362,7 +362,6 @@ public class QuartzAutoConfigurationTests {
return new HikariDataSource();
}
@QuartzDataSource
@Bean
public DataSource quartzDataSource() {