Example usage for org.apache.commons.beanutils MappedPropertyDescriptor MappedPropertyDescriptor

List of usage examples for org.apache.commons.beanutils MappedPropertyDescriptor MappedPropertyDescriptor

Introduction

In this page you can find the example usage for org.apache.commons.beanutils MappedPropertyDescriptor MappedPropertyDescriptor.

Prototype

public MappedPropertyDescriptor(String propertyName, Class beanClass) throws IntrospectionException 

Source Link

Document

Constructs a MappedPropertyDescriptor for a property that follows the standard Java convention by having getFoo and setFoo accessor methods, with the addition of a String parameter (the key).

Usage

From source file:no.sesat.search.datamodel.generic.MapDataObjectBeanInfo.java

/**
 *
 * @param name//ww w. j  a  va  2  s. c o  m
 * @param cls
 * @return
 */
public static PropertyDescriptor[] addSingleMappedPropertyDescriptor(final String name, final Class<?> cls) {

    try {
        final PropertyDescriptor[] existing = Introspector.getBeanInfo(cls, Introspector.IGNORE_ALL_BEANINFO)
                .getPropertyDescriptors();
        // remove this introspection from the cache to avoid reuse of the IGNORE_ALL_BEANINFO result
        Introspector.flushFromCaches(cls);

        final PropertyDescriptor[] result = new PropertyDescriptor[existing.length + 2];
        System.arraycopy(existing, 0, result, 0, existing.length);
        result[existing.length] = new MappedPropertyDescriptor(name, cls);
        if (!System.getProperty("java.version").startsWith("1.6")) {
            fixForJavaBug4984912(cls, (MappedPropertyDescriptor) result[existing.length]);
        }

        final String captialised = Character.toUpperCase(name.charAt(0)) + name.substring(1);
        try {
            // try plural with "s" first, then fall back onto "es"
            result[existing.length + 1] = new PropertyDescriptor(name + 's', cls, "get" + captialised + 's',
                    null);
        } catch (IntrospectionException ie) {
            // who on earth designed the english language!?? :@
            result[existing.length + 1] = new PropertyDescriptor(name + "es", cls, "get" + captialised + 's',
                    null);
        }
        return result;

    } catch (IntrospectionException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new IllegalStateException('[' + cls.getSimpleName() + ']' + ex.getMessage(), ex);
    }
}

From source file:org.apache.torque.dsfactory.AbstractDataSourceFactory.java

/**
 * Encapsulates setting configuration properties on
 * <code>DataSource</code> objects.
 *
 * @param property the property to read from the configuration
 * @param c the configuration to read the property from
 * @param ds the <code>DataSource</code> instance to write the property to
 * @throws Exception if anything goes wrong
 *//*from w  ww.  java2s  . co m*/
protected void setProperty(String property, Configuration c, Object ds) throws Exception {
    if (c == null || c.isEmpty()) {
        return;
    }

    String key = property;
    Class<?> dsClass = ds.getClass();
    int dot = property.indexOf('.');
    try {
        if (dot > 0) {
            property = property.substring(0, dot);

            MappedPropertyDescriptor mappedPD = new MappedPropertyDescriptor(property, dsClass);
            Class<?> propertyType = mappedPD.getMappedPropertyType();
            Configuration subProps = c.subset(property);
            // use reflection to set properties
            Iterator<?> j = subProps.getKeys();
            while (j.hasNext()) {
                String subProp = (String) j.next();
                String propVal = subProps.getString(subProp);
                Object value = ConvertUtils.convert(propVal, propertyType);
                PropertyUtils.setMappedProperty(ds, property, subProp, value);

                if (log.isDebugEnabled()) {
                    log.debug(
                            "setMappedProperty(" + ds + ", " + property + ", " + subProp + ", " + value + ")");
                }
            }
        } else {
            if ("password".equals(key)) {
                // do not log value of password
                // for this, ConvertUtils.convert cannot be used
                // as it also logs the value of the converted property
                // so it is assumed here that the password is a String
                String value = c.getString(property);
                PropertyUtils.setSimpleProperty(ds, property, value);
                if (log.isDebugEnabled()) {
                    log.debug("setSimpleProperty(" + ds + ", " + property + ", " + " (value not logged)" + ")");
                }
            } else {
                Class<?> propertyType = PropertyUtils.getPropertyType(ds, property);
                Object value = ConvertUtils.convert(c.getString(property), propertyType);
                PropertyUtils.setSimpleProperty(ds, property, value);

                if (log.isDebugEnabled()) {
                    log.debug("setSimpleProperty(" + ds + ", " + property + ", " + value + ")");
                }
            }
        }
    } catch (RuntimeException e) {
        throw new TorqueRuntimeException("Runtime error setting property " + property, e);
    } catch (Exception e) {
        log.error("Property: " + property + " value: " + c.getString(key) + " is not supported by DataSource: "
                + ds.getClass().getName());
    }
}

From source file:org.kuali.rice.kns.web.struts.form.pojo.PojoPropertyUtilsBean.java

/**
 * <p>/*from w  ww.  ja va  2 s.com*/
 * Retrieve the property descriptor for the specified property of the specified bean, or return <code>null</code> if there is
 * no such descriptor. This method resolves indexed and nested property references in the same manner as other methods in this
 * class, except that if the last (or only) name element is indexed, the descriptor for the last resolved property itself is
 * returned.
 * </p>
 *
 * <p>
 * <strong>FIXME </strong>- Does not work with DynaBeans.
 * </p>
 *
 * @param bean Bean for which a property descriptor is requested
 * @param name Possibly indexed and/or nested name of the property for which a property descriptor is requested
 *
 * @exception IllegalAccessException if the caller does not have access to the property accessor method
 * @exception IllegalArgumentException if <code>bean</code> or <code>name</code> is null
 * @exception IllegalArgumentException if a nested reference to a property returns null
 * @exception InvocationTargetException if the property accessor method throws an exception
 * @exception NoSuchMethodException if an accessor method for this propety cannot be found
 */
