Example usage for org.apache.commons.configuration SubnodeConfiguration getString

List of usage examples for org.apache.commons.configuration SubnodeConfiguration getString

Introduction

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

Prototype

public String getString(String key) 

Source Link

Usage

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

/**
 * Read the Server settings from a INI Configuration file.
 *
 * @param configReader The initialized configuration reader to read
 *                     the server configuration from file.
 * @return True if everything was read in correctly otherwise false.
 *//*  w w w . j ava 2 s.  com*/
@SuppressWarnings("unchecked")
protected boolean readServersFromConfig(final HierarchicalINIConfiguration configReader) {
    final Map<String, ServerConfiguration> servers = new HashMap<String, ServerConfiguration>();

    // Read in and process the servers
    if (!configReader.getRootNode().getChildren("servers").isEmpty()) {
        final SubnodeConfiguration serversNode = configReader.getSection("servers");
        for (final Iterator<String> it = serversNode.getKeys(); it.hasNext();) {
            final String key = it.next();

            // Find the prefix (aka server name) on urls
            if (key.endsWith(".url")) {
                String prefix = key.substring(0, key.length() - ".url".length());

                final String name = prefix.substring(0, prefix.length() - 1);
                final String url = serversNode.getString(prefix + ".url");
                final String username = serversNode.getString(prefix + ".username");

                // Check that a url was specified
                if (url == null) {
                    command.printError(ClientUtilities.getMessage("NO_SERVER_URL_MSG", name), false);
                    return false;
                }

                // Create the Server Configuration
                final ServerConfiguration serverConfig = new ServerConfiguration();
                serverConfig.setName(name);
                serverConfig.setUrl(url);
                serverConfig.setUsername(username);

                servers.put(name, serverConfig);
            }
            // Just the default server name
            else if (key.equals(Constants.DEFAULT_SERVER_NAME)) {
                // Create the Server Configuration
                final ServerConfiguration serverConfig = new ServerConfiguration();
                serverConfig.setName(key);
                serverConfig.setUrl(serversNode.getString(key));
                serverConfig.setUsername(serversNode.getString(key + "..username"));

                servers.put(Constants.DEFAULT_SERVER_NAME, serverConfig);
            }
        }
    } else {
        // Add the default server config to the list of server configurations
        final ServerConfiguration config = new ServerConfiguration();
        config.setName(Constants.DEFAULT_SERVER_NAME);
        config.setUrl("http://localhost:8080/TopicIndex/");
        servers.put(Constants.DEFAULT_SERVER_NAME, config);
    }

    // Add the servers to the client configuration
    clientConfig.setServers(servers);
    return true;
}

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

/**
 * Read the Zanata Server settings from a INI Configuration file.
 *
 * @param configReader The initialized configuration reader to read
 *                     the server configuration from file.
 * @return True if everything was read in correctly otherwise false.
 *//* www  . j  a  va2s.  co m*/
