Example usage for org.springframework.beans PropertyValues contains

List of usage examples for org.springframework.beans PropertyValues contains

Introduction

In this page you can find the example usage for org.springframework.beans PropertyValues contains.

Prototype

boolean contains(String propertyName);

Source Link

Document

Is there a property value (or other processing entry) for this property?

Usage

From source file:org.cloudfoundry.reconfiguration.util.StandardPropertyAugmenter.java

@SuppressWarnings("unchecked")
private Properties getPropertiesFactoryBeanProperties(BeanDefinition beanDefinition) {
    PropertyValues propertyValues = beanDefinition.getPropertyValues();
    if (propertyValues.contains("location")) {
        return loadPropertiesForLocation(propertyValues.getPropertyValue("location").getValue());
    } else if (propertyValues.contains("locations")) {
        return loadPropertiesForLocations(propertyValues.getPropertyValue("locations").getValue());
    } else if (propertyValues.contains("properties")) {
        Object value = propertyValues.getPropertyValue("properties").getValue();
        if (value instanceof BeanDefinitionHolder) {
            return extractProperties((BeanDefinitionHolder) value);
        } else {/*from w w  w  .j a  v  a 2  s  .  c o  m*/
            return convert((Map<String, String>) value);
        }
    }

    throw new IllegalArgumentException(
            "Unable to process PropertiesFactoryBean; doesn't contain either 'locations' or 'properties' property");
}

From source file:org.jdal.aop.SerializableReference.java

private Object readResolve() throws ObjectStreamException {
    if (beanFactory == null) {
        log.error("Can't not deserialize reference without bean factory");
        return null;
    }/*from   w  w w .j  a v a  2 s .c  o m*/

    if (useMemoryCache) {
        Object ret = serializedObjects.remove(this.id);
        if (ret != null) {
            if (log.isDebugEnabled())
                log.debug("Removed a serialized reference. serialized objects size [" + serializedObjects.size()
                        + "]");

            return getSerializableProxy(ret);
        }
    }

    if (targetBeanName != null) {
        if (log.isDebugEnabled())
            log.debug("Resolving serializable object to bean name [" + targetBeanName + "]");

        return getSerializableProxy();
    }

    if (log.isDebugEnabled())
        log.debug("Resolving serializable object for [" + descriptor.getDependencyName() + "]");

    Field field = descriptor.getField();

    // Check bean definition
    BeanDefinition rbd = beanFactory.getBeanDefinition(beanName);
    PropertyValues pvs = rbd.getPropertyValues();
    if (pvs.contains(field.getName())) {
        Object value = pvs.getPropertyValue(field.getName());
        if (value instanceof BeanReference) {
            // cache the bean name 
            this.targetBeanName = ((BeanReference) value).getBeanName();
            return getSerializableProxy();
        }
    }

    // Check Autowired
    try {
        Object bean = beanFactory.resolveDependency(descriptor, beanName);

        if (bean != null)
            return getSerializableProxy(bean);
    } catch (BeansException be) {
        // dependency not found.
    }

    // Check Resource annotation
    if (field.isAnnotationPresent(Resource.class)) {
        Resource r = field.getAnnotation(Resource.class);
        String name = StringUtils.isEmpty(r.name()) ? descriptor.getField().getName() : r.name();

        if (beanFactory.containsBean(name)) {
            this.targetBeanName = name;
            return getSerializableProxy();
        }
    }

    // Try with depend beans.      
    String[] dependentBeans = beanFactory.getDependenciesForBean(beanName);
    List<String> candidates = new ArrayList<String>();

    for (String name : dependentBeans) {
        if (beanFactory.isTypeMatch(name, descriptor.getDependencyType()))
            ;
        candidates.add(name);
    }

    if (candidates.size() == 1)
        return getSerializableProxy(beanFactory.getBean(candidates.get(0)));

    if (candidates.size() > 1) {
        for (PropertyValue pv : pvs.getPropertyValues()) {
            if (pv.getValue() instanceof BeanReference) {
                BeanReference br = (BeanReference) pv.getValue();
                if (candidates.contains(br.getBeanName()))
                    return getSerializableProxy(beanFactory.getBean(br.getBeanName()));
            }
        }
    }

    log.error("Cant not resolve serializable reference wiht candidates " + candidates.toArray());

    return null;
}

From source file:org.okj.commons.annotations.ServiceReferenceInjectionBeanPostProcessor.java

