Search in sources :

Example 1 with YAMLConfiguration

use of org.apache.commons.configuration2.YAMLConfiguration in project studio by craftercms.

the class StudioConfigurationImpl method loadConfig.

@Override
public void loadConfig() {
    YamlConfiguration baseConfig = new YamlConfiguration();
    YamlConfiguration overrideConfig = new YamlConfiguration();
    Resource resource = new ClassPathResource(configLocation);
    try (InputStream in = resource.getInputStream()) {
        baseConfig.setExpressionEngine(getExpressionEngine());
        baseConfig.read(in);
        logger.debug("Loaded configuration from location: {0} \n {1}", configLocation, baseConfig);
    } catch (IOException | ConfigurationException e) {
        logger.error("Failed to load studio configuration from: " + configLocation, e);
    }
    if (baseConfig.containsKey(STUDIO_CONFIG_OVERRIDE_CONFIG)) {
        String overrideConfigLocation = baseConfig.getString(STUDIO_CONFIG_OVERRIDE_CONFIG);
        resource = new ClassPathResource(overrideConfigLocation);
        try (InputStream in = resource.getInputStream()) {
            overrideConfig.setExpressionEngine(getExpressionEngine());
            overrideConfig.read(in);
            if (!overrideConfig.isEmpty()) {
                logger.debug("Loaded additional configuration from location: {0} \n {1}", overrideConfigLocation, overrideConfig);
            }
        } catch (IOException | ConfigurationException e) {
            logger.error("Failed to load studio configuration from: " + overrideConfigLocation, e);
        }
    }
    // Merge the base properties and additional properties
    if (!overrideConfig.isEmpty()) {
        CombinedConfiguration combinedConfig = new CombinedConfiguration(new OverrideCombiner());
        combinedConfig.setExpressionEngine(getExpressionEngine());
        combinedConfig.addConfiguration(overrideConfig);
        combinedConfig.addConfiguration(baseConfig);
        systemConfig = combinedConfig;
    } else {
        systemConfig = baseConfig;
    }
}
Also used : ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException) InputStream(java.io.InputStream) ClassPathResource(org.springframework.core.io.ClassPathResource) FileSystemResource(org.springframework.core.io.FileSystemResource) Resource(org.springframework.core.io.Resource) CombinedConfiguration(org.apache.commons.configuration2.CombinedConfiguration) IOException(java.io.IOException) YamlConfiguration(org.craftercms.commons.config.YamlConfiguration) ClassPathResource(org.springframework.core.io.ClassPathResource) OverrideCombiner(org.apache.commons.configuration2.tree.OverrideCombiner)

Example 2 with YAMLConfiguration

use of org.apache.commons.configuration2.YAMLConfiguration in project incubator-hugegraph by apache.

the class ConfigUtil method checkGremlinConfig.

public static void checkGremlinConfig(String conf) {
    Parameters params = new Parameters();
    try {
        FileBasedConfigurationBuilder<FileBasedConfiguration> builder = new FileBasedConfigurationBuilder(YAMLConfiguration.class).configure(params.fileBased().setFileName(conf));
        YAMLConfiguration config = (YAMLConfiguration) builder.getConfiguration();
        List<HierarchicalConfiguration<ImmutableNode>> nodes = config.childConfigurationsAt(NODE_GRAPHS);
        if (nodes == null || nodes.isEmpty()) {
            return;
        }
        E.checkArgument(nodes.size() == 1, "Not allowed to specify multiple '%s' " + "nodes in config file '%s'", NODE_GRAPHS, conf);
        ImmutableNode root = null;
        NodeHandler<ImmutableNode> nodeHandler = null;
        for (HierarchicalConfiguration<ImmutableNode> node : nodes) {
            NodeModel<ImmutableNode> nodeModel = node.getNodeModel();
            E.checkArgument(nodeModel != null && (nodeHandler = nodeModel.getNodeHandler()) != null && (root = nodeHandler.getRootNode()) != null, "Node '%s' must contain root", node);
        }
    } catch (ConfigurationException e) {
        throw new HugeException("Failed to load yaml config file '%s'", conf);
    }
}
Also used : Parameters(org.apache.commons.configuration2.builder.fluent.Parameters) FileBasedConfigurationBuilder(org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder) ImmutableNode(org.apache.commons.configuration2.tree.ImmutableNode) FileBasedConfiguration(org.apache.commons.configuration2.FileBasedConfiguration) HierarchicalConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration) HugeException(com.baidu.hugegraph.HugeException) YAMLConfiguration(org.apache.commons.configuration2.YAMLConfiguration) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException)

