From bb6385e08a0cf467a4d5b2c9ee2ecf07c54d2f9e Mon Sep 17 00:00:00 2001 From: Ken Dombeck Date: Thu, 12 Dec 2013 15:43:34 -0600 Subject: [PATCH] DATACOUCH-57 - Allow for the configuration of CouchbaseClient to use properties from property files or SPEL expressions. --- .../couchbase/config/CouchbaseParser.java | 42 ++------------- .../CouchbaseTranslationServiceParser.java | 7 ++- .../couchbase/core/CouchbaseFactoryBean.java | 53 +++++++++++++++++++ .../CouchbaseParserIntegrationTests.java | 40 +++++--------- .../resources/namespace/couchbase-bean.xml | 9 +++- .../resources/namespace/couchbase.properties | 3 ++ 6 files changed, 86 insertions(+), 68 deletions(-) create mode 100644 src/test/resources/namespace/couchbase.properties diff --git a/src/main/java/org/springframework/data/couchbase/config/CouchbaseParser.java b/src/main/java/org/springframework/data/couchbase/config/CouchbaseParser.java index 3473f8fd..52b01da5 100644 --- a/src/main/java/org/springframework/data/couchbase/config/CouchbaseParser.java +++ b/src/main/java/org/springframework/data/couchbase/config/CouchbaseParser.java @@ -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}. - *

- * 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 convertHosts(final String hosts) { - final String[] split = hosts.split(","); - final List nodes = new ArrayList(); - 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; - } - } diff --git a/src/main/java/org/springframework/data/couchbase/config/CouchbaseTranslationServiceParser.java b/src/main/java/org/springframework/data/couchbase/config/CouchbaseTranslationServiceParser.java index e879916c..eb4920cc 100644 --- a/src/main/java/org/springframework/data/couchbase/config/CouchbaseTranslationServiceParser.java +++ b/src/main/java/org/springframework/data/couchbase/config/CouchbaseTranslationServiceParser.java @@ -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. * diff --git a/src/main/java/org/springframework/data/couchbase/core/CouchbaseFactoryBean.java b/src/main/java/org/springframework/data/couchbase/core/CouchbaseFactoryBean.java index 90b8fd40..8918efb9 100644 --- a/src/main/java/org/springframework/data/couchbase/core/CouchbaseFactoryBean.java +++ b/src/main/java/org/springframework/data/couchbase/core/CouchbaseFactoryBean.java @@ -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, 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 convertHosts(final String hosts) { + String[] split = hosts.split(","); + List nodes = new ArrayList(); + + 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. * diff --git a/src/test/java/org/springframework/data/couchbase/config/CouchbaseParserIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/config/CouchbaseParserIntegrationTests.java index 08c65b1d..7b25521b 100644 --- a/src/test/java/org/springframework/data/couchbase/config/CouchbaseParserIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/config/CouchbaseParserIntegrationTests.java @@ -16,42 +16,28 @@ package org.springframework.data.couchbase.config; -import org.junit.Before; import org.junit.Test; -import org.springframework.beans.factory.config.BeanDefinition; -import org.springframework.beans.factory.support.BeanDefinitionReader; -import org.springframework.beans.factory.support.DefaultListableBeanFactory; -import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; -import org.springframework.core.io.ClassPathResource; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.*; /** * @author Michael Nitschinger */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = "classpath:namespace/couchbase-bean.xml") public class CouchbaseParserIntegrationTests { - - DefaultListableBeanFactory factory; - BeanDefinitionReader reader; - - @Before - public void setUp() { - factory = new DefaultListableBeanFactory(); - reader = new XmlBeanDefinitionReader(factory); - } + @Autowired + ApplicationContext ctx; @Test public void readsCouchbaseAttributesCorrectly() { - reader.loadBeanDefinitions(new ClassPathResource("namespace/couchbase-bean.xml")); - - BeanDefinition definition = factory.getBeanDefinition("couchbase"); - assertEquals(3, definition.getConstructorArgumentValues().getArgumentCount()); - - definition = factory.getBeanDefinition("couchbase2"); - assertEquals(3, definition.getConstructorArgumentValues().getArgumentCount()); - - factory.getBean("couchbase"); - factory.getBean("couchbase2"); + assertTrue(ctx.containsBean("couchbase")); + assertTrue(ctx.containsBean("couchbase2")); + assertTrue(ctx.containsBean("couchbase3")); } - } diff --git a/src/test/resources/namespace/couchbase-bean.xml b/src/test/resources/namespace/couchbase-bean.xml index 57b406c0..3b64b645 100644 --- a/src/test/resources/namespace/couchbase-bean.xml +++ b/src/test/resources/namespace/couchbase-bean.xml @@ -2,8 +2,12 @@ + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> + + @@ -13,4 +17,5 @@ - \ No newline at end of file + + diff --git a/src/test/resources/namespace/couchbase.properties b/src/test/resources/namespace/couchbase.properties new file mode 100644 index 00000000..8fcc7d11 --- /dev/null +++ b/src/test/resources/namespace/couchbase.properties @@ -0,0 +1,3 @@ +couchbase.host=127.0.0.1 +couchbase.bucket=default +couchbase.password=