org.apache.commons.configuration2.YAMLConfiguration Java Examples

The following examples show how to use org.apache.commons.configuration2.YAMLConfiguration. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DbEnvironmentXmlEnricherTest.java    From obevo with Apache License 2.0 6 votes vote down vote up
@Test
    public void convert() throws Exception {
        XMLConfiguration configuration = new FileBasedConfigurationBuilder<>(XMLConfiguration.class)
                .configure(new Parameters().hierarchical()
                        .setFile(new File("./src/test/resources/DbEnvironmentXmlEnricher/system-config.xml"))
                ).getConfiguration();

        Map<String, Object> myMap = constructMap(configuration.getNodeModel().getNodeHandler().getRootNode());

        YAMLConfiguration yamlConfiguration = new YAMLConfiguration(configuration);
        StringWriter sw = new StringWriter();
//        yamlConfiguration.write();
        DumperOptions dumperOptions = new DumperOptions();
//        dumperOptions.setPrettyFlow(true);
        dumperOptions.setDefaultFlowStyle(FlowStyle.BLOCK);
        Yaml yaml = new Yaml(dumperOptions);
        yaml.dump(myMap, sw);

//        yamlConfiguration.dump(sw, new DumperOptions());
        System.out.println(sw.toString());
    }
 
Example #2
Source File: MainModule.java    From cassandra-sidecar with Apache License 2.0 5 votes vote down vote up
@Provides
@Singleton
public Configuration configuration() throws ConfigurationException, IOException
{
    final String confPath = System.getProperty("sidecar.config", "file://./conf/config.yaml");
    logger.info("Reading configuration from {}", confPath);
    try
    {
        URL url = new URL(confPath);

        YAMLConfiguration yamlConf = new YAMLConfiguration();
        InputStream stream = url.openStream();
        yamlConf.read(stream);

        return new Configuration.Builder()
                .setCassandraHost(yamlConf.get(String.class, "cassandra.host"))
                .setCassandraPort(yamlConf.get(Integer.class, "cassandra.port"))
                .setHost(yamlConf.get(String.class, "sidecar.host"))
                .setPort(yamlConf.get(Integer.class, "sidecar.port"))
                .setHealthCheckFrequency(yamlConf.get(Integer.class, "healthcheck.poll_freq_millis"))
                .setKeyStorePath(yamlConf.get(String.class, "sidecar.ssl.keystore.path", null))
                .setKeyStorePassword(yamlConf.get(String.class, "sidecar.ssl.keystore.password", null))
                .setTrustStorePath(yamlConf.get(String.class, "sidecar.ssl.truststore.path", null))
                .setTrustStorePassword(yamlConf.get(String.class, "sidecar.ssl.truststore.password", null))
                .setSslEnabled(yamlConf.get(Boolean.class, "sidecar.ssl.enabled", false))
                .build();
    }
    catch (MalformedURLException e)
    {
        throw new ConfigurationException("Failed reading from sidebar.config path: " + confPath, e);
    }
}
 
Example #3
Source File: DbEnvironmentXmlEnricherTest.java    From obevo with Apache License 2.0 5 votes vote down vote up
@Test
    public void yamlTest() throws Exception {
        ImmutableHierarchicalConfiguration configuration = new FileBasedConfigurationBuilder<>(YAMLConfiguration.class)
                .configure(new Parameters().hierarchical()
                        .setFile(new File("./src/test/resources/DbEnvironmentXmlEnricher/system-config.yaml"))
//                        .setFile(new File("./src/test/resources/DbEnvironmentXmlEnricher/system-config.xml"))
                ).getConfiguration();
        System.out.println(configuration);
    }
 
Example #4
Source File: PlatformConfigReader.java    From obevo with Apache License 2.0 5 votes vote down vote up
private HierarchicalConfiguration<ImmutableNode> loadPropertiesFromUrl(FileObject file) {
    try {
        return new FileBasedConfigurationBuilder<>(YAMLConfiguration.class)
                .configure(new Parameters().hierarchical().setURL(file.getURLDa()))
                .getConfiguration();
    } catch (ConfigurationException e) {
        throw new DeployerRuntimeException(e);
    }
}
 
Example #5
Source File: GraphFactory.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private static org.apache.commons.configuration2.Configuration getConfiguration(final File configurationFile) {
    if (!configurationFile.isFile())
        throw new IllegalArgumentException(String.format("The location configuration must resolve to a file and [%s] does not", configurationFile));

    try {
        final String fileName = configurationFile.getName();
        final String fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1);

        final Configuration conf;
        final Configurations configs = new Configurations();

        switch (fileExtension) {
            case "yml":
            case "yaml":
                final Parameters params = new Parameters();
                final FileBasedConfigurationBuilder<FileBasedConfiguration> builder =
                        new FileBasedConfigurationBuilder<FileBasedConfiguration>(YAMLConfiguration.class).
                                configure(params.fileBased().setFile(configurationFile));

                final org.apache.commons.configuration2.Configuration copy = new org.apache.commons.configuration2.BaseConfiguration();
                ConfigurationUtils.copy(builder.configure(params.fileBased().setFile(configurationFile)).getConfiguration(), copy);
                conf = copy;
                break;
            case "xml":
                conf = configs.xml(configurationFile);
                break;
            default:
                conf = configs.properties(configurationFile);
        }
        return conf;
    } catch (ConfigurationException e) {
        throw new IllegalArgumentException(String.format("Could not load configuration at: %s", configurationFile), e);
    }
}
 
Example #6
Source File: XmlFileConfigReader.java    From obevo with Apache License 2.0 4 votes vote down vote up
@Override
public HierarchicalConfiguration getConfig(FileObject checkoutFolder) {
    try {
        FileObject envFileToRead = getEnvFileToRead(checkoutFolder);
        if (envFileToRead.getName().getExtension().equals("xml")) {
            // For the XML lookup, we want all access to fall to attribute queries if we choose. To do that, we override
            // the expression engine.
            DefaultExpressionEngine engine = new DefaultExpressionEngine(DefaultExpressionEngineSymbols.DEFAULT_SYMBOLS) {
                @Override
                public <T> List<QueryResult<T>> query(T root, String key, NodeHandler<T> handler) {
                    List<QueryResult<T>> results = super.query(root, key, handler);
                    if (!results.isEmpty()) {
                        return results;
                    }

                    // If we find no results, fall back to the query that specifies the attribute handler
                    return super.query(root, this.attributeKey(null, key), handler);
                }
            };

            XMLConfiguration configuration = new FileBasedConfigurationBuilder<>(XMLConfiguration.class)
                    .configure(new Parameters().hierarchical()
                            .setURL(envFileToRead.getURLDa())
                            .setListDelimiterHandler(new DisabledListDelimiterHandler())
                            .setExpressionEngine(engine)
                    )
                    .getConfiguration();
            postProcess(configuration);
            return configuration;
        } else {
            return new FileBasedConfigurationBuilder<>(YAMLConfiguration.class)
                    .configure(new Parameters().hierarchical()
                            .setURL(envFileToRead.getURLDa())
                            .setListDelimiterHandler(new DisabledListDelimiterHandler())
                    )
                    .getConfiguration();
        }
    } catch (ConfigurationException e) {
        throw new DeployerRuntimeException(e);
    }
}