DATACOUCH-57 - Allow for the configuration of CouchbaseClient to use properties
from property files or SPEL expressions.
This commit is contained in:
committed by
Michael Nitschinger
parent
7b6a3a9894
commit
bb6385e08a
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,8 +2,12 @@
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:couchbase="http://www.springframework.org/schema/data/couchbase"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/data/couchbase http://www.springframework.org/schema/data/couchbase/spring-couchbase.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
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">
|
||||
|
||||
<context:property-placeholder location="classpath:/namespace/couchbase.properties"/>
|
||||
|
||||
<couchbase:couchbase/>
|
||||
|
||||
@@ -13,4 +17,5 @@
|
||||
|
||||
<bean id="myCustomObjectMapper" class="com.fasterxml.jackson.databind.ObjectMapper"/>
|
||||
|
||||
</beans>
|
||||
<couchbase:couchbase id="couchbase3" host="${couchbase.host}" bucket="${couchbase.bucket}" password="${couchbase.password}" />
|
||||
</beans>
|
||||
|
||||
3
src/test/resources/namespace/couchbase.properties
Normal file
3
src/test/resources/namespace/couchbase.properties
Normal file
@@ -0,0 +1,3 @@
|
||||
couchbase.host=127.0.0.1
|
||||
couchbase.bucket=default
|
||||
couchbase.password=
|
||||
Reference in New Issue
Block a user