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

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

Introduction

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

Prototype

public static Object getSimpleProperty(Object bean, String name)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException 

Source Link

Document

Return the value of the specified simple property of the specified bean, with no type conversions.

For more details see PropertyUtilsBean.

Usage

From source file:org.nuclos.client.statemodel.admin.CollectableStateModel.java

@Override
public Object getValue(String sFieldName) {
    try {//from w  ww.  jav  a2 s  . c o m
        return PropertyUtils.getSimpleProperty(this.getStateModelVO(), sFieldName);
    } catch (IllegalAccessException ex) {
        throw new NuclosFatalException(ex);
    } catch (InvocationTargetException ex) {
        throw new NuclosFatalException(ex);
    } catch (NoSuchMethodException ex) {
        throw new NuclosFatalException(ex);
    }
}

From source file:org.nuclos.common.collect.collectable.BeanPropertyCollectableField.java

private Object getProperty(Object oBean, String sPropertyName) {
    try {//from w  w w  .  j  a v  a2  s. c om
        return PropertyUtils.getSimpleProperty(oBean, sPropertyName);
        /** @todo return BeanUtils.getSimpleProperty(oBean, sPropertyName); */
    } catch (IllegalAccessException ex) {
        throw new CommonFatalException(ex);
    } catch (InvocationTargetException ex) {
        throw new CommonFatalException(ex);
    } catch (NoSuchMethodException ex) {
        throw new CommonFatalException(ex);
    }
}

From source file:org.objectstyle.cayenne.modeler.util.ModelerUtil.java

/**
 * Returns the "name" property of the object.
 * //from w w w  .  j a  va2 s.  c  om
 * @since 1.1
 */
public static String getObjectName(Object object) {
    if (object == null) {
        return null;
    } else if (object instanceof MapObject) {
        return ((MapObject) object).getName();
    } else if (object instanceof String) {
        return (String) object;
    } else {
        try {
            // use reflection
            return (String) PropertyUtils.getSimpleProperty(object, "name");
        } catch (Exception ex) {
            return null;
        }
    }
}

From source file:org.oddjob.framework.WrapDynaBean.java

/**
 * Return the value of a simple property with the specified name.
 *
 * @param name Name of the property whose value is to be retrieved
 *
 * @exception IllegalArgumentException if there is no property
 *  of the specified name//from   ww w  .  ja  v a  2s  .  co  m
 */
public Object get(String name) {

    if (!dynaClass.isReadable(name)) {
        return null;
    }
    Object value = null;
    try {
        value = PropertyUtils.getSimpleProperty(instance, name);
    } catch (Throwable t) {
        throw new RuntimeException("Failed getting property " + name, t);
    }
    return (value);
}

From source file:org.ow2.sirocco.cimi.server.request.CimiSelect.java

/**
 * Copy the values of bean in the map with the attributes found in the
 * CimiSelect./*from   www. ja v  a 2 s . c  o m*/
 * <p>
 * If a attribute name is not found in bean, it is not copied in map.
 * </p>
 * 
 * @param bean The bean where are the values
 * @return A map with the attribute name and his value
 */
public Map<String, Object> copyBeanAttributes(final Object bean) {
    Map<String, Object> attrValues = new HashMap<String, Object>();
    Object value;
    if (false == this.isEmpty()) {
        for (String name : this.getValues()) {
            try {
                value = PropertyUtils.getSimpleProperty(bean, name);
                if (null != value) {
                    attrValues.put(name, value);
                }
            } catch (Exception e) {
                CimiSelect.LOGGER.debug("Property [{}] not found in bean [{}] with this message error: {}",
                        new Object[] { name, bean.getClass().getName(), e.getMessage() });
            }
        }
    }
    return attrValues;
}

From source file:org.ow2.sirocco.cloudmanager.core.utils.QueryHelper.java

