Example usage for org.apache.commons.beanutils ConvertUtils convert

List of usage examples for org.apache.commons.beanutils ConvertUtils convert

Introduction

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

Prototype

public static Object convert(String values[], Class clazz) 

Source Link

Document

Convert an array of specified values to an array of objects of the specified class (if possible).

For more details see ConvertUtilsBean.

Usage

From source file:com.continuuity.http.HttpResourceModel.java

/**
 * Handle http Request./*from w w w.j a  va  2  s . c  om*/
 *
 * @param request  HttpRequest to be handled.
 * @param responder HttpResponder to write the response.
 * @param groupValues Values needed for the invocation.
 */
public void handle(HttpRequest request, HttpResponder responder, Map<String, String> groupValues) {
    //TODO: Refactor group values.
    try {
        if (httpMethods.contains(request.getMethod())) {
            //Setup args for reflection call
            Object[] args = new Object[method.getParameterTypes().length];
            int parameterIndex = 0;
            args[parameterIndex] = request;
            parameterIndex++;
            args[parameterIndex] = responder;

            if (method.getParameterTypes().length > 2) {
                Class<?>[] parameterTypes = method.getParameterTypes();
                for (Annotation[] annotations : method.getParameterAnnotations()) {
                    for (Annotation annotation : annotations) {
                        if (annotation.annotationType().isAssignableFrom(PathParam.class)) {
                            PathParam param = (PathParam) annotation;
                            String value = groupValues.get(param.value());
                            Preconditions.checkArgument(value != null,
                                    "Could not resolve value for parameter %s", param.value());
                            parameterIndex++;
                            args[parameterIndex] = ConvertUtils.convert(value, parameterTypes[parameterIndex]);
                        }
                    }
                }
                Preconditions.checkArgument(method.getParameterTypes().length == parameterIndex + 1,
                        "Could not resolve all parameters for method %s", method.getName());
            }
            method.invoke(handler, args);
        } else {
            //Found a matching resource but could not find the right HttpMethod so return 405
            responder.sendError(HttpResponseStatus.METHOD_NOT_ALLOWED,
                    String.format("Problem accessing: %s. Reason: Method Not Allowed", request.getUri()));
        }
    } catch (Throwable e) {
        LOG.error("Error processing path {} {}", request.getUri(), e, e);
        responder.sendError(HttpResponseStatus.INTERNAL_SERVER_ERROR,
                String.format("Error in executing path: %s", request.getUri()));
    }
}

From source file:com.siberhus.tdfl.transform.DefaultFieldSet.java

@SuppressWarnings("unchecked")
@Override/*from  www .j  av a2 s  .  c o  m*/
public <D> D read(Class<D> clazz, int index, D defaultValue) {
    String value = readString(index);
    if (value == null) {
        return defaultValue;
    }
    return (D) ConvertUtils.convert(value, clazz);
}

From source file:com.creactiviti.piper.core.MapObject.java

@Override
public <T> T get(Object aKey, Class<T> aReturnType) {
    Object value = get(aKey);//from   ww  w .  j av a2 s .  c o m
    if (value == null) {
        return null;
    }
    return (T) ConvertUtils.convert(value, aReturnType);
}

From source file:co.cask.http.HttpResourceModel.java

/**
 * Handle http Request.//  ww  w .  ja  v  a 2  s .  c  om
 *
 * @param request  HttpRequest to be handled.
 * @param responder HttpResponder to write the response.
 * @param groupValues Values needed for the invocation.
 */

public HttpMethodInfo handle(HttpRequest request, HttpResponder responder, Map<String, String> groupValues)
        throws Exception {

    //TODO: Refactor group values.
    try {
        if (httpMethods.contains(request.getMethod())) {
            //Setup args for reflection call
            Object[] args = new Object[method.getParameterTypes().length - 2];

            if (method.getParameterTypes().length > 2) {
                int parameterIndex = 0;
                Class<?>[] parameterTypes = method.getParameterTypes();
                Annotation[][] parameterAnnotations = method.getParameterAnnotations();
                for (int i = 2; i < parameterAnnotations.length; i++) {
                    Annotation[] annotations = parameterAnnotations[i];

                    boolean foundPathParam = false;
                    for (Annotation annotation : annotations) {
                        if (annotation.annotationType().isAssignableFrom(PathParam.class)) {
                            PathParam param = (PathParam) annotation;
                            String value = groupValues.get(param.value());
                            Preconditions.checkArgument(value != null,
                                    "Could not resolve value for parameter %s", param.value());
                            args[parameterIndex] = ConvertUtils.convert(value,
                                    parameterTypes[parameterIndex + 2]);
                            parameterIndex++;
                            foundPathParam = true;
                            break;
                        }
                    }
                    Preconditions.checkArgument(foundPathParam,
                            "Missing @PathParam annotation for parameter in method %s.", method.getName());
                }
            }
            return new HttpMethodInfo(method, handler, request, responder, args);
        } else {
            //Found a matching resource but could not find the right HttpMethod so return 405
            throw new HandlerException(HttpResponseStatus.METHOD_NOT_ALLOWED,
                    String.format("Problem accessing: %s. Reason: Method Not Allowed", request.getUri()));
        }
    } catch (Throwable e) {
        throw new HandlerException(HttpResponseStatus.INTERNAL_SERVER_ERROR,
                String.format("Error in executing path:"));
    }
}