public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean,
        String beanName) throws BeansException {

    MutablePropertyValues newprops = new MutablePropertyValues(pvs);
    for (PropertyDescriptor pd : pds) {
        ServiceReference s = hasServiceProperty(pd);
        if (s != null && !pvs.contains(pd.getName())) {
            try {
                if (logger.isDebugEnabled())
                    logger.debug(/* w ww .j  a  v  a 2 s .co  m*/
                            "Processing annotation [" + s + "] for [" + beanName + "." + pd.getName() + "]");
                FactoryBean importer = getServiceImporter(s, pd.getWriteMethod(), beanName);
                // BPPs are created in stageOne(), even though they are run in stageTwo(). This check means that
                // the call to getObject() will not fail with ServiceUnavailable. This is safe to do because
                // ServiceReferenceDependencyBeanFactoryPostProcessor will ensure that mandatory services are
                // satisfied before stageTwo() is run.
                if (bean instanceof BeanPostProcessor) {
                    ImporterCallAdapter.setCardinality(importer, Cardinality.C_0__1);
                }
                newprops.addPropertyValue(pd.getName(), importer.getObject());
            } catch (Exception e) {
                throw new FatalBeanException("Could not create service reference", e);
            }
        }
    }
    return newprops;
}

From source file:org.apache.james.container.spring.lifecycle.osgi.AbstractOSGIAnnotationBeanPostProcessor.java

@Override
@SuppressWarnings("rawtypes")
public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean,
        String beanName) throws BeansException {

    MutablePropertyValues newprops = new MutablePropertyValues(pvs);
    for (PropertyDescriptor pd : pds) {
        A s = hasAnnotatedProperty(pd);/*from www. j  a v  a 2 s  .  com*/
        if (s != null && !pvs.contains(pd.getName())) {
            try {
                if (logger.isDebugEnabled())
                    logger.debug(
                            "Processing annotation [" + s + "] for [" + beanName + "." + pd.getName() + "]");
                FactoryBean importer = getServiceImporter(s, pd.getWriteMethod(), beanName);
                // BPPs are created in stageOne(), even though they are run in stageTwo(). This check means that
                // the call to getObject() will not fail with ServiceUnavailable. This is safe to do because
                // ServiceReferenceDependencyBeanFactoryPostProcessor will ensure that mandatory services are
                // satisfied before stageTwo() is run.
                if (bean instanceof BeanPostProcessor) {
                    ImporterCallAdapter.setCardinality(importer, Cardinality.C_0__1);
                }
                newprops.addPropertyValue(pd.getName(), importer.getObject());
            } catch (Exception e) {
                throw new FatalBeanException("Could not create service reference", e);
            }
        }
    }
    return newprops;
}

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

protected Map<String, PropertyDescriptor> unsatisfiedNonSimpleProperties(BeanDefinition mbd) {
    Map<String, PropertyDescriptor> properties = CollectUtils.newHashMap();
    PropertyValues pvs = mbd.getPropertyValues();
    Class<?> clazz = null;//from   w  w w  .  j  a v  a 2  s .  co  m
    try {
        clazz = Class.forName(mbd.getBeanClassName());
    } catch (ClassNotFoundException e) {
        logger.error("Class " + mbd.getBeanClassName() + "not found", e);
        return properties;
    }
    PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(clazz);
    for (PropertyDescriptor pd : pds) {
        if (pd.getWriteMethod() != null && !isExcludedFromDependencyCheck(pd) && !pvs.contains(pd.getName())
                && !BeanUtils.isSimpleProperty(pd.getPropertyType())) {
            properties.put(pd.getName(), pd);
        }
    }
    return properties;
}

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/*w  ww  . ja v  a  2s. co  m*/
 * @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.uif.util.ViewModelUtils.java

/**
 * Helper method for getting the string value of a property from a {@link PropertyValues}
 *
 * @param propertyValues property values instance to pull from
 * @param propertyName name of property whose value should be retrieved
 * @return String value for property or null if property was not found
 *///  ww  w  .j av a2s. co m
public static String getStringValFromPVs(PropertyValues propertyValues, String propertyName) {
    String propertyValue = null;

    if ((propertyValues != null) && propertyValues.contains(propertyName)) {
        Object pvValue = propertyValues.getPropertyValue(propertyName).getValue();
        if (pvValue instanceof TypedStringValue) {
            TypedStringValue typedStringValue = (TypedStringValue) pvValue;
            propertyValue = typedStringValue.getValue();
        } else if (pvValue instanceof String) {
            propertyValue = (String) pvValue;
        }
    }

    return propertyValue;
}

