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() 

Source Link

Document

Creates a new empty MutablePropertyValues object.

Usage

From source file:de.siegmar.securetransfer.controller.SendController.java

private List<SecretFile> handleStream(final HttpServletRequest req, final KeyIv encryptionKey,
        final DataBinder binder) throws FileUploadException, IOException {

    final BindingResult errors = binder.getBindingResult();

    final MutablePropertyValues propertyValues = new MutablePropertyValues();
    final List<SecretFile> tmpFiles = new ArrayList<>();

    @SuppressWarnings("checkstyle:anoninnerlength")
    final AbstractMultipartVisitor visitor = new AbstractMultipartVisitor() {

        private OptionalInt expiration = OptionalInt.empty();

        @Override//from w w w  .jav  a  2 s.  c  o m
        void emitField(final String name, final String value) {
            propertyValues.addPropertyValue(name, value);

            if ("expirationDays".equals(name)) {
                expiration = OptionalInt.of(Integer.parseInt(value));
            }
        }

        @Override
        void emitFile(final String fileName, final InputStream inStream) {
            final Integer expirationDays = expiration
                    .orElseThrow(() -> new IllegalStateException("No expirationDays configured"));

            tmpFiles.add(messageService.encryptFile(fileName, inStream, encryptionKey,
                    Instant.now().plus(expirationDays, ChronoUnit.DAYS)));
        }

    };

    try {
        visitor.processRequest(req);
        binder.bind(propertyValues);
        binder.validate();
    } catch (final IllegalStateException ise) {
        errors.reject(null, ise.getMessage());
    }

    return tmpFiles;
}

From source file:ome.client.utests.Preferences3Test.java

@Test(groups = "ticket:1058")
public void testOmeroUserIsProperlySetWithSpring2_5_5Manual() {

    Server s = new Server("localhost", 1099);
    Login l = new Login("me", "password");
    Properties p = s.asProperties();
    p.putAll(l.asProperties());/*from w  w  w  . ja  v a 2  s . c  o m*/

    // This is copied from OmeroContext. This is the parent context which
    // should contain the properties;
    Properties copy = new Properties(p);
    ConstructorArgumentValues ctorArg1 = new ConstructorArgumentValues();
    ctorArg1.addGenericArgumentValue(copy);
    BeanDefinition definition1 = new RootBeanDefinition(Properties.class, ctorArg1, null);
    StaticApplicationContext staticContext = new StaticApplicationContext();
    staticContext.registerBeanDefinition("properties", definition1);
    staticContext.refresh();

    // This is the child context and contains a definition of a
    // PlaceHolderConfigurer
    // as well as a user of
    StaticApplicationContext childContext = new StaticApplicationContext();

    MutablePropertyValues mpv2 = new MutablePropertyValues();
    mpv2.addPropertyValue("properties", new RuntimeBeanReference("properties"));
    mpv2.addPropertyValue("systemPropertiesModeName", "SYSTEM_PROPERTIES_MODE_FALLBACK");
    mpv2.addPropertyValue("localOverride", "true");
    BeanDefinition definitionConfigurer = new RootBeanDefinition(PreferencesPlaceholderConfigurer.class, null,
            mpv2);
    childContext.registerBeanDefinition("propertiesPlaceholderConfigurer", definitionConfigurer);

    ConstructorArgumentValues cav2 = new ConstructorArgumentValues();
    cav2.addGenericArgumentValue("${omero.user}");
    BeanDefinition definitionTest = new RootBeanDefinition(String.class, cav2, null);
    childContext.registerBeanDefinition("test", definitionTest);

    childContext.setParent(staticContext);
    childContext.refresh();

    String test = (String) childContext.getBean("test");
    assertEquals(test, "me");

}

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

public void testFiltersPropertyValues() {
    MutablePropertyValues vals = new MutablePropertyValues();
    vals.addPropertyValue("joe.name", "joe");
    vals.addPropertyValue("tom.name", "tom");
    TestBean testBean = new TestBean();
    GrailsDataBinder binder = new GrailsDataBinder(testBean, "testBean");
    binder.bind(vals, "tom");
    assertEquals("tom", testBean.getName());
}

From source file:org.easyrec.plugin.configuration.ConfigurationHelper.java

/**
 * Same as getValues(), but property keys are prefixed with the specified prefix.
 *
 * @param propertyKeyPrefix//from   ww  w. ja  v a  2  s. c  o m
 *
 */
