Example usage for org.springframework.beans BeanWrapper isWritableProperty

List of usage examples for org.springframework.beans BeanWrapper isWritableProperty

Introduction

In this page you can find the example usage for org.springframework.beans BeanWrapper isWritableProperty.

Prototype

boolean isWritableProperty(String propertyName);

Source Link

Document

Determine whether the specified property is writable.

Usage

From source file:net.solarnetwork.node.support.LocationDatumDataSource.java

private void populateLocation(T datum) {
    if (locationId != null && sourceId != null && !shouldIgnoreDatum(datum)) {
        log.debug("Augmenting datum {} with locaiton ID {} ({})", datum, locationId, sourceId);
        if (datum instanceof GeneralLocationDatum) {
            GeneralLocationDatum gDatum = (GeneralLocationDatum) datum;
            gDatum.setLocationId(locationId);
            gDatum.setSourceId(sourceId);
        } else if (datum instanceof GeneralNodeDatum) {
            GeneralNodeDatum gDatum = (GeneralNodeDatum) datum;
            gDatum.putStatusSampleValue(PricedDatum.PRICE_LOCATION_KEY, locationId);
            gDatum.putStatusSampleValue(PricedDatum.PRICE_SOURCE_KEY, sourceId);
        } else {//from   www. j ava2s . co m
            BeanWrapper bean = PropertyAccessorFactory.forBeanPropertyAccess(datum);
            if (bean.isWritableProperty(locationIdPropertyName)
                    && bean.isWritableProperty(sourceIdPropertyName)) {
                bean.setPropertyValue(locationIdPropertyName, locationId);
                bean.setPropertyValue(sourceIdPropertyName, sourceId);
            }
        }
    }
}

From source file:com.alibaba.citrus.service.form.impl.GroupImpl.java

/**
 * group/*w w w  . jav a2s.  c  o  m*/
 * <p>
 * <code>isValidated()</code><code>false</code>group
 * </p>
 */
public void setProperties(Object object) {
    if (!isValidated() || object == null) {
        return;
    }

    if (isValid()) {
        if (log.isDebugEnabled()) {
            log.debug("Set validated properties of group \"" + getName() + "\" to object "
                    + ObjectUtil.identityToString(object));
        }

        BeanWrapper bean = new BeanWrapperImpl(object);
        getForm().getFormConfig().getPropertyEditorRegistrar().registerCustomEditors(bean);

        for (Field field : getFields()) {
            String propertyName = field.getFieldConfig().getPropertyName();

            if (bean.isWritableProperty(propertyName)) {
                PropertyDescriptor pd = bean.getPropertyDescriptor(propertyName);
                MethodParameter mp = BeanUtils.getWriteMethodParameter(pd);
                Object value = field.getValueOfType(pd.getPropertyType(), mp, null);

                bean.setPropertyValue(propertyName, value);
            } else {
                log.debug("No writable property \"{}\" found in type {}", propertyName,
                        object.getClass().getName());
            }
        }
    } else {
        throw new InvalidGroupStateException("Attempted to call setProperties from an invalid input");
    }
}

From source file:com.saysth.commons.quartz.SpringBeanJobFactory.java

/**
 * Create the job instance, populating it with property values taken from
 * the scheduler context, job data map and trigger data map.
 *///  w ww.  j a v  a2 s .c o  m
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
    Object job = bundle.getJobDetail().getJobClass().newInstance();
    BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(job);
    if (isEligibleForPropertyPopulation(bw.getWrappedInstance())) {
        MutablePropertyValues pvs = new MutablePropertyValues();
        if (this.schedulerContext != null) {
            pvs.addPropertyValues(this.schedulerContext);
        }
        pvs.addPropertyValues(bundle.getJobDetail().getJobDataMap());
        pvs.addPropertyValues(bundle.getTrigger().getJobDataMap());
        if (this.ignoredUnknownProperties != null) {
            for (String propName : this.ignoredUnknownProperties) {
                if (pvs.contains(propName) && !bw.isWritableProperty(propName)) {
                    pvs.removePropertyValue(propName);
                }
            }
            bw.setPropertyValues(pvs);
        } else {
            bw.setPropertyValues(pvs, true);
        }
    }
    return job;
}

From source file:net.tirasa.blog.springquartz.SpringBeanJobFactory.java

/**
 * An implementation of SpringBeanJobFactory that retrieves the bean from
 * the Spring context so that autowiring and transactions work
 *
 * This method is overriden./*from   w  w  w .  j a  va 2s  .c  o m*/
 * @see org.springframework.scheduling.quartz.SpringBeanJobFactory#createJobInstance(org.quartz.spi.TriggerFiredBundle)
 */
