Skip to content

Commit 0e72214

Browse files
committed
Merge pull request #42 from simonbasle/beanClusterBucket
[2.0] xml configuration for env, cluster, bucket This implements parsers (and FactoryBeans as needed) for the environment, cluster and bucket definitions. Most tunings on the environment are possible through the xml configuration, except a few that are both more complicated to implement and obscure enough not to warrant the possibility (observeIntervalDelay, reconnectDelay, retryDelay, userAgent, packageNameAndVersion, ioPool, scheduler and eventBus).
2 parents dd521b7 + 38d440a commit 0e72214

20 files changed

+1660
-10
lines changed

src/main/java/org/springframework/data/couchbase/config/BeanNames.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,30 @@
2222
* @author Michael Nitschinger
2323
* @author Simon Baslé
2424
*/
25-
public class BeanNames {
25+
class BeanNames {
2626

2727
/**
28-
* Refers to the <couchbase:env /> bean.
28+
* Refers to the "&lt;couchbase:env /&gt;" bean.
2929
*/
3030
static final String COUCHBASE_ENV = "couchbaseEnv";
3131
/**
32-
* Refers to the "<couchbase:cluster />" bean.
32+
* Refers to the "&lt;couchbase:cluster /&gt;" bean.
3333
*/
3434
static final String COUCHBASE_CLUSTER = "couchbaseCluster";
3535

3636
/**
37-
* Refers to the "<couchbase:bucket />" bean.
37+
* Refers to the "&lt;couchbase:bucket /&gt;" bean.
3838
*/
3939
static final String COUCHBASE_BUCKET = "couchbaseBucket";
4040

4141
/**
42-
* Refers to the "<couchbase:template />" bean.
42+
* Refers to the "&lt;couchbase:template /&gt;" bean.
4343
*/
4444
static final String COUCHBASE_TEMPLATE = "couchbaseTemplate";
4545

4646
/**
47-
* Refers to the "<couchbase:translation-service />" bean
47+
* Refers to the "&lt;couchbase:translation-service /&gt;" bean
4848
*/
4949
static final String TRANSLATION_SERVICE = "couchbaseTranslationService";
5050

51-
5251
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2012-2015 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.java.Bucket;
20+
import com.couchbase.client.java.Cluster;
21+
import com.couchbase.client.java.CouchbaseBucket;
22+
23+
import org.springframework.beans.factory.config.AbstractFactoryBean;
24+
25+
/**
26+
* The Factory Bean to help {@link CouchbaseBucketParser} constructing a {@link Bucket} from a given
27+
* {@link Cluster} reference.
28+
*
29+
* @author Simon Baslé
30+
*/
31+
public class CouchbaseBucketFactoryBean extends AbstractFactoryBean<Bucket> {
32+
33+
private final Cluster cluster;
34+
private final String bucketName;
35+
private final String bucketPassword;
36+
37+
public CouchbaseBucketFactoryBean(Cluster cluster) {
38+
this(cluster, null, null);
39+
}
40+
41+
public CouchbaseBucketFactoryBean(Cluster cluster, String bucketName) {
42+
this(cluster, bucketName, null);
43+
}
44+
45+
public CouchbaseBucketFactoryBean(Cluster cluster, String bucketName, String bucketPassword) {
46+
this.cluster = cluster;
47+
this.bucketName = bucketName;
48+
this.bucketPassword = bucketPassword;
49+
}
50+
51+
@Override
52+
public Class<?> getObjectType() {
53+
return CouchbaseBucket.class;
54+
}
55+
56+
@Override
57+
protected Bucket createInstance() throws Exception {
58+
if (bucketName == null) {
59+
return cluster.openBucket();
60+
} else if (bucketPassword == null) {
61+
return cluster.openBucket(bucketName);
62+
} else {
63+
return cluster.openBucket(bucketName, bucketPassword);
64+
}
65+
}
66+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2012-2015 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.java.Bucket;
20+
import com.couchbase.client.java.Cluster;
21+
import org.w3c.dom.Element;
22+
23+
import org.springframework.beans.factory.support.AbstractBeanDefinition;
24+
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
25+
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
26+
import org.springframework.beans.factory.xml.ParserContext;
27+
import org.springframework.util.StringUtils;
28+
29+
/**
30+
* The parser for XML definition of a {@link Bucket}, to be constructed from a {@link Cluster} reference.
31+
* If no reference is given, the default reference <code>{@value BeanNames#COUCHBASE_CLUSTER}</code> is used.
32+
*
33+
* See attributes {@link #CLUSTER_REF_ATTR}, {@link #BUCKETNAME_ATTR} and {@link #BUCKETPASSWORD_ATTR}.
34+
*
35+
* @author Simon Baslé
36+
*/
37+
public class CouchbaseBucketParser extends AbstractSingleBeanDefinitionParser {
38+
39+
/**
40+
* The <code>cluster-ref</code> attribute in a bucket definition defines the cluster to build from.
41+
*/
42+
public static final String CLUSTER_REF_ATTR = "cluster-ref";
43+
44+
/**
45+
* The <code>bucketName</code> attribute in a bucket definition defines the name of the bucket to open.
46+
*/
47+
public static final String BUCKETNAME_ATTR = "bucketName";
48+
49+
/**
50+
* The <code>bucketPassword</code> attribute in a bucket definition defines the password of the bucket to open.
51+
*/
52+
public static final String BUCKETPASSWORD_ATTR = "bucketPassword";
53+
54+
/**
55+
* Resolve the bean ID and assign a default if not set.
56+
*
57+
* @param element the XML element which contains the attributes.
58+
* @param definition the bean definition to work with.
59+
* @param parserContext encapsulates the parsing state and configuration.
60+
* @return the ID to work with.
61+
*/
62+
@Override
63+
protected String resolveId(final Element element, final AbstractBeanDefinition definition, final ParserContext parserContext) {
64+
String id = super.resolveId(element, definition, parserContext);
65+
return StringUtils.hasText(id) ? id : BeanNames.COUCHBASE_BUCKET;
66+
}
67+
68+
/**
69+
* Defines the bean class that will be constructed.
70+
*
71+
* @param element the XML element which contains the attributes.
72+
* @return the class type to instantiate.
73+
*/
74+
@Override
75+
protected Class getBeanClass(final Element element) {
76+
return CouchbaseBucketFactoryBean.class;
77+
}
78+
79+
/**
80+
* Parse the bean definition and build up the bean.
81+
*
82+
* @param element the XML element which contains the attributes.
83+
* @param builder the builder which builds the bean.
84+
*/
85+
@Override
86+
protected void doParse(final Element element, final BeanDefinitionBuilder builder) {
87+
String clusterRef = element.getAttribute(CLUSTER_REF_ATTR);
88+
if (!StringUtils.hasText(clusterRef)) {
89+
clusterRef = BeanNames.COUCHBASE_CLUSTER;
90+
}
91+
builder.addConstructorArgReference(clusterRef);
92+
93+
String bucketName = element.getAttribute(BUCKETNAME_ATTR);
94+
if (StringUtils.hasText(bucketName)) {
95+
builder.addConstructorArgValue(bucketName);
96+
}
97+
98+
String bucketPassword = element.getAttribute(BUCKETPASSWORD_ATTR);
99+
if (StringUtils.hasText(bucketPassword)) {
100+
builder.addConstructorArgValue(bucketPassword);
101+
}
102+
}
103+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
* Copyright 2012-2015 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 java.util.ArrayList;
20+
import java.util.List;
21+
22+
import com.couchbase.client.java.Cluster;
23+
import com.couchbase.client.java.CouchbaseCluster;
24+
import com.couchbase.client.java.env.CouchbaseEnvironment;
25+
import org.w3c.dom.Element;
26+
27+
import org.springframework.beans.factory.support.AbstractBeanDefinition;
28+
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
29+
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
30+
import org.springframework.beans.factory.xml.ParserContext;
31+
import org.springframework.util.StringUtils;
32+
import org.springframework.util.xml.DomUtils;
33+
34+
/**
35+
* The XML parser for a {@link Cluster} definition.
36+
*
37+
* Such a definition can be tuned by either referencing a {@link CouchbaseEnvironment} via
38+
* the {@value #CLUSTER_ENVIRONMENT_REF} attribute or define a custom environment inline via
39+
* the &lt;{@value #CLUSTER_ENVIRONMENT_TAG}&gt; tag (not recommended, environments should be
40+
* shared as possible). If no environment reference or inline description is provided, the
41+
* default environment reference {@value BeanNames#COUCHBASE_ENV} is used.
42+
*
43+
* To bootstrap the connection, one can provide IPs or hostnames of nodes to connect to
44+
* via 1 or more &lt;{@value #CLUSTER_NODE_TAG}&gt; tags.
45+
*
46+
* @author Simon Baslé
47+
*/
48+
public class CouchbaseClusterParser extends AbstractSingleBeanDefinitionParser {
49+
50+
/**
51+
* The &lt;node&gt; elements in a cluster definition define the bootstrap hosts to use
52+
*/
53+
public static final String CLUSTER_NODE_TAG = "node";
54+
55+
/**
56+
* The unique &lt;env&gt; element in a cluster definition define the environment customizations.
57+
*
58+
* @see CouchbaseEnvironmentParser CouchbaseEnvironmentParser for the possible fields.
59+
* @see #CLUSTER_ENVIRONMENT_REF CLUSTER_ENVIRONMENT_REF as an alternative (giving a reference to
60+
* an env instead of inline description, lower precedence)
61+
*/
62+
public static final String CLUSTER_ENVIRONMENT_TAG = "env";
63+
64+
/**
65+
* The &lt;env-ref&gt; attribute allows to use a reference to an {@link CouchbaseEnvironment} to
66+
* tune the connection.
67+
*
68+
* @see #CLUSTER_ENVIRONMENT_TAG CLUSTER_ENVIRONMENT_TAG for an inline alternative
69+
* (which takes priority over this reference)
70+
*/
71+
public static final String CLUSTER_ENVIRONMENT_REF = "env-ref";
72+
73+
/**
74+
* Resolve the bean ID and assign a default if not set.
75+
*
76+
* @param element the XML element which contains the attributes.
77+
* @param definition the bean definition to work with.
78+
* @param parserContext encapsulates the parsing state and configuration.
79+
* @return the ID to work with.
80+
*/
81+
@Override
82+
protected String resolveId(final Element element, final AbstractBeanDefinition definition, final ParserContext parserContext) {
83+
String id = super.resolveId(element, definition, parserContext);
84+
return StringUtils.hasText(id) ? id : BeanNames.COUCHBASE_CLUSTER;
85+
}
86+
87+
/**
88+
* Defines the bean class that will be constructed.
89+
*
90+
* @param element the XML element which contains the attributes.
91+
* @return the class type to instantiate.
92+
*/
93+
@Override
94+
protected Class getBeanClass(final Element element) {
95+
return CouchbaseCluster.class;
96+
}
97+
98+
/**
99+
* Parse the bean definition and build up the bean.
100+
*
101+
* @param element the XML element which contains the attributes.
102+
* @param bean the builder which builds the bean.
103+
*/
104+
@Override
105+
protected void doParse(final Element element, final BeanDefinitionBuilder bean) {
106+
bean.setFactoryMethod("create");
107+
bean.setDestroyMethodName("disconnect");
108+
109+
parseEnvironment(bean, element);
110+
111+
List<Element> nodes = DomUtils.getChildElementsByTagName(element, CLUSTER_NODE_TAG);
112+
if (nodes != null && nodes.size() > 0) {
113+
List<String> bootstrapUrls = new ArrayList<String>(nodes.size());
114+
for (int i = 0; i < nodes.size(); i++) {
115+
bootstrapUrls.add(nodes.get(i).getTextContent());
116+
}
117+
bean.addConstructorArgValue(bootstrapUrls);
118+
}
119+
}
120+
121+
/**
122+
* @return true if a custom environment was parsed and injected (either reference or inline), false if
123+
* the default environment reference was used.
124+
*/
125+
protected boolean parseEnvironment(BeanDefinitionBuilder clusterBuilder, Element clusterElement) {
126+
//any inline environment description would take precedence over a reference
127+
Element envElement = DomUtils.getChildElementByTagName(clusterElement, CLUSTER_ENVIRONMENT_TAG);
128+
if (envElement != null && envElement.hasAttributes()) {
129+
injectEnvElement(clusterBuilder, envElement);
130+
return true;
131+
}
132+
133+
//secondly try to see if an env has been referenced
134+
String envRef = clusterElement.getAttribute(CLUSTER_ENVIRONMENT_REF);
135+
if (StringUtils.hasText(envRef)) {
136+
injectEnvReference(clusterBuilder, envRef);
137+
return true;
138+
}
139+
140+
//if no custom value provided, consider it a reference to the default bean for Couchbase Environment
141+
injectEnvReference(clusterBuilder, BeanNames.COUCHBASE_ENV);
142+
return false;
143+
}
144+
145+
protected void injectEnvElement(BeanDefinitionBuilder clusterBuilder, Element envElement) {
146+
BeanDefinitionBuilder envDefinitionBuilder = BeanDefinitionBuilder
147+
.genericBeanDefinition(CouchbaseEnvironmentFactoryBean.class);
148+
new CouchbaseEnvironmentParser().doParse(envElement, envDefinitionBuilder);
149+
150+
clusterBuilder.addConstructorArgValue(envDefinitionBuilder.getBeanDefinition());
151+
}
152+
153+
protected void injectEnvReference(BeanDefinitionBuilder clusterBuilder, String envRef) {
154+
clusterBuilder.addConstructorArgReference(envRef);
155+
}
156+
}

0 commit comments

Comments
 (0)