Example usage for org.springframework.beans.factory.config BeanDefinition getParentName

List of usage examples for org.springframework.beans.factory.config BeanDefinition getParentName

Introduction

In this page you can find the example usage for org.springframework.beans.factory.config BeanDefinition getParentName.

Prototype

@Nullable
String getParentName();

Source Link

Document

Return the name of the parent definition of this bean definition, if any.

Usage

From source file:org.shept.util.SheptBeanFactoryUtils.java

/**
 * @param ctx/*from w w w  . j  a va 2 s . c o  m*/
 * @param sourceName
 */
public static String getParentBeanName(ApplicationContext ctx, String beanName) {
    ApplicationContext pc = getRootContext(ctx);
    AutowireCapableBeanFactory factory = pc.getAutowireCapableBeanFactory();
    if (factory instanceof DefaultListableBeanFactory) {
        BeanDefinition def = ((DefaultListableBeanFactory) factory).getBeanDefinition(beanName);
        if (def != null) {
            return def.getParentName();
        }

    }
    return null;
}

From source file:org.beangle.spring.bind.DefinitionBindRegistry.java

public DefinitionBindRegistry(BeanDefinitionRegistry registry) {
    for (String name : registry.getBeanDefinitionNames()) {
        BeanDefinition bd = registry.getBeanDefinition(name);
        if (bd.isAbstract())
            continue;
        // find classname
        String className = bd.getBeanClassName();
        if (null == className) {
            String parentName = bd.getParentName();
            if (null == parentName)
                continue;
            else {
                BeanDefinition parentDef = registry.getBeanDefinition(parentName);
                className = parentDef.getBeanClassName();
            }//from  w w w  .j a  v a2s .c  o m
        }
        if (null == className)
            continue;

        try {
            Class<?> beanClass = ClassUtils.forName(className, ClassUtils.getDefaultClassLoader());
            if (FactoryBean.class.isAssignableFrom(beanClass)) {
                register(beanClass, "&" + name);
                PropertyValue pv = bd.getPropertyValues().getPropertyValue("target");
                if (null == pv) {
                    Class<?> artifactClass = ((FactoryBean<?>) beanClass.newInstance()).getObjectType();
                    if (null != artifactClass)
                        register(artifactClass, name);
                } else {
                    if (pv.getValue() instanceof BeanDefinitionHolder) {
                        String nestedClassName = ((BeanDefinitionHolder) pv.getValue()).getBeanDefinition()
                                .getBeanClassName();
                        if (null != nestedClassName) {
                            register(ClassUtils.forName(nestedClassName, ClassUtils.getDefaultClassLoader()),
                                    name);
                        }
                    }
                }
            } else {
                register(beanClass, name);
            }
        } catch (Exception e) {
            logger.error("class not found", e);
            continue;
        }
    }
}

From source file:org.kuali.rice.krad.datadictionary.DictionaryBeanProcessorBase.java

/**
 * Retrieves the class for the object that will be created from the bean definition. Since the class might not
 * be configured on the bean definition, but by a parent, each parent bean definition is recursively checked for
 * a class until one is found/*from   w ww .ja v a2 s .c  o  m*/
 *
 * @param beanDefinition bean definition to get class for
 * @param beanFactory bean factory that contains the bean definition
 * @return Class<?> class configured for the bean definition, or null
 */
protected Class<?> getBeanClass(BeanDefinition beanDefinition, ConfigurableListableBeanFactory beanFactory) {
    if (StringUtils.isNotBlank(beanDefinition.getBeanClassName())) {
        try {
            return Class.forName(beanDefinition.getBeanClassName());
        } catch (ClassNotFoundException e) {
            // swallow exception and return null so bean is not processed
            return null;
        }
    } else if (StringUtils.isNotBlank(beanDefinition.getParentName())) {
        BeanDefinition parentBeanDefinition = beanFactory.getBeanDefinition(beanDefinition.getParentName());
        if (parentBeanDefinition != null) {
            return getBeanClass(parentBeanDefinition, beanFactory);
        }
    }

    return null;
}

From source file:org.kuali.rice.krad.datadictionary.MessageBeanProcessor.java

/**
 * Attempts to find a property value for the given property name within the bean definition, if the property
 * does not exist in the bean and there is a parent, the parent is checked for containing the property. This
 * continues until a property value is found or all the parents have been traversed
 *
 * @param beanDefinition bean definition to find property value in
 * @param propertyName name of the property to find the value for
 * @return String value for property in the bean definition or null if the property was not found
 *//*w  ww. ja  v a 2  s. co  m*/