@SuppressWarnings("unchecked")
protected boolean readZanataDetailsFromConfig(final HierarchicalINIConfiguration configReader) {
    // Read in the zanata server information
    final Map<String, ZanataServerConfiguration> zanataServers = new HashMap<String, ZanataServerConfiguration>();

    // Read in and process the servers
    if (!configReader.getRootNode().getChildren("zanata").isEmpty()) {
        final SubnodeConfiguration serversNode = configReader.getSection("zanata");
        for (final Iterator<String> it = serversNode.getKeys(); it.hasNext();) {
            final String key = it.next();

            // Find the prefix (aka server name) on urls
            if (key.endsWith(".url")) {
                String prefix = key.substring(0, key.length() - ".url".length());

                final String name = prefix.substring(0, prefix.length() - 1);
                final String url = serversNode.getString(prefix + ".url");
                final String username = serversNode.getString(prefix + ".username");
                final String token = serversNode.getString(prefix + ".key");
                final boolean cache = serversNode.getBoolean(prefix + ".cache", true);

                // Check that a url was specified
                if (url == null) {
                    command.printError(ClientUtilities.getMessage("NO_ZANATA_SERVER_URL_MSG", name), false);
                    return false;
                }

                // Create the Server Configuration
                final ZanataServerConfiguration serverConfig = new ZanataServerConfiguration();
                serverConfig.setName(name);
                serverConfig.setUrl(url);
                serverConfig.setUsername(username);
                serverConfig.setToken(token);
                serverConfig.setCache(cache);

                zanataServers.put(name, serverConfig);
            }
        }
    }

    // Setup the default zanata server
    if (zanataServers.containsKey(Constants.DEFAULT_SERVER_NAME)
            && !zanataServers.get(Constants.DEFAULT_SERVER_NAME).getUrl().matches("^(http://|https://).*")) {
        if (!zanataServers.containsKey(zanataServers.get(Constants.DEFAULT_SERVER_NAME).getUrl())) {
            command.printError(ClientUtilities.getMessage("NO_ZANATA_SERVER_FOUND_FOR_DEFAULT_SERVER_MSG"),
                    false);
            return false;
        } else {
            final ZanataServerConfiguration defaultConfig = zanataServers.get(Constants.DEFAULT_SERVER_NAME);
            final ZanataServerConfiguration config = zanataServers.get(defaultConfig.getUrl());
            defaultConfig.setUrl(config.getUrl());
            defaultConfig.setUsername(config.getUsername());
            defaultConfig.setToken(config.getToken());
            defaultConfig.setUsername(config.getUsername());
            defaultConfig.setCache(config.useCache());
        }
    }

    clientConfig.setZanataServers(zanataServers);
    return true;
}

From source file:sf.net.experimaestro.tasks.ServerTask.java

private ConstraintSecurityHandler getSecurityHandler() {
    // -- Security
    // From//from   w w  w  .  ja v  a 2  s  . c  o  m
    // http://docs.codehaus.org/display/JETTY/How+to+Configure+Security+with+Embedded+Jetty

    Constraint constraint = new Constraint();
    constraint.setName(Constraint.__BASIC_AUTH);
    constraint.setRoles(new String[] { "user" });
    constraint.setAuthenticate(true);

    ConstraintMapping cm = new ConstraintMapping();
    cm.setConstraint(constraint);
    cm.setPathSpec("/*");

    String passwordProperty = configuration.getString("passwords");
    final HashLoginService loginService;
    if (passwordProperty != null) {
        File passwordFile = new File(passwordProperty);
        loginService = new HashLoginService(XPM_REALM, passwordFile.getAbsolutePath());
    } else {
        loginService = new HashLoginService(XPM_REALM);
    }

    // Read passwords
    final SubnodeConfiguration passwords = configuration.getSection("passwords");
    final Iterator keys = passwords.getKeys();
    while (keys.hasNext()) {
        String user = (String) keys.next();
        final String[] fields = passwords.getString(user).split("\\s*,\\s*", 2);
        final String[] roles = fields[1].split("\\s*,\\s*");

        LOGGER.info("Adding user %s", user);
        loginService.putUser(user, new Password(fields[0]), roles);
    }

    ConstraintSecurityHandler csh = new ConstraintSecurityHandler();
    csh.setRealmName(XPM_REALM);
    csh.setAuthenticator(new BasicAuthenticator());
    csh.setConstraintMappings(new ConstraintMapping[] { cm });
    csh.addConstraintMapping(cm);
    csh.setLoginService(loginService);
    return csh;
}

From source file:uk.q3c.krail.core.config.InheritingConfigurationTest.java

@Test
public void getSection() throws ConfigurationException {

    // given//from www.  j  a v a 2 s .  c  om
    config1 = config("config1.ini");
    config2 = config("config2.ini");
    String key1 = "a";
    // when
    configuration.addConfiguration(config1);
    configuration.addConfiguration(config2);
    SubnodeConfiguration section = configuration.getSection(key1);
    // then
    assertThat(section).isNotNull();
    assertThat(section.getString("k1")).isEqualTo("2-1");
    assertThat(section.getString("k2")).isEqualTo("2-2");
}