From source file:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.java

/**
 * Return an array of non-simple bean properties that are unsatisfied.
 * These are probably unsatisfied references to other beans in the
 * factory. Does not include simple properties like primitives or Strings.
 * @param mbd the merged bean definition the bean was created with
 * @param bw the BeanWrapper the bean was created with
 * @return an array of bean property names
 * @see org.springframework.beans.BeanUtils#isSimpleProperty
 *//*w  ww  . ja va2s  .c  o  m*/
protected String[] unsatisfiedNonSimpleProperties(AbstractBeanDefinition mbd, BeanWrapper bw) {
    Set<String> result = new TreeSet<>();
    PropertyValues pvs = mbd.getPropertyValues();
    PropertyDescriptor[] pds = bw.getPropertyDescriptors();
    for (PropertyDescriptor pd : pds) {
        if (pd.getWriteMethod() != null && !isExcludedFromDependencyCheck(pd) && !pvs.contains(pd.getName())
                && !BeanUtils.isSimpleProperty(pd.getPropertyType())) {
            result.add(pd.getName());
        }
    }
    return StringUtils.toStringArray(result);
}

From source file:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.java

/**
 * Perform a dependency check that all properties exposed have been set,
 * if desired. Dependency checks can be objects (collaborating beans),
 * simple (primitives and String), or all (both).
 * @param beanName the name of the bean//from   w w  w  . java2 s .  c  o m
 * @param mbd the merged bean definition the bean was created with
 * @param pds the relevant property descriptors for the target bean
 * @param pvs the property values to be applied to the bean
 * @see #isExcludedFromDependencyCheck(java.beans.PropertyDescriptor)
 */
protected void checkDependencies(String beanName, AbstractBeanDefinition mbd, PropertyDescriptor[] pds,
        @Nullable PropertyValues pvs) throws UnsatisfiedDependencyException {

    int dependencyCheck = mbd.getDependencyCheck();
    for (PropertyDescriptor pd : pds) {
        if (pd.getWriteMethod() != null && (pvs == null || !pvs.contains(pd.getName()))) {
            boolean isSimple = BeanUtils.isSimpleProperty(pd.getPropertyType());
            boolean unsatisfied = (dependencyCheck == RootBeanDefinition.DEPENDENCY_CHECK_ALL)
                    || (isSimple && dependencyCheck == RootBeanDefinition.DEPENDENCY_CHECK_SIMPLE)
                    || (!isSimple && dependencyCheck == RootBeanDefinition.DEPENDENCY_CHECK_OBJECTS);
            if (unsatisfied) {
                throw new UnsatisfiedDependencyException(mbd.getResourceDescription(), beanName, pd.getName(),
                        "Set this property value or disable dependency checking for this bean.");
            }
        }
    }
}

From source file:org.springframework.osgi.extensions.annotation.ServiceReferenceInjectionBeanPostProcessor.java

public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean,
        String beanName) throws BeansException {

    MutablePropertyValues newprops = new MutablePropertyValues(pvs);
    for (PropertyDescriptor pd : pds) {
        ServiceReference s = hasServiceProperty(pd);
        if (s != null && !pvs.contains(pd.getName())) {
            try {
                if (logger.isDebugEnabled())
                    logger.debug(//from   w w  w .  j  ava2 s. c  o m
                            "Processing annotation [" + s + "] for [" + beanName + "." + pd.getName() + "]");
                FactoryBean importer = getServiceImporter(s, pd.getWriteMethod(), beanName);
                // BPPs are created in stageOne(), even though they are run
                // in stageTwo(). This check means that
                // the call to getObject() will not fail with
                // ServiceUnavailable. This is safe to do because
                // ServiceReferenceDependencyBeanFactoryPostProcessor will
                // ensure that mandatory services are
                // satisfied before stageTwo() is run.
                if (bean instanceof BeanPostProcessor) {
                    ImporterCallAdapter.setAvailability(importer, Availability.OPTIONAL);
                }
                newprops.addPropertyValue(pd.getName(), importer.getObject());
            } catch (Exception e) {
                throw new FatalBeanException("Could not create service reference", e);
            }
        }
    }
    return newprops;
}