protected String findPropertyValueInBeanDefinition(BeanDefinition beanDefinition, String propertyName) {
    String beanPropertyValue = null;

    MutablePropertyValues pvs = beanDefinition.getPropertyValues();
    if (pvs.contains(propertyName)) {
        PropertyValue propertyValue = pvs.getPropertyValue(propertyName);
        if (propertyValue.getValue() != null) {
            beanPropertyValue = propertyValue.getValue().toString();
        }
    } else {
        if (StringUtils.isNotBlank(beanDefinition.getParentName())) {
            BeanDefinition parentBeanDefinition = beanFactory.getBeanDefinition(beanDefinition.getParentName());

            beanPropertyValue = findPropertyValueInBeanDefinition(parentBeanDefinition, propertyName);
        }
    }

    return beanPropertyValue;
}

From source file:org.kuali.rice.krad.datadictionary.parse.CustomSchemaParser.java

/**
 * Parses a bean of the custom namespace.
 *
 * @param tag - The Element to be parsed.
 * @param parent - The parent bean that the tag is nested in.
 * @param parserContext - Provided information and functionality regarding current bean set.
 * @return The parsed bean.//from   ww w.j  a  v  a  2  s. c  om
 */
protected Object parseCustomBean(Element tag, BeanDefinitionBuilder parent, ParserContext parserContext) {
    BeanDefinition beanDefinition = parserContext.getDelegate().parseCustomElement(tag,
            parent.getBeanDefinition());

    String name = beanDefinition.getParentName() + "$Customchild" + beanNumber;
    if (tag.getAttribute("id") != null && !StringUtils.isEmpty(tag.getAttribute("id"))) {
        name = tag.getAttribute("id");
    } else {
        beanNumber++;
    }

    return new BeanDefinitionHolder(beanDefinition, name);
}

From source file:org.kuali.rice.krad.datadictionary.uif.UifBeanFactoryPostProcessor.java

/**
 * If the bean class is type UifDictionaryBean, iterate through configured property values
 * and check for expressions.//from ww w  .j  a v  a 2 s.co  m
 *
 * @param beanName name of the bean in the factory (only set for top level beans, not nested)
 * @param beanDefinition bean definition to process for expressions
 * @param nestedPropertyName
 * @param expressionGraph
 * @param beanFactory bean factory being processed
 * @param processedBeanNames
 */
protected void processNestedBeanDefinition(String beanName, BeanDefinition beanDefinition,
        String nestedPropertyName, Map<String, String> expressionGraph,
        ConfigurableListableBeanFactory beanFactory, Set<String> processedBeanNames) {
    Class<?> beanClass = getBeanClass(beanDefinition, beanFactory);
    if ((beanClass == null) || !UifDictionaryBean.class.isAssignableFrom(beanClass)
            || processedBeanNames.contains(beanName)) {
        return;
    }

    LOG.debug("Processing bean name '" + beanName + "'");

    Map<String, String> parentExpressionGraph = getExpressionGraphFromParent(beanDefinition.getParentName(),
            beanFactory, processedBeanNames);

    // process expressions on property values
    MutablePropertyValues pvs = beanDefinition.getPropertyValues();
    PropertyValue[] pvArray = pvs.getPropertyValues();
    for (PropertyValue pv : pvArray) {
        if (pv.getName().equals(UifPropertyPaths.EXPRESSION_GRAPH)) {
            continue;
        }

        String propertyPath = pv.getName();
        if (StringUtils.isNotBlank(nestedPropertyName)) {
            propertyPath = nestedPropertyName + "." + propertyPath;
        }

        // for reloading, need to remove the property from the previously loaded bean definition
        if (expressionGraph.containsKey(propertyPath)) {
            expressionGraph.remove(propertyPath);
        }

        if (hasExpression(pv.getValue())) {
            // process expression
            String strValue = getStringValue(pv.getValue());
            expressionGraph.put(propertyPath, strValue);

            // remove property value so expression will not cause binding exception
            pvs.removePropertyValue(pv.getName());
        } else {
            // process nested objects
            Object newValue = processPropertyValue(propertyPath, pv.getName(), pv.getValue(), beanDefinition,
                    parentExpressionGraph, expressionGraph, beanFactory, processedBeanNames);

            pvs.removePropertyValue(pv.getName());
            pvs.addPropertyValue(pv.getName(), newValue);
        }

        // removed expression (if exists) from parent map since the property was set on child
        if (parentExpressionGraph.containsKey(pv.getName())) {
            parentExpressionGraph.remove(pv.getName());
        }
    }

    // if nested bean set expression graph to null so it is not inherited from parent definition
    if (StringUtils.isNotBlank(nestedPropertyName)) {
        pvs.addPropertyValue(UifPropertyPaths.EXPRESSION_GRAPH, null);
    }

    // add remaining expressions from parent to expression graph
    for (Map.Entry<String, String> parentExpression : parentExpressionGraph.entrySet()) {
        String expressionPath = parentExpression.getKey();
        if (StringUtils.isNotBlank(nestedPropertyName)) {
            expressionPath = nestedPropertyName + "." + expressionPath;
        }

        if (!expressionGraph.containsKey(expressionPath)) {
            expressionGraph.put(expressionPath, parentExpression.getValue());
        }
    }

    if (StringUtils.isNotBlank(beanName)) {
        processedBeanNames.add(beanName);
    }
}

