Search in sources :

Example 51 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project disconf by knightliao.

the class ReloadingPropertyPlaceholderConfigurer method propertiesReloaded.

/**
 * 当配置更新时,被调用
 *
 * @param event
 */
public void propertiesReloaded(PropertiesReloadedEvent event) {
    Properties oldProperties = lastMergedProperties;
    try {
        // 
        Properties newProperties = mergeProperties();
        // 
        // 获取哪些 dynamic property 被影响
        // 
        Set<String> placeholders = placeholderToDynamics.keySet();
        Set<DynamicProperty> allDynamics = new HashSet<DynamicProperty>();
        for (String placeholder : placeholders) {
            String newValue = newProperties.getProperty(placeholder);
            String oldValue = oldProperties.getProperty(placeholder);
            if (newValue != null && !newValue.equals(oldValue) || newValue == null && oldValue != null) {
                if (logger.isInfoEnabled()) {
                    logger.info("Property changed detected: " + placeholder + (newValue != null ? "=" + newValue : " removed"));
                }
                List<DynamicProperty> affectedDynamics = placeholderToDynamics.get(placeholder);
                allDynamics.addAll(affectedDynamics);
            }
        }
        // 
        // 获取受影响的beans
        // 
        Map<String, List<DynamicProperty>> dynamicsByBeanName = new HashMap<String, List<DynamicProperty>>();
        Map<String, Object> beanByBeanName = new HashMap<String, Object>();
        for (DynamicProperty dynamic : allDynamics) {
            String beanName = dynamic.getBeanName();
            List<DynamicProperty> l = dynamicsByBeanName.get(beanName);
            if (l == null) {
                dynamicsByBeanName.put(beanName, (l = new ArrayList<DynamicProperty>()));
                Object bean = null;
                try {
                    bean = applicationContext.getBean(beanName);
                    beanByBeanName.put(beanName, bean);
                } catch (BeansException e) {
                    // keep dynamicsByBeanName list, warn only once.
                    logger.error("Error obtaining bean " + beanName, e);
                }
                // 
                try {
                    if (bean instanceof IReconfigurationAware) {
                        // hello!
                        ((IReconfigurationAware) bean).beforeReconfiguration();
                    }
                } catch (Exception e) {
                    logger.error("Error calling beforeReconfiguration on " + beanName, e);
                }
            }
            l.add(dynamic);
        }
        // 
        // 处理受影响的bean
        // 
        Collection<String> beanNames = dynamicsByBeanName.keySet();
        for (String beanName : beanNames) {
            Object bean = beanByBeanName.get(beanName);
            if (// problems obtaining bean, earlier
            bean == null) {
                continue;
            }
            BeanWrapper beanWrapper = new BeanWrapperImpl(bean);
            // for all affected ...
            List<DynamicProperty> dynamics = dynamicsByBeanName.get(beanName);
            for (DynamicProperty dynamic : dynamics) {
                String propertyName = dynamic.getPropertyName();
                String unparsedValue = dynamic.getUnparsedValue();
                // obtain an updated value, including dependencies
                String newValue;
                removeDynamic(dynamic);
                currentBeanName = beanName;
                currentPropertyName = propertyName;
                try {
                    newValue = parseStringValue(unparsedValue, newProperties, new HashSet());
                } finally {
                    currentBeanName = null;
                    currentPropertyName = null;
                }
                if (logger.isInfoEnabled()) {
                    logger.info("Updating property " + beanName + "." + propertyName + " to " + newValue);
                }
                // assign it to the bean
                try {
                    beanWrapper.setPropertyValue(propertyName, newValue);
                } catch (BeansException e) {
                    logger.error("Error setting property " + beanName + "." + propertyName + " to " + newValue, e);
                }
            }
        }
        // 
        for (String beanName : beanNames) {
            Object bean = beanByBeanName.get(beanName);
            try {
                if (bean instanceof IReconfigurationAware) {
                    ((IReconfigurationAware) bean).afterReconfiguration();
                }
            } catch (Exception e) {
                logger.error("Error calling afterReconfiguration on " + beanName, e);
            }
        }
    } catch (IOException e) {
        logger.error("Error trying to reload net.unicon.iamlabs.spring.properties.example.net.unicon.iamlabs" + ".spring" + ".properties: " + e.getMessage(), e);
    }
}
Also used : BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) HashMap(java.util.HashMap) IOException(java.io.IOException) Properties(java.util.Properties) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) BeanWrapper(org.springframework.beans.BeanWrapper) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) BeansException(org.springframework.beans.BeansException)