@Override
protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {

    final ConfigurableApplicationContext ctx = ((ConfigurableApplicationContext) schedulerContext
            .get("applicationContext"));
    final Object job = ctx.getBean(bundle.getJobDetail().getName());
    final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(job);
    if (isEligibleForPropertyPopulation(wrapper.getWrappedInstance())) {
        final MutablePropertyValues pvs = new MutablePropertyValues();
        if (this.schedulerContext != null) {
            pvs.addPropertyValues(this.schedulerContext);
        }
        pvs.addPropertyValues(bundle.getJobDetail().getJobDataMap());
        pvs.addPropertyValues(bundle.getTrigger().getJobDataMap());
        if (this.ignoredUnknownProperties == null) {
            wrapper.setPropertyValues(pvs, true);
        } else {
            for (String propName : this.ignoredUnknownProperties) {
                if (pvs.contains(propName) && !wrapper.isWritableProperty(propName)) {

                    pvs.removePropertyValue(propName);
                }
            }
            wrapper.setPropertyValues(pvs);
        }
    }
    return job;
}

From source file:org.brushingbits.jnap.common.bean.cloning.BeanCloner.java

private Object cloneBean(Object bean, Class<?> type) {
    BeanWrapper source = PropertyAccessorFactory.forBeanPropertyAccess(bean);
    BeanWrapper copy = PropertyAccessorFactory.forBeanPropertyAccess(BeanUtils.instantiate(type));

    // keep instance for circular and multiple references
    context.setAsVisited(bean);/*from   w w  w  . j a v a2s  .  co  m*/
    alreadyCloned.put(bean, copy.getWrappedInstance());

    PropertyDescriptor[] beanProperties = copy.getPropertyDescriptors();
    for (PropertyDescriptor propertyDescriptor : beanProperties) {
        String name = propertyDescriptor.getName();
        context.pushPath(name);
        if (copy.isReadableProperty(name) && copy.isWritableProperty(name)) {
            Object value = source.getPropertyValue(name);
            copy.setPropertyValue(name, clone(value));
        }
        context.popPath();
    }
    Object beanCopy = copy.getWrappedInstance();
    source = null;
    copy = null;
    return beanCopy;
}

From source file:org.syncope.core.scheduling.SpringBeanJobFactory.java

/**
 * An implementation of SpringBeanJobFactory that retrieves the bean from
 * the Spring context so that autowiring and transactions work.
 *
 * {@inheritDoc}/*  w w w .j  a va  2s .c o m*/
 */
@Override
protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {

    final ApplicationContext ctx = ((ConfigurableApplicationContext) schedulerContext
            .get("applicationContext"));

    // Try to re-create job bean from underlying task (useful for managing
    // failover scenarios)
    if (!ctx.containsBean(bundle.getJobDetail().getName())) {
        Long taskId = JobInstanceLoader.getTaskIdFromJobName(bundle.getJobDetail().getName());
        if (taskId != null) {
            TaskDAO taskDAO = ctx.getBean(TaskDAO.class);
            SchedTask task = taskDAO.find(taskId);

            JobInstanceLoader jobInstanceLoader = ctx.getBean(JobInstanceLoader.class);
            jobInstanceLoader.registerJob(task, task.getJobClassName(), task.getCronExpression());
        }

        Long reportId = JobInstanceLoader.getReportIdFromJobName(bundle.getJobDetail().getName());
        if (reportId != null) {
            ReportDAO reportDAO = ctx.getBean(ReportDAO.class);
            Report report = reportDAO.find(reportId);

            JobInstanceLoader jobInstanceLoader = ctx.getBean(JobInstanceLoader.class);
            jobInstanceLoader.registerJob(report);
        }
    }

    final Object job = ctx.getBean(bundle.getJobDetail().getName());
    final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(job);
    if (isEligibleForPropertyPopulation(wrapper.getWrappedInstance())) {
        final MutablePropertyValues pvs = new MutablePropertyValues();
        if (this.schedulerContext != null) {
            pvs.addPropertyValues(this.schedulerContext);
        }
        pvs.addPropertyValues(bundle.getJobDetail().getJobDataMap());
        pvs.addPropertyValues(bundle.getTrigger().getJobDataMap());
        if (this.ignoredUnknownProperties == null) {
            wrapper.setPropertyValues(pvs, true);
        } else {
            for (String propName : this.ignoredUnknownProperties) {
                if (pvs.contains(propName) && !wrapper.isWritableProperty(propName)) {

                    pvs.removePropertyValue(propName);
                }
            }
            wrapper.setPropertyValues(pvs);
        }
    }
    return job;
}