From source file:edu.scripps.fl.curves.CurveFit.java

public static void curveClassification(Curve curve) {
    double y[] = (double[]) ConvertUtils.convert(curve.getResponses(), double[].class);
    double x[] = (double[]) ConvertUtils.convert(curve.getConcentrations(), double[].class);
    for (int ii = 0; ii < x.length; ii++)
        x[ii] = Math.log10(x[ii]);
    boolean flags[] = (boolean[]) ConvertUtils.convert(curve.getMask(), boolean[].class);
    curveClassification(curve, y, x, flags);
}

From source file:com.creactiviti.piper.core.MapObject.java

@Override
public <T> T get(Object aKey, Class<T> aReturnType, T aDefaultValue) {
    Object value = get(aKey);//w  w  w.  j  a va 2  s. c o m
    if (value == null) {
        return aDefaultValue;
    }
    return (T) ConvertUtils.convert(value, aReturnType);
}

From source file:jp.co.acroquest.endosnipe.perfdoctor.rule.RuleInstanceUtil.java

/**
 * ??true??????<br>/*from  w w w  .  j  a va2s . c om*/
 * org.apache.commons.beanUtils.ConvertUtils??????<br>
 * Boolean?????????<br>
 * ????true??????????null??true?<br>
 * @param value 
 * @return ?true?????
 */
protected static boolean checkEnabled(final String value) {
    if (StringUtils.isEmpty(value)) {
        return true;
    }

    Boolean b = (Boolean) ConvertUtils.convert(value, Boolean.TYPE);
    return b.booleanValue();
}

From source file:com.ebiz.modules.persistence.repository.support.MyRepositoryImpl.java

@Override
@Transactional/*from   w  w  w.j a va 2  s.  c  o m*/
public void delete(ID[] ids) {
    logger.trace("----->MyRepositoryImpl.delete(ID[] ids)");
    String qlString;
    Class<T> clazz = ei.getJavaType();
    if (clazz.isAnnotationPresent(LogicallyDelete.class)) {
        LogicallyDelete logicallyDelete = clazz.getAnnotation(LogicallyDelete.class);
        Object value = ConvertUtils.convert(logicallyDelete.value(), logicallyDelete.type().getClazz());
        qlString = String.format(STATE_DELETE_QUERY_STRING, ei.getEntityName(), logicallyDelete.name(), value);
    } else {
        qlString = String.format(DELETE_QUERY_STRING, ei.getEntityName());
    }
    logger.debug("......qlString:={}", qlString);
    Query query = em.createQuery(qlString);
    query.setParameter(1, Arrays.asList(ids));
    query.executeUpdate();
}

From source file:com.feilong.commons.core.io.CSVUtil.java

/**
 * .<br>//from   w  w w .  ja v  a2 s  .c  o m
 *  {@link com.feilong.commons.core.bean.BeanUtil#describe(Object)} ?
 *
 * @param <T>
 *            the generic type
 * @param fileName
 *            the file name
 * @param collection
 *            the iterable
 * @param excludePropertyNames
 *            ??
 * @throws UncheckedIOException
 *             the unchecked io exception
 * @throws IllegalArgumentException
 *             the illegal argument exception
 * @see #write(String, String[], List, CSVParams)
 * @see com.feilong.commons.core.bean.BeanUtil#describe(Object)
 * @see org.apache.commons.beanutils.ConvertUtils#convert(Object)
 * @since 1.0.9
 */
public static final <T> void write(String fileName, Collection<T> collection, String[] excludePropertyNames)
        throws UncheckedIOException, IllegalArgumentException {

    if (Validator.isNullOrEmpty(fileName)) {
        throw new NullPointerException("fileName can't be null/empty!");
    }

    if (Validator.isNullOrEmpty(collection)) {
        throw new NullPointerException("iterable can't be null/empty!");
    }

    String[] columnTitles = null;
    List<Object[]> dataList = new ArrayList<Object[]>(collection.size());

    for (T t : collection) {

        // Map<String, Object> fieldValueMap = FieldUtil.getFieldValueMap(t, excludeFields);
        Map<String, Object> propertyValueMap = PropertyUtil.describe(t);
        propertyValueMap = MapUtil.getSubMapExcludeKeys(propertyValueMap, excludePropertyNames);

        int size = propertyValueMap.size();

        Object[] rowData = new Object[size];

        //??
        if (null == columnTitles) {
            columnTitles = new String[size];
        }

        int i = 0;
        for (Map.Entry<String, Object> entry : propertyValueMap.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();

            if (Validator.isNullOrEmpty(value)) {
                rowData[i] = StringUtils.EMPTY;
            } else {
                //rowData[i] = ConvertUtils.convert(value);
                rowData[i] = ConvertUtils.convert(value, String.class);
            }

            columnTitles[i] = key;
            i++;
        }
        dataList.add(rowData);
    }
    write(fileName, columnTitles, dataList, new CSVParams());
}

From source file:com.siberhus.tdfl.transform.DefaultFieldSet.java

@SuppressWarnings("unchecked")
@Override/* w w w.ja v a 2s.  c o m*/
public <D> D[] readArray(Class<D> clazz, int index, String separator) {
    String subValues[] = StringUtils.split(readString(index), separator);
    if (subValues == null) {
        return null;
    }
    return (D[]) ConvertUtils.convert(subValues, clazz);
}