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:com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl.java

protected final <T> T getOrCreateChildBean(Class<T> beanType, String beanClass, Property[] properties) {
    final StringBuilder beanNameBuilder = new StringBuilder();

    beanNameBuilder.append(beanClass);/*from w w w.  j  ava 2 s  .  c o  m*/

    final MutablePropertyValues mutablePropertyValues = new MutablePropertyValues();

    //Sort the properties array first so bean name generation is always consistent
    Arrays.sort(properties, PropertyComparator.INSTANCE);

    for (Property property : properties) {
        final String name = property.name();
        final String value = property.value();
        final String ref = property.ref();

        beanNameBuilder.append("[").append(name).append(",").append(value).append(",").append(ref).append("]");

        if (value.length() > 0) {
            if (ref.length() > 0) {
                throw new IllegalArgumentException(
                        "Only one of value or ref must be specified no both on Property with name: " + name);
            }

            mutablePropertyValues.addPropertyValue(name, value);
        } else if (ref.length() > 0) {
            mutablePropertyValues.addPropertyValue(name, new RuntimeBeanReference(ref));
        } else {
            throw new IllegalArgumentException(
                    "Either value or ref must be specified on Property with name: " + name);
        }
    }

    final String beanName = beanNameBuilder.toString();

    //See if the bean is already registered using the compiled bean name, if so just use that instance
    if (this.childBeanFactory.containsBean(beanName)) {
        return this.childBeanFactory.getBean(beanName, beanType);
    }

    //Create and register the bean if it didn't already exist
    final AbstractBeanDefinition beanDefinition;
    try {
        beanDefinition = BeanDefinitionReaderUtils.createBeanDefinition(null, beanClass,
                ClassUtils.getDefaultClassLoader());
    } catch (ClassNotFoundException e) {
        throw new BeanCreationException(
                "Could not find class '" + beanClass + "' to create " + beanType + " from", e);
    }

    if (ReflectionHelperAware.class.isAssignableFrom(beanDefinition.getBeanClass())) {
        mutablePropertyValues.addPropertyValue("reflectionHelper", this.reflectionHelper);
    }

    beanDefinition.setPropertyValues(mutablePropertyValues);
    this.childBeanFactory.registerBeanDefinition(beanName, beanDefinition);

    return this.childBeanFactory.getBean(beanName, beanType);
}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java

@Test
public void test2Invalid() {
    TestBean t = new TestBean();
    String newName = "tony";
    String invalidTouchy = ".valid";
    try {//  www.  jav a2 s .c om
        BeanWrapper bw = new JuffrouSpringBeanWrapper(t);
        MutablePropertyValues pvs = new MutablePropertyValues();
        pvs.addPropertyValue(new PropertyValue("age", "foobar"));
        pvs.addPropertyValue(new PropertyValue("name", newName));
        pvs.addPropertyValue(new PropertyValue("touchy", invalidTouchy));
        bw.setPropertyValues(pvs);
        fail("Should throw exception when everything is valid");
    } catch (PropertyBatchUpdateException ex) {
        assertTrue("Must contain 2 exceptions", ex.getExceptionCount() == 2);
        // Test validly set property matches
        assertTrue("Validly set property must stick", t.getName().equals(newName));
        assertTrue("Invalidly set property must retain old value", t.getAge() == 0);
        assertTrue("New value of dodgy setter must be available through exception",
                ex.getPropertyAccessException("touchy").getPropertyChangeEvent().getNewValue()
                        .equals(invalidTouchy));
    }
}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java

@Test
public void testSetPropertyValuesIgnoresInvalidNestedOnRequest() {
    ITestBean rod = new TestBean();
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.addPropertyValue(new PropertyValue("name", "rod"));
    pvs.addPropertyValue(new PropertyValue("graceful.rubbish", "tony"));
    pvs.addPropertyValue(new PropertyValue("more.garbage", new Object()));
    BeanWrapper bw = new JuffrouSpringBeanWrapper(rod);
    bw.setPropertyValues(pvs, true);//from   ww  w .  j a  v  a 2 s  . c om
    assertTrue("Set valid and ignored invalid", rod.getName().equals("rod"));
    try {
        // Don't ignore: should fail
        bw.setPropertyValues(pvs, false);
        fail("Shouldn't have ignored invalid updates");
    } catch (NotWritablePropertyException ex) {
        // OK: but which exception??
    }
}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java

