Example usage for org.springframework.beans MutablePropertyValues MutablePropertyValues

List of usage examples for org.springframework.beans MutablePropertyValues MutablePropertyValues

Introduction

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

Prototype

public MutablePropertyValues(@Nullable List<PropertyValue> propertyValueList) 

Source Link

Document

Construct a new MutablePropertyValues object using the given List of PropertyValue objects as-is.

Usage

From source file:org.apache.james.container.spring.lifecycle.osgi.AbstractOSGIAnnotationBeanPostProcessor.java

@Override
@SuppressWarnings("rawtypes")
public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean,
        String beanName) throws BeansException {

    MutablePropertyValues newprops = new MutablePropertyValues(pvs);
    for (PropertyDescriptor pd : pds) {
        A s = hasAnnotatedProperty(pd);/*from  www .  ja  va2s.com*/
        if (s != null && !pvs.contains(pd.getName())) {
            try {
                if (logger.isDebugEnabled())
                    logger.debug(
                            "Processing annotation [" + s + "] for [" + beanName + "." + pd.getName() + "]");
                FactoryBean importer = getServiceImporter(s, pd.getWriteMethod(), beanName);
                // BPPs are created in stageOne(), even though they are run in stageTwo(). This check means that
                // the call to getObject() will not fail with ServiceUnavailable. This is safe to do because
                // ServiceReferenceDependencyBeanFactoryPostProcessor will ensure that mandatory services are
                // satisfied before stageTwo() is run.
                if (bean instanceof BeanPostProcessor) {
                    ImporterCallAdapter.setCardinality(importer, Cardinality.C_0__1);
                }
                newprops.addPropertyValue(pd.getName(), importer.getObject());
            } catch (Exception e) {
                throw new FatalBeanException("Could not create service reference", e);
            }
        }
    }
    return newprops;
}

From source file:org.terasoluna.gfw.web.el.ObjectToMapConverterTest.java

@Test
public void testConvert5_at_DateTimeFormat() throws Exception {
    LocalDate date1 = new LocalDate(2015, 4, 1);
    LocalDate localDate1 = new LocalDate(2015, 6, 10);
    LocalDate date2 = new LocalDate(2015, 5, 1);
    LocalDate localDate2 = new LocalDate(2015, 7, 10);

    Map<String, String> map = converter
            .convert(new DateForm5(date1.toDate(), localDate1, new DateFormItem5(date2.toDate(), localDate2)));
    assertThat(map.size(), is(4));/* w  ww .j  a  v  a2s  .com*/
    assertThat(map, hasEntry("date", "2015-04-01"));
    assertThat(map, hasEntry("localDate", "2015-06-10"));
    assertThat(map, hasEntry("item.date", "2015-05-01"));
    assertThat(map, hasEntry("item.localDate", "2015-07-10"));

    DateForm5 form = new DateForm5();
    WebDataBinder binder = new WebDataBinder(form);
    binder.setConversionService(new DefaultFormattingConversionService());
    binder.bind(new MutablePropertyValues(map));
    assertThat(form.getDate(), is(date1.toDate()));
    assertThat(form.getLocalDate(), is(localDate1));
    assertThat(form.getItem().getDate(), is(date2.toDate()));
    assertThat(form.getItem().getLocalDate(), is(localDate2));
}

From source file:org.terasoluna.gfw.web.el.ObjectToMapConverterTest.java