public PropertyDescriptor getPropertyDescriptor(Object bean, String name)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    if (bean == null) {
        if (LOG.isDebugEnabled())
            LOG.debug("No bean specified, name = " + name);
        return null;
    }
    if (name == null) {
        throw new IllegalArgumentException("No name specified");
    }
    try {
        // Resolve nested references
        Object propBean = null;
        while (true) {
            int delim = findNextNestedIndex(name);
            //int delim = name.indexOf(PropertyUtils.NESTED_DELIM);
            if (delim < 0) {
                break;
            }
            String next = name.substring(0, delim);
            int indexOfINDEXED_DELIM = next.indexOf(PropertyUtils.INDEXED_DELIM);
            int indexOfMAPPED_DELIM = next.indexOf(PropertyUtils.MAPPED_DELIM);
            if (indexOfMAPPED_DELIM >= 0
                    && (indexOfINDEXED_DELIM < 0 || indexOfMAPPED_DELIM < indexOfINDEXED_DELIM)) {
                propBean = getMappedProperty(bean, next);
            } else {
                if (indexOfINDEXED_DELIM >= 0) {
                    propBean = getIndexedProperty(bean, next);
                } else {
                    propBean = getSimpleProperty(bean, next);
                }
            }
            if (ObjectUtils.isNull(propBean)) {
                Class propertyType = getPropertyType(bean, next);
                if (propertyType != null) {
                    Object newInstance = ObjectUtils.createNewObjectFromClass(propertyType);
                    setSimpleProperty(bean, next, newInstance);
                    propBean = getSimpleProperty(bean, next);
                }
            }
            bean = propBean;
            name = name.substring(delim + 1);
        }

        // Remove any subscript from the final name value
        int left = name.indexOf(PropertyUtils.INDEXED_DELIM);
        if (left >= 0) {
            name = name.substring(0, left);
        }
        left = name.indexOf(PropertyUtils.MAPPED_DELIM);
        if (left >= 0) {
            name = name.substring(0, left);
        }

        // Look up and return this property from our cache
        // creating and adding it to the cache if not found.
        if ((bean == null) || (name == null)) {
            return (null);
        }

        PropertyDescriptor descriptors[] = getPropertyDescriptors(bean);
        if (descriptors != null) {

            for (int i = 0; i < descriptors.length; i++) {
                if (name.equals(descriptors[i].getName()))
                    return (descriptors[i]);
            }
        }

        PropertyDescriptor result = null;
        FastHashMap mappedDescriptors = getMappedPropertyDescriptors(bean);
        if (mappedDescriptors == null) {
            mappedDescriptors = new FastHashMap();
            mappedDescriptors.setFast(true);
        }
        result = (PropertyDescriptor) mappedDescriptors.get(name);
        if (result == null) {
            // not found, try to create it
            try {
                result = new MappedPropertyDescriptor(name, bean.getClass());
            } catch (IntrospectionException ie) {
            }
            if (result != null) {
                mappedDescriptors.put(name, result);
            }
        }

        return result;
    } catch (RuntimeException ex) {
        LOG.error("Unable to get property descriptor for " + bean.getClass().getName() + " . " + name + "\n"
                + ex.getClass().getName() + ": " + ex.getMessage());
        throw ex;
    }
}

From source file:org.mypsycho.beans.PropertyUtilsBean.java

/**
 * <p>/* w  w w  .j  a v a 2s.c  o  m*/
 * Retrieve the property descriptor for the specified property of the specified bean, or return
 * <code>null</code> if there is no such descriptor. This method resolves indexed and nested
 * property references in the same manner as other methods in this class, except that if the
 * last (or only) name element is indexed, the descriptor for the last resolved property itself
 * is returned.
 * </p>
 *
 * @param bean Bean for which a property descriptor is requested
 * @param simpleName name of the property for which a property descriptor is requested
 *        (no nested property supported)
 * @return the property descriptor
 * @exception IllegalAccessException if the caller does not have
 *            access to the property accessor method
 * @exception IllegalArgumentException if <code>bean</code> or <code>name</code> is null
 * @exception IllegalArgumentException if a nested reference to a
 *            property returns null
 * @exception InvocationTargetException if the property accessor method
 *            throws an exception
 * @exception NoSuchMethodException if an accessor method for this
 *            propety cannot be found
 */
public PropertyDescriptor getPropertyDescriptor(Class<?> beanClass, String name) throws NoSuchMethodException {
    PropertyDescriptor[] descriptors = getPropertyDescriptors(beanClass);

    for (PropertyDescriptor descriptor : descriptors) {
        if (name.equals(descriptor.getName())) {
            if (descriptor instanceof StubDescriptor) {
                throw new NoSuchMethodException("No property '" + name + "' at " + beanClass.getName());
            }
            return (descriptor);
        }
    }

    PropertyDescriptor result;
    try {
        result = new MappedPropertyDescriptor(name, beanClass);
    } catch (IntrospectionException ie) {
        try {
            result = new StubDescriptor(name);
        } catch (IntrospectionException e) {
            return null;
        }
    }
    PropertyDescriptor[] newDescriptors = new PropertyDescriptor[descriptors.length + 1];
    System.arraycopy(descriptors, 0, newDescriptors, 0, descriptors.length);
    newDescriptors[descriptors.length] = result;
    descriptorsCache.put(beanClass, newDescriptors);

    if (result instanceof StubDescriptor) {
        throw new NoSuchMethodException("No property '" + name + "' at " + beanClass.getName());
    }
    return result;
}