Example usage for org.apache.commons.configuration Configuration getKeys

List of usage examples for org.apache.commons.configuration Configuration getKeys

Introduction

In this page you can find the example usage for org.apache.commons.configuration Configuration getKeys.

Prototype

Iterator getKeys();

Source Link

Document

Get the list of the keys contained in the configuration.

Usage

From source file:org.apache.whirr.service.elasticsearch.ElasticSearchConfigurationBuilder.java

private static List<String> asYamlLines(Configuration config, int depth) {
    List<String> lines = Lists.newArrayList();
    Set<String> prefixes = Sets.newHashSet();

    Iterator<String> keys = config.getKeys();
    while (keys.hasNext()) {
        String key = keys.next();

        String[] parts = key.split("\\.");
        String prefix = parts[0];

        if (prefixes.contains(prefix)) {
            continue; // skip parsed set of keys
        }//from  w  w  w . j  a v a 2 s. co m

        if (parts.length == 1) {
            lines.add(spaces(depth * 2) + key + ": " + config.getProperty(key));

        } else if (parts.length > 1) {
            lines.add(spaces(depth * 2) + prefix + ":");
            lines.addAll(asYamlLines(config.subset(prefix), depth + 1));
        }

        prefixes.add(prefix);
    }

    return lines;
}

From source file:org.apache.whirr.service.hadoop.HadoopConfigurationBuilderTest.java

@Test
public void testCommon() throws Exception {
    Configuration conf = HadoopConfigurationBuilder.buildCommonConfiguration(clusterSpec, cluster, defaults);
    assertThat(Iterators.size(conf.getKeys()), is(3));
    assertThat(conf.getString("p1"), is("common1"));
    assertThat(conf.getString("p2"), is("common2"));
    assertThat(conf.getString("fs.default.name"), matches("hdfs://.+:8020/"));
}

From source file:org.apache.whirr.service.hadoop.HadoopConfigurationBuilderTest.java

@Test
public void testOverrides() throws Exception {
    Configuration overrides = new PropertiesConfiguration();
    overrides.addProperty("hadoop-common.p1", "overridden1");
    overrides.addProperty("hadoop-common.p2", "overridden2");
    overrides.addProperty("hadoop-common.fs.default.name", "not-overridden");
    clusterSpec = ClusterSpec.withNoDefaults(overrides);
    Configuration conf = HadoopConfigurationBuilder.buildCommonConfiguration(clusterSpec, cluster, defaults);
    assertThat(Iterators.size(conf.getKeys()), is(3));
    assertThat(conf.getString("p1"), is("overridden1"));
    assertThat(conf.getString("p2"), is("overridden2"));
    assertThat("Can't override dynamically set properties", conf.getString("fs.default.name"),
            matches("hdfs://.+:8020/"));
}

From source file:org.apache.whirr.service.hadoop.HadoopConfigurationBuilderTest.java

@Test
public void testHdfs() throws Exception {
    Configuration conf = HadoopConfigurationBuilder.buildHdfsConfiguration(clusterSpec, cluster, defaults);
    assertThat(Iterators.size(conf.getKeys()), is(1));
    assertThat(conf.getString("p1"), is("hdfs1"));
}

From source file:org.apache.whirr.service.hadoop.HadoopConfigurationConverter.java

@VisibleForTesting
static List<String> asXmlConfigurationLines(Configuration hadoopConfig) {
    List<String> lines = Lists.newArrayList();
    lines.add("<configuration>");
    for (@SuppressWarnings("unchecked")
    Iterator<String> it = hadoopConfig.getKeys(); it.hasNext();) {
        String key = it.next();/*from  www.j  a  v  a  2s. com*/
        if (key.endsWith(FINAL_SUFFIX)) {
            continue;
        }

        // rebuild the original value by joining all of them with the default separator
        String value = StringUtils.join(hadoopConfig.getStringArray(key),
                AbstractConfiguration.getDefaultListDelimiter());
        lines.add("  <property>");
        lines.add(String.format("    <name>%s</name>", key));
        lines.add(String.format("    <value>%s</value>", value));
        String finalValue = hadoopConfig.getString(key + FINAL_SUFFIX);
        if (finalValue != null) {
            lines.add(String.format("    <final>%s</final>", finalValue));
        }
        lines.add("  </property>");
    }
    lines.add("</configuration>");
    return lines;
}