Example 3 with YAMLConfiguration

use of org.apache.commons.configuration2.YAMLConfiguration in project winery by eclipse.

the class EnvironmentTest method testSave.

/**
 * This test checks whether changes made to the configuration are persisted correctly. and whenever the file is
 * reloaded when a change occurs
 */
@Test
public void testSave() {
    YAMLConfiguration configuration = Environment.getInstance().getConfiguration();
    configuration.setProperty("ui.features.foo", false);
    configuration.setProperty("ui.features.bar", true);
    configuration.setProperty("ui.endpoints.quaz", "");
    Environment.getInstance().save();
    Environment.getInstance().setConfiguration(null);
    configuration = Environment.getInstance().getConfiguration();
    assertFalse(configuration.getBoolean("ui.features.foo"));
    assertTrue(configuration.getBoolean("ui.features.bar"));
    assertEquals(configuration.getString("ui.endpoints.quaz"), "");
}
Also used : YAMLConfiguration(org.apache.commons.configuration2.YAMLConfiguration) Test(org.junit.jupiter.api.Test)

Example 4 with YAMLConfiguration

use of org.apache.commons.configuration2.YAMLConfiguration in project winery by eclipse.

the class EnvironmentTest method testReload.

/**
 * Tests whenever a change in the configuration file is detected.
 */
@Test
@Disabled("This test seems to fail transiently on test infrastructure")
public void testReload() {
    YAMLConfiguration configuration = Environment.getInstance().getConfiguration();
    assertFalse(Environment.getInstance().checkConfigurationForUpdate());
    configuration.setProperty("ui.features.foo", false);
    Environment.getInstance().save();
    assertTrue(Environment.getInstance().checkConfigurationForUpdate());
}
Also used : YAMLConfiguration(org.apache.commons.configuration2.YAMLConfiguration) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 5 with YAMLConfiguration

use of org.apache.commons.configuration2.YAMLConfiguration in project winery by eclipse.

the class EnvironmentTest method testConfigValues.

/**
 * This test checks for correctness of the retrieved values from the config file. Also tests whether or not the
 * properties in the config file are found.
 */
@Test
public void testConfigValues() {
    YAMLConfiguration tested = Environment.getInstance().getConfiguration();
    assertTrue(tested.getBoolean("ui.features.foo"));
    assertFalse(tested.getBoolean("ui.features.bar"));
    assertEquals("http://quaz:8080", tested.getString("ui.endpoints.quaz"));
}
Also used : YAMLConfiguration(org.apache.commons.configuration2.YAMLConfiguration) Test(org.junit.jupiter.api.Test)

Aggregations

YAMLConfiguration (org.apache.commons.configuration2.YAMLConfiguration)10 ConfigurationException (org.apache.commons.configuration2.ex.ConfigurationException)7 IOException (java.io.IOException)5 Test (org.junit.jupiter.api.Test)4 FileReader (java.io.FileReader)3 InputStream (java.io.InputStream)3 File (java.io.File)2 CombinedConfiguration (org.apache.commons.configuration2.CombinedConfiguration)2 ImmutableNode (org.apache.commons.configuration2.tree.ImmutableNode)2 OverrideCombiner (org.apache.commons.configuration2.tree.OverrideCombiner)2 YamlConfiguration (org.craftercms.commons.config.YamlConfiguration)2 FileSystemResource (org.springframework.core.io.FileSystemResource)2 HugeException (com.baidu.hugegraph.HugeException)1 Path (java.nio.file.Path)1 Element (net.sf.ehcache.Element)1 FileBasedConfiguration (org.apache.commons.configuration2.FileBasedConfiguration)1 HierarchicalConfiguration (org.apache.commons.configuration2.HierarchicalConfiguration)1 FileBasedConfigurationBuilder (org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder)1 Parameters (org.apache.commons.configuration2.builder.fluent.Parameters)1 FileHandler (org.apache.commons.configuration2.io.FileHandler)1