001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.configuration.spring;
019:
020: import org.w3c.dom.Attr;
021: import org.w3c.dom.Element;
022: import org.w3c.dom.NamedNodeMap;
023: import org.w3c.dom.Node;
024: import org.w3c.dom.NodeList;
025:
026: import org.springframework.beans.factory.support.BeanDefinitionBuilder;
027: import org.springframework.beans.factory.xml.ParserContext;
028:
029: /**
030: * This class makes it easy to create two simultaneous beans - a factory bean and the bean
031: * that the factory produces.
032: */
033: public abstract class AbstractFactoryBeanDefinitionParser extends
034: AbstractBeanDefinitionParser {
035:
036: @Override
037: protected void doParse(Element element, ParserContext ctx,
038: BeanDefinitionBuilder bean) {
039: BeanDefinitionBuilder factoryBean = BeanDefinitionBuilder
040: .rootBeanDefinition(getFactoryClass());
041:
042: NamedNodeMap atts = element.getAttributes();
043: boolean createdFromAPI = false;
044: boolean setBus = false;
045: for (int i = 0; i < atts.getLength(); i++) {
046: Attr node = (Attr) atts.item(i);
047: String val = node.getValue();
048: String pre = node.getPrefix();
049: String name = node.getLocalName();
050:
051: if ("createdFromAPI".equals(name)) {
052: factoryBean.setAbstract(true);
053: bean.setAbstract(true);
054: createdFromAPI = true;
055: } else if ("abstract".equals(name)) {
056: factoryBean.setAbstract(true);
057: bean.setAbstract(true);
058: } else if (!"id".equals(name) && !"name".equals(name)
059: && isAttribute(pre, name)) {
060: if ("bus".equals(name)) {
061: setBus = true;
062: }
063: mapAttribute(factoryBean, element, name, val);
064: }
065: }
066:
067: if (!setBus && ctx.getRegistry().containsBeanDefinition("cxf")) {
068: wireBus(factoryBean, "cxf");
069: }
070:
071: NodeList children = element.getChildNodes();
072: for (int i = 0; i < children.getLength(); i++) {
073: Node n = children.item(i);
074: if (n.getNodeType() == Node.ELEMENT_NODE) {
075: String name = n.getLocalName();
076:
077: mapElement(ctx, factoryBean, (Element) n, name);
078: }
079: }
080:
081: String id = getIdOrName(element);
082: if (createdFromAPI) {
083: id = id + getSuffix();
084: }
085:
086: String factoryId = id + getFactoryIdSuffix();
087:
088: ctx.getRegistry().registerBeanDefinition(factoryId,
089: factoryBean.getBeanDefinition());
090: bean.getBeanDefinition().setAttribute("id", id);
091: bean.setFactoryBean(factoryId, "create");
092: }
093:
094: protected abstract Class getFactoryClass();
095:
096: /**
097: * @return The Spring ID of the factory bean.
098: */
099: protected abstract String getFactoryIdSuffix();
100: }
|