Search in sources :

Example 1 with BeanReference

use of com.revolsys.spring.BeanReference in project com.revolsys.open by revolsys.

the class BeanConfigurrer method processOverride.

/**
 * Process the given key as 'beanName.property' entry.
 */
protected void processOverride(final ConfigurableListableBeanFactory factory, final String key, Object value) {
    try {
        if (value instanceof BeanReference) {
            final BeanReference reference = (BeanReference) value;
            value = reference.getBean();
        }
        final Matcher matcher = BeanConfigurrer.KEY_PATTERN.matcher(key);
        if (matcher.matches()) {
            final String beanName = matcher.group(1);
            final String mapKey = matcher.group(2);
            final String propertyName = matcher.group(3);
            if (mapKey == null) {
                if (propertyName == null) {
                    if (factory.containsBean(beanName)) {
                        BeanDefinition beanDefinition = factory.getBeanDefinition(beanName);
                        try {
                            final ClassLoader classLoader = this.applicationContext.getClassLoader();
                            final String beanClassName = beanDefinition.getBeanClassName();
                            final Class<?> beanClass = Class.forName(beanClassName, true, classLoader);
                            if (Parameter.class.isAssignableFrom(beanClass)) {
                                while (beanDefinition.getOriginatingBeanDefinition() != null) {
                                    beanDefinition = beanDefinition.getOriginatingBeanDefinition();
                                }
                                final MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
                                PropertyValue propertyValue = new PropertyValue("value", value);
                                final PropertyValue typeValue = propertyValues.getPropertyValue("type");
                                if (typeValue != null) {
                                    try {
                                        final Class<?> typeClass;
                                        final Object typeValueObject = typeValue.getValue();
                                        if (typeValueObject instanceof Class<?>) {
                                            typeClass = (Class<?>) typeValueObject;
                                        } else {
                                            final String typeClassName = typeValueObject.toString();
                                            typeClass = Class.forName(typeClassName, true, classLoader);
                                        }
                                        final Object convertedValue = new SimpleTypeConverter().convertIfNecessary(value, typeClass);
                                        propertyValue = new PropertyValue("value", convertedValue);
                                    } catch (final Throwable e) {
                                        Logs.error(this, "Unable to set " + beanName + ".value=" + value, e);
                                    }
                                }
                                propertyValues.addPropertyValue(propertyValue);
                            }
                        } catch (final ClassNotFoundException e) {
                            Logs.error(this, "Unable to set " + beanName + ".value=" + value, e);
                        }
                    } else if (value != null) {
                        newParameterBeanDefinition(factory, beanName, value);
                    }
                } else {
                    setAttributeValue(factory, beanName, propertyName, value);
                    Logs.debug(this, "Property '" + key + "' set to value [" + value + "]");
                }
            } else if (propertyName == null) {
                setMapValue(factory, key, beanName, mapKey, value);
            } else {
                Logs.error(this, "Invalid syntax unable to set " + key + "=" + value);
            }
        }
    } catch (final BeansException ex) {
        final String msg = "Could not process key '" + key + "' in PropertyOverrideConfigurer";
        if (!this.ignoreInvalidKeys) {
            throw new BeanInitializationException(msg, ex);
        }
        Logs.debug(this, msg, ex);
    }
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) Matcher(java.util.regex.Matcher) PropertyValue(org.springframework.beans.PropertyValue) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) SimpleTypeConverter(org.springframework.beans.SimpleTypeConverter) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) BeanReference(com.revolsys.spring.BeanReference) BeansException(org.springframework.beans.BeansException)

Example 2 with BeanReference

use of com.revolsys.spring.BeanReference in project com.revolsys.open by revolsys.

the class BeanConfigurrer method processPlaceholderAttributes.

protected void processPlaceholderAttributes(final ConfigurableListableBeanFactory beanFactory, final Map<String, Object> attributes) throws BeansException {
    final Map<String, Object> attributeMap = new LinkedHashMap<>();
    for (final Entry<String, Object> entry : attributes.entrySet()) {
        final String key = entry.getKey();
        final Object value = entry.getValue();
        if (!(value instanceof BeanReference)) {
            attributeMap.put(key, value);
        }
    }
    final StringValueResolver valueResolver = new PlaceholderResolvingStringValueResolver("${", "}", this.ignoreUnresolvablePlaceholders, null, attributeMap);
    final BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver);
    final String[] beanNames = beanFactory.getBeanDefinitionNames();
    for (int i = 0; i < beanNames.length; i++) {
        // locations.
        if (!(beanNames[i].equals(this.beanName) && beanFactory.equals(this.applicationContext))) {
            final BeanDefinition bd = beanFactory.getBeanDefinition(beanNames[i]);
            try {
                visitor.visitBeanDefinition(bd);
            } catch (final BeanDefinitionStoreException ex) {
                throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanNames[i], ex.getMessage());
            }
        }
    }
    // NEW in Spring 2.5: resolve placeholders in alias target names and aliases
    // as well.
    beanFactory.resolveAliases(valueResolver);
}
Also used : PlaceholderResolvingStringValueResolver(com.revolsys.spring.util.PlaceholderResolvingStringValueResolver) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) LinkedHashMap(java.util.LinkedHashMap) StringValueResolver(org.springframework.util.StringValueResolver) PlaceholderResolvingStringValueResolver(com.revolsys.spring.util.PlaceholderResolvingStringValueResolver) BeanDefinitionVisitor(org.springframework.beans.factory.config.BeanDefinitionVisitor) BeanReference(com.revolsys.spring.BeanReference)

Aggregations

BeanReference (com.revolsys.spring.BeanReference)2 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)2 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)2 PlaceholderResolvingStringValueResolver (com.revolsys.spring.util.PlaceholderResolvingStringValueResolver)1 LinkedHashMap (java.util.LinkedHashMap)1 Matcher (java.util.regex.Matcher)1 BeansException (org.springframework.beans.BeansException)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1 PropertyValue (org.springframework.beans.PropertyValue)1 SimpleTypeConverter (org.springframework.beans.SimpleTypeConverter)1 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)1 BeanInitializationException (org.springframework.beans.factory.BeanInitializationException)1 BeanDefinitionVisitor (org.springframework.beans.factory.config.BeanDefinitionVisitor)1 StringValueResolver (org.springframework.util.StringValueResolver)1