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

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

Introduction

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

Prototype

MutablePropertyValues getPropertyValues();

Source Link

Document

Return the property values to be applied to a new instance of the bean.

Usage

From source file:org.grails.spring.DefaultRuntimeSpringConfiguration.java

private void registerBeanDefinitionsWithRegistry(BeanDefinitionRegistry registry) {
    for (Object key : beanDefinitions.keySet()) {
        BeanDefinition bd = beanDefinitions.get(key);
        if (LOG.isDebugEnabled()) {
            LOG.debug("[RuntimeConfiguration] Registering bean [" + key + "]");
            if (LOG.isTraceEnabled()) {
                PropertyValue[] pvs = bd.getPropertyValues().getPropertyValues();
                for (PropertyValue pv : pvs) {
                    LOG.trace("[RuntimeConfiguration] With property [" + pv.getName() + "] set to ["
                            + pv.getValue() + "]");
                }/*from w ww .jav a  2 s  . com*/
            }
        }
        final String beanName = key.toString();
        registry.registerBeanDefinition(beanName, bd);
    }
}

From source file:org.gridgain.grid.kernal.processors.spring.GridSpringProcessorImpl.java

/**
 * Creates Spring application context. Optionally excluded properties can be specified,
 * it means that if such a property is found in {@link GridConfiguration}
 * then it is removed before the bean is instantiated.
 * For example, {@code streamerConfiguration} can be excluded from the configs that Visor uses.
 *
 * @param cfgUrl Resource where config file is located.
 * @param excludedProps Properties to be excluded.
 * @return Spring application context.//from  w w w .ja  v  a  2s . c o  m
 */
public static ApplicationContext applicationContext(URL cfgUrl, final String... excludedProps) {
    GenericApplicationContext springCtx = new GenericApplicationContext();

    BeanFactoryPostProcessor postProc = new BeanFactoryPostProcessor() {
        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
            for (String beanName : beanFactory.getBeanDefinitionNames()) {
                BeanDefinition def = beanFactory.getBeanDefinition(beanName);

                if (def.getBeanClassName() != null) {
                    try {
                        Class.forName(def.getBeanClassName());
                    } catch (ClassNotFoundException ignored) {
                        ((BeanDefinitionRegistry) beanFactory).removeBeanDefinition(beanName);

                        continue;
                    }
                }

                MutablePropertyValues vals = def.getPropertyValues();

                for (PropertyValue val : new ArrayList<>(vals.getPropertyValueList())) {
                    for (String excludedProp : excludedProps) {
                        if (val.getName().equals(excludedProp))
                            vals.removePropertyValue(val);
                    }
                }
            }
        }
    };

    springCtx.addBeanFactoryPostProcessor(postProc);

    new XmlBeanDefinitionReader(springCtx).loadBeanDefinitions(new UrlResource(cfgUrl));

    springCtx.refresh();

    return springCtx;
}

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

/**
 * Returns a property value for the bean with the given name from the dictionary.
 *
 * @param beanName id or name for the bean definition
 * @param propertyName name of the property to retrieve, must be a valid property configured on
 * the bean definition/*from w w w . j a  va 2s  .c om*/
 * @return Object property value for property
 */
public Object getDictionaryBeanProperty(String beanName, String propertyName) {
    Object bean = ddBeans.getSingleton(beanName);
    if (bean != null) {
        return ObjectPropertyUtils.getPropertyValue(bean, propertyName);
    }

    BeanDefinition beanDefinition = ddBeans.getMergedBeanDefinition(beanName);

    if (beanDefinition == null) {
        throw new RuntimeException("Unable to get bean for bean name: " + beanName);
    }

    PropertyValues pvs = beanDefinition.getPropertyValues();
    if (pvs.contains(propertyName)) {
        PropertyValue propertyValue = pvs.getPropertyValue(propertyName);

        Object value;
        if (propertyValue.isConverted()) {
            value = propertyValue.getConvertedValue();
        } else if (propertyValue.getValue() instanceof String) {
            String unconvertedValue = (String) propertyValue.getValue();
            Scope scope = ddBeans.getRegisteredScope(beanDefinition.getScope());
            BeanExpressionContext beanExpressionContext = new BeanExpressionContext(ddBeans, scope);

            value = ddBeans.getBeanExpressionResolver().evaluate(unconvertedValue, beanExpressionContext);
        } else {
            value = propertyValue.getValue();
        }

        return value;
    }

    return null;
}

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

/**
 * Iterates through the properties defined for the bean definition and invokes helper methods to process
 * the property value/*from   ww  w.  jav  a  2  s. c o m*/
 *
 * @param beanDefinition bean definition whose properties will be processed
 * @param nestedBeanStack stack of beans which contain the given bean
 */
