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.elasticsearch.config; |
18 | |
19 | import org.springframework.beans.factory.support.AbstractBeanDefinition; |
20 | import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
21 | import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser; |
22 | import org.springframework.beans.factory.xml.ParserContext; |
23 | import org.springframework.data.elasticsearch.client.TransportClientFactoryBean; |
24 | import org.w3c.dom.Element; |
25 | |
26 | import static org.apache.commons.lang.StringUtils.split; |
27 | /** |
28 | * TransportClientBeanDefinitionParser |
29 | * |
30 | * @author Rizwan Idrees |
31 | * @author Mohsin Husen |
32 | */ |
33 | |
34 | public class TransportClientBeanDefinitionParser extends AbstractBeanDefinitionParser { |
35 | |
36 | private static final String SEPARATOR_CHARS = ","; |
37 | |
38 | @Override |
39 | protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) { |
40 | BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(TransportClientFactoryBean.class); |
41 | setClusterNodes(element, builder); |
42 | return getSourcedBeanDefinition(builder,element, parserContext); |
43 | } |
44 | |
45 | private void setClusterNodes(Element element, BeanDefinitionBuilder builder){ |
46 | builder.addPropertyValue("clusterNodes", split(element.getAttribute("cluster-nodes"), SEPARATOR_CHARS)); |
47 | } |
48 | |
49 | private AbstractBeanDefinition getSourcedBeanDefinition(BeanDefinitionBuilder builder, Element source, |
50 | ParserContext context) { |
51 | AbstractBeanDefinition definition = builder.getBeanDefinition(); |
52 | definition.setSource(context.extractSource(source)); |
53 | return definition; |
54 | } |
55 | } |