@Test
public void testConvert6_Array() throws Exception {
    Map<String, String> map = converter.convert(new ArrayForm6(new int[] { 1, 2, 3 }, new double[] { 1.1, 1.2 },
            new byte[] { 4, 5, 6 }, new String[] { "a", "b", "c" }, new ArrayFormItem6(new int[] { 11, 12, 13 },
                    new double[] { 11.1, 11.2 }, new byte[] { 14, 15, 16 }, new String[] { "d", "e", "f" })));
    assertThat(map.size(), is(22));/*from w ww.j av a2  s  .c  o m*/
    assertThat(map, hasEntry("array1[0]", "1"));
    assertThat(map, hasEntry("array1[1]", "2"));
    assertThat(map, hasEntry("array1[2]", "3"));
    assertThat(map, hasEntry("array2[0]", "1.1"));
    assertThat(map, hasEntry("array2[1]", "1.2"));
    assertThat(map, hasEntry("array3[0]", "4"));
    assertThat(map, hasEntry("array3[1]", "5"));
    assertThat(map, hasEntry("array3[2]", "6"));
    assertThat(map, hasEntry("array4[0]", "a"));
    assertThat(map, hasEntry("array4[1]", "b"));
    assertThat(map, hasEntry("array4[2]", "c"));
    assertThat(map, hasEntry("item.array1[0]", "11"));
    assertThat(map, hasEntry("item.array1[1]", "12"));
    assertThat(map, hasEntry("item.array1[2]", "13"));
    assertThat(map, hasEntry("item.array2[0]", "11.1"));
    assertThat(map, hasEntry("item.array2[1]", "11.2"));
    assertThat(map, hasEntry("item.array3[0]", "14"));
    assertThat(map, hasEntry("item.array3[1]", "15"));
    assertThat(map, hasEntry("item.array3[2]", "16"));
    assertThat(map, hasEntry("item.array4[0]", "d"));
    assertThat(map, hasEntry("item.array4[1]", "e"));
    assertThat(map, hasEntry("item.array4[2]", "f"));

    ArrayForm6 form = new ArrayForm6();
    WebDataBinder binder = new WebDataBinder(form);
    binder.setConversionService(new DefaultFormattingConversionService());
    binder.bind(new MutablePropertyValues(map));
    assertThat(form.getArray1().length, is(3));
    assertThat(form.getArray1()[0], is(1));
    assertThat(form.getArray1()[1], is(2));
    assertThat(form.getArray1()[2], is(3));
    assertThat(form.getArray2().length, is(2));
    assertThat(form.getArray2()[0], is(1.1));
    assertThat(form.getArray2()[1], is(1.2));
    assertThat(form.getArray3().length, is(3));
    assertThat(form.getArray3()[0], is((byte) 4));
    assertThat(form.getArray3()[1], is((byte) 5));
    assertThat(form.getArray3()[2], is((byte) 6));
    assertThat(form.getArray4().length, is(3));
    assertThat(form.getArray4()[0], is("a"));
    assertThat(form.getArray4()[1], is("b"));
    assertThat(form.getArray4()[2], is("c"));
    assertThat(form.getItem(), is(notNullValue()));
    assertThat(form.getItem().getArray1().length, is(3));
    assertThat(form.getItem().getArray1()[0], is(11));
    assertThat(form.getItem().getArray1()[1], is(12));
    assertThat(form.getItem().getArray1()[2], is(13));
    assertThat(form.getItem().getArray2().length, is(2));
    assertThat(form.getItem().getArray2()[0], is(11.1));
    assertThat(form.getItem().getArray2()[1], is(11.2));
    assertThat(form.getItem().getArray3().length, is(3));
    assertThat(form.getItem().getArray3()[0], is((byte) 14));
    assertThat(form.getItem().getArray3()[1], is((byte) 15));
    assertThat(form.getItem().getArray3()[2], is((byte) 16));
    assertThat(form.getItem().getArray4().length, is(3));
    assertThat(form.getItem().getArray4()[0], is("d"));
    assertThat(form.getItem().getArray4()[1], is("e"));
    assertThat(form.getItem().getArray4()[2], is("f"));
}

From source file:co.paralleluniverse.common.spring.SpringContainerHelper.java

public static MutablePropertyValues properties(Map<String, ? extends Object> properties) {
    MutablePropertyValues mpv = new MutablePropertyValues(properties);
    return mpv;/*  w  w  w.  ja  v  a  2 s.co  m*/
}

From source file:org.codehaus.groovy.grails.web.binding.GrailsDataBinder.java

public void bind(GrailsParameterMap params, String prefix) {
    Map paramsMap = params;/* w  ww .  jav a 2 s.  c  o  m*/
    if (prefix != null) {
        Object o = params.get(prefix);
        if (o instanceof Map)
            paramsMap = (Map) o;
    }
    bindWithRequestAndPropertyValues(params.getRequest(), new MutablePropertyValues(paramsMap));
}

From source file:cherry.foundation.type.format.CustomNumberFormatTest.java

private String parseAndPrint(String name, String value) throws BindException {
    Map<String, String> paramMap = new HashMap<>();
    paramMap.put(name, value);/*from   w  w  w. j ava  2s .co  m*/

    Form form = new Form();
    DataBinder binder = new DataBinder(form, "target");
    binder.setConversionService(conversionService);
    binder.bind(new MutablePropertyValues(paramMap));

    BindingResult binding = BindingResultUtils.getBindingResult(binder.close(), "target");
    return (String) binding.getFieldValue(name);
}

