001/* 002 * Copyright 2012-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.autoconfigure.couchbase; 018 019import java.util.List; 020 021import com.couchbase.client.core.env.KeyValueServiceConfig; 022import com.couchbase.client.core.env.QueryServiceConfig; 023import com.couchbase.client.core.env.ViewServiceConfig; 024import com.couchbase.client.java.Bucket; 025import com.couchbase.client.java.Cluster; 026import com.couchbase.client.java.CouchbaseCluster; 027import com.couchbase.client.java.cluster.ClusterInfo; 028import com.couchbase.client.java.env.DefaultCouchbaseEnvironment; 029 030import org.springframework.boot.autoconfigure.couchbase.CouchbaseProperties.Endpoints; 031import org.springframework.context.annotation.Bean; 032import org.springframework.context.annotation.Configuration; 033import org.springframework.context.annotation.DependsOn; 034import org.springframework.context.annotation.Primary; 035 036/** 037 * Support class to configure Couchbase based on {@link CouchbaseProperties}. 038 * 039 * @author Stephane Nicoll 040 * @since 2.1.0 041 */ 042@Configuration 043public class CouchbaseConfiguration { 044 045 private final CouchbaseProperties properties; 046 047 public CouchbaseConfiguration(CouchbaseProperties properties) { 048 this.properties = properties; 049 } 050 051 @Bean 052 @Primary 053 public DefaultCouchbaseEnvironment couchbaseEnvironment() { 054 return initializeEnvironmentBuilder(this.properties).build(); 055 } 056 057 @Bean 058 @Primary 059 public Cluster couchbaseCluster() { 060 return CouchbaseCluster.create(couchbaseEnvironment(), determineBootstrapHosts()); 061 } 062 063 /** 064 * Determine the Couchbase nodes to bootstrap from. 065 * @return the Couchbase nodes to bootstrap from 066 */ 067 protected List<String> determineBootstrapHosts() { 068 return this.properties.getBootstrapHosts(); 069 } 070 071 @Bean 072 @Primary 073 @DependsOn("couchbaseClient") 074 public ClusterInfo couchbaseClusterInfo() { 075 return couchbaseCluster().clusterManager(this.properties.getBucket().getName(), 076 this.properties.getBucket().getPassword()).info(); 077 } 078 079 @Bean 080 @Primary 081 public Bucket couchbaseClient() { 082 return couchbaseCluster().openBucket(this.properties.getBucket().getName(), 083 this.properties.getBucket().getPassword()); 084 } 085 086 /** 087 * Initialize an environment builder based on the specified settings. 088 * @param properties the couchbase properties to use 089 * @return the {@link DefaultCouchbaseEnvironment} builder. 090 */ 091 protected DefaultCouchbaseEnvironment.Builder initializeEnvironmentBuilder( 092 CouchbaseProperties properties) { 093 CouchbaseProperties.Endpoints endpoints = properties.getEnv().getEndpoints(); 094 CouchbaseProperties.Timeouts timeouts = properties.getEnv().getTimeouts(); 095 DefaultCouchbaseEnvironment.Builder builder = DefaultCouchbaseEnvironment 096 .builder(); 097 if (timeouts.getConnect() != null) { 098 builder = builder.connectTimeout(timeouts.getConnect().toMillis()); 099 } 100 builder = builder.keyValueServiceConfig( 101 KeyValueServiceConfig.create(endpoints.getKeyValue())); 102 if (timeouts.getKeyValue() != null) { 103 builder = builder.kvTimeout(timeouts.getKeyValue().toMillis()); 104 } 105 if (timeouts.getQuery() != null) { 106 builder = builder.queryTimeout(timeouts.getQuery().toMillis()); 107 builder = builder.queryServiceConfig(getQueryServiceConfig(endpoints)); 108 builder = builder.viewServiceConfig(getViewServiceConfig(endpoints)); 109 } 110 if (timeouts.getSocketConnect() != null) { 111 builder = builder 112 .socketConnectTimeout((int) timeouts.getSocketConnect().toMillis()); 113 } 114 if (timeouts.getView() != null) { 115 builder = builder.viewTimeout(timeouts.getView().toMillis()); 116 } 117 CouchbaseProperties.Ssl ssl = properties.getEnv().getSsl(); 118 if (ssl.getEnabled()) { 119 builder = builder.sslEnabled(true); 120 if (ssl.getKeyStore() != null) { 121 builder = builder.sslKeystoreFile(ssl.getKeyStore()); 122 } 123 if (ssl.getKeyStorePassword() != null) { 124 builder = builder.sslKeystorePassword(ssl.getKeyStorePassword()); 125 } 126 } 127 return builder; 128 } 129 130 private QueryServiceConfig getQueryServiceConfig(Endpoints endpoints) { 131 return QueryServiceConfig.create(endpoints.getQueryservice().getMinEndpoints(), 132 endpoints.getQueryservice().getMaxEndpoints()); 133 } 134 135 private ViewServiceConfig getViewServiceConfig(Endpoints endpoints) { 136 return ViewServiceConfig.create(endpoints.getViewservice().getMinEndpoints(), 137 endpoints.getViewservice().getMaxEndpoints()); 138 } 139 140}