From source file:org.kuali.rice.krad.datadictionary.uif.UifBeanFactoryPostProcessor.java

@SuppressWarnings("unchecked")
protected void visitList(String nestedPropertyName, String propertyName, BeanDefinition beanDefinition,
        Map<String, String> parentExpressionGraph, Map<String, String> expressionGraph, List listVal,
        ConfigurableListableBeanFactory beanFactory, Set<String> processedBeanNames) {
    boolean isMergeEnabled = false;
    if (listVal instanceof ManagedList) {
        isMergeEnabled = ((ManagedList) listVal).isMergeEnabled();
    }/*from ww  w .ja  va  2 s . c o m*/

    ManagedList newList = new ManagedList();
    newList.setMergeEnabled(isMergeEnabled);

    // if merging, need to find size of parent list so we can know which element to set
    // when evaluating expressions
    int parentListSize = 0;
    if (isMergeEnabled && StringUtils.isNotBlank(beanDefinition.getParentName())) {
        BeanDefinition parentBeanDefinition = beanFactory
                .getMergedBeanDefinition(beanDefinition.getParentName());
        PropertyValue parentListPropertyValue = parentBeanDefinition.getPropertyValues()
                .getPropertyValue(propertyName);
        if (parentListPropertyValue != null) {
            List parentList = (List) parentListPropertyValue.getValue();
            parentListSize = parentList.size();
        }
    }

    for (int i = 0; i < listVal.size(); i++) {
        Object elem = listVal.get(i);

        int elementPosition = i + parentListSize;
        String elemPropertyName = nestedPropertyName + "[" + elementPosition + "]";

        if (hasExpression(elem)) {
            String strValue = getStringValue(elem);

            expressionGraph.put(elemPropertyName, strValue);
            newList.add(i, null);
        } else {
            // process list value bean definition as a top level bean
            if ((elem instanceof BeanDefinition) || (elem instanceof BeanDefinitionHolder)) {
                String beanName = null;
                BeanDefinition beanDefinitionValue;
                if (elem instanceof BeanDefinition) {
                    beanDefinitionValue = (BeanDefinition) elem;
                } else {
                    beanDefinitionValue = ((BeanDefinitionHolder) elem).getBeanDefinition();
                    beanName = ((BeanDefinitionHolder) elem).getBeanName();
                }

                processBeanDefinition(beanName, beanDefinitionValue, beanFactory, processedBeanNames);
            }

            newList.add(i, elem);
        }
    }

    // determine if we need to clear any parent expressions for this list
    if (!isMergeEnabled) {
        // clear any expressions that match the property name minus index
        Map<String, String> adjustedParentExpressionGraph = new HashMap<String, String>();
        for (Map.Entry<String, String> parentExpression : parentExpressionGraph.entrySet()) {
            if (!parentExpression.getKey().startsWith(nestedPropertyName + "[")) {
                adjustedParentExpressionGraph.put(parentExpression.getKey(), parentExpression.getValue());
            }
        }

        parentExpressionGraph.clear();
        parentExpressionGraph.putAll(adjustedParentExpressionGraph);
    }

    listVal.clear();
    listVal.addAll(newList);
}

From source file:org.kuali.rice.krad.uif.util.UifBeanFactoryPostProcessor.java