@Test
public void testIndexedProperties() {
    IndexedTestBean bean = new IndexedTestBean();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(bean);
    TestBean tb0 = bean.getArray()[0];/*from   w  ww  .  j av  a  2 s  . c  om*/
    TestBean tb1 = bean.getArray()[1];
    TestBean tb2 = ((TestBean) bean.getList().get(0));
    TestBean tb3 = ((TestBean) bean.getList().get(1));
    TestBean tb6 = ((TestBean) bean.getSet().toArray()[0]);
    TestBean tb7 = ((TestBean) bean.getSet().toArray()[1]);
    TestBean tb4 = ((TestBean) bean.getMap().get("key1"));
    TestBean tb5 = ((TestBean) bean.getMap().get("key.3"));
    assertEquals("name0", tb0.getName());
    assertEquals("name1", tb1.getName());
    assertEquals("name2", tb2.getName());
    assertEquals("name3", tb3.getName());
    assertEquals("name6", tb6.getName());
    assertEquals("name7", tb7.getName());
    assertEquals("name4", tb4.getName());
    assertEquals("name5", tb5.getName());
    assertEquals("name0", bw.getPropertyValue("array[0].name"));
    assertEquals("name1", bw.getPropertyValue("array[1].name"));
    assertEquals("name2", bw.getPropertyValue("list[0].name"));
    assertEquals("name3", bw.getPropertyValue("list[1].name"));
    assertEquals("name6", bw.getPropertyValue("set[0].name"));
    assertEquals("name7", bw.getPropertyValue("set[1].name"));
    assertEquals("name4", bw.getPropertyValue("map[key1].name"));
    assertEquals("name5", bw.getPropertyValue("map[key.3].name"));
    assertEquals("name4", bw.getPropertyValue("map['key1'].name"));
    assertEquals("name5", bw.getPropertyValue("map[\"key.3\"].name"));
    assertEquals("nameX", bw.getPropertyValue("map[key4][0].name"));
    assertEquals("nameY", bw.getPropertyValue("map[key4][1].name"));

    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("array[0].name", "name5");
    pvs.add("array[1].name", "name4");
    pvs.add("list[0].name", "name3");
    pvs.add("list[1].name", "name2");
    pvs.add("set[0].name", "name8");
    pvs.add("set[1].name", "name9");
    pvs.add("map[key1].name", "name1");
    pvs.add("map['key.3'].name", "name0");
    pvs.add("map[key4][0].name", "nameA");
    pvs.add("map[key4][1].name", "nameB");
    bw.setPropertyValues(pvs);
    assertEquals("name5", tb0.getName());
    assertEquals("name4", tb1.getName());
    assertEquals("name3", tb2.getName());
    assertEquals("name2", tb3.getName());
    assertEquals("name1", tb4.getName());
    assertEquals("name0", tb5.getName());
    assertEquals("name5", bw.getPropertyValue("array[0].name"));
    assertEquals("name4", bw.getPropertyValue("array[1].name"));
    assertEquals("name3", bw.getPropertyValue("list[0].name"));
    assertEquals("name2", bw.getPropertyValue("list[1].name"));
    assertEquals("name8", bw.getPropertyValue("set[0].name"));
    assertEquals("name9", bw.getPropertyValue("set[1].name"));
    assertEquals("name1", bw.getPropertyValue("map[\"key1\"].name"));
    assertEquals("name0", bw.getPropertyValue("map['key.3'].name"));
    assertEquals("nameA", bw.getPropertyValue("map[key4][0].name"));
    assertEquals("nameB", bw.getPropertyValue("map[key4][1].name"));
}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java

