Example usage for org.apache.commons.configuration HierarchicalINIConfiguration getSections

List of usage examples for org.apache.commons.configuration HierarchicalINIConfiguration getSections

Introduction

In this page you can find the example usage for org.apache.commons.configuration HierarchicalINIConfiguration getSections.

Prototype

public Set getSections() 

Source Link

Document

Return a set containing the sections in this ini configuration.

Usage

From source file:ee.ria.xroad.signer.tokenmanager.module.ModuleConf.java

private static void reload(String fileName) throws Exception {
    log.trace("Loading module configuration from '{}'", fileName);

    MODULES.clear();// w  ww  .  j av a 2  s .  com
    MODULES.put(SoftwareModuleType.TYPE, new SoftwareModuleType());

    HierarchicalINIConfiguration conf = new HierarchicalINIConfiguration(fileName);

    for (String uid : conf.getSections()) {
        if (StringUtils.isBlank(uid)) {
            log.error("No UID specified for module, skipping...");

            continue;
        }

        try {
            parseSection(uid, conf.getSection(uid));
        } catch (ConfigurationRuntimeException e) {
            log.error("Parse section failed with", e);
        }
    }
}

From source file:de.clusteval.framework.repository.config.RepositoryConfig.java

/**
 * This method parses a repository configuration from the file at the given
 * absolute path./*from  w ww.ja  v  a  2s.c o m*/
 * 
 * <p>
 * A repository configuration contains several sections and possible
 * options:
 * <ul>
 * <li><b>[mysql]</b></li>
 * <ul>
 * <li><b>host</b>: The ip address of the mysql host server.</li>
 * <li><b>database</b>: The mysql database name.</li>
 * <li><b>user</b>: The username used to connect to the database.</li>
 * <li><b>password</b>: The mysql password used to connect to the database.
 * The password is prompted from the console and not parsed from the file.</li>
 * </ul>
 * <li><b>[threading]</b></li>
 * <li><b>NameOfTheThreadSleepTime</b>: Sleeping time of the thread
 * 'NameOfTheThread'. This option can be used to control the frequency, with
 * which the threads check for changes on the filesystem.</li> </ul>
 * 
 * @param absConfigPath
 *            The absolute path of the repository configuration file.
 * @return The parsed repository configuration.
 * @throws RepositoryConfigNotFoundException
 * @throws RepositoryConfigurationException
 */
public static RepositoryConfig parseFromFile(final File absConfigPath)
        throws RepositoryConfigNotFoundException, RepositoryConfigurationException {
    if (!absConfigPath.exists())
        throw new RepositoryConfigNotFoundException(
                "Repository config \"" + absConfigPath + "\" does not exist!");

    Logger log = LoggerFactory.getLogger(RepositoryConfig.class);

    log.debug("Parsing repository config \"" + absConfigPath + "\"");

    try {

        HierarchicalINIConfiguration props = new HierarchicalINIConfiguration(absConfigPath);
        props.setThrowExceptionOnMissing(true);

        boolean usesMysql = false;
        MysqlConfig mysqlConfig = null;

        if (props.getSections().contains("mysql")
                && !ClustevalBackendServer.getBackendServerConfiguration().getNoDatabase()) {
            usesMysql = true;
            String mysqlUsername, mysqlDatabase, mysqlHost;
            SubnodeConfiguration mysql = props.getSection("mysql");
            mysqlUsername = mysql.getString("user");

            mysqlDatabase = mysql.getString("database");
            mysqlHost = mysql.getString("host");
            mysqlConfig = new MysqlConfig(usesMysql, mysqlUsername, mysqlDatabase, mysqlHost);
        } else
            mysqlConfig = new MysqlConfig(usesMysql, "", "", "");

        Map<String, Long> threadingSleepTimes = new HashMap<String, Long>();

        if (props.getSections().contains("threading")) {
            SubnodeConfiguration threading = props.getSection("threading");
            Iterator<String> it = threading.getKeys();
            while (it.hasNext()) {
                String key = it.next();
                if (key.endsWith("SleepTime")) {
                    String subKey = key.substring(0, key.indexOf("SleepTime"));
                    try {
                        threadingSleepTimes.put(subKey, threading.getLong(key));
                    } catch (Exception e) {
                        // in case anything goes wrong, we just ignore this
                        // option
                        e.printStackTrace();
                    }
                }
            }
        }

        return new RepositoryConfig(mysqlConfig, threadingSleepTimes);
    } catch (ConfigurationException e) {
        throw new RepositoryConfigurationException(e.getMessage());
    } catch (NoSuchElementException e) {
        throw new RepositoryConfigurationException(e.getMessage());
    }
}

