Example usage for org.apache.commons.beanutils PropertyUtils setProperty

List of usage examples for org.apache.commons.beanutils PropertyUtils setProperty

Introduction

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

Prototype

public static void setProperty(Object bean, String name, Object value)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException 

Source Link

Document

Set the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.

For more details see PropertyUtilsBean.

Usage

From source file:org.carewebframework.ui.zk.ZKUtil.java

/**
 * Wires variables from a map into a controller. Useful to inject parameters passed in an
 * argument map.// w  ww .j a va2 s .co  m
 * 
 * @param map The argument map.
 * @param controller The controller to be wired.
 */
public static void wireController(Map<?, ?> map, Object controller) {
    if (map == null || map.isEmpty() || controller == null) {
        return;
    }

    for (Entry<?, ?> entry : map.entrySet()) {
        String key = entry.getKey().toString();
        Object value = entry.getValue();

        try {
            PropertyUtils.setProperty(controller, key, value);
        } catch (Exception e) {
            try {
                FieldUtils.writeField(controller, key, value, true);
            } catch (Exception e1) {
            }
        }

    }
}

From source file:org.craftercms.commons.jackson.mvc.CrafterJackson2MessageConverter.java

private void secureProperty(final Object object, final Field field) {
    //Should be null due we ask before if the annotation exists !!
    String[] roles = field.getAnnotation(SecureProperty.class).role();
    try {//from w  ww. j  a  v a2  s .com
        if (!securePropertyHandler.suppressProperty(object, roles)) {
            PropertyUtils.setProperty(object, field.getName(), null);
        }
    } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
        log.error("Unable to inject value " + field.getName() + " for class " + object.getClass(), e);
    }
}

From source file:org.craftercms.commons.jackson.mvc.CrafterJackson2MessageConverter.java

private void injectValue(final Object object, final Field field) {
    //Should be null due we ask before if the annotation exists !!
    String propertyToUseName = field.getAnnotation(InjectValue.class).useProperty();
    try {//from ww w .ja va2  s  . co  m
        Object propertyValue = PropertyUtils.getProperty(object, propertyToUseName);
        Object valueToInject = injectValueFactory.getObjectFor(
                PropertyUtils.getPropertyType(object, field.getName()), propertyValue, propertyToUseName,
                object);
        if (valueToInject != null) {
            PropertyUtils.setProperty(object, field.getName(), valueToInject);
        }
    } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
        log.error("Unable to inject value " + field.getName() + " for class " + object.getClass(), e);
    }
}

From source file:org.cropinformatics.ui.components.impl.PropertySetValueOperation.java

@Override
public IStatus execute(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {

    try {/*  w ww  . j a v a  2  s  . co m*/
        oldValue = PropertyUtils.getProperty(bean, property);
    } catch (Exception e) {
        canUndo = false;
    }

    try {
        PropertyUtils.setProperty(bean, property, value);

        return Status.OK_STATUS;
    } catch (IllegalAccessException e) {
        throw new ExecutionException(e.getLocalizedMessage(), e);
    } catch (InvocationTargetException e) {
        throw new ExecutionException(e.getLocalizedMessage(), e);
    } catch (NoSuchMethodException e) {
        throw new ExecutionException(e.getLocalizedMessage(), e);
    }
}

From source file:org.cropinformatics.ui.components.impl.PropertySetValueOperation.java

@Override
public IStatus redo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
    try {//from w w w . j  ava  2  s  .c  o m
        PropertyUtils.setProperty(bean, property, value);

        return Status.OK_STATUS;
    } catch (IllegalAccessException e) {
        throw new ExecutionException(e.getLocalizedMessage(), e);
    } catch (InvocationTargetException e) {
        throw new ExecutionException(e.getLocalizedMessage(), e);
    } catch (NoSuchMethodException e) {
        throw new ExecutionException(e.getLocalizedMessage(), e);
    }
}

From source file:org.cropinformatics.ui.components.impl.PropertySetValueOperation.java

@Override
public IStatus undo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
    try {//from w  w w  .  j  av  a  2s .co m
        PropertyUtils.setProperty(bean, property, oldValue);

        return Status.OK_STATUS;
    } catch (IllegalAccessException e) {
        throw new ExecutionException(e.getLocalizedMessage(), e);
    } catch (InvocationTargetException e) {
        throw new ExecutionException(e.getLocalizedMessage(), e);
    } catch (NoSuchMethodException e) {
        throw new ExecutionException(e.getLocalizedMessage(), e);
    }
}

From source file:org.cruk.genologics.api.impl.GenologicsAPIImpl.java

