net.phoenix.thrift.xml.ArgBeanDefinitionParser.java Source code

Java tutorial

Introduction

Here is the source code for net.phoenix.thrift.xml.ArgBeanDefinitionParser.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 net.phoenix.thrift.xml;

import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

/**
 *
 * @author Shamphone Lee<shamphone@gmail.com>
 *
 */
public class ArgBeanDefinitionParser extends ComplexBeanDefinitionParser {

    @Override
    protected Class<?> getBeanClass(Element element) {
        return OneParamMethodInvokingFactoryBean.class;
    }

    @Override
    protected void preParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
        Element parent = (Element) element.getParentNode();
        String argsBeanDefinitionName = parent.getAttribute("id");
        String name = element.getAttribute("name");
        builder.addPropertyReference("targetObject", argsBeanDefinitionName);
        builder.addPropertyValue("targetMethod", name);
    }

    /**
     * ?arg
     */
    @Override
    protected void postParse(Element element, ParserContext parserContext, AbstractBeanDefinition current) {
        Object argument = parserContext.getDelegate().parsePropertyValue(element, current, "argument");
        if (argument == null)
            throw new BeanCreationException(
                    "Could not found value for arg '" + element.getAttribute("name") + "'.");
        current.getPropertyValues().add("argument", argument);
        // register the definition;
        if (parserContext.isNested()) {
            this.registerBeanDefinition(element, parserContext, current);
        }
    }

    /**
     * register to the bean factory;
     *
     * @param element
     * @param parserContext
     * @param current
     */
    private void registerBeanDefinition(Element element, ParserContext parserContext,
            AbstractBeanDefinition current) {
        try {
            Element parent = (Element) element.getParentNode();
            String argsBeanDefinitionName = parent.getAttribute("id");
            String name = argsBeanDefinitionName + "-" + element.getAttribute("name");
            String[] alias = null;
            if (StringUtils.hasLength(name)) {
                alias = StringUtils.trimArrayElements(StringUtils.commaDelimitedListToStringArray(name));
            }
            BeanDefinitionHolder holder = new BeanDefinitionHolder(current, name, alias);
            registerBeanDefinition(holder, parserContext.getRegistry());
        } catch (BeanDefinitionStoreException ex) {
            parserContext.getReaderContext().error(ex.getMessage(), element);
        }
    }

}