From source file:com.baasbox.db.DbHelper.java

public static void populateConfiguration() throws IOException, ConfigurationException {
    BaasBoxLogger.info("Load initial configuration...");
    InputStream is;/*from  ww w . ja v  a2s  .co  m*/
    if (Play.application().isProd())
        is = Play.application().resourceAsStream(CONFIGURATION_FILE_NAME);
    else
        is = new FileInputStream(Play.application().getFile("conf/" + CONFIGURATION_FILE_NAME));
    HierarchicalINIConfiguration c = new HierarchicalINIConfiguration();
    c.setEncoding("UTF-8");
    c.load(is);
    CharSequence doubleDot = "..";
    CharSequence dot = ".";

    Set<String> sections = c.getSections();
    for (String section : sections) {
        Class en = PropertiesConfigurationHelper.CONFIGURATION_SECTIONS.get(section);
        if (en == null) {
            BaasBoxLogger.warn(section + " is not a valid configuration section, it will be skipped!");
            continue;
        }
        SubnodeConfiguration subConf = c.getSection(section);
        Iterator<String> it = subConf.getKeys();
        while (it.hasNext()) {
            String key = (it.next());
            Object value = subConf.getString(key);
            key = key.replace(doubleDot, dot);//bug on the Apache library: if the key contain a dot, it will be doubled!
            try {
                BaasBoxLogger.info("Setting " + value + " to " + key);
                PropertiesConfigurationHelper.setByKey(en, key, value);
            } catch (Exception e) {
                BaasBoxLogger.warn("Error loading initial configuration: Section " + section + ", key: " + key
                        + ", value: " + value, e);
            }
        }
    }
    is.close();
    BaasBoxLogger.info("...done");
}

From source file:com.bist.elasticsearch.jetty.security.AuthorizationConfigurationLoader.java

protected QueryConstraints loadIni(HierarchicalINIConfiguration iniConfiguration,
        QueryConstraints queryConstraints) {
    for (String section : iniConfiguration.getSections()) {
        SubnodeConfiguration subnodeConfiguration = iniConfiguration.getSection(section);
        switch (section) {
        case "Public":
            for (String query : subnodeConfiguration.getStringArray("Queries")) {
                queryConstraints.addPublicQuery(query);
            }//from ww  w .jav a  2  s .c  o  m
            break;
        case "Authenticated":
            for (String query : subnodeConfiguration.getStringArray("Queries")) {
                queryConstraints.addAuthenticatedQuery(query);
            }
            break;
        default:
            loadIndiceAuthZ(queryConstraints, section, subnodeConfiguration);
            break;
        }
    }
    { // make it atomic
        return queryConstraints;

    }
}

From source file:iddb.web.security.SecurityConfig.java

private SecurityConfig() {
    try {//from   w w  w  .j av a2 s  . co m
        HierarchicalINIConfiguration configFile = new HierarchicalINIConfiguration(
                this.getClass().getClassLoader().getResource("security.properties"));
        for (Object section : configFile.getSections()) {
            SubnodeConfiguration node = configFile.getSection((String) section);
            Map<String, String> map = new LinkedHashMap<String, String>();
            for (@SuppressWarnings("unchecked")
            Iterator<Object> it = node.getKeys(); it.hasNext();) {
                String key = it.next().toString();
                if (log.isTraceEnabled()) {
                    log.trace("Loading '{}' with value '{}' on section '{}'",
                            new String[] { key, node.getString(key), section.toString() });
                }
                map.put(key, node.getString(key));
            }
            config.put(section.toString(), map);
        }
    } catch (ConfigurationException e) {
        log.error(e.getMessage());
    }
}

From source file:com.yfiton.core.Yfiton.java

private Map<String, Map<String, String>> loadPreferences(HierarchicalINIConfiguration configuration,
        Notifier notifier) {// w  ww .  j a v  a2  s  .c  o  m
    Set<String> sections = configuration.getSections();

    return sections.stream().filter(isEqual(null).negate().and(section -> notifier.getKey().equals(section)))
            .collect(Collectors.toMap(Function.identity(),
                    section -> configuration.getSection(section).getRootNode().getChildren().stream().collect(
                            Collectors.toMap(ConfigurationNode::getName, node -> (String) node.getValue()))));
}

From source file:eu.itesla_project.dymola.DymolaAdaptersMatParamsWriter.java

public DymolaAdaptersMatParamsWriter(HierarchicalINIConfiguration configuration) {
    if (configuration == null) {
        throw new RuntimeException("null config");
    }/*from w  w  w .  j a  v a2s.  c  o  m*/
    this.configuration = configuration;
    //this below will simply log parameters ..
    for (String section : configuration.getSections()) {
        SubnodeConfiguration node = configuration.getSection(section);
        List<String> paramsSummary = StreamSupport
                .stream(Spliterators.spliteratorUnknownSize(node.getKeys(), Spliterator.ORDERED), false)
                .map(p -> p + "=" + node.getString(p)).collect(Collectors.<String>toList());
        LOGGER.info("index {}: {}", section, paramsSummary);
    }
}

From source file:au.org.intersect.dms.instrument.harvester.WindowsIniLogParser.java

private String parseLogIniFile(HierarchicalINIConfiguration iniConf) {
    StringTemplate template = ST_HARVESTERS.getInstanceOf(templateFilePath);
    Map<String, Map<String, Object>> values = new HashMap<String, Map<String, Object>>();
    for (String sectionName : (Set<String>) iniConf.getSections()) {
        Map<String, Object> sectionMap = new HashMap<String, Object>();
        SubnodeConfiguration section = iniConf.getSection(sectionName);
        if (sectionChecker == null || sectionChecker.includeSection(section)) {
            for (Iterator it = section.getRootNode().getChildren().iterator(); it.hasNext();) {
                ConfigurationNode node = (ConfigurationNode) it.next();
                sectionMap = parseFields(sectionName, sectionMap, node);
            }/*from   w w w .  j a  v a  2  s  .  c  o  m*/
            if (!sectionMap.isEmpty()) {
                values.put(AbstractHarvester.toXML(sectionName), sectionMap);
            }
        }
    }
    template.setAttribute("sections", values);
    return template.toString();
}

From source file:ee.ria.xroad.common.SystemPropertiesLoader.java

private void load(FileWithSections file) {
    try {/* w w w  . j  ava 2s .co m*/
        // turn off list delimiting (before parsing),
        // otherwise we lose everything after first ","
        // in loadSection/sec.getString(key)
        HierarchicalINIConfiguration ini = new HierarchicalINIConfiguration();
        ini.setDelimiterParsingDisabled(true);
        ini.load(file.getName());

        for (String sectionName : ini.getSections()) {
            if (isEmpty(file.getSections()) || contains(file.getSections(), sectionName)) {
                loadSection(sectionName, ini.getSection(sectionName));
            }
        }
    } catch (ConfigurationException e) {
        log.warn("Error while loading {}: {}", file.getName(), e);
    }
}

From source file:io.datalayer.conf.HierarchicalIniConfigurationTest.java

/**
 * Tests whether the specified configuration contains exactly the expected
 * sections./*from www  .  j a  v a2s.  c om*/
 *
 * @param config the configuration to check
 * @param expected an array with the expected sections
 */
private void checkSectionNames(HierarchicalINIConfiguration config, String[] expected) {
    Set<String> sectionNames = config.getSections();
    Iterator<String> it = sectionNames.iterator();
    for (int idx = 0; idx < expected.length; idx++) {
        assertEquals("Wrong section at " + idx, expected[idx], it.next());
    }
    assertFalse("Too many sections", it.hasNext());
}