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

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

Introduction

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

Prototype

public HierarchicalINIConfiguration(URL url) throws ConfigurationException 

Source Link

Document

Create and load the ini configuration from the given url.

Usage

From source file:org.apache.tamaya.commons.IniConfigurationFormat.java

@Override
public ConfigurationData readConfiguration(String name, InputStream inputStream) {
    PropertyValue data = PropertyValue.createObject();
    data.setMeta("name", name);
    try {//  ww  w. j  ava 2  s .c o  m
        HierarchicalINIConfiguration commonIniConfiguration;
        File file = new File(name);
        if (file.exists()) {
            commonIniConfiguration = new HierarchicalINIConfiguration(file);
        } else {
            commonIniConfiguration = new HierarchicalINIConfiguration(new URL(name));
        }
        for (String section : commonIniConfiguration.getSections()) {
            SubnodeConfiguration sectionConfig = commonIniConfiguration.getSection(section);
            PropertyValue sectionNode = ((ObjectValue) data).getOrSetValue(section,
                    () -> PropertyValue.createObject(section));
            Map<String, String> properties = new HashMap<>();
            Iterator<String> keyIter = sectionConfig.getKeys();
            while (keyIter.hasNext()) {
                String key = keyIter.next();
                ((ObjectValue) sectionNode).setValue(key, sectionConfig.getString(key));
            }
        }
    } catch (Exception e) {
        throw new ConfigException("Failed to parse ini-file format from " + name, e);
    }
    return new ConfigurationData(name, this, data);
}

From source file:org.apache.tamaya.format.IniConfigurationFormat.java

@Override
public Map<String, Map<String, String>> readConfiguration(URL url) {
    Map<String, Map<String, String>> result = new HashMap<>();
    try {/*from  www . j a  va 2 s.c  o m*/
        HierarchicalINIConfiguration commonIniConfiguration = new HierarchicalINIConfiguration(url);
        for (String section : commonIniConfiguration.getSections()) {
            SubnodeConfiguration sectionConfig = commonIniConfiguration.getSection(section);
            Map<String, String> properties = new HashMap<>();
            Iterator<String> keyIter = sectionConfig.getKeys();
            while (keyIter.hasNext()) {
                String key = keyIter.next();
                properties.put(key, sectionConfig.getString(key));
            }
            result.put(section, properties);
        }
        return result;
    } catch (ConfigurationException e) {
        throw new ConfigException("Failed to parse ini-file format from " + url, e);
    }
}

From source file:org.apache.tamaya.integration.commons.IniConfigurationFormat.java

public ConfigurationData readConfiguration(URL url) {
    ConfigurationDataBuilder builder = ConfigurationDataBuilder.of(url.toString(), this);
    try {/*from w ww . ja  v  a 2  s  .c  om*/
        HierarchicalINIConfiguration commonIniConfiguration = new HierarchicalINIConfiguration(url);
        for (String section : commonIniConfiguration.getSections()) {
            SubnodeConfiguration sectionConfig = commonIniConfiguration.getSection(section);
            Map<String, String> properties = new HashMap<>();
            Iterator<String> keyIter = sectionConfig.getKeys();
            while (keyIter.hasNext()) {
                String key = keyIter.next();
                properties.put(key, sectionConfig.getString(key));
            }
            builder.addProperties(section, properties);
        }
    } catch (ConfigurationException e) {
        throw new ConfigException("Failed to parse ini-file format from " + url, e);
    }
    return builder.build();
}

From source file:org.atomictagging.core.configuration.Configuration.java

/**
 * Adds a configuration file to the global configuration.
 * //from w  w  w .  j  a v  a2s.  c o  m
 * @param file
 * @throws Exception
 */
public static void addFile(File file) throws Exception {
    conf.addConfiguration(new HierarchicalINIConfiguration(file));
}

From source file:org.chenillekit.core.services.impl.ConfigurationServiceImpl.java

/**
 * get the configuration from the named resource.
 *
 * @param configurationResource the configuration resource
 * @param mergeWithSysProps     merge the configuration resource with system properties
 *
 * @return the configuration/*from ww  w  .j av  a  2  s  . co m*/
 */