From source file:org.apache.whirr.service.hadoop.HadoopConfigurationConverter.java

@VisibleForTesting
static List<String> asEnvironmentVariablesLines(Configuration hadoopConfig) {
    List<String> lines = Lists.newArrayList();
    lines.add(". /etc/profile");

    for (@SuppressWarnings("unchecked")
    Iterator<String> it = hadoopConfig.getKeys(); it.hasNext();) {
        String key = it.next();//w ww  . j a  v a 2 s . co  m
        if (key.endsWith(FINAL_SUFFIX)) {
            continue;
        }

        // Write the export line. We only allow one value per key 
        String value = hadoopConfig.getString(key);
        lines.add(new StringBuilder("export ").append(key).append("=\"").append(value).append("\"").toString());
    }
    return lines;
}

From source file:org.apache.whirr.service.puppet.functions.ModulePropertiesFromConfiguration.java

public Map<String, String> apply(final Configuration config) {
    Iterator<String> allKeys = Iterators.transform(config.getKeys(), Functions.toStringFunction());
    Set<String> moduleKeys = Sets.newHashSet(Iterators.<String>filter(allKeys, Predicates.and(
            Predicates.containsPattern(PUPPET + "\\.[^.]+\\." + MODULE_SOURCE_SUBKEY), new Predicate<String>() {

                @Override//from  w w  w .  j  a  va  2 s.c o  m
                public boolean apply(String arg0) {
                    // TODO not sure that we have to check this
                    return config.getString(arg0, null) != null;
                }

            })));
    Builder<String, String> builder = ImmutableMap.<String, String>builder();
    for (String key : moduleKeys) {
        builder.put(key, config.getString(key));
    }
    return builder.build();
}

From source file:org.apache.whirr.service.puppet.statements.CreateSitePpAndApplyRoles.java

@SuppressWarnings("unchecked")
@VisibleForTesting/*  w  w  w  .j  av  a  2  s . c  o m*/
static Manifest getManifestForClusterSpecAndRole(String subroleModuleManifest, Configuration manifestProps) {
    int firstColon = subroleModuleManifest.indexOf(':');
    String moduleName, manifestClassName;
    if (firstColon == -1) {
        moduleName = subroleModuleManifest;
        manifestClassName = null;
    } else {
        moduleName = subroleModuleManifest.substring(0, firstColon);
        int firstDoubleColon = subroleModuleManifest.indexOf("::");
        if (firstDoubleColon != firstColon)
            throw new IllegalArgumentException("Malformed subrole spec for role puppet: "
                    + "format should be puppet:module or puppet:module::manifest");
        manifestClassName = subroleModuleManifest.substring(firstDoubleColon + 2);
    }

    // now create and populate the manifest
    Manifest manifest = new Manifest(moduleName, manifestClassName);

    for (Iterator<?> longkeyI = manifestProps.getKeys(); longkeyI.hasNext();) {
        String longkey = (String) longkeyI.next();
        String key = longkey.substring(subroleModuleManifest.replaceAll(":+", ".").length() + 1);
        if (key.indexOf('.') >= 0) {
            // it's for a sub-manifest; skip it
        } else {
            Object value = manifestProps.getProperty(longkey);
            // an array, e.g. ['1', '2'] gets parsed as a list of two strings, which we need to join
            // with ", "
            String vs = "";
            if (value == null)
                LOG.warn("Invalid value for key " + longkey + ": null"); // shouldn't happen
            else if (value instanceof Collection) {
                Iterator<?> vi = ((Collection<?>) value).iterator();
                if (!vi.hasNext())
                    LOG.warn("Invalid value for key " + longkey + ": empty list"); // shouldn't happen
                else {
                    vs += vi.next();
                    while (vi.hasNext())
                        vs += ", " + vi.next();
                }
            } else {
                vs = value.toString();
            }
            manifest.attribs.put(key, vs);
        }
    }
    LOG.debug("Bootstrapping " + subroleModuleManifest + ", produced manifest:\n" + manifest);
    return manifest;
}

From source file:org.bhave.experiment.AbstractConfigurablePrototype.java

/**
 * Creates a deep copy for the supplied configuration object
 *
 * @param config a configuration object//from  w  w w.j  av a  2s  . c  o m
 */
@Override
public void loadConfiguration(Configuration config) {
    Configuration newConfig = new PropertiesConfiguration();

    Iterator<String> keyIt = config.getKeys();

    while (keyIt.hasNext()) {
        String key = keyIt.next();
        newConfig.setProperty(key, config.getProperty(key));
    }
    this.config = newConfig;
}