public static <E extends Identifiable> QueryResult<E> getEntityList(final EntityManager em,
        final QueryParamsBuilder params) throws InvalidRequestException {
    StringBuffer whereClauseSB = new StringBuffer();
    if (params.getTenantId() != null) {
        if (!params.isReturnPublicEntities()) {
            whereClauseSB.append(" v.tenant.id=:tenantId ");
        } else {// w w  w .  j a  v  a  2 s .co m
            whereClauseSB.append(
                    "( v.tenant.id=:tenantId OR v.visibility = org.ow2.sirocco.cloudmanager.model.cimi.extension.Visibility.PUBLIC) ");
        }
    }
    if (params.getStateToIgnore() != null) {
        if (whereClauseSB.length() > 0) {
            whereClauseSB.append(" AND ");
        }
        whereClauseSB.append(" v.state<>" + params.getStateToIgnore().getClass().getName() + "."
                + params.getStateToIgnore().name() + " ");
    }
    if (params.isFilterEmbbededTemplate()) {
        if (whereClauseSB.length() > 0) {
            whereClauseSB.append(" AND ");
        }
        whereClauseSB.append(" v.isEmbeddedInSystemTemplate=false ");
    }
    if (params.getFilters() != null) {
        String filterClause;
        try {
            filterClause = QueryHelper.generateFilterClause(params.getFilters(), "v",
                    params.getClazz().getName() + "$State.");
        } catch (ParseException ex) {
            throw new InvalidRequestException("Parsing error in filter expression " + ex.getMessage());
        } catch (TokenMgrError ex) {
            throw new InvalidRequestException(ex.getMessage());
        }
        if (!filterClause.isEmpty()) {
            if (whereClauseSB.length() > 0) {
                whereClauseSB.append(" AND ");
            }
            whereClauseSB.append(filterClause);
        }
    }

    if (params.getMarker() != null) {
        try {
            Resource resourceAtMarker = (Resource) em
                    .createQuery("SELECT r FROM " + params.getEntityType() + " r WHERE uuid=:uuid")
                    .setParameter("uuid", params.getMarker()).getSingleResult();
            if (whereClauseSB.length() > 0) {
                whereClauseSB.append(" AND ");
            }
            whereClauseSB.append(" v.id>" + resourceAtMarker.getId() + " ");
        } catch (NoResultException e) {
            throw new InvalidRequestException("Invalid marker " + params.getMarker());
        }
    }

    String whereClause = whereClauseSB.toString();

    try {
        int count = ((Number) em
                .createQuery("SELECT COUNT(v) FROM " + params.getEntityType() + " v WHERE " + whereClause)
                .setParameter("tenantId", params.getTenantId()).getSingleResult()).intValue();
        Query query = em.createQuery(
                "SELECT v FROM " + params.getEntityType() + " v  WHERE " + whereClause + " ORDER BY v.id DESC")
                .setParameter("tenantId", params.getTenantId());
        if (params.getLimit() != null) {
            query.setMaxResults(params.getLimit());
        } else {
            if (params.getFirst() != null) {
                query.setFirstResult(params.getFirst());
            }
            if (params.getLast() != null) {
                if (params.getFirst() != null) {
                    query.setMaxResults(params.getLast() - params.getFirst() + 1);
                } else {
                    query.setMaxResults(params.getLast() + 1);
                }
            }
        }
        List<E> queryResult = query.getResultList();
        if (params.getAttributes() != null && params.getAttributes().size() != 0) {
            List<E> items = new ArrayList<E>();
            for (E from : queryResult) {
                E resource = (E) params.getClazz().newInstance();
                for (int i = 0; i < params.getAttributes().size(); i++) {
                    try {
                        PropertyUtils.setSimpleProperty(resource, params.getAttributes().get(i),
                                PropertyUtils.getSimpleProperty(from, params.getAttributes().get(i)));
                    } catch (NoSuchMethodException e) {
                        // ignore wrong attribute name
                    }
                }
                resource.setUuid(from.getUuid());
                if (resource instanceof ICloudProviderResource) {
                    ICloudProviderResource fromResource = (ICloudProviderResource) from;
                    ICloudProviderResource toResource = (ICloudProviderResource) resource;
                    toResource.setLocation(fromResource.getLocation());
                    toResource.setProviderAssignedId(fromResource.getProviderAssignedId());
                    toResource.setCloudProviderAccount(fromResource.getCloudProviderAccount());
                } else if (resource instanceof IMultiCloudResource) {
                    IMultiCloudResource fromResource = (IMultiCloudResource) from;
                    IMultiCloudResource toResource = (IMultiCloudResource) resource;
                    toResource.setProviderMappings(fromResource.getProviderMappings());
                }
                items.add(resource);
            }
            return new QueryResult<E>(count, items);
        } else {
            return new QueryResult<E>(count, queryResult);
        }
    } catch (IllegalArgumentException ex) {
        ex.printStackTrace();
        throw new InvalidRequestException(ex.getMessage());
    } catch (InstantiationException ex) {
        ex.printStackTrace();
        throw new InvalidRequestException(ex.getMessage());
    } catch (IllegalAccessException ex) {
        ex.printStackTrace();
        throw new InvalidRequestException(ex.getMessage());
    } catch (InvocationTargetException ex) {
        throw new InvalidRequestException(ex.getMessage());
    }
}