public Configuration getConfiguration(Resource configurationResource, boolean mergeWithSysProps) {
    Configuration configuration;

    assert configurationResource != null;

    if (!configurationResource.exists())
        throw new RuntimeException(
                String.format("configuration resource '%s' not found", configurationResource.toString()));

    try {
        if (configurationResource.getFile().endsWith(".xml"))
            configuration = new XMLConfiguration(configurationResource.toURL());
        else if (configurationResource.getFile().endsWith(".properties"))
            configuration = new PropertiesConfiguration(configurationResource.toURL());
        else if (configurationResource.getFile().endsWith(".plist"))
            configuration = new PropertyListConfiguration(configurationResource.toURL());
        else if (configurationResource.getFile().endsWith(".plist"))
            configuration = new PropertyListConfiguration(configurationResource.toURL());
        else if (configurationResource.getFile().endsWith(".ini"))
            configuration = new HierarchicalINIConfiguration(configurationResource.toURL());
        else
            throw new RuntimeException(String.format("cant resolve configuration type of resource '%s'",
                    configurationResource.toString()));

        if (mergeWithSysProps) {
            CombinedConfiguration mergedConfiguration = new CombinedConfiguration();
            mergedConfiguration.addConfiguration((AbstractConfiguration) configuration);
            mergedConfiguration.addConfiguration((AbstractConfiguration) getConfiguration());

            configuration = mergedConfiguration;
        }
    } catch (ConfigurationException e) {
        throw new RuntimeException(e);
    }

    return configuration;
}

From source file:org.davidmason.zayf.config.ConfigLoader.java

private List<ServerInfo> getServerList() throws ConfigurationException {
    HierarchicalINIConfiguration config = new HierarchicalINIConfiguration(userConfig);
    SubnodeConfiguration serverConfig = config.getSection("servers");
    DataConfiguration dataConfig = new DataConfiguration(serverConfig);
    List<String> prefixes = findPrefixes(dataConfig);
    return findServerInfoByPrefixes(dataConfig, prefixes);
}

From source file:org.davidmason.zayf.rest.ServerProxyImpl.java

private void loadZanataUserConfig() {
    File userConfig = new File(System.getProperty("user.home"), DEFAULT_CONFIG_LOCATION);
    System.out.println("Config file: " + userConfig.getAbsolutePath());
    try {/* w  w  w . java  2s .co m*/
        HierarchicalINIConfiguration config = new HierarchicalINIConfiguration(userConfig);
        SubnodeConfiguration serverConfig = config.getSection("servers");
        DataConfiguration dataConfig = new DataConfiguration(serverConfig);
        servers = getServerList(dataConfig);
    } catch (ConfigurationException e) {
        System.out.println("Failed to load configuration from " + userConfig.getAbsolutePath());
        e.printStackTrace();
    }
}

From source file:org.jboss.pressgang.ccms.contentspec.client.Client.java

/**
 * Sets the configuration options from the csprocessor.ini configuration file
 *
 * @param location The location of the csprocessor.ini file (eg. /home/&lt;USERNAME&gt;/.config/)
 * @return Returns false if an error occurs otherwise true
 *///from w  w w . j  a  va 2  s . c om
