Example usage for org.apache.commons.configuration2 PropertiesConfiguration clearProperty

List of usage examples for org.apache.commons.configuration2 PropertiesConfiguration clearProperty

Introduction

In this page you can find the example usage for org.apache.commons.configuration2 PropertiesConfiguration clearProperty.

Prototype

@Override
public final void clearProperty(final String key) 

Source Link

Document

Removes the specified property from this configuration.

Usage

From source file:com.sikulix.core.SX.java

private static void setOptions(PropertiesConfiguration someOptions) {
    if (isNull(someOptions) || someOptions.size() == 0) {
        return;//w ww .  j  a  v  a2 s . c  om
    }
    Iterator<String> allKeys = someOptions.getKeys();
    List<String> sxSettings = new ArrayList<>();
    while (allKeys.hasNext()) {
        String key = allKeys.next();
        if (key.startsWith("Settings.")) {
            sxSettings.add(key);
            continue;
        }
        trace("!setOptions: %s = %s", key, someOptions.getProperty(key));
    }
    if (sxSettings.size() > 0) {
        Class cClass = null;
        try {
            cClass = Class.forName("org.sikuli.basics.Settings");
        } catch (ClassNotFoundException e) {
            error("!setOptions: %s", cClass);
        }
        if (!isNull(cClass)) {
            for (String sKey : sxSettings) {
                String sAttr = sKey.substring("Settings.".length());
                Field cField = null;
                Class ccField = null;
                try {
                    cField = cClass.getField(sAttr);
                    ccField = cField.getType();
                    if (ccField.getName() == "boolean") {
                        cField.setBoolean(null, someOptions.getBoolean(sKey));
                    } else if (ccField.getName() == "int") {
                        cField.setInt(null, someOptions.getInt(sKey));
                    } else if (ccField.getName() == "float") {
                        cField.setFloat(null, someOptions.getFloat(sKey));
                    } else if (ccField.getName() == "double") {
                        cField.setDouble(null, someOptions.getDouble(sKey));
                    } else if (ccField.getName() == "String") {
                        cField.set(null, someOptions.getString(sKey));
                    }
                    trace("!setOptions: %s = %s", sAttr, someOptions.getProperty(sKey));
                    someOptions.clearProperty(sKey);
                } catch (Exception ex) {
                    error("!setOptions: %s = %s", sKey, sxOptions.getProperty(sKey));
                }
            }
        }
    }
}

From source file:org.dspace.servicemanager.config.DSpaceConfigurationServiceTest.java

/**
 * Tests the ability of our ConfigurationService to automatically reload properties after a set period
 * of time.//from w ww  .j av  a  2s.c om
 */
@Test
public void testAutomaticReload() throws ConfigurationException, InterruptedException {
    // Initialize new config service
    DSpaceConfigurationService dscs = new DSpaceConfigurationService();

    // Assert a property exists with a specific initial value
    assertNotNull(dscs.getProperty("prop.to.auto.reload"));
    assertEquals("D-space", dscs.getProperty("prop.to.auto.reload"));

    // Now, change the value of that Property in the file itself (using a separate builder instance)
    FileBasedConfigurationBuilder<PropertiesConfiguration> builder = new Configurations()
            .propertiesBuilder(propertyFilePath);
    PropertiesConfiguration config = builder.getConfiguration();
    // Clear out current value. Add in a new value
    config.clearProperty("prop.to.auto.reload");
    config.addProperty("prop.to.auto.reload", "DSpace");
    // Save updates to file
    builder.save();

    // Check immediately. Property should be unchanged
    // NOTE: If this fails, then somehow the configuration reloaded *immediately*
    assertEquals("D-space", dscs.getProperty("prop.to.auto.reload"));

    // Wait now for 3 seconds
    Thread.sleep(3_000);

    // Check again. Property should have reloaded
    // NOTE: reload time is set in config-definition.xml to reload every 2 seconds
    assertEquals("DSpace", dscs.getProperty("prop.to.auto.reload"));
}