/**
 * If the bean class is type UifDictionaryBean, iterate through configured property values
 * and check for expressions// ww  w.j a v a2  s  .com
 *
 * @param beanName name of the bean in the factory (only set for top level beans, not nested)
 * @param beanDefinition bean definition to process for expressions
 * @param nestedPropertyName
 * @param expressionGraph
 * @param beanFactory bean factory being processed
 * @param processedBeanNames
 */
protected void processNestedBeanDefinition(String beanName, BeanDefinition beanDefinition,
        String nestedPropertyName, Map<String, String> expressionGraph,
        ConfigurableListableBeanFactory beanFactory, Set<String> processedBeanNames) {
    Class<?> beanClass = getBeanClass(beanDefinition, beanFactory);
    if ((beanClass == null) || !UifDictionaryBean.class.isAssignableFrom(beanClass)
            || processedBeanNames.contains(beanName)) {
        return;
    }

    LOG.debug("Processing bean name '" + beanName + "'");

    Map<String, String> parentExpressionGraph = getExpressionGraphFromParent(beanDefinition.getParentName(),
            beanFactory, processedBeanNames);

    // process expressions on property values
    MutablePropertyValues pvs = beanDefinition.getPropertyValues();
    PropertyValue[] pvArray = pvs.getPropertyValues();
    for (PropertyValue pv : pvArray) {
        if (pv.getName().equals(UifPropertyPaths.EXPRESSION_GRAPH)) {
            continue;
        }

        String propertyPath = pv.getName();
        if (StringUtils.isNotBlank(nestedPropertyName)) {
            propertyPath = nestedPropertyName + "." + propertyPath;
        }

        // for reloading, need to remove the property from the previously loaded bean definition
        if (expressionGraph.containsKey(propertyPath)) {
            expressionGraph.remove(propertyPath);
        }

        if (hasExpression(pv.getValue())) {
            // process expression
            String strValue = getStringValue(pv.getValue());
            expressionGraph.put(propertyPath, strValue);

            // remove property value so expression will not cause binding exception
            pvs.removePropertyValue(pv.getName());
        } else {
            // process nested objects
            Object newValue = processPropertyValue(propertyPath, pv.getName(), pv.getValue(), beanDefinition,
                    parentExpressionGraph, expressionGraph, beanFactory, processedBeanNames);

            pvs.removePropertyValue(pv.getName());
            pvs.addPropertyValue(pv.getName(), newValue);
        }

        // removed expression (if exists) from parent map since the property was set on child
        if (parentExpressionGraph.containsKey(pv.getName())) {
            parentExpressionGraph.remove(pv.getName());
        }
    }

    // if nested bean set expression graph to null so it is not inherited from parent definition
    if (StringUtils.isNotBlank(nestedPropertyName)) {
        pvs.addPropertyValue(UifPropertyPaths.EXPRESSION_GRAPH, null);
    }

    // add remaining expressions from parent to expression graph
    for (Map.Entry<String, String> parentExpression : parentExpressionGraph.entrySet()) {
        String expressionPath = parentExpression.getKey();
        if (StringUtils.isNotBlank(nestedPropertyName)) {
            expressionPath = nestedPropertyName + "." + expressionPath;
        }

        if (!expressionGraph.containsKey(expressionPath)) {
            expressionGraph.put(expressionPath, parentExpression.getValue());
        }
    }

    // if bean name is given and factory does not have it registered we need to add it (inner beans that
    // were given an id)
    if (StringUtils.isNotBlank(beanName) && !StringUtils.contains(beanName, "$")
            && !StringUtils.contains(beanName, "#") && !beanFactory.containsBean(beanName)) {
        ((BeanDefinitionRegistry) beanFactory).registerBeanDefinition(beanName, beanDefinition);
    }

    if (StringUtils.isNotBlank(beanName)) {
        processedBeanNames.add(beanName);
    }
}

From source file:org.statefulj.framework.core.StatefulFactory.java

/**
 * @param bf/*  w ww. j  av  a 2 s  . co  m*/
 * @param reg
 * @param clazz
 * @return
 * @throws ClassNotFoundException
 */
private Class<?> getClassFromParentBean(BeanDefinition bf, BeanDefinitionRegistry reg)
        throws ClassNotFoundException {
    Class<?> clazz = null;
    String parentBeanName = bf.getParentName();
    if (parentBeanName != null) {
        BeanDefinition parent = reg.getBeanDefinition(parentBeanName);
        if (parent != null) {
            clazz = this.getClassFromBeanDefinition(parent, reg);
        }
    }
    return clazz;
}