PageRenderTime 27ms CodeModel.GetById 16ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/hazelcast-spring/src/main/java/com/hazelcast/spring/HazelcastTypeBeanDefinitionParser.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 82 lines | 54 code | 13 blank | 15 comment | 6 complexity | 90183526ace05d3fc448c7222990d594 MD5 | raw file
 1/*
 2 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
 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
17package com.hazelcast.spring;
18
19import com.hazelcast.core.HazelcastInstance;
20import org.springframework.beans.factory.support.AbstractBeanDefinition;
21import org.springframework.beans.factory.support.BeanDefinitionBuilder;
22import org.springframework.beans.factory.xml.ParserContext;
23import org.w3c.dom.Element;
24import org.w3c.dom.NamedNodeMap;
25import org.w3c.dom.Node;
26
27public class HazelcastTypeBeanDefinitionParser extends AbstractHazelcastBeanDefinitionParser {
28
29    private final String type;
30    private final String methodName;
31
32    public HazelcastTypeBeanDefinitionParser(final String type) {
33        super();
34        this.type = type;
35        this.methodName = "get" + Character.toUpperCase(type.charAt(0)) + type.substring(1);
36    }
37
38    protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
39        final SpringXmlBuilder springXmlBuilder = new SpringXmlBuilder(parserContext);
40        springXmlBuilder.handle(element);
41        final BeanDefinitionBuilder builder = springXmlBuilder.getBuilder();
42        builder.setFactoryMethod(methodName);
43        return builder.getBeanDefinition();
44    }
45
46    private class SpringXmlBuilder extends SpringXmlBuilderHelper {
47
48        private final ParserContext parserContext;
49
50        private BeanDefinitionBuilder builder;
51
52        public SpringXmlBuilder(ParserContext parserContext) {
53            this.parserContext = parserContext;
54            this.builder = BeanDefinitionBuilder.rootBeanDefinition(HazelcastInstance.class);
55        }
56
57        public BeanDefinitionBuilder getBuilder() {
58            return this.builder;
59        }
60
61        public void handle(Element element) {
62            handleCommonBeanAttributes(element, builder, parserContext);
63            final NamedNodeMap attrs = element.getAttributes();
64            if (attrs != null) {
65                Node instanceRefNode = attrs.getNamedItem("instance-ref") ;
66                if (instanceRefNode == null) {
67                    throw new IllegalStateException("'instance-ref' attribute is required for creating" +
68                                                    " Hazelcast " + type);
69                }
70                final String instanceRef = getValue(instanceRefNode);
71                builder.getRawBeanDefinition().setFactoryBeanName(instanceRef);
72                builder.addDependsOn(instanceRef);
73
74                Node nameNode = attrs.getNamedItem("name");
75                if (nameNode == null) {
76                    nameNode = attrs.getNamedItem("id");
77                }
78                builder.addConstructorArgValue(getValue(nameNode));
79            }
80        }
81    }
82}