@Test
public void testIndexedPropertiesWithDirectAccess() {
    IndexedTestBean bean = new IndexedTestBean();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(bean);
    TestBean tb0 = bean.getArray()[0];/* ww w  . j  a v  a2 s .co  m*/
    TestBean tb1 = bean.getArray()[1];
    TestBean tb2 = ((TestBean) bean.getList().get(0));
    TestBean tb3 = ((TestBean) bean.getList().get(1));
    TestBean tb6 = ((TestBean) bean.getSet().toArray()[0]);
    TestBean tb7 = ((TestBean) bean.getSet().toArray()[1]);
    TestBean tb4 = ((TestBean) bean.getMap().get("key1"));
    TestBean tb5 = ((TestBean) bean.getMap().get("key2"));
    assertEquals(tb0, bw.getPropertyValue("array[0]"));
    assertEquals(tb1, bw.getPropertyValue("array[1]"));
    assertEquals(tb2, bw.getPropertyValue("list[0]"));
    assertEquals(tb3, bw.getPropertyValue("list[1]"));
    assertEquals(tb6, bw.getPropertyValue("set[0]"));
    assertEquals(tb7, bw.getPropertyValue("set[1]"));
    assertEquals(tb4, bw.getPropertyValue("map[key1]"));
    assertEquals(tb5, bw.getPropertyValue("map[key2]"));
    assertEquals(tb4, bw.getPropertyValue("map['key1']"));
    assertEquals(tb5, bw.getPropertyValue("map[\"key2\"]"));

    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("array[0]", tb5);
    pvs.add("array[1]", tb4);
    pvs.add("list[0]", tb3);
    pvs.add("list[1]", tb2);
    pvs.add("list[2]", tb0);
    pvs.add("list[4]", tb1);
    pvs.add("map[key1]", tb1);
    pvs.add("map['key2']", tb0);
    pvs.add("map[key5]", tb4);
    pvs.add("map['key9']", tb5);
    bw.setPropertyValues(pvs);
    assertEquals(tb5, bean.getArray()[0]);
    assertEquals(tb4, bean.getArray()[1]);
    assertEquals(tb3, (bean.getList().get(0)));
    assertEquals(tb2, (bean.getList().get(1)));
    assertEquals(tb0, (bean.getList().get(2)));
    assertEquals(null, (bean.getList().get(3)));
    assertEquals(tb1, (bean.getList().get(4)));
    assertEquals(tb1, (bean.getMap().get("key1")));
    assertEquals(tb0, (bean.getMap().get("key2")));
    assertEquals(tb4, (bean.getMap().get("key5")));
    assertEquals(tb5, (bean.getMap().get("key9")));
    assertEquals(tb5, bw.getPropertyValue("array[0]"));
    assertEquals(tb4, bw.getPropertyValue("array[1]"));
    assertEquals(tb3, bw.getPropertyValue("list[0]"));
    assertEquals(tb2, bw.getPropertyValue("list[1]"));
    assertEquals(tb0, bw.getPropertyValue("list[2]"));
    assertEquals(null, bw.getPropertyValue("list[3]"));
    assertEquals(tb1, bw.getPropertyValue("list[4]"));
    assertEquals(tb1, bw.getPropertyValue("map[\"key1\"]"));
    assertEquals(tb0, bw.getPropertyValue("map['key2']"));
    assertEquals(tb4, bw.getPropertyValue("map[\"key5\"]"));
    assertEquals(tb5, bw.getPropertyValue("map['key9']"));
}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java

@Test
public void testMapAccessWithTypeConversion() {
    IndexedTestBean bean = new IndexedTestBean();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(bean);
    bw.registerCustomEditor(TestBean.class, new PropertyEditorSupport() {
        @Override/*from  ww  w.j  a  v a 2  s  .c  om*/
        public void setAsText(String text) throws IllegalArgumentException {
            if (!StringUtils.hasLength(text)) {
                throw new IllegalArgumentException();
            }
            setValue(new TestBean(text));
        }
    });

    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("map[key1]", "rod");
    pvs.add("map[key2]", "rob");
    bw.setPropertyValues(pvs);
    assertEquals("rod", ((TestBean) bean.getMap().get("key1")).getName());
    assertEquals("rob", ((TestBean) bean.getMap().get("key2")).getName());

    pvs = new MutablePropertyValues();
    pvs.add("map[key1]", "rod");
    pvs.add("map[key2]", "");
    try {
        bw.setPropertyValues(pvs);
        fail("Should have thrown TypeMismatchException");
    } catch (PropertyBatchUpdateException ex) {
        PropertyAccessException pae = ex.getPropertyAccessException("map[key2]");
        assertTrue(pae instanceof TypeMismatchException);
    }
}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java

