Example usage for org.springframework.beans BeanWrapperImpl registerCustomEditor

List of usage examples for org.springframework.beans BeanWrapperImpl registerCustomEditor

Introduction

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

Prototype

@Override
    public void registerCustomEditor(Class<?> requiredType, PropertyEditor propertyEditor) 

Source Link

Usage

From source file:com.emc.ecs.sync.service.VdcEditorTest.java

@Test
public void testBeanWrapperWithList() {
    EcsS3Source source = new EcsS3Source();
    BeanWrapperImpl wrapper = new BeanWrapperImpl(source);

    wrapper.registerCustomEditor(Vdc.class, new VdcEditor());

    wrapper.setPropertyValue("vdcs", Arrays.asList("foo(1.1.1.1,2.2.2.2)", "bar(3.3.3.3,4.4.4.4)"));

    Assert.assertEquals(2, source.getVdcs().size());
    Vdc foo = source.getVdcs().get(0);/*  w w w  . ja va2 s  .c  om*/
    Assert.assertEquals("foo", foo.getName());
    Assert.assertEquals(2, foo.getHosts().size());
    Assert.assertEquals("1.1.1.1", foo.getHosts().get(0).getName());
    Assert.assertEquals("2.2.2.2", foo.getHosts().get(1).getName());
    Vdc bar = source.getVdcs().get(1);
    Assert.assertEquals("bar", bar.getName());
    Assert.assertEquals(2, bar.getHosts().size());
    Assert.assertEquals("3.3.3.3", bar.getHosts().get(0).getName());
    Assert.assertEquals("4.4.4.4", bar.getHosts().get(1).getName());
}

From source file:com.emc.ecs.sync.service.SyncJobService.java

@SuppressWarnings("unchecked")
protected <T extends SyncPlugin> T createPlugin(PluginConfig pluginConfig, SyncConfig syncConfig,
        Class<T> clazz) {//from   www . j a  v a 2 s .c  om
    try {
        T plugin = (T) Class.forName(pluginConfig.getPluginClass()).newInstance();
        BeanWrapperImpl wrapper = new BeanWrapperImpl(plugin);

        // register property converters
        wrapper.registerCustomEditor(Date.class,
                new CustomDateEditor(new SimpleDateFormat(ISO_8601_FORMAT), true));
        wrapper.registerCustomEditor(Vdc.class, new VdcEditor());

        // set custom properties
        for (Map.Entry<String, String> prop : pluginConfig.getCustomProperties().entrySet()) {
            wrapper.setPropertyValue(prop.getKey(), prop.getValue());
        }
        // set custom list properties
        for (Map.Entry<String, List<String>> listProp : pluginConfig.getCustomListProperties().entrySet()) {
            wrapper.setPropertyValue(listProp.getKey(), listProp.getValue());
        }

        copyProperties(syncConfig, plugin); // set common properties

        return plugin;
    } catch (ClassCastException e) {
        throw new ConfigurationException(
                pluginConfig.getPluginClass() + " does not extend " + clazz.getSimpleName());
    } catch (BeansException e) {
        throw new ConfigurationException("could not set property on plugin " + pluginConfig.getPluginClass(),
                e);
    } catch (Throwable t) {
        throw new ConfigurationException("could not create plugin instance " + pluginConfig.getPluginClass(),
                t);
    }
}