Skip to content

Commit 2ed710c

Browse files
committed
DATACOUCH-6 - add support for xml-based configurations
1 parent dd9c3ed commit 2ed710c

File tree

6 files changed

+412
-1
lines changed

6 files changed

+412
-1
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.data.couchbase.config;
18+
19+
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
20+
import org.springframework.data.couchbase.repository.config.CouchbaseRepositoryConfigurationExtension;
21+
import org.springframework.data.repository.config.RepositoryBeanDefinitionParser;
22+
import org.springframework.data.repository.config.RepositoryConfigurationExtension;
23+
24+
/**
25+
* {@link org.springframework.beans.factory.xml.NamespaceHandler} for Couchbase configuration.
26+
*
27+
* @author Michael Nitschinger
28+
*/
29+
public class CouchbaseNamespaceHandler extends NamespaceHandlerSupport {
30+
31+
public void init() {
32+
RepositoryConfigurationExtension extension = new CouchbaseRepositoryConfigurationExtension();
33+
RepositoryBeanDefinitionParser repositoryBeanDefinitionParser = new RepositoryBeanDefinitionParser(extension);
34+
35+
registerBeanDefinitionParser("repositories", repositoryBeanDefinitionParser);
36+
registerBeanDefinitionParser("mongo", new CouchbaseParser());
37+
registerBeanDefinitionParser("jmx", new CouchbaseJmxParser());
38+
registerBeanDefinitionParser("template", new CouchbaseTemplateParser());
39+
}
40+
41+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.data.couchbase.config;
18+
19+
import com.couchbase.client.CouchbaseClient;
20+
import org.springframework.beans.factory.config.BeanDefinition;
21+
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
22+
import org.springframework.beans.factory.xml.BeanDefinitionParser;
23+
import org.springframework.beans.factory.xml.ParserContext;
24+
import org.springframework.data.config.BeanComponentDefinitionBuilder;
25+
import org.springframework.data.config.ParsingUtils;
26+
import org.w3c.dom.Element;
27+
28+
/**
29+
* Parser for "<couchbase>" definitions.
30+
*
31+
* @author Michael Nitschinger
32+
*/
33+
public class CouchbaseParser implements BeanDefinitionParser {
34+
35+
@Override
36+
public BeanDefinition parse(final Element element, final ParserContext parserContext) {
37+
Object source = parserContext.extractSource(element);
38+
String id = element.getAttribute("id");
39+
40+
BeanComponentDefinitionBuilder helper = new BeanComponentDefinitionBuilder(element, parserContext);
41+
42+
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CouchbaseClient.class);
43+
builder.s
44+
ParsingUtils.setPropertyValue(builder, element, "port", "port");
45+
ParsingUtils.setPropertyValue(builder, element, "host", "host");
46+
47+
}
48+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.data.couchbase.core;
18+
19+
import com.couchbase.client.CouchbaseClient;
20+
import com.couchbase.client.CouchbaseConnectionFactory;
21+
import com.couchbase.client.CouchbaseConnectionFactoryBuilder;
22+
import net.spy.memcached.FailureMode;
23+
import org.springframework.beans.factory.DisposableBean;
24+
import org.springframework.beans.factory.FactoryBean;
25+
import org.springframework.beans.factory.InitializingBean;
26+
import org.springframework.dao.DataAccessException;
27+
import org.springframework.dao.support.PersistenceExceptionTranslator;
28+
29+
import java.net.URI;
30+
import java.util.ArrayList;
31+
import java.util.Arrays;
32+
import java.util.Collections;
33+
import java.util.List;
34+
import java.util.concurrent.TimeUnit;
35+
36+
/**
37+
* Convenient Factory for configuring Couchbase.
38+
*
39+
* @author Michael Nitschinger
40+
*/
41+
public class CouchbaseFactoryBean implements FactoryBean<CouchbaseClient>, InitializingBean,
42+
DisposableBean, PersistenceExceptionTranslator{
43+
44+
public static final String DEFAULT_NODE = "127.0.0.1";
45+
public static final String DEFAULT_BUCKET = "default";
46+
public static final String DEFAULT_PASSWORD = "";
47+
48+
private CouchbaseClient couchbaseClient;
49+
private PersistenceExceptionTranslator exceptionTranslator = new CouchbaseExceptionTranslator();
50+
private String bucket;
51+
private String password;
52+
private List<URI> nodes;
53+
private CouchbaseConnectionFactoryBuilder builder = new CouchbaseConnectionFactoryBuilder();
54+
55+
public void setObservePollInterval(final int interval) {
56+
builder.setObsPollInterval(interval);
57+
}
58+
59+
public void setObservePollMax(final int max) {
60+
builder.setObsPollMax(max);
61+
}
62+
63+
public void setReconnectThresholdTime(final int time) {
64+
builder.setReconnectThresholdTime(time, TimeUnit.SECONDS);
65+
}
66+
67+
public void setViewTimeout(final int timeout) {
68+
builder.setViewTimeout(timeout);
69+
}
70+
71+
public void setFailureMode(final String mode) {
72+
builder.setFailureMode(FailureMode.valueOf(mode));
73+
}
74+
75+
public void setOpTimeout(final int timeout) {
76+
builder.setOpTimeout(timeout);
77+
}
78+
79+
public void setOpQueueMaxBlockTime(final int time) {
80+
builder.setOpQueueMaxBlockTime(time);
81+
}
82+
83+
@Override
84+
public void destroy() throws Exception {
85+
couchbaseClient.shutdown();
86+
}
87+
88+
@Override
89+
public CouchbaseClient getObject() throws Exception {
90+
return couchbaseClient;
91+
}
92+
93+
@Override
94+
public Class<?> getObjectType() {
95+
return CouchbaseClient.class;
96+
}
97+
98+
@Override
99+
public boolean isSingleton() {
100+
return true;
101+
}
102+
103+
public void setNodes(final URI[] nodes) {
104+
this.nodes = filterNonNullElementsAsList(nodes);
105+
}
106+
107+
private <T> List<T> filterNonNullElementsAsList(T[] elements) {
108+
if (elements == null) {
109+
return Collections.emptyList();
110+
}
111+
112+
List<T> candidateElements = new ArrayList<T>();
113+
for (T element : elements) {
114+
if (element != null) {
115+
candidateElements.add(element);
116+
}
117+
}
118+
119+
return Collections.unmodifiableList(candidateElements);
120+
}
121+
122+
@Override
123+
public void afterPropertiesSet() throws Exception {
124+
nodes = nodes != null ? nodes : Arrays.asList(new URI("http://" + DEFAULT_NODE + ":8091/pools"));
125+
bucket = bucket != null ? bucket : DEFAULT_BUCKET;
126+
password = password != null ? password : DEFAULT_PASSWORD;
127+
128+
CouchbaseConnectionFactory factory = builder.buildCouchbaseConnection(nodes, bucket, password);
129+
couchbaseClient = new CouchbaseClient(factory);
130+
}
131+
132+
@Override
133+
public DataAccessException translateExceptionIfPossible(final RuntimeException ex) {
134+
return exceptionTranslator.translateExceptionIfPossible(ex);
135+
}
136+
}

src/main/resources/org/springframework/data/couchbase/config/spring-couchbase-1.0.xsd

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,122 @@ Defines a CouchbaseClient instance used for accessing a Couchbase Cluster.
2828
</xsd:annotation>
2929
</xsd:element>
3030

31-
<!-- nodes -->
31+
<xsd:element name="repositories">
32+
<xsd:complexType>
33+
<xsd:complexContent>
34+
<xsd:extension base="repository:repositories">
35+
<xsd:attributeGroup ref="couchbase-repository-attributes"/>
36+
<xsd:attributeGroup ref="repository:repository-attributes"/>
37+
</xsd:extension>
38+
</xsd:complexContent>
39+
</xsd:complexType>
40+
</xsd:element>
41+
42+
<xsd:element name="template">
43+
<xsd:complexType>
44+
<xsd:attribute name="id" type="xsd:string" use="optional">
45+
<xsd:annotation>
46+
<xsd:documentation><![CDATA[
47+
The name of the couchbase definition (by default "couchbaseFactory").]]></xsd:documentation>
48+
</xsd:annotation>
49+
</xsd:attribute>
50+
<xsd:attribute name="converter-ref" type="converterRef" use="optional">
51+
<xsd:annotation>
52+
<xsd:documentation><![CDATA[
53+
The reference to a CouchbaseConverter instance.
54+
]]>
55+
</xsd:documentation>
56+
<xsd:appinfo>
57+
<tool:annotation kind="ref">
58+
<tool:assignable-to type="org.springframework.data.couchbase.core.convert.CouchbaseConverter"/>
59+
</tool:annotation>
60+
</xsd:appinfo>
61+
</xsd:annotation>
62+
</xsd:attribute>
63+
<xsd:attribute name="db-ref" type="xsd:string"
64+
use="optional">
65+
<xsd:annotation>
66+
<xsd:documentation>
67+
The reference to a CouchbaseClient object.
68+
</xsd:documentation>
69+
<xsd:appinfo>
70+
<tool:annotation kind="ref">
71+
<tool:assignable-to
72+
type="com.couchbase.client.CouchbaseClient" />
73+
</tool:annotation>
74+
</xsd:appinfo>
75+
</xsd:annotation>
76+
</xsd:attribute>
77+
</xsd:complexType>
78+
</xsd:element>
79+
80+
<xsd:element name="jmx">
81+
<xsd:annotation>
82+
<xsd:documentation><![CDATA[
83+
Defines a JMX Model MBeans for monitoring a Couchbase cluster'.
84+
]]></xsd:documentation>
85+
</xsd:annotation>
86+
<xsd:complexType>
87+
<xsd:attribute name="couchbase-ref" type="couchbaseRef" use="optional">
88+
<xsd:annotation>
89+
<xsd:documentation><![CDATA[
90+
The name of the Couchbase object that determines what connection to monitor. (by default "couchbase").
91+
]]></xsd:documentation>
92+
</xsd:annotation>
93+
</xsd:attribute>
94+
</xsd:complexType>
95+
</xsd:element>
96+
97+
<xsd:simpleType name="converterRef">
98+
<xsd:annotation>
99+
<xsd:appinfo>
100+
<tool:annotation kind="ref">
101+
<tool:assignable-to type="org.springframework.data.couchbase.core.convert.CouchbaseConverter"/>
102+
</tool:annotation>
103+
</xsd:appinfo>
104+
</xsd:annotation>
105+
<xsd:union memberTypes="xsd:string"/>
106+
</xsd:simpleType>
107+
108+
<xsd:simpleType name="couchbaseTemplateRef">
109+
<xsd:annotation>
110+
<xsd:appinfo>
111+
<tool:annotation kind="ref">
112+
<tool:assignable-to type="org.springframework.data.couchbase.core.CouchbaseTemplate"/>
113+
</tool:annotation>
114+
</xsd:appinfo>
115+
</xsd:annotation>
116+
<xsd:union memberTypes="xsd:string"/>
117+
</xsd:simpleType>
118+
119+
<xsd:simpleType name="couchbaseRef">
120+
<xsd:annotation>
121+
<xsd:appinfo>
122+
<tool:annotation kind="ref">
123+
<tool:assignable-to type="org.springframework.data.couchbase.core.CouchbaseFactoryBean"/>
124+
</tool:annotation>
125+
</xsd:appinfo>
126+
</xsd:annotation>
127+
<xsd:union memberTypes="xsd:string"/>
128+
</xsd:simpleType>
129+
130+
<xsd:attributeGroup name="couchbase-repository-attributes">
131+
<xsd:attribute name="couchbase-template-ref" type="couchbaseTemplateRef" default="couchbaseTemplate">
132+
<xsd:annotation>
133+
<xsd:documentation>
134+
The reference to a CouchbaseTemplate. Will default to 'couchbaseTemplate'.
135+
</xsd:documentation>
136+
</xsd:annotation>
137+
</xsd:attribute>
138+
</xsd:attributeGroup>
32139

33140
<xsd:complexType name="couchbaseType">
141+
<xsd:attribute name="id" type="xsd:string" use="optional">
142+
<xsd:annotation>
143+
<xsd:documentation><![CDATA[
144+
The name of the couchbase definition (by default "couchbase").]]></xsd:documentation>
145+
</xsd:annotation>
146+
</xsd:attribute>
34147
<xsd:attribute name="bucket" type="xsd:string" use="optional">
35148
<xsd:annotation>
36149
<xsd:documentation><![CDATA[
@@ -45,6 +158,13 @@ The password of the bucket to connect to. Default is "" (empty).
45158
]]></xsd:documentation>
46159
</xsd:annotation>
47160
</xsd:attribute>
161+
<xsd:attribute name="host" type="xsd:string" use="optional">
162+
<xsd:annotation>
163+
<xsd:documentation><![CDATA[
164+
The host to connect to a Couchbase server. Default is localhost.
165+
]]></xsd:documentation>
166+
</xsd:annotation>
167+
</xsd:attribute>
48168
</xsd:complexType>
49169

50170

0 commit comments

Comments
 (0)