public PropertyValues getValues(String propertyKeyPrefix) {
    if (propertyKeyPrefix == null) {
        propertyKeyPrefix = "";
    } else {
        propertyKeyPrefix += ".";
    }
    MutablePropertyValues props = new MutablePropertyValues();
    for (String propertyName : getParameterNames()) {
        String strVal = getParameterStringValue(propertyName);
        if (strVal != null) {
            props.addPropertyValue(propertyKeyPrefix + propertyName, strVal);
        }
    }
    return props;
}

From source file:org.easyrec.plugin.configuration.ConfigurationHelper.java

/**
 * Sets the parameter values from the specified Properties object. A parameter value is set to <code>null</code> if
 * the respective property is not present.
 *
 * @param properties/*from   www  .j  ava 2 s . c  o  m*/
 * @param propertyKeyPrefix
 *
 */
public BindingResult setValues(Properties properties, String propertyKeyPrefix) {
    if (propertyKeyPrefix == null) {
        propertyKeyPrefix = "";
    } else {
        propertyKeyPrefix += ".";
    }
    MutablePropertyValues propertyValues = new MutablePropertyValues();
    for (String propertyName : getParameterNames()) {
        propertyValues.addPropertyValue(propertyName, properties.getProperty(propertyKeyPrefix + propertyName));
    }
    return setValues(propertyValues);
}

From source file:org.openadaptor.spring.SpringAdaptor.java

/**
 * Configure the default PropertyPlaceholderConfigurer.
 * <br>/*from  w  w w.  ja va 2s  .c  o  m*/
 * Unless suppressAutomaticPropsConfig is set true by supplying -noprops as a command line arg then
 * this method creates and installs a PropertyPlaceholderConfigurer which allows access to System Properties
 * and optionally add all -props defined urls as property resource locations.<br>
 * With one exception all urls are passed through to the context in order to locate the resource. The exception
 * is a url that is not prefixed with a protocol is assumed to be a file and arbitratily prefixed with "file:"<br>
 * NB the arbitrary name given to the generated PropertyPlaceholderConfigurer is "openadaptorAutoGeneratedSystemPropertyConfigurer".
 *
 * @param context Spring Context being used.
 * @param propsUrlList Supplied List or Property URLS.
 */
protected void configureProperties(GenericApplicationContext context, ArrayList propsUrlList) {
    // System properties

    if (!suppressAutomaticPropsConfig) {

        if (isPropertyPlaceholderPresent(context)) {
            log.warn(
                    "Spring configuration file already has PropertyPlaceholderConfigurers defined. Please ensure any conflicts are resolved satisfactorily.");
        }

        MutablePropertyValues systemPropertiesModeProperties = new MutablePropertyValues();
        systemPropertiesModeProperties.addPropertyValue("staticField",
                "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_FALLBACK");

        RootBeanDefinition systemPropertiesMode = new RootBeanDefinition(FieldRetrievingFactoryBean.class);
        systemPropertiesMode.setPropertyValues(systemPropertiesModeProperties);

        MutablePropertyValues properties = new MutablePropertyValues();
        properties.addPropertyValue("ignoreResourceNotFound", "false"); // Will cause an eror if a resource url is bogus
        Iterator propsUrlIter = propsUrlList.iterator();
        ArrayList resourceList = new ArrayList();
        while (propsUrlIter.hasNext()) {
            resourceList.add(context.getResource(ensureProtocol((String) propsUrlIter.next())));
        }
        properties.addPropertyValue("locations", resourceList);
        properties.addPropertyValue("systemPropertiesMode", systemPropertiesMode);
        RootBeanDefinition propertyHolder = new RootBeanDefinition(PropertyPlaceholderConfigurer.class);
        propertyHolder.setPropertyValues(properties);

        context.registerBeanDefinition("openadaptorAutoGeneratedSystemPropertyConfigurer", propertyHolder);
    }
}

From source file:org.openadaptor.spring.SpringAdaptor.java

