Skip to content

Commit

Permalink
Add Couchbase support
Browse files Browse the repository at this point in the history
  • Loading branch information
eddumelendez authored and snicoll committed Feb 16, 2016
1 parent ecf11e0 commit 76f1ca4
Show file tree
Hide file tree
Showing 30 changed files with 1,006 additions and 2 deletions.
5 changes: 5 additions & 0 deletions spring-boot-actuator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@
<artifactId>spring-data-cassandra</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-couchbase</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.jms.ConnectionFactory;
import javax.sql.DataSource;

import com.couchbase.client.java.Bucket;
import com.datastax.driver.core.Cluster;
import org.apache.solr.client.solrj.SolrClient;
import org.elasticsearch.client.Client;
Expand All @@ -32,6 +33,7 @@
import org.springframework.boot.actuate.health.ApplicationHealthIndicator;
import org.springframework.boot.actuate.health.CassandraHealthIndicator;
import org.springframework.boot.actuate.health.CompositeHealthIndicator;
import org.springframework.boot.actuate.health.CouchbaseHealthIndicator;
import org.springframework.boot.actuate.health.DataSourceHealthIndicator;
import org.springframework.boot.actuate.health.DiskSpaceHealthIndicator;
import org.springframework.boot.actuate.health.DiskSpaceHealthIndicatorProperties;
Expand All @@ -54,6 +56,7 @@
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.CouchbaseAutoConfiguration;
import org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
Expand All @@ -71,6 +74,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.core.ResolvableType;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.couchbase.core.CouchbaseOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.jdbc.core.JdbcTemplate;
Expand All @@ -84,11 +88,12 @@
* @author Stephane Nicoll
* @author Phillip Webb
* @author Tommy Ludwig
* @author Eddú Meléndez
* @since 1.1.0
*/
@Configuration
@AutoConfigureBefore({ EndpointAutoConfiguration.class })
@AutoConfigureAfter({ CassandraAutoConfiguration.class,
@AutoConfigureAfter({ CouchbaseAutoConfiguration.class, CassandraAutoConfiguration.class,
CassandraDataAutoConfiguration.class, DataSourceAutoConfiguration.class,
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class,
RedisAutoConfiguration.class, RabbitAutoConfiguration.class,
Expand Down Expand Up @@ -176,6 +181,24 @@ public HealthIndicator cassandraHealthIndicator() {

}

@Configuration
@ConditionalOnClass({ CouchbaseOperations.class, Bucket.class})
@ConditionalOnBean(CouchbaseOperations.class)
@ConditionalOnEnabledHealthIndicator("couchbase")
public static class CouchbaseHealthIndicatorConfiguration extends
CompositeHealthIndicatorConfiguration<CouchbaseHealthIndicator, CouchbaseOperations> {

@Autowired
private Map<String, CouchbaseOperations> couchbaseOperations;

@Bean
@ConditionalOnMissingBean(name = "couchbaseHealthIndicator")
public HealthIndicator couchbaseHealthIndicator() {
return createHealthIndicator(this.couchbaseOperations);
}

}

@Configuration
@ConditionalOnClass(JdbcTemplate.class)
@ConditionalOnBean(DataSource.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.actuate.health;

import java.util.List;

import com.couchbase.client.java.util.features.Version;

import org.springframework.data.couchbase.core.CouchbaseOperations;
import org.springframework.util.Assert;

/**
* {@link HealthIndicator} for Couchbase.
*
* @author Eddú Meléndez
* @since 1.4.0
*/
public class CouchbaseHealthIndicator extends AbstractHealthIndicator {

private CouchbaseOperations couchbaseOperations;

public CouchbaseHealthIndicator(CouchbaseOperations couchbaseOperations) {
Assert.notNull(couchbaseOperations, "CouchbaseOperations must not be null");
this.couchbaseOperations = couchbaseOperations;
}

@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
List<Version> versions = this.couchbaseOperations.getCouchbaseClusterInfo().getAllVersions();
builder.up().withDetail("version", versions.get(0).toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.springframework.boot.actuate.health.ApplicationHealthIndicator;
import org.springframework.boot.actuate.health.CassandraHealthIndicator;
import org.springframework.boot.actuate.health.CouchbaseHealthIndicator;
import org.springframework.boot.actuate.health.DataSourceHealthIndicator;
import org.springframework.boot.actuate.health.DiskSpaceHealthIndicator;
import org.springframework.boot.actuate.health.ElasticsearchHealthIndicator;
Expand Down Expand Up @@ -56,6 +57,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.couchbase.core.CouchbaseOperations;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
Expand All @@ -66,6 +68,7 @@
* @author Christian Dupuis
* @author Stephane Nicoll
* @author Andy Wilkinson
* @author Eddú Meléndez
*/
public class HealthIndicatorAutoConfigurationTests {

Expand Down Expand Up @@ -436,6 +439,19 @@ public void cassandraHealthIndicator() throws Exception {
.isEqualTo(CassandraHealthIndicator.class);
}

@Test
public void couchbaseHealthIndicator() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context,
"management.health.diskspace.enabled:false");
this.context.register(CouchbaseConfiguration.class,
ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class);
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans.size()).isEqualTo(1);
assertThat(beans.values().iterator().next().getClass()).isEqualTo(CouchbaseHealthIndicator.class);
}

@Configuration
@EnableConfigurationProperties
protected static class DataSourceConfig {
Expand Down Expand Up @@ -476,4 +492,15 @@ public CassandraOperations cassandraOperations() {

}

@Configuration
protected static class CouchbaseConfiguration {

@Bean
public CouchbaseOperations couchbaseOperations() {
CouchbaseOperations operations = mock(CouchbaseOperations.class);
return operations;
}

}

}
11 changes: 11 additions & 0 deletions spring-boot-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,17 @@
<artifactId>spring-batch-core</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-couchbase</artifactId>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.autoconfigure.couchbase;

import java.util.Arrays;
import java.util.List;

import javax.validation.Validator;

import com.couchbase.client.java.CouchbaseBucket;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
import org.springframework.data.couchbase.config.CouchbaseBucketFactoryBean;
import org.springframework.data.couchbase.core.mapping.event.ValidatingCouchbaseEventListener;

/**
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration
* Auto-Configuration} for Couchbase.
*
* @author Eddú Meléndez
* @since 1.4.0
*/
@Configuration
@ConditionalOnClass({ CouchbaseBucket.class, CouchbaseBucketFactoryBean.class })
@EnableConfigurationProperties(CouchbaseProperties.class)
public class CouchbaseAutoConfiguration {

@Bean
@ConditionalOnBean(Validator.class)
public ValidatingCouchbaseEventListener validationEventListener(Validator validator) {
return new ValidatingCouchbaseEventListener(validator);
}

@Configuration
static class CouchbaseConfiguration extends AbstractCouchbaseConfiguration {

@Autowired
private CouchbaseProperties properties;

@Override
protected List<String> getBootstrapHosts() {
return Arrays.asList(this.properties.getHosts());
}

@Override
protected String getBucketName() {
return this.properties.getBucketName();
}

@Override
protected String getBucketPassword() {
return this.properties.getBucketPassword();
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.autoconfigure.couchbase;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* Configuration properties for Couchbase.
*
* @author Eddú Meléndez
* @since 1.4.0
*/
@ConfigurationProperties(prefix = "spring.data.couchbase")
public class CouchbaseProperties {

/**
* Couchabase server hosts.
*/
private String[] hosts;

/**
* Couchbase bucket name.
*/
private String bucketName;

/**
* Couchbase bucket password.
*/
private String bucketPassword;

public String[] getHosts() {
return this.hosts;
}

public void setHosts(String[] hosts) {
this.hosts = hosts;
}

public String getBucketName() {
return this.bucketName;
}

public void setBucketName(String bucketName) {
this.bucketName = bucketName;
}

public String getBucketPassword() {
return this.bucketPassword;
}

public void setBucketPassword(String bucketPassword) {
this.bucketPassword = bucketPassword;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Auto-configuration for Couchbase.
*/
package org.springframework.boot.autoconfigure.couchbase;
Loading

0 comments on commit 76f1ca4

Please sign in to comment.