From source file:org.ow2.sirocco.cloudmanager.core.utils.QueryHelper.java

public static <E> QueryResult<E> getCollectionItemList(final EntityManager em, final QueryParamsBuilder params)
        throws InvalidRequestException {
    StringBuffer whereClauseSB = new StringBuffer();
    if (params.getTenantId() != null) {
        whereClauseSB.append(" v.tenant.id=:tenantId ");
    }//  w w w .ja  v  a 2  s  .  com
    if (params.getStateToIgnore() != null) {
        if (whereClauseSB.length() > 0) {
            whereClauseSB.append(" AND ");
        }
        whereClauseSB.append(" vv.state<>" + params.getStateToIgnore().getClass().getName() + "."
                + params.getStateToIgnore().name() + " ");
    }
    if (whereClauseSB.length() > 0) {
        whereClauseSB.append(" AND ");
    }
    whereClauseSB.append("v.uuid=:cid ");
    if (params.getFilters() != null) {
        String filterClause;
        try {
            filterClause = QueryHelper.generateFilterClause(params.getFilters(), "vv",
                    params.getClazz().getName() + "$State.");
        } catch (ParseException ex) {
            throw new InvalidRequestException("Parsing error in filter expression " + ex.getMessage());
        } catch (TokenMgrError ex) {
            throw new InvalidRequestException(ex.getMessage());
        }
        if (!filterClause.isEmpty()) {
            if (whereClauseSB.length() > 0) {
                whereClauseSB.append(" AND ");
            }
            whereClauseSB.append(filterClause);
        }
    }

    String whereClause = whereClauseSB.toString();
    String queryExpression = "SELECT COUNT(vv) FROM " + params.getEntityType() + " vv, "
            + params.getContainerType() + " v WHERE vv MEMBER OF v." + params.getContainerAttributeName()
            + " AND " + whereClause;
    try {
        int count = ((Number) em.createQuery(queryExpression).setParameter("cid", params.getContainerId())
                .setParameter("tenantId", params.getTenantId()).getSingleResult()).intValue();
        queryExpression = "SELECT vv FROM " + params.getEntityType() + " vv, " + params.getContainerType()
                + " v WHERE vv MEMBER OF v." + params.getContainerAttributeName() + " AND " + whereClause
                + " ORDER BY vv.id";
        Query query = em.createQuery(queryExpression).setParameter("cid", params.getContainerId())
                .setParameter("tenantId", params.getTenantId());

        if (params.getFirst() != null) {
            query.setFirstResult(params.getFirst());
        }
        if (params.getLast() != null) {
            if (params.getFirst() != null) {
                query.setMaxResults(params.getLast() - params.getFirst() + 1);
            } else {
                query.setMaxResults(params.getLast() + 1);
            }
        }
        List<E> queryResult = query.getResultList();
        if (params.getAttributes() != null && params.getAttributes().size() != 0) {
            List<E> items = new ArrayList<E>();
            for (E from : queryResult) {
                E resource = (E) params.getClazz().newInstance();
                for (int i = 0; i < params.getAttributes().size(); i++) {
                    try {
                        PropertyUtils.setSimpleProperty(resource, params.getAttributes().get(i),
                                PropertyUtils.getSimpleProperty(from, params.getAttributes().get(i)));
                    } catch (NoSuchMethodException e) {
                        // ignore wrong attribute name
                    }
                }
                items.add(resource);
            }
            return new QueryResult<E>(count, items);
        } else {
            return new QueryResult<E>(count, queryResult);
        }
    } catch (IllegalArgumentException ex) {
        ex.printStackTrace();
        throw new InvalidRequestException(ex.getMessage());
    } catch (InstantiationException ex) {
        ex.printStackTrace();
        throw new InvalidRequestException(ex.getMessage());
    } catch (IllegalAccessException ex) {
        ex.printStackTrace();
        throw new InvalidRequestException(ex.getMessage());
    } catch (InvocationTargetException ex) {
        throw new InvalidRequestException(ex.getMessage());
    }
}