From source file:org.jdto.spring.BeanWrapperBeanModifier.java

/**
 * Set a property using the property path invoking a spring framework {@link BeanWrapper}.
 * @param propertyPath//from  w  w w  .  ja v  a 2  s  . co m
 * @param value
 * @param instance 
 */
@Override
public void doWritePropertyValue(String propertyPath, Object value, Object instance) {
    BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(instance);

    //check and make up for missing association parts.
    StringBuilder builder = new StringBuilder();

    String[] subProps = StringUtils.split(propertyPath, '.');

    //go through all the parts but one
    for (int i = 0; i < subProps.length - 1; i++) {
        String prop = subProps[i];

        if (i > 0) {
            builder.append(".");
        }

        builder.append(prop);

        Object partialValue = beanWrapper.getPropertyValue(builder.toString());
        if (partialValue == null) {
            //make up for it
            Class propCls = beanWrapper.getPropertyType(builder.toString());
            Object madeUpValue = BeanClassUtils.createInstance(propCls);
            if (madeUpValue != null) {
                if (beanWrapper.isWritableProperty(builder.toString())) {
                    beanWrapper.setPropertyValue(builder.toString(), madeUpValue);
                }
            }
        }
    }

    if (!beanWrapper.isWritableProperty(propertyPath)) {
        logger.info("Cannot write property path " + propertyPath + " of bean", instance);
        return;
    }

    //this can be improved by registering property editors on the bean wrapper
    //at moment this approach is not so simple as the current functionality.
    //nevertheless on the future this situation may change.
    Class expectedType = beanWrapper.getPropertyType(propertyPath);
    value = ValueConversionHelper.applyCompatibilityLogic(expectedType, value);

    beanWrapper.setPropertyValue(propertyPath, value);
}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java

@Test
public void testIsWritablePropertyNull() {
    NoRead nr = new NoRead();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(nr);
    try {/*from   w w  w.j a va 2  s. c om*/
        bw.isWritableProperty(null);
        fail("Can't inquire into writability of null property");
    } catch (IllegalArgumentException ex) {
        // expected
    }
}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java

@Test
public void testReadableAndWritableForIndexedProperties() {
    BeanWrapper bw = new JuffrouSpringBeanWrapper(IndexedTestBean.class);

    assertTrue(bw.isReadableProperty("array"));
    assertTrue(bw.isReadableProperty("list"));
    assertTrue(bw.isReadableProperty("set"));
    assertTrue(bw.isReadableProperty("map"));
    assertFalse(bw.isReadableProperty("xxx"));

    assertTrue(bw.isWritableProperty("array"));
    assertTrue(bw.isWritableProperty("list"));
    assertTrue(bw.isWritableProperty("set"));
    assertTrue(bw.isWritableProperty("map"));
    assertFalse(bw.isWritableProperty("xxx"));

    assertTrue(bw.isReadableProperty("array[0]"));
    assertTrue(bw.isReadableProperty("array[0].name"));
    assertTrue(bw.isReadableProperty("list[0]"));
    assertTrue(bw.isReadableProperty("list[0].name"));
    assertTrue(bw.isReadableProperty("set[0]"));
    assertTrue(bw.isReadableProperty("set[0].name"));
    assertTrue(bw.isReadableProperty("map[key1]"));
    assertTrue(bw.isReadableProperty("map[key1].name"));
    assertTrue(bw.isReadableProperty("map[key4][0]"));
    assertTrue(bw.isReadableProperty("map[key4][0].name"));
    assertTrue(bw.isReadableProperty("map[key4][1]"));
    assertTrue(bw.isReadableProperty("map[key4][1].name"));
    assertFalse(bw.isReadableProperty("array[key1]"));

    assertTrue(bw.isWritableProperty("array[0]"));
    assertTrue(bw.isWritableProperty("array[0].name"));
    assertTrue(bw.isWritableProperty("list[0]"));
    assertTrue(bw.isWritableProperty("list[0].name"));
    assertTrue(bw.isWritableProperty("set[0]"));
    assertTrue(bw.isWritableProperty("set[0].name"));
    assertTrue(bw.isWritableProperty("map[key1]"));
    assertTrue(bw.isWritableProperty("map[key1].name"));
    assertTrue(bw.isWritableProperty("map[key4][0]"));
    assertTrue(bw.isWritableProperty("map[key4][0].name"));
    assertTrue(bw.isWritableProperty("map[key4][1]"));
    assertTrue(bw.isWritableProperty("map[key4][1].name"));
    assertFalse(bw.isWritableProperty("array[key1]"));
}