protected void processBeanProperties(BeanDefinition beanDefinition,
        Stack<BeanDefinitionHolder> nestedBeanStack) {
    // iterate through properties and check for any configured message keys within the value
    MutablePropertyValues pvs = beanDefinition.getPropertyValues();
    PropertyValue[] pvArray = pvs.getPropertyValues();
    for (PropertyValue pv : pvArray) {
        Object newPropertyValue = null;
        if (isStringValue(pv.getValue())) {
            newPropertyValue = processStringPropertyValue(pv.getName(), getString(pv.getValue()),
                    nestedBeanStack);
        } else {
            newPropertyValue = visitPropertyValue(pv.getName(), pv.getValue(), nestedBeanStack);
        }

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

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

/**
 * Applies the given property name and value to the bean definition
 *
 * @param propertyPath name of the property to add value for
 * @param propertyValue value for the property
 * @param beanDefinition bean definition to add property value to
 *///from   w  w w  .j  av a  2 s  . c  o  m
protected void applyPropertyValueToBean(String propertyPath, String propertyValue,
        BeanDefinition beanDefinition) {
    applyPropertyValueToBean(propertyPath, propertyValue, beanDefinition.getPropertyValues());
}

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

/**
 * Applies the text for a given message to the associated bean definition property
 *
 * @param message message instance to apply
 * @param beanDefinition bean definition the message should be applied to
 * @param beanClass class for the bean definition
 *//*from www  . jav a  2 s . co  m*/
protected void applyMessageToBean(Message message, BeanDefinition beanDefinition, Class<?> beanClass) {
    String key = message.getKey().trim();

    // if message doesn't start with path indicator, it will be an explicit key that is matched when
    // iterating over the property values, so we will just return in that case
    if (!key.startsWith(KRADConstants.MESSAGE_KEY_PATH_INDICATOR)) {
        return;
    }

    // if here dealing with a path key, strip off indicator and then process as a property path
    key = StringUtils.stripStart(key, KRADConstants.MESSAGE_KEY_PATH_INDICATOR);

    // list factory beans just have the one list property (with no name)
    if (ListFactoryBean.class.isAssignableFrom(beanClass)) {
        MutablePropertyValues pvs = beanDefinition.getPropertyValues();

        PropertyValue propertyValue = pvs.getPropertyValueList().get(0);
        List<?> listValue = (List<?>) propertyValue.getValue();

        applyMessageToNestedListBean(message, listValue, key);
    } else if (StringUtils.contains(key, ".")) {
        applyMessageToNestedBean(message, beanDefinition, key);
    } else {
        applyMessageTextToPropertyValue(key, message.getText(), beanDefinition);
    }
}

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

/**
 * Applies the given message text to the property within the given bean definition
 *
 * <p>//from  www  .ja  va2 s  .c  o m
 * The message text is applied to the bean definiton based on the property path. The path could be nested
 * in which case the property values of the bean definition are traversed to find the nested bean definition
 * that should hold the property value. The path could also represent a nested list bean. In this case a
 * helper method is called to find the correct list bean to apply the message to
 * </p>
 *
 * @param message message containing the text to apply
 * @param beanDefinition bean definition containing the property
 * @param propertyPath path to property within the bean definition the message should be applied to
 */
protected void applyMessageToNestedBean(Message message, BeanDefinition beanDefinition, String propertyPath) {
    MutablePropertyValues pvs = beanDefinition.getPropertyValues();

    String beanPath = StringUtils.substringBefore(propertyPath, ".");
    String nestedPropertyPath = StringUtils.substringAfter(propertyPath, ".");

    boolean foundNestedBean = false;
    while (StringUtils.isNotBlank(nestedPropertyPath)) {
        if (pvs.contains(beanPath)) {
            PropertyValue propertyValue = pvs.getPropertyValue(beanPath);

            BeanDefinition propertyBeanDefinition = getPropertyValueBeanDefinition(propertyValue);
            if (propertyBeanDefinition != null) {
                applyMessageToNestedBean(message, propertyBeanDefinition, nestedPropertyPath);

                foundNestedBean = true;
                break;
            } else if (propertyValue.getValue() instanceof List) {
                applyMessageToNestedListBean(message, (List<?>) propertyValue.getValue(), nestedPropertyPath);

                foundNestedBean = true;
                break;
            }
        }

        beanPath += "." + StringUtils.substringBefore(nestedPropertyPath, ".");
        nestedPropertyPath = StringUtils.substringAfter(nestedPropertyPath, ".");
    }

    if (!foundNestedBean) {
        applyMessageTextToPropertyValue(propertyPath, message.getText(), beanDefinition);
    }
}

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 .j a  v  a2s. c  om*/
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.MessageBeanProcessor.java

/**
 * Applies the given message text to the bean definition with the given property name, if a current
 * value exists for the property name the value is checked for expressions which are then merged with
 * the message//from  w  w w.  j  a v a2s.co m
 *
 * @param propertyName - name of the property to set on the bean definition
 * @param messageText - message text that will be the property value
 * @param beanDefinition - bean definition to set property on
 */
protected void applyMessageTextToPropertyValue(String propertyName, String messageText,
        BeanDefinition beanDefinition) {
    String newPropertyValue = messageText;

    MutablePropertyValues pvs = beanDefinition.getPropertyValues();
    if (pvs.contains(propertyName)) {
        PropertyValue propertyValue = pvs.getPropertyValue(propertyName);

        String stringPropertyValue = getStringValue(propertyValue.getValue());
        if (StringUtils.isNotBlank(stringPropertyValue)) {
            newPropertyValue = getMergedMessageText(messageText, stringPropertyValue);
        }
    }

    applyPropertyValueToBean(propertyName, newPropertyValue, pvs);
}

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

/**
 * Retrieves the namespace associated with the bean definition
 *
 * @param beanName name of the bean to find namespace for
 * @param beanDefinition bean definition to find namespace for
 * @return String namespace for bean or null if a namespace was not found
 *///from w w w . j  av  a 2  s  . c o m
protected String getNamespaceForBean(String beanName, BeanDefinition beanDefinition) {
    String namespace = null;

    MutablePropertyValues pvs = beanDefinition.getPropertyValues();
    if (pvs.contains(KRADPropertyConstants.NAMESPACE_CODE)) {
        PropertyValue propertyValue = pvs.getPropertyValue(KRADPropertyConstants.NAMESPACE_CODE);
        namespace = getStringValue(propertyValue.getValue());
    } else if (StringUtils.isNotBlank(beanName) && !isGeneratedBeanName(beanName)) {
        // if not on bean definition, get from associated module in the dictionary
        namespace = dataDictionary.getNamespaceForBeanDefinition(beanName);
    }

    return namespace;
}