From source file:net.paoding.rose.web.paramresolver.ServletRequestDataBinder.java

/**
 * Bind the parameters of the given request to this binder's target, also
 * binding multipart files in case of a multipart request.
 * <p>/*from  w ww  . j ava 2  s  .  c o  m*/
 * This call can create field errors, representing basic binding errors like
 * a required field (code "required"), or type mismatch between value and
 * bean property (code "typeMismatch").
 * <p>
 * Multipart files are bound via their parameter name, just like normal HTTP
 * parameters: i.e. "uploadedFile" to an "uploadedFile" bean property,
 * invoking a "setUploadedFile" setter method.
 * <p>
 * The type of the target property for a multipart file can be
 * MultipartFile, byte[], or String. The latter two receive the contents of
 * the uploaded file; all metadata like original file name, content type,
 * etc are lost in those cases.
 * 
 * @param request
 *            request with parameters to bind (can be multipart)
 * @see org.springframework.web.multipart.MultipartHttpServletRequest
 * @see org.springframework.web.multipart.MultipartFile
 * @see #bindMultipartFiles
 * @see #bind(org.springframework.beans.PropertyValues)
 */
public void bind(ServletRequest request) {
    MutablePropertyValues mpvs = new MutablePropertyValues(WebUtils.getParametersStartingWith(request, prefix));
    MultipartRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartRequest.class);
    if (multipartRequest != null) {
        bindMultipart(multipartRequest.getMultiFileMap(), mpvs);
    }
    addBindValues(mpvs, request);
    doBind(mpvs);
}

From source file:no.abmu.questionnaire.webflow.MuseumStatisticFormAction.java

/**
 * Bind allowed parameters in the external context request parameter map to the form object using given binder.
 * @param context the action execution context, for accessing and setting data in "flow scope" or "request scope"
 * @param binder the data binder to use//  w w w  .j  a  va2 s  .c o m
 * @throws Exception when an unrecoverable exception occurs
 */
protected void doBind(RequestContext context, DataBinder binder) throws Exception {
    logger.debug("Execute local doBind");
    if (logger.isDebugEnabled()) {
        logger.debug("Binding allowed request parameters in "
                + StylerUtils.style(context.getExternalContext().getRequestParameterMap())
                + " to form object with name '" + binder.getObjectName() + "', pre-bind formObject toString = "
                + binder.getTarget());
        if (binder.getAllowedFields() != null && binder.getAllowedFields().length > 0) {
            logger.debug("(Allowed fields are " + StylerUtils.style(binder.getAllowedFields()) + ")");
        } else {
            logger.debug("(Any field is allowed)");
        }
    }
    logger.debug("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBb");
    logger.debug("BBBBBBBBB Value of binder befor binding BBBBBBBBBBBBBBBBBBb");
    logger.debug("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBb");
    logger.debug(binder);
    binder.bind(new MutablePropertyValues(context.getRequestParameters().asMap()));
    logger.debug("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBb");
    logger.debug("BBBBBBBBB Value of binder after binding BBBBBBBBBBBBBBBBBBb");
    logger.debug("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBb");
    logger.debug(binder);

    if (logger.isDebugEnabled()) {
        logger.debug("Binding completed for form object with name '" + binder.getObjectName()
                + "', post-bind formObject toString = " + binder.getTarget());
        logger.debug("There are [" + binder.getErrors().getErrorCount() + "] errors, details: "
                + binder.getErrors().getAllErrors());
    }
}

From source file:org.codehaus.groovy.grails.web.binding.DataBindingUtils.java

/**
 * Binds the given source object to the given target object performing type conversion if necessary
 *
 * @param domain The GrailsDomainClass instance
 * @param object The object to bind to//from   w  ww  .  j  a  v  a  2  s . co m
 * @param source The source object
 * @param include The list of properties to include
 * @param exclude The list of properties to exclud
 * @param filter The prefix to filter by
 *
 * @see org.codehaus.groovy.grails.commons.GrailsDomainClass
 *
 * @return A BindingResult or null if it wasn't successful
 */