Example 52 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project spring-framework by spring-projects.

the class CustomEditorTests method testUninitializedArrayPropertyWithCustomEditor.

@Test
void testUninitializedArrayPropertyWithCustomEditor() {
    IndexedTestBean bean = new IndexedTestBean(false);
    BeanWrapper bw = new BeanWrapperImpl(bean);
    PropertyEditor pe = new CustomNumberEditor(Integer.class, true);
    bw.registerCustomEditor(null, "list.age", pe);
    TestBean tb = new TestBean();
    bw.setPropertyValue("list", new ArrayList<>());
    bw.setPropertyValue("list[0]", tb);
    assertThat(bean.getList().get(0)).isEqualTo(tb);
    assertThat(bw.findCustomEditor(int.class, "list.age")).isEqualTo(pe);
    assertThat(bw.findCustomEditor(null, "list.age")).isEqualTo(pe);
    assertThat(bw.findCustomEditor(int.class, "list[0].age")).isEqualTo(pe);
    assertThat(bw.findCustomEditor(null, "list[0].age")).isEqualTo(pe);
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) BooleanTestBean(org.springframework.beans.testfixture.beans.BooleanTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) NumberTestBean(org.springframework.beans.testfixture.beans.NumberTestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) PropertyEditor(java.beans.PropertyEditor) Test(org.junit.jupiter.api.Test)

Example 53 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project spring-framework by spring-projects.

the class CustomEditorTests method testDefaultNumberEditor.

@Test
void testDefaultNumberEditor() {
    NumberTestBean tb = new NumberTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.setPropertyValue("short1", "1");
    bw.setPropertyValue("short2", "2");
    bw.setPropertyValue("int1", "7");
    bw.setPropertyValue("int2", "8");
    bw.setPropertyValue("long1", "5");
    bw.setPropertyValue("long2", "6");
    bw.setPropertyValue("bigInteger", "3");
    bw.setPropertyValue("float1", "7.1");
    bw.setPropertyValue("float2", "8.1");
    bw.setPropertyValue("double1", "5.1");
    bw.setPropertyValue("double2", "6.1");
    bw.setPropertyValue("bigDecimal", "4.5");
    assertThat(Short.valueOf("1").equals(bw.getPropertyValue("short1"))).as("Correct short1 value").isTrue();
    assertThat(tb.getShort1() == 1).as("Correct short1 value").isTrue();
    assertThat(Short.valueOf("2").equals(bw.getPropertyValue("short2"))).as("Correct short2 value").isTrue();
    assertThat(Short.valueOf("2").equals(tb.getShort2())).as("Correct short2 value").isTrue();
    assertThat(Integer.valueOf("7").equals(bw.getPropertyValue("int1"))).as("Correct int1 value").isTrue();
    assertThat(tb.getInt1() == 7).as("Correct int1 value").isTrue();
    assertThat(Integer.valueOf("8").equals(bw.getPropertyValue("int2"))).as("Correct int2 value").isTrue();
    assertThat(Integer.valueOf("8").equals(tb.getInt2())).as("Correct int2 value").isTrue();
    assertThat(Long.valueOf("5").equals(bw.getPropertyValue("long1"))).as("Correct long1 value").isTrue();
    assertThat(tb.getLong1() == 5).as("Correct long1 value").isTrue();
    assertThat(Long.valueOf("6").equals(bw.getPropertyValue("long2"))).as("Correct long2 value").isTrue();
    assertThat(Long.valueOf("6").equals(tb.getLong2())).as("Correct long2 value").isTrue();
    assertThat(new BigInteger("3").equals(bw.getPropertyValue("bigInteger"))).as("Correct bigInteger value").isTrue();
    assertThat(new BigInteger("3").equals(tb.getBigInteger())).as("Correct bigInteger value").isTrue();
    assertThat(Float.valueOf("7.1").equals(bw.getPropertyValue("float1"))).as("Correct float1 value").isTrue();
    assertThat(Float.valueOf("7.1").equals(tb.getFloat1())).as("Correct float1 value").isTrue();
    assertThat(Float.valueOf("8.1").equals(bw.getPropertyValue("float2"))).as("Correct float2 value").isTrue();
    assertThat(Float.valueOf("8.1").equals(tb.getFloat2())).as("Correct float2 value").isTrue();
    assertThat(Double.valueOf("5.1").equals(bw.getPropertyValue("double1"))).as("Correct double1 value").isTrue();
    assertThat(tb.getDouble1() == 5.1).as("Correct double1 value").isTrue();
    assertThat(Double.valueOf("6.1").equals(bw.getPropertyValue("double2"))).as("Correct double2 value").isTrue();
    assertThat(Double.valueOf("6.1").equals(tb.getDouble2())).as("Correct double2 value").isTrue();
    assertThat(new BigDecimal("4.5").equals(bw.getPropertyValue("bigDecimal"))).as("Correct bigDecimal value").isTrue();
    assertThat(new BigDecimal("4.5").equals(tb.getBigDecimal())).as("Correct bigDecimal value").isTrue();
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) NumberTestBean(org.springframework.beans.testfixture.beans.NumberTestBean) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Example 54 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project spring-framework by spring-projects.