From source file:org.powertac.common.state.StateLogging.java

private Object[] collectProperties(Object thing) {
    ArrayList<Object> properties = new ArrayList<Object>();
    try {// www  . ja  v a  2  s .  com
        //TODO: 
        // - use XStream annotation to figure out fields to log instead
        // - cache fields list to reduce lookup
        Domain domain = thing.getClass().getAnnotation(Domain.class);
        if (domain instanceof Domain) {
            String[] fields = domain.fields();
            for (String field : fields) {
                Object obj = PropertyUtils.getSimpleProperty(thing, field);
                properties.add(obj);
            }
        }
    } catch (IllegalAccessException e) {
        log.error("Failed to introspect " + thing.getClass().getSimpleName(), e);
    } catch (InvocationTargetException e) {
        log.error("Failed to introspect " + thing.getClass().getSimpleName(), e);
    } catch (NoSuchMethodException e) {
        log.error("Failed to introspect " + thing.getClass().getSimpleName(), e);
    }
    return properties.toArray();
}

From source file:org.powertac.server.MessageRouter.java

public boolean route(Object message) {
    boolean routed = false;

    boolean byPassed = (message instanceof BrokerAuthentication);

    String username = "unknown";
    Broker broker = null;/*from   w w w  .j  a va2 s. c  om*/
    if (!byPassed) {
        try {
            broker = (Broker) PropertyUtils.getSimpleProperty(message, "broker");
            username = broker.getUsername();
        } catch (IllegalAccessException e) {
            log.error("Failed to extract broker", e);
        } catch (InvocationTargetException e) {
            log.error("Failed to extract broker", e);
        } catch (NoSuchMethodException e) {
            log.error("Failed to extract broker", e);
        }
    }
    if (byPassed || (broker != null && broker.isEnabled())) {
        log.debug("route(Object) - routing " + message.getClass().getSimpleName() + " from " + username);
        Set<Object> targets = registrations.get(message.getClass());
        if (targets == null) {
            log.warn("no targets for message of type " + message.getClass().getSimpleName());
        } else {
            for (Object target : targets) {
                dispatch(target, "handleMessage", message);
            }
            routed = true;
        }
    }
    log.debug("route(Object) - routed:" + routed);
    return routed;
}

From source file:org.sipfoundry.sipxconfig.api.ApiBeanUtil.java

public static void copyProperties(Object to, Object from, Set<String> properties, Set ignoreList) {
    for (String name : properties) {
        if (ignoreList != null && ignoreList.contains(name)) {
            continue;
        }/*from w  ww.  j  a v a2s.c  o m*/
        try {
            Object value = PropertyUtils.getSimpleProperty(from, name);
            BeanUtils.copyProperty(to, name, value);
        } catch (IllegalAccessException e) {
            throw new PropertyException(name, e);
        } catch (InvocationTargetException e) {
            throw new PropertyException(name, e);
        } catch (NoSuchMethodException e) {
            throw new PropertyException(name, e);
        }
    }
}