/**
 * Registers an instance of an {@link Adaptor} with the Spring context.
 * <p>/*  ww  w  .  j a v a 2s . co m*/
 * Common OpenAdaptor configurations include an {@link Adaptor} with a 
 * {@link Router} which defines an ordered list of {@link IComponent}s. If 
 * no adaptor bean is explicitly registered, this method will auto-configure 
 * and register one.
 * <p>
 * If a {@link Router} is explicitly registered in the Spring context, that
 * will be autowired as the {@link Adaptor}'s <code>messageProcessor</code>.
 * Otherwise, a {@link Router} will be automatically configured and registered.
 * 
 * @param factory the ListableBeanFactory Spring context factory
 * @param id - String containing the id to use for the adaptor
 * @return String containing the id for the Adaptor (
 */

protected String registerAdaptor(ListableBeanFactory factory, String id) {
    String[] ids = factory.getBeanNamesForType(Router.class);
    Router router = null;

    if (ids.length == 1) {
        router = (Router) factory.getBean(ids[0]);
    } else if (ids.length == 0) {
        router = registerRouter(factory);
    }

    MutablePropertyValues properties = new MutablePropertyValues();
    properties.addPropertyValue("messageProcessor", router);
    RootBeanDefinition beanDefinition = new RootBeanDefinition(Adaptor.class);
    beanDefinition.setPropertyValues(properties);
    ((GenericApplicationContext) factory).registerBeanDefinition(id, beanDefinition);
    setBeanId(id);
    return id;
}

From source file:org.openadaptor.spring.SpringAdaptor.java

/**
 * Registers an instance of a {@link Router} with the Spring context. 
 * <p>/*from w w  w  . j  av a  2 s . c  o m*/
 * This method adds all beans registered in the Spring context that are
 * instances of {@link IComponent} to the router's <code>processors</code> 
 * list in <strong>the order in which they are declared in the Spring 
 * configuration</strong>.
 * <p>
 * The only exception is any bean with an id of <code>ExceptionProcessor</code>,
 * which is set as the <code>exceptionProcessor</code> property of the
 * {@link Router}.
 * <p>
 * Note that this means only basic, single pipeline router configurations
 * may be auto-configured in this way. Complex configurations that require
 * a <code>processMap</code> must be defined explicitly. 
 * 
 * @param factory the ListableBeanFactory Spring context factory
 * @return a configured Router
 */
protected Router registerRouter(ListableBeanFactory factory) {
    String[] ids = factory.getBeanNamesForType(IComponent.class);

    if (ids.length == 0) {
        throw new RuntimeException("No Component beans found in config");
    }

    List processors = new ArrayList(ids.length);
    Object exceptionProcessor = null;

    for (int i = 0; i < ids.length; i++) {
        if (!ids[i].equals(DEFAULT_EXCEPTION_PROCESSOR_ID)) {
            processors.add(factory.getBean(ids[i]));
        } else {
            exceptionProcessor = factory.getBean(ids[i]);
        }
    }

    MutablePropertyValues properties = new MutablePropertyValues();
    properties.addPropertyValue("processors", processors);

    if (exceptionProcessor != null) {
        properties.addPropertyValue("exceptionProcessor", exceptionProcessor);
    }

    RootBeanDefinition beanDefinition = new RootBeanDefinition(Router.class);
    beanDefinition.setPropertyValues(properties);
    ((GenericApplicationContext) factory).registerBeanDefinition(DEFAULT_ROUTER_ID, beanDefinition);

    return (Router) factory.getBean(DEFAULT_ROUTER_ID);
}

From source file:org.springframework.beans.AbstractPropertyAccessorTests.java

@Test
public void setEmptyPropertyValues() {
    TestBean target = new TestBean();
    int age = 50;
    String name = "Tony";
    target.setAge(age);/*  www . j av  a 2  s .  c  o m*/
    target.setName(name);
    try {
        AbstractPropertyAccessor accessor = createAccessor(target);
        assertTrue("age is OK", target.getAge() == age);
        assertTrue("name is OK", name.equals(target.getName()));
        accessor.setPropertyValues(new MutablePropertyValues());
        // Check its unchanged
        assertTrue("age is OK", target.getAge() == age);
        assertTrue("name is OK", name.equals(target.getName()));
    } catch (BeansException ex) {
        fail("Shouldn't throw exception when everything is valid");
    }
}

From source file:org.springframework.beans.AbstractPropertyAccessorTests.java

@Test
public void setValidPropertyValues() {
    TestBean target = new TestBean();
    String newName = "tony";
    int newAge = 65;
    String newTouchy = "valid";
    try {//from   w w  w . j  a  v a2  s  . c  o  m
        AbstractPropertyAccessor accessor = createAccessor(target);
        MutablePropertyValues pvs = new MutablePropertyValues();
        pvs.addPropertyValue(new PropertyValue("age", newAge));
        pvs.addPropertyValue(new PropertyValue("name", newName));
        pvs.addPropertyValue(new PropertyValue("touchy", newTouchy));
        accessor.setPropertyValues(pvs);
        assertTrue("Name property should have changed", target.getName().equals(newName));
        assertTrue("Touchy property should have changed", target.getTouchy().equals(newTouchy));
        assertTrue("Age property should have changed", target.getAge() == newAge);
    } catch (BeansException ex) {
        fail("Shouldn't throw exception when everything is valid");
    }
}