protected boolean setConfigOptions(final String location) {
    final String fixedLocation = ClientUtilities.fixConfigLocation(location);
    final HierarchicalINIConfiguration configReader;

    // Good point to check for a shutdown
    allowShutdownToContinueIfRequested();

    // Checks if the file exists in the specified location
    final File file = new File(location);
    if (file.exists() && !file.isDirectory()) {
        JCommander.getConsole().println(ClientUtilities.getMessage("CONFIG_LOADING_MSG", location));
        // Initialise the configuration reader with the skynet.ini content
        try {
            configReader = new HierarchicalINIConfiguration(fixedLocation);
        } catch (ConfigurationException e) {
            command.printError(ClientUtilities.getMessage("ERROR_INI_NOT_FOUND_MSG"), false);
            return false;
        } catch (Exception e) {
            command.printError(ClientUtilities.getMessage("ERROR_PROCESSING_CONFIG_MSG"), false);
            return false;
        }
    } else if (location.equals(Constants.DEFAULT_CONFIG_LOCATION)) {
        JCommander.getConsole().println(ClientUtilities.getMessage("CONFIG_CREATING_MSG", location));
        firstRun = true;

        // Save the configuration file
        try {
            // Make sure the directory exists
            if (file.getParentFile() != null) {
                // TODO Check that this succeeded
                file.getParentFile().mkdirs();
            }

            // Save the config
            FileUtilities.saveFile(file, ConfigConstants.DEFAULT_CONFIG_FILE, Constants.FILE_ENCODING);
        } catch (IOException e) {
            printError(ClientUtilities.getMessage("ERROR_FAILED_CREATING_CONFIG_MSG"), false);
            return false;
        }
        return setConfigOptions(location);
    } else {
        command.printError(ClientUtilities.getMessage("ERROR_INI_NOT_FOUND_MSG"), false);
        return false;
    }

    // Good point to check for a shutdown
    allowShutdownToContinueIfRequested();

    /* Read in the servers from the config file */
    if (!readServersFromConfig(configReader)) {
        return false;
    }

    // Read in the root directory
    if (!configReader.getRootNode().getChildren("directory").isEmpty()) {
        // Load the root content specs directory
        if (configReader.getProperty("directory.root") != null
                && !configReader.getProperty("directory.root").equals("")) {
            clientConfig.setRootDirectory(
                    ClientUtilities.fixDirectoryPath(configReader.getProperty("directory.root").toString()));
        }

        // Load the install directory
        if (configReader.getProperty("directory.install") != null
                && !configReader.getProperty("directory.install").equals("")) {
            clientConfig.setInstallPath(
                    ClientUtilities.fixDirectoryPath(configReader.getProperty("directory.install").toString()));
        }
    }

    // Read in the publican build options
    if (!configReader.getRootNode().getChildren("publican").isEmpty()) {
        // Load the publican setup values
        if (configReader.getProperty("publican.build..parameters") != null
                && !configReader.getProperty("publican.build..parameters").equals("")) {
            clientConfig
                    .setPublicanBuildOptions(configReader.getProperty("publican.build..parameters").toString());
        }
        if (configReader.getProperty("publican.preview..format") != null
                && !configReader.getProperty("publican.preview..format").equals("")) {
            clientConfig
                    .setPublicanPreviewFormat(configReader.getProperty("publican.preview..format").toString());
        }
        if (configReader.getProperty("publican.common_content") != null
                && !configReader.getProperty("publican.common_content").equals("")) {
            clientConfig.setPublicanCommonContentDirectory(ClientUtilities
                    .fixDirectoryPath(configReader.getProperty("publican.common_content").toString()));
        }
    } else {
        clientConfig.setPublicanBuildOptions(Constants.DEFAULT_PUBLICAN_OPTIONS);
        clientConfig.setPublicanPreviewFormat(Constants.DEFAULT_PUBLICAN_FORMAT);
    }

    // Read in the jDocbook build options
    if (!configReader.getRootNode().getChildren("jDocbook").isEmpty()) {
        // Load the jDocbook setup values
        if (configReader.getProperty("jDocbook.build..parameters") != null
                && !configReader.getProperty("jDocbook.build..parameters").equals("")) {
            clientConfig
                    .setjDocbookBuildOptions(configReader.getProperty("jDocbook.build..parameters").toString());
        }
        if (configReader.getProperty("jDocbook.preview..format") != null
                && !configReader.getProperty("jDocbook.preview..format").equals("")) {
            clientConfig
                    .setjDocbookPreviewFormat(configReader.getProperty("jDocbook.preview..format").toString());
        }
    } else {
        clientConfig.setjDocbookBuildOptions(Constants.DEFAULT_JDOCBOOK_OPTIONS);
        clientConfig.setjDocbookPreviewFormat(Constants.DEFAULT_JDOCBOOK_FORMAT);
    }

    /* Read in the zanata details from the config file */
    if (!readZanataDetailsFromConfig(configReader)) {
        return false;
    }

    // Read in the publishing information
    if (!configReader.getRootNode().getChildren("publish").isEmpty()) {
        // Load the koji hub url
        if (configReader.getProperty("publish.koji..huburl") != null
                && !configReader.getProperty("publish.koji..huburl").equals("")) {
            clientConfig.setKojiHubUrl(configReader.getProperty("publish.koji..huburl").toString());
        }

        // Load the publish command name
        if (configReader.getProperty("publish.command") != null
                && !configReader.getProperty("publish.command").equals("")) {
            clientConfig.setPublishCommand(configReader.getProperty("publish.command").toString());
        }
    }

    // Read in the editor settings
    readEditorSettingsFromConfig(configReader);

    // Read in the defaults
    readDefaultDetailsFromConfig(configReader);

    return true;
}