the class CustomEditorTests method testCustomBooleanEditorWithAllowEmpty.

@Test
void testCustomBooleanEditorWithAllowEmpty() {
    BooleanTestBean tb = new BooleanTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(Boolean.class, new CustomBooleanEditor(true));
    bw.setPropertyValue("bool2", "true");
    assertThat(Boolean.TRUE.equals(bw.getPropertyValue("bool2"))).as("Correct bool2 value").isTrue();
    assertThat(tb.getBool2().booleanValue()).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "false");
    assertThat(Boolean.FALSE.equals(bw.getPropertyValue("bool2"))).as("Correct bool2 value").isTrue();
    boolean condition3 = !tb.getBool2();
    assertThat(condition3).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "on");
    assertThat(tb.getBool2().booleanValue()).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "off");
    boolean condition2 = !tb.getBool2();
    assertThat(condition2).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "yes");
    assertThat(tb.getBool2().booleanValue()).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "no");
    boolean condition1 = !tb.getBool2();
    assertThat(condition1).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "1");
    assertThat(tb.getBool2().booleanValue()).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "0");
    boolean condition = !tb.getBool2();
    assertThat(condition).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "");
    assertThat(bw.getPropertyValue("bool2")).as("Correct bool2 value").isNull();
    assertThat(tb.getBool2()).as("Correct bool2 value").isNull();
}
Also used : BooleanTestBean(org.springframework.beans.testfixture.beans.BooleanTestBean) BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) Test(org.junit.jupiter.api.Test)

Example 55 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project spring-framework by spring-projects.

the class CustomEditorTests method testCustomEditorForAllNestedStringProperties.

@Test
void testCustomEditorForAllNestedStringProperties() {
    TestBean tb = new TestBean();
    tb.setSpouse(new TestBean());
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(String.class, new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue("prefix" + text);
        }
    });
    bw.setPropertyValue("spouse.name", "value");
    bw.setPropertyValue("touchy", "value");
    assertThat(bw.getPropertyValue("spouse.name")).isEqualTo("prefixvalue");
    assertThat(tb.getSpouse().getName()).isEqualTo("prefixvalue");
    assertThat(bw.getPropertyValue("touchy")).isEqualTo("prefixvalue");
    assertThat(tb.getTouchy()).isEqualTo("prefixvalue");
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) BooleanTestBean(org.springframework.beans.testfixture.beans.BooleanTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) NumberTestBean(org.springframework.beans.testfixture.beans.NumberTestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.jupiter.api.Test)

Aggregations

BeanWrapper (org.springframework.beans.BeanWrapper)144 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)79 Test (org.junit.jupiter.api.Test)31 NumberTestBean (org.springframework.beans.testfixture.beans.NumberTestBean)21 BooleanTestBean (org.springframework.beans.testfixture.beans.BooleanTestBean)20 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)19 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)18 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)18 TestBean (org.springframework.beans.testfixture.beans.TestBean)18 Consumes (javax.ws.rs.Consumes)16 PUT (javax.ws.rs.PUT)16 Path (javax.ws.rs.Path)16 PropertyEditorSupport (java.beans.PropertyEditorSupport)14 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)14 HashSet (java.util.HashSet)12 BeansException (org.springframework.beans.BeansException)12 PropertyDescriptor (java.beans.PropertyDescriptor)11 OnmsNode (org.opennms.netmgt.model.OnmsNode)9 Test (org.junit.Test)6 BigDecimal (java.math.BigDecimal)5