|
| 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 <{@value #CLUSTER_ENVIRONMENT_TAG}> 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 <{@value #CLUSTER_NODE_TAG}> tags. |
| 45 | + * |
| 46 | + * @author Simon Baslé |
| 47 | + */ |
| 48 | +public class CouchbaseClusterParser extends AbstractSingleBeanDefinitionParser { |
| 49 | + |
| 50 | + /** |
| 51 | + * The <node> 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 <env> 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 <env-ref> 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