DATACOUCH-19 - adding optional bucket creation step.

This commit is contained in:
Michael Nitschinger
2013-07-29 13:19:54 +02:00
parent a1279c971e
commit 3a72ca7f96
3 changed files with 107 additions and 7 deletions

View File

@@ -74,6 +74,13 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>

View File

@@ -0,0 +1,62 @@
package org.springframework.data.couchbase;
import com.couchbase.client.ClusterManager;
import com.couchbase.client.clustermanager.BucketType;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.DefaultHttpClient;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
import java.util.Arrays;
public class BucketCreator implements InitializingBean {
private final String hostUri;
private final String adminUser;
private final String adminPass;
public BucketCreator(String host, String user, String pass) {
hostUri = host;
adminUser = user;
adminPass = pass;
}
@Override
public void afterPropertiesSet() throws Exception {
DefaultHttpClient client = new DefaultHttpClient();
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(adminUser, adminPass));
client.setCredentialsProvider(credentialsProvider);
ClientHttpRequestFactory rf = new HttpComponentsClientHttpRequestFactory(client);
RestTemplate template = new RestTemplate(rf);
String fullUri = hostUri + "/default/buckets/default";
try {
template.getForEntity(fullUri, String.class);
} catch (HttpClientErrorException ex) {
if (ex.getMessage().equals("404 Object Not Found")) {
createBucket();
} else {
throw new RuntimeException("Could not see if bucket is already created.", ex);
}
}
}
private void createBucket() throws Exception {
ClusterManager bucketManager =
new ClusterManager(Arrays.asList(new URI(hostUri)), adminUser, adminPass);
bucketManager.createDefaultBucket(BucketType.COUCHBASE, 128, 0, true);
Thread.sleep(5000);
bucketManager.shutdown();
}
}

View File

@@ -20,6 +20,7 @@ import com.couchbase.client.CouchbaseClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.env.Environment;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
@@ -35,14 +36,44 @@ public class TestApplicationConfig extends AbstractCouchbaseConfiguration {
@Autowired
private Environment env;
@Override
@Bean
public CouchbaseClient couchbaseClient() throws Exception {
String host = env.getProperty("couchbase.host", "http://127.0.0.1:8091/pools");
String bucket = env.getProperty("couchbase.bucket", "default");
String password = env.getProperty("couchbase.password", "");
return new CouchbaseClient(Arrays.asList(new URI(host)), bucket, password);
public String couchbaseHost() {
return env.getProperty("couchbase.host", "http://127.0.0.1:8091/pools");
}
@Bean
public String couchbaseBucket() {
return env.getProperty("couchbase.bucket", "default");
}
@Bean
public String couchbasePassword() {
return env.getProperty("couchbase.password", "");
}
@Bean
public String couchbaseAdminUser() {
return env.getProperty("couchbase.adminUser", "Administrator");
}
@Bean
public String couchbaseAdminPassword() {
return env.getProperty("couchbase.adminUser", "password");
}
@Bean
public BucketCreator bucketCreator() throws Exception {
return new BucketCreator(couchbaseHost(), couchbaseAdminUser(), couchbaseAdminPassword());
}
@Override
@Bean
@DependsOn("bucketCreator")
public CouchbaseClient couchbaseClient() throws Exception {
return new CouchbaseClient(
Arrays.asList(new URI(couchbaseHost())), couchbaseBucket(), couchbasePassword());
}
}