Search in sources :

Example 1 with ValidationException

use of com.netflix.config.validation.ValidationException in project archaius by Netflix.

the class DynamicPropertyUpdater method addOrChangeProperty.

/**
     * Add or update the property in the underlying config depending on if it exists
     * 
     * @param name
     * @param newValue
     * @param config
     */
void addOrChangeProperty(final String name, final Object newValue, final Configuration config) {
    // We do not want to abort the operation due to failed validation on one property
    try {
        if (!config.containsKey(name)) {
            logger.debug("adding property key [{}], value [{}]", name, newValue);
            config.addProperty(name, newValue);
        } else {
            Object oldValue = config.getProperty(name);
            if (newValue != null) {
                Object newValueArray;
                if (oldValue instanceof CopyOnWriteArrayList && AbstractConfiguration.getDefaultListDelimiter() != '\0') {
                    newValueArray = new CopyOnWriteArrayList();
                    Iterable<String> stringiterator = Splitter.on(AbstractConfiguration.getDefaultListDelimiter()).omitEmptyStrings().trimResults().split((String) newValue);
                    for (String s : stringiterator) {
                        ((CopyOnWriteArrayList) newValueArray).add(s);
                    }
                } else {
                    newValueArray = newValue;
                }
                if (!newValueArray.equals(oldValue)) {
                    logger.debug("updating property key [{}], value [{}]", name, newValue);
                    config.setProperty(name, newValue);
                }
            } else if (oldValue != null) {
                logger.debug("nulling out property key [{}]", name);
                config.setProperty(name, null);
            }
        }
    } catch (ValidationException e) {
        logger.warn("Validation failed for property " + name, e);
    }
}
Also used : ValidationException(com.netflix.config.validation.ValidationException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 2 with ValidationException

use of com.netflix.config.validation.ValidationException in project archaius by Netflix.

the class ValidationTest method testValidation.

@Test
public void testValidation() {
    DynamicStringProperty prop = new DynamicStringProperty("abc", "default") {

        public void validate(String newValue) {
            throw new ValidationException("failed");
        }
    };
    try {
        ConfigurationManager.getConfigInstance().setProperty("abc", "new");
        fail("ValidationException expected");
    } catch (ValidationException e) {
        assertNotNull(e);
    }
    assertEquals("default", prop.get());
    assertNull(ConfigurationManager.getConfigInstance().getProperty("abc"));
    try {
        ConfigurationManager.getConfigInstance().addProperty("abc", "new");
        fail("ValidationException expected");
    } catch (ValidationException e) {
        assertNotNull(e);
    }
    assertEquals("default", prop.get());
    assertNull(ConfigurationManager.getConfigInstance().getProperty("abc"));
}
Also used : ValidationException(com.netflix.config.validation.ValidationException) DynamicStringProperty(com.netflix.config.DynamicStringProperty) Test(org.junit.Test)

Aggregations

ValidationException (com.netflix.config.validation.ValidationException)2 DynamicStringProperty (com.netflix.config.DynamicStringProperty)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Test (org.junit.Test)1