From source file:org.cesecore.audit.AuditDevicesConfig.java

@SuppressWarnings("unchecked")
private static void setup() {
    try {//  w  ww  . j av  a  2  s.  com
        lock.lock();
        if (loggers == null) {
            loggers = new HashMap<String, AuditLogDevice>();
            final Configuration conf = ConfigurationHolder.instance();
            final String DEVICE_CLASS = "securityeventsaudit.implementation.";
            final String EXPORTER_CLASS = "securityeventsaudit.exporter.";
            // Extract custom properties configured for any device, to avoid lookup for each device later on..
            // Default devices should not require configuring of 'deviceproperty' in defaultvalues.properties, 
            // since the below Iterator does not read from default values. 
            final String DEVICE_PROPERTY = "securityeventsaudit\\.deviceproperty\\.(\\d+)\\.(.+)";
            final Map<Integer, Properties> allDeviceProperties = new HashMap<Integer, Properties>();
            final Iterator<String> iterator = conf.getKeys();
            while (iterator.hasNext()) {
                final String currentKey = iterator.next();
                Pattern pattern = Pattern.compile(DEVICE_PROPERTY);
                Matcher matcher = pattern.matcher(currentKey);
                if (matcher.matches()) {
                    final Integer deviceConfId = Integer.parseInt(matcher.group(1));
                    Properties deviceProperties = allDeviceProperties.get(deviceConfId);
                    if (deviceProperties == null) {
                        deviceProperties = new Properties();
                    }
                    final String devicePropertyName = matcher.group(2);
                    final String devicePropertyValue = conf.getString(currentKey);
                    if (log.isDebugEnabled()) {
                        log.debug("deviceConfId=" + deviceConfId.toString() + " " + devicePropertyName + "="
                                + devicePropertyValue);
                    }
                    deviceProperties.put(devicePropertyName, devicePropertyValue);
                    allDeviceProperties.put(deviceConfId, deviceProperties);
                }
            }
            for (int i = 0; i < 255; i++) {
                if (!checkNoDuplicateProperties(DEVICE_CLASS + i)) {
                    continue;
                }
                final String deviceClass = ConfigurationHolder.getString(DEVICE_CLASS + i);
                if ((deviceClass != null) && (!"null".equalsIgnoreCase(deviceClass))) {
                    if (log.isDebugEnabled()) {
                        log.debug("Trying to register audit device using implementation: " + deviceClass);
                    }
                    try {
                        // Instantiate device
                        final Class<AuditLogDevice> implClass = (Class<AuditLogDevice>) Class
                                .forName(deviceClass);
                        final AuditLogDevice auditLogDevice = implClass.newInstance();
                        final String name = implClass.getSimpleName();
                        loggers.put(name, auditLogDevice);
                        log.info("Registered audit device using implementation: " + deviceClass);
                        // Store custom properties for this device, so they are searchable by name
                        if (!allDeviceProperties.containsKey(Integer.valueOf(i))) {
                            allDeviceProperties.put(Integer.valueOf(i), new Properties());
                        }
                        deviceProperties.put(name, allDeviceProperties.get(Integer.valueOf(i)));
                        // Setup an exporter for this device
                        final String exporterClassName = ConfigurationHolder.getString(EXPORTER_CLASS + i);
                        Class<? extends AuditExporter> exporterClass = AuditExporterDummy.class;
                        if (exporterClassName != null) {
                            try {
                                exporterClass = (Class<? extends AuditExporter>) Class
                                        .forName(exporterClassName);
                            } catch (Exception e) {
                                // ClassCastException, ClassNotFoundException
                                log.error("Could not configure exporter for audit device " + name
                                        + " using implementation: " + exporterClass, e);
                            }
                        }
                        log.info(
                                "Configured exporter " + exporterClass.getSimpleName() + " for device " + name);
                        exporters.put(name, exporterClass);
                    } catch (Exception e) {
                        // ClassCastException, ClassNotFoundException, InstantiationException, IllegalAccessException
                        log.error("Could not creating audit device using implementation: " + deviceClass, e);
                    }
                }
            }
            if (loggers.isEmpty()) {
                log.warn("No security event audit devices has been configured.");
            }
        }
    } finally {
        lock.unlock();
    }
}