@SuppressWarnings("unchecked")
public static BindingResult bindObjectToDomainInstance(GrailsDomainClass domain, Object object, Object source,
        List include, List exclude, String filter) {
    BindingResult bindingResult = null;
    boolean useSpringBinder = false;
    GrailsApplication grailsApplication = null;
    if (domain != null) {
        grailsApplication = domain.getGrailsApplication();
    }
    if (grailsApplication == null) {
        grailsApplication = GrailsWebRequest.lookupApplication();
    }
    if (grailsApplication != null) {
        if (Boolean.TRUE.equals(grailsApplication.getFlatConfig().get("grails.databinding.useSpringBinder"))) {
            useSpringBinder = true;
        }
    }
    if (!useSpringBinder) {
        try {
            final DataBindingSource bindingSource = createDataBindingSource(grailsApplication,
                    object.getClass(), source);
            final DataBinder grailsWebDataBinder = getGrailsWebDataBinder(grailsApplication);
            grailsWebDataBinder.bind(object, bindingSource, filter, include, exclude);
        } catch (InvalidRequestBodyException e) {
            String messageCode = "invalidRequestBody";
            Class objectType = object.getClass();
            String defaultMessage = "An error occurred parsing the body of the request";
            String[] codes = getMessageCodes(messageCode, objectType);
            bindingResult = new BeanPropertyBindingResult(object, objectType.getName());
            bindingResult.addError(new ObjectError(bindingResult.getObjectName(), codes, null, defaultMessage));
        } catch (Exception e) {
            bindingResult = new BeanPropertyBindingResult(object, object.getClass().getName());
            bindingResult.addError(new ObjectError(bindingResult.getObjectName(), e.getMessage()));
        }
    } else {
        if (source instanceof GrailsParameterMap) {
            GrailsParameterMap parameterMap = (GrailsParameterMap) source;
            HttpServletRequest request = parameterMap.getRequest();
            GrailsDataBinder dataBinder = createDataBinder(object, include, exclude, request);
            dataBinder.bind(parameterMap, filter);
            bindingResult = dataBinder.getBindingResult();
        } else if (source instanceof HttpServletRequest) {
            HttpServletRequest request = (HttpServletRequest) source;
            GrailsDataBinder dataBinder = createDataBinder(object, include, exclude, request);
            performBindFromRequest(dataBinder, request, filter);
            bindingResult = dataBinder.getBindingResult();
        } else if (source instanceof Map) {
            Map propertyMap = convertPotentialGStrings((Map) source);
            GrailsDataBinder binder = createDataBinder(object, include, exclude, null);
            performBindFromPropertyValues(binder, new MutablePropertyValues(propertyMap), filter);
            bindingResult = binder.getBindingResult();
        }

        else {
            GrailsWebRequest webRequest = (GrailsWebRequest) RequestContextHolder.getRequestAttributes();
            if (webRequest != null) {
                GrailsDataBinder binder = createDataBinder(object, include, exclude,
                        webRequest.getCurrentRequest());
                HttpServletRequest request = webRequest.getCurrentRequest();
                performBindFromRequest(binder, request, filter);
            }
        }
    }

    if (domain != null && bindingResult != null) {
        BindingResult newResult = new ValidationErrors(object);
        for (Object error : bindingResult.getAllErrors()) {
            if (error instanceof FieldError) {
                FieldError fieldError = (FieldError) error;
                final boolean isBlank = BLANK.equals(fieldError.getRejectedValue());
                if (!isBlank) {
                    newResult.addError(fieldError);
                } else if (domain.hasPersistentProperty(fieldError.getField())) {
                    final boolean isOptional = domain.getPropertyByName(fieldError.getField()).isOptional();
                    if (!isOptional) {
                        newResult.addError(fieldError);
                    }
                } else {
                    newResult.addError(fieldError);
                }
            } else {
                newResult.addError((ObjectError) error);
            }
        }
        bindingResult = newResult;
    }
    MetaClass mc = GroovySystem.getMetaClassRegistry().getMetaClass(object.getClass());
    if (mc.hasProperty(object, "errors") != null && bindingResult != null) {
        ValidationErrors errors = new ValidationErrors(object);
        errors.addAllErrors(bindingResult);
        mc.setProperty(object, "errors", errors);
    }
    return bindingResult;
}

From source file:org.firewaterframework.test.TestRouteMapper.java

private Response handle(Method method, String url, Map<String, Object> args) {
    RouteMapper service = (RouteMapper) appContext.getBean("routingService");
    MutablePropertyValues mpvs = new MutablePropertyValues(args);
    Request request = new Request(url, method, mpvs);
    return service.handle(request);
}