@Override
public <E extends LimsEntity<E>> GenologicsFile uploadFile(LimsEntityLinkable<E> entity, URL fileURL,
        boolean publishInLablink) throws IOException {
    if (entity == null) {
        throw new IllegalArgumentException("entity cannot be null");
    }//from w ww  .j a v  a2  s. c  om
    if (fileURL == null) {
        throw new IllegalArgumentException("fileURL cannot be null");
    }

    checkServerSet();

    GenologicsFile storageRequest;

    URLInputStreamResource fileURLResource = new URLInputStreamResource(fileURL);
    try {
        long length = fileURLResource.contentLength();

        if (length < 0) {
            logger.warn("Cannot determine size of file from the " + fileURL.getProtocol() + " protocol.");
        }

        // Post a request to the "glsstorage" to create a new GenologicsFile object to
        // hold the uploaded file. Requirements are for the holding entity and the
        // original location to be set.
        // See http://www.genologics.com/files/permanent/API/latest/rest.version.glsstorage.html

        storageRequest = new GenologicsFile();
        storageRequest.setAttachedTo(entity);
        storageRequest.setOriginalLocation(fileURL.toExternalForm());

        String storageUri = apiRoot + "glsstorage";

        ResponseEntity<GenologicsFile> response = restClient.postForEntity(storageUri, storageRequest,
                GenologicsFile.class);

        storageRequest = response.getBody();

        // May as well set the "Publish to LabLink" flag on the file now.
        // By either upload mechanism, this object will be posted back at some point.
        // (Setting it initially is ignored.)

        storageRequest.setPublished(publishInLablink);

        // See which protocol the resulting file gives. If it is "sftp", we can upload.
        // Anything else cannot allow an upload (haven't seen anything else so far).

        URL targetURL = new URL(null, storageRequest.getContentLocation().toString(),
                NullURLStreamHandler.INSTANCE);

        if (!SFTP_PROTOCOL.equals(targetURL.getProtocol())) {
            throw new GenologicsUpdateException("File upload to the file store for links giving the "
                    + targetURL.getProtocol().toUpperCase() + " protocol is not supported.");
        }

        if (uploadOverHttp) {
            if (length >= 0 && length <= httpUploadSizeLimit) {
                // Have a length and it's within the set limit. Use HTTP.

                uploadViaHTTP(fileURLResource, storageRequest);
            } else if (autoRevertToSFTP) {
                // Could not get the length, or the file is too big. Allowed to
                // revert to SFTP, so use that.

                if (length < 0) {
                    logger.info("Size of {} cannot be determined, so reverting to SFTP.", fileURL);
                } else {
                    logger.info(
                            "Upload of {} is too large to be uploaded through the HTTP mechanism. Reverting to SFTP.",
                            fileURL);
                }

                uploadViaSFTP(fileURLResource, storageRequest);
            } else {
                // Could not get the length, or the file is too big. Not allowed to
                // revert to SFTP, so fail.

                if (length < 0) {
                    throw new GenologicsUpdateException("Cannot upload " + fileURL
                            + " - cannot determine its size, so it may exceed the maximum HTTP upload size of "
                            + httpUploadSizeLimit);
                } else {
                    throw new GenologicsUpdateException("Cannot upload " + fileURL
                            + " - the content exceeds the maximum HTTP upload size of " + httpUploadSizeLimit);
                }
            }
        } else {
            // Not using HTTP upload at all, so straight to SFTP.

            uploadViaSFTP(fileURLResource, storageRequest);
        }
    } finally {
        fileURLResource.close();
    }

    try {
        PropertyUtils.setProperty(entity, "file", storageRequest);
    } catch (Exception e) {
        // Quietly leave the file property of the entity as it was,
        // or ignore if there was no file property.
    }

    return storageRequest;
}

From source file:org.drools.informer.domain.DomainModelSupportTest.java

private void setTextProperty(String property, String value) {
    try {//from ww w  .j  a  va 2 s  .co m
        logger.debug("Setting " + property);
        Class<?> propertyClass = PropertyUtils.getPropertyType(data, property);
        Object v = DomainModelSupport.answerToObject(Question.QuestionType.TYPE_TEXT, value, propertyClass);
        PropertyUtils.setProperty(data, property, v);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.drools.informer.domain.DomainModelSupportTest.java

private void setNumberProperty(String property, Long value) {
    try {/*from  w  ww  . jav  a2 s  . c o  m*/
        logger.debug("Setting " + property);
        Class<?> propertyClass = PropertyUtils.getPropertyType(data, property);
        Object v = DomainModelSupport.answerToObject(Question.QuestionType.TYPE_NUMBER, value, propertyClass);
        PropertyUtils.setProperty(data, property, v);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.drools.informer.domain.DomainModelSupportTest.java

private void setDecimalProperty(String property, BigDecimal value) {
    try {//from  w  ww. j a  v a2 s  .c o  m
        logger.debug("Setting " + property);
        Class<?> propertyClass = PropertyUtils.getPropertyType(data, property);
        Object v = DomainModelSupport.answerToObject(Question.QuestionType.TYPE_DECIMAL, value, propertyClass);
        PropertyUtils.setProperty(data, property, v);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}