DATACOUCH-57 - Allow for the configuration of CouchbaseClient to use properties

from property files or SPEL expressions.
This commit is contained in:
Ken Dombeck
2013-12-12 15:43:34 -06:00
committed by Michael Nitschinger
parent 7b6a3a9894
commit bb6385e08a
6 changed files with 86 additions and 68 deletions

View File

@@ -17,19 +17,15 @@
package org.springframework.data.couchbase.config;
import com.couchbase.client.CouchbaseClient;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.data.config.ParsingUtils;
import org.springframework.data.couchbase.core.CouchbaseFactoryBean;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
@@ -50,7 +46,7 @@ public class CouchbaseParser extends AbstractSingleBeanDefinitionParser {
*/
@Override
protected Class getBeanClass(final Element element) {
return CouchbaseClient.class;
return CouchbaseFactoryBean.class;
}
/**
@@ -61,14 +57,9 @@ public class CouchbaseParser extends AbstractSingleBeanDefinitionParser {
*/
@Override
protected void doParse(final Element element, final BeanDefinitionBuilder bean) {
String host = element.getAttribute("host");
bean.addConstructorArgValue(convertHosts(StringUtils.hasText(host) ? host : CouchbaseFactoryBean.DEFAULT_NODE));
String bucket = element.getAttribute("bucket");
bean.addConstructorArgValue(StringUtils.hasText(bucket) ? bucket : CouchbaseFactoryBean.DEFAULT_BUCKET);
String password = element.getAttribute("password");
bean.addConstructorArgValue(StringUtils.hasText(password) ? password : CouchbaseFactoryBean.DEFAULT_PASSWORD);
bean.setDestroyMethodName(CouchbaseFactoryBean.DEFAULT_DESTROY_METHOD);
ParsingUtils.setPropertyValue(bean, element, "host", "host");
ParsingUtils.setPropertyValue(bean, element, "bucket", "bucket");
ParsingUtils.setPropertyValue(bean, element, "password", "password");
setLogger();
}
@@ -95,27 +86,4 @@ public class CouchbaseParser extends AbstractSingleBeanDefinitionParser {
return StringUtils.hasText(id) ? id : BeanNames.COUCHBASE;
}
/**
* Convert a list of hosts into a URI format that can be used by the {@link CouchbaseClient}.
* <p/>
* To make it simple to use, the list of hosts can be passed in as a comma separated list. This list gets parsed
* and converted into a URI format that is suitable for the underlying {@link CouchbaseClient} object.
*
* @param hosts the host list to convert.
*
* @return the converted list with URIs.
*/
private List<URI> convertHosts(final String hosts) {
final String[] split = hosts.split(",");
final List<URI> nodes = new ArrayList<URI>();
try {
for (final String aSplit : split) {
nodes.add(new URI("http://" + aSplit + ":8091/pools"));
}
} catch (URISyntaxException ex) {
throw new BeanCreationException("Could not convert host list." + ex);
}
return nodes;
}
}

View File

@@ -53,9 +53,12 @@ public class CouchbaseTranslationServiceParser extends AbstractSingleBeanDefinit
@Override
protected void doParse(final Element element, final BeanDefinitionBuilder bean) {
final String objectMapper = element.getAttribute("objectMapper");
bean.addPropertyValue("objectMapper", StringUtils.hasText(objectMapper) ? objectMapper : new ObjectMapper());
if (StringUtils.hasText(objectMapper)) {
bean.addPropertyReference("objectMapper", objectMapper);
} else {
bean.addPropertyValue("objectMapper", new ObjectMapper());
}
}
/**
* Resolve the bean ID and assign a default if not set.
*

View File

@@ -20,6 +20,7 @@ import com.couchbase.client.CouchbaseClient;
import com.couchbase.client.CouchbaseConnectionFactory;
import com.couchbase.client.CouchbaseConnectionFactoryBuilder;
import net.spy.memcached.FailureMode;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
@@ -27,6 +28,7 @@ import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -205,6 +207,57 @@ public class CouchbaseFactoryBean implements FactoryBean<CouchbaseClient>, Initi
return true;
}
/**
* Set the bucket to be used.
*
* @param bucket the bucket to use.
*/
public void setBucket(final String bucket) {
this.bucket = bucket;
}
/**
* Set the password.
*
* @param password the password to use.
*/
public void setPassword(final String password) {
this.password = password;
}
/**
* Set the array of nodes from a delimited list of hosts.
*
* @param hosts a comma separated list of hosts.
*/
public void setHost(final String hosts) {
this.nodes = convertHosts(hosts);
}
/**
* Convert a list of hosts into a URI format that can be used by the {@link CouchbaseClient}.
*
* To make it simple to use, the list of hosts can be passed in as a comma separated list. This list gets parsed
* and converted into a URI format that is suitable for the underlying {@link CouchbaseClient} object.
*
* @param hosts the host list to convert.
* @return the converted list with URIs.
*/
private List<URI> convertHosts(final String hosts) {
String[] split = hosts.split(",");
List<URI> nodes = new ArrayList<URI>();
try {
for (int i = 0; i < split.length; i++) {
nodes.add(new URI("http://" + split[i] + ":8091/pools"));
}
} catch (URISyntaxException ex) {
throw new BeanCreationException("Could not convert host list." + ex);
}
return nodes;
}
/**
* Set the nodes as an array of URIs.
*