@Test
public void testMapAccessWithUnmodifiableMap() {
    IndexedTestBean bean = new IndexedTestBean();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(bean);
    bw.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() {
        @Override/*from ww w  .  j  ava2  s.  c om*/
        public void setAsText(String text) throws IllegalArgumentException {
            if (!StringUtils.hasLength(text)) {
                throw new IllegalArgumentException();
            }
            setValue(new TestBean(text));
        }
    });

    Map<Integer, String> inputMap = new HashMap<Integer, String>();
    inputMap.put(new Integer(1), "rod");
    inputMap.put(new Integer(2), "rob");
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("map", Collections.unmodifiableMap(inputMap));
    bw.setPropertyValues(pvs);
    assertEquals("rod", ((TestBean) bean.getMap().get(new Integer(1))).getName());
    assertEquals("rob", ((TestBean) bean.getMap().get(new Integer(2))).getName());
}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java

@Test
public void testMapAccessWithCustomUnmodifiableMap() {
    IndexedTestBean bean = new IndexedTestBean();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(bean);
    bw.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() {
        @Override//from ww w.  jav  a  2 s  .co m
        public void setAsText(String text) throws IllegalArgumentException {
            if (!StringUtils.hasLength(text)) {
                throw new IllegalArgumentException();
            }
            setValue(new TestBean(text));
        }
    });

    Map<Object, Object> inputMap = new HashMap<Object, Object>();
    inputMap.put(new Integer(1), "rod");
    inputMap.put(new Integer(2), "rob");
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("map", new ReadOnlyMap(inputMap));
    bw.setPropertyValues(pvs);
    assertEquals("rod", ((TestBean) bean.getMap().get(new Integer(1))).getName());
    assertEquals("rob", ((TestBean) bean.getMap().get(new Integer(2))).getName());
}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java

@SuppressWarnings("unchecked")
// must work with raw map in this test
@Test/*from   w  ww .  j  a v  a  2 s . c om*/
public void testRawMapAccessWithNoEditorRegistered() {
    IndexedTestBean bean = new IndexedTestBean();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(bean);
    Map inputMap = new HashMap();
    inputMap.put(new Integer(1), "rod");
    inputMap.put(new Integer(2), "rob");
    ReadOnlyMap readOnlyMap = new ReadOnlyMap(inputMap);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("map", readOnlyMap);
    bw.setPropertyValues(pvs);
    assertSame(readOnlyMap, bean.getMap());
    assertFalse(readOnlyMap.isAccessed());
}

From source file:com.laxser.blitz.web.impl.module.ModuleAppContext.java

/** ?messageSourceRose? */
public static void registerMessageSourceIfNecessary(BeanDefinitionRegistry registry,
        String[] messageBaseNames) {
    if (!ArrayUtils.contains(registry.getBeanDefinitionNames(), MESSAGE_SOURCE_BEAN_NAME)) {
        logger.debug("registerMessageSource  " + ArrayUtils.toString(messageBaseNames));
        GenericBeanDefinition messageSource = new GenericBeanDefinition();
        messageSource.setBeanClass(ReloadableResourceBundleMessageSource.class);
        MutablePropertyValues propertyValues = new MutablePropertyValues();
        propertyValues.addPropertyValue("useCodeAsDefaultMessage", true);
        propertyValues.addPropertyValue("defaultEncoding", "UTF-8"); // propertiesUTF-8?ISO-9959-1
        propertyValues.addPropertyValue("cacheSeconds", 60); // hardcode! 60
        propertyValues.addPropertyValue("basenames", messageBaseNames);

        messageSource.setPropertyValues(propertyValues);
        registry.registerBeanDefinition(MESSAGE_SOURCE_BEAN_NAME, messageSource);
    }// w ww  .j  a  v  a2  s  . com
}