org.smf4j.spring.RegistrarBeanDefinitionParser.java Source code

Java tutorial

Introduction

Here is the source code for org.smf4j.spring.RegistrarBeanDefinitionParser.java

Source

/*
 * Copyright 2012 Russell Morris (wrussellmorris@gmail.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.smf4j.spring;

import java.util.List;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author Russell Morris (wrussellmorris@gmail.com)
 */
public class RegistrarBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {

    public static final String MASTER_REGISTRAR_ID = "__registrar__#ID#";

    public static final String NODE_TAG = "node";
    public static final String NODE_TEMPLATE_TAG = "node-template";

    public static final String NAME_ATTR = "name";
    public static final String NODES_ATTR = "nodeProxies";

    @Override
    protected String getBeanClassName(Element element) {
        return RegistrarFactoryBean.class.getCanonicalName();
    }

    @Override
    protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {

        // Parse child nodes into a list of runtime bean references to
        // the proxy beans
        ManagedList<RuntimeBeanReference> nodes = parseChildren(parserContext, element);

        builder.addPropertyValue(NODES_ATTR, nodes);
        builder.setLazyInit(false);
    }

    protected ManagedList<RuntimeBeanReference> parseChildren(ParserContext context, Element element) {
        ManagedList<RuntimeBeanReference> nodes = new ManagedList<RuntimeBeanReference>();
        RegistryNodeTemplateDefinitionParser p = new RegistryNodeTemplateDefinitionParser(false);

        NodeList childNodes = element.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node childNode = childNodes.item(i);
            if (childNode.getNodeType() != Node.ELEMENT_NODE) {
                continue;
            }
            Element child = (Element) childNode;
            String childTagName = child.getLocalName();
            String childBeanId = null;
            if (NODE_TAG.equals(childTagName)) {
                BeanDefinitionBuilder bdb = BeanDefinitionBuilder.genericBeanDefinition(RegistryNodeProxy.class);
                childBeanId = p.parseNode(context, child, bdb);
            } else if (NODE_TEMPLATE_TAG.equals(NODE_TEMPLATE_TAG)) {
                childBeanId = p.createNodeTemplateRef(context, child);
            } else {
                context.getReaderContext().error("Unknown tag", child);
            }

            if (childBeanId != null) {
                nodes.add(new RuntimeBeanReference(childBeanId));
            }
        }

        return nodes;
    }

    @Override
    protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext)
            throws BeanDefinitionStoreException {
        String id = element.getAttribute(ID_ATTRIBUTE);
        if (!StringUtils.hasText(id)) {
            id = MASTER_REGISTRAR_ID;
        }
        return id;
    }

}