From source file:org.midonet.midolman.Setup.java

private void run(String[] args) throws Exception {
    Options options = new Options();
    options.addOption("c", "configFile", true, "config file path");
    CommandLineParser parser = new GnuParser();
    CommandLine cl = parser.parse(options, args);
    String configFilePath = cl.getOptionValue('c', "./conf/midolman.conf");

    config = new HierarchicalINIConfiguration(configFilePath);

    args = cl.getArgs();/*  w ww.jav  a2s.  com*/
    if (args.length == 0)
        return;
    String command = args[0].toLowerCase();
    if (command.equals(MIDONET_QDISC_CREATE))
        setupTrafficPriorityQdiscsMidonet();
    else if (command.equals(NOVA_QDISC_CREATE))
        setupTrafficPriorityQdiscsNova();
    else if (command.equals(QDISC_DESTROY))
        removeTrafficPriorityQdiscs();
    else
        System.out.println("Unrecognized command. Exiting.");
}

From source file:org.porquebox.core.app.processors.AppBaconIniParsingProcessor.java

@Override
public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    DeploymentUnit unit = phaseContext.getDeploymentUnit();
    PorqueBoxMetaData metaData = null;/*  w w  w.  j  a  va 2 s .c  o m*/
    File rootFile = null;
    ResourceRoot appRoot = null;
    try {
        VirtualFile appBaconIni = getFile(unit);
        if (appBaconIni == null) {
            return;
        }
        metaData = new PorqueBoxMetaData(new HierarchicalINIConfiguration(appBaconIni.asFileURL()));
        rootFile = metaData.getApplicationRootFile();

        if (rootFile != null) {
            VirtualFile root = VFS.getChild(rootFile.toURI());
            if (!root.exists()) {
                throw new DeploymentUnitProcessingException(
                        "Application root does not exist: " + root.toURL().toExternalForm());
            }

            if (root.exists() && !root.isDirectory()) {
                // Expand the referenced root if it's not a directory (ie
                // .bacon archive)
                final Closeable closable = VFS.mountZipExpanded(root, root, TempFileProviderService.provider());
                final MountHandle mountHandle = new MountHandle(closable);
                appRoot = new ResourceRoot(root, mountHandle);

            } else {
                appRoot = new ResourceRoot(root, null);
            }
            appRoot.putAttachment(Attachments.INDEX_RESOURCE_ROOT, false);
            unit.putAttachment(Attachments.DEPLOYMENT_ROOT, appRoot);
        } else {
            log.infof("Rootless deployment detected: %s", unit.getName());
            DeploymentUtils.markUnitAsRootless(unit);
        }
    } catch (Exception e) {
        throw new DeploymentUnitProcessingException(e);
    }
    unit.putAttachment(PorqueBoxMetaData.ATTACHMENT_KEY, metaData);

    PhpAppMetaData phpAppMetaData = new PhpAppMetaData(unit.getName());
    phpAppMetaData.setRoot(rootFile);
    phpAppMetaData.setEnvironmentName(metaData.getApplicationEnvironment());
    phpAppMetaData.attachTo(unit);
}