/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. package com.hazelcast.spring;
  17. import com.hazelcast.core.HazelcastInstance;
  18. import org.springframework.beans.factory.support.AbstractBeanDefinition;
  19. import org.springframework.beans.factory.support.BeanDefinitionBuilder;
  20. import org.springframework.beans.factory.xml.ParserContext;
  21. import org.w3c.dom.Element;
  22. import org.w3c.dom.NamedNodeMap;
  23. import org.w3c.dom.Node;
  24. public class HazelcastTypeBeanDefinitionParser extends AbstractHazelcastBeanDefinitionParser {
  25. private final String type;
  26. private final String methodName;
  27. public HazelcastTypeBeanDefinitionParser(final String type) {
  28. super();
  29. this.type = type;
  30. this.methodName = "get" + Character.toUpperCase(type.charAt(0)) + type.substring(1);
  31. }
  32. protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
  33. final SpringXmlBuilder springXmlBuilder = new SpringXmlBuilder(parserContext);
  34. springXmlBuilder.handle(element);
  35. final BeanDefinitionBuilder builder = springXmlBuilder.getBuilder();
  36. builder.setFactoryMethod(methodName);
  37. return builder.getBeanDefinition();
  38. }
  39. private class SpringXmlBuilder extends SpringXmlBuilderHelper {
  40. private final ParserContext parserContext;
  41. private BeanDefinitionBuilder builder;
  42. public SpringXmlBuilder(ParserContext parserContext) {
  43. this.parserContext = parserContext;
  44. this.builder = BeanDefinitionBuilder.rootBeanDefinition(HazelcastInstance.class);
  45. }
  46. public BeanDefinitionBuilder getBuilder() {
  47. return this.builder;
  48. }
  49. public void handle(Element element) {
  50. handleCommonBeanAttributes(element, builder, parserContext);
  51. final NamedNodeMap attrs = element.getAttributes();
  52. if (attrs != null) {
  53. Node instanceRefNode = attrs.getNamedItem("instance-ref") ;
  54. if (instanceRefNode == null) {
  55. throw new IllegalStateException("'instance-ref' attribute is required for creating" +
  56. " Hazelcast " + type);
  57. }
  58. final String instanceRef = getValue(instanceRefNode);
  59. builder.getRawBeanDefinition().setFactoryBeanName(instanceRef);
  60. builder.addDependsOn(instanceRef);
  61. Node nameNode = attrs.getNamedItem("name");
  62. if (nameNode == null) {
  63. nameNode = attrs.getNamedItem("id");
  64. }
  65. builder.addConstructorArgValue(getValue(nameNode));
  66. }
  67. }
  68. }
  69. }