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

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

Introduction

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

Prototype

Object getProperty(String key);

Source Link

Document

Gets a property from the configuration.

Usage

From source file:org.apache.marmotta.loader.core.test.CLITest.java

@Test
public void testPluginBoolean() throws ParseException {
    Configuration cfg = MarmottaLoader.parseOptions(new String[] { "-E", "-f", "file1.ttl" });

    Assert.assertNotNull(cfg.getProperty("backend.dummy.enabled"));

    Assert.assertTrue(cfg.getBoolean("backend.dummy.enabled"));

}

From source file:org.apache.marmotta.loader.core.test.CLITest.java

@Test
public void testPluginSingle() throws ParseException {
    Configuration cfg = MarmottaLoader.parseOptions(new String[] { "-U", "user1", "-f", "file1.ttl" });

    Assert.assertNotNull(cfg.getProperty("backend.dummy.user"));

    Assert.assertEquals("user1", cfg.getString("backend.dummy.user"));

}

From source file:org.apache.marmotta.platform.core.services.config.ConfigurationServiceImpl.java

/**
 * Initialise the ConfigurationService. Takes the marmotta.home system property from the servlet
 * init parameters/*from   w ww  .  ja va  2  s .c  om*/
 * (web.xml) as bootstrap home. In case a system-config.properties file is found in this
 * directory, the
 * configuration is then loaded from this file. In case a system-config.properties file does not
 * yet exist,
 * the method loads bootstrap settings from the resource default-config.properties in the
 * classpath (in the
 * standard bundling, the file is in the kiwi-core-XXX.jar archive). After loading the
 * configuration, the
 * service initialises the plugin subsystem and the SOLR home directory by unpacking the default
 * configuration
 * if the SOLR configuration does not yet exist.
 * @param override
 */
@Override
public void initialize(String home, Configuration override) {
    lock.writeLock().lock();
    try {
        initialising = true;

        log.info("Apache Marmotta Configuration Service starting up ...");

        if (isTomcat7()) {
            log.info("Apache Marmotta running on Apache Tomcat 7.x");
        } else if (isTomcat6()) {
            log.info("Apache Marmotta running on Apache Tomcat <= 6.x");
        } else if (isJetty7()) {
            log.info("Apache Marmotta running on Jetty 7.x");
        } else if (isJetty6()) {
            log.info("Apache Marmotta running on Jetty <= 6.x");
        } else {
            log.info("Apache Marmotta running on an unknown servlet container");
        }

        setHome(home);

        if (getHome() != null) {
            File f1 = new File(getHome());
            if (!f1.exists()) {
                f1.mkdirs();
            }
            // ensure directory for user configuration files
            File f2 = new File(getHome() + File.separator + DIR_CONFIG);
            if (!f2.exists()) {
                f2.mkdirs();
            }

            // ensure directory for logging messages
            File f3 = new File(getHome() + File.separator + DIR_LOG);
            if (!f3.exists()) {
                f3.mkdirs();
            }

            // ensure directory for importing data
            File f4 = new File(getHome() + File.separator + DIR_IMPORT);
            if (!f4.exists()) {
                f4.mkdirs();
            }

        }

        // the save configuration will be in the  home directory
        try {
            if (getHome() != null) {
                configFileName = getHome() + File.separator + "system-config.properties";
                metaFileName = getHome() + File.separator + "system-meta.properties";

                File configFile = new File(configFileName);
                if (!configFile.exists()) {
                    log.info("creating system configuration in configuration file {}",
                            configFile.getAbsolutePath());
                } else {
                    log.info("reading system configuration from existing configuration file {}",
                            configFile.getAbsolutePath());
                }
                saveConfiguration = new PropertiesConfiguration(configFile);

                File metaFile = new File(metaFileName);
                if (!metaFile.exists()) {
                    log.info("creating system configuration metadata in configuration file {}",
                            metaFile.getAbsolutePath());
                } else {
                    log.info("reading system configuration metadata from existing configuration file {}",
                            metaFile.getAbsolutePath());
                }
                saveMetadata = new PropertiesConfiguration(metaFile);

            } else {
                log.error(
                        "error while initialising configuration: no marmotta.home property given; creating memory-only configuration");
                saveConfiguration = new MapConfiguration(new HashMap<String, Object>());
                saveMetadata = new MapConfiguration(new HashMap<String, Object>());
            }
        } catch (Exception e) {
            log.error("error while initialising configuration file {}: {}; creating memory-only configuration",
                    configFileName, e.getMessage());
            saveConfiguration = new MapConfiguration(new HashMap<String, Object>());
            saveMetadata = new MapConfiguration(new HashMap<String, Object>());
        }
        config = new FallbackConfiguration();
        final ConfigurationInterpolator _int = config.getInterpolator();
        _int.registerLookup("pattern.quote", new StrLookup() {
            @Override
            public String lookup(String key) {
                return Pattern.quote(_int.getDefaultLookup().lookup(key));
            }
        });
        _int.registerLookup("urlencode", new StrLookup() {
            @Override
            public String lookup(String key) {
                try {
                    return URLEncoder.encode(_int.getDefaultLookup().lookup(key), "utf8");
                } catch (UnsupportedEncodingException e) {
                    return _int.getDefaultLookup().lookup(key);
                }
            }
        });
        config.addConfiguration(saveConfiguration, true);

        // load all default-config.properties

        try {
            Enumeration<URL> configs = this.getClass().getClassLoader()
                    .getResources("config-defaults.properties");
            while (configs.hasMoreElements()) {
                URL url = configs.nextElement();
                config.addConfiguration(new PropertiesConfiguration(url));
            }

        } catch (IOException e) {
            log.error("I/O error while loading default configurations", e);
        } catch (ConfigurationException e) {
            log.error("configuration error while loading default configurations", e);
        }

        // legacy support (to be removed)
        try {
            Enumeration<URL> configs = this.getClass().getClassLoader()
                    .getResources("default-config.properties");
            while (configs.hasMoreElements()) {
                URL url = configs.nextElement();
                config.addConfiguration(new PropertiesConfiguration(url));
                log.warn(
                        "found legacy configuration file {}; should be replaced with per-module configuration!",
                        url);
            }

        } catch (IOException e) {
            log.error("I/O error while loading default configurations", e);
        } catch (ConfigurationException e) {
            log.error("configuration error while loading default configurations", e);
        }

        // create the configuration that is responsible for getting metadata about configuration keys in the main
        // configuration; since the keys will be different, we store changes also to the main save configuration
        configDescriptions = new FallbackConfiguration();
        configDescriptions.addConfiguration(saveMetadata, true);
        try {
            Enumeration<URL> configs = this.getClass().getClassLoader()
                    .getResources("config-descriptions.properties");
            while (configs.hasMoreElements()) {
                URL url = configs.nextElement();
                configDescriptions.addConfiguration(new PropertiesConfiguration(url));
            }

        } catch (IOException e) {
            log.error("I/O error while loading configuration descriptions", e);
        } catch (ConfigurationException e) {
            log.error("configuration error while loading configuration descriptions", e);
        }

        // setup home if it is given as system property,
        // the bootstrap configuration is overwritten
        if (getHome() != null) {
            config.setProperty("marmotta.home", getHome());
        }

        // in case override configuration is given, change all settings in the configuration accordingly
        if (override != null) {
            for (Iterator<String> it = override.getKeys(); it.hasNext();) {
                String key = it.next();

                config.setProperty(key, override.getProperty(key));
            }
        }

        save();

        // configuration service is now ready to use
        initialised = true;

        loggingStartEvent.fire(new LoggingStartEvent());

        // this should maybe move to the KiWiPreStartupFilter ...
        initDatabaseConfiguration();

        save();

        log.info("Apache Marmotta Configuration Service: initialisation completed");

        configurationInitEvent.fire(new ConfigurationServiceInitEvent());

    } finally {
        lock.writeLock().unlock();
    }

}

From source file:org.apache.marmotta.platform.core.webservices.modules.ModuleWebService.java

/**
 * Return the configuration of the module identified by the name passed as query argument. The module will be
 * a map containing the values specified in the kiwi-module.properties file of the module.
 *
 * @param moduleName the name of the module for which to return the configuration
 * @return a map with key/value pairs representing the module configuration as contained in kiwi-module.properties
 *//*from w  w w  .ja  v a  2  s  .c  o  m*/
@Path("/module")
@GET
@Produces("application/json")
public Map<String, Object> getConfiguration(@QueryParam("name") String moduleName) {
    Configuration cfg = null;
    try {
        cfg = moduleService.getModuleConfiguration(URLDecoder.decode(moduleName, "UTF-8")).getConfiguration();
    } catch (UnsupportedEncodingException e) {
        return null;
    }
    if (cfg != null) {
        Map<String, Object> result = new HashMap<String, Object>();
        for (Iterator<String> it = cfg.getKeys(); it.hasNext();) {
            String key = it.next();
            result.put(key, cfg.getProperty(key));
        }
        return result;
    } else
        return null;
}

From source file:org.apache.marmotta.platform.security.services.SecurityServiceImpl.java

/**
 * Load a pre-configured security profile from the classpath. When calling this method, the service will
 * look for files called security-profile.<name>.properties and replace all existing security constraints by
 * the new security constraints.//  w w  w. j a v a2 s  .  c o  m
 *
 * @param profile
 */
@Override
public void loadSecurityProfile(String profile) {
    profileLoading = true;

    LinkedHashSet<String> profiles = new LinkedHashSet<String>();
    Configuration securityConfig = loadProfile(profile, profiles);
    if (securityConfig != null) {
        // remove all configuration keys that define permissions or restrictions
        for (String type : Lists.newArrayList("permission", "restriction")) {
            // determine the names of constraints that are configured
            for (String key : configurationService.listConfigurationKeys("security." + type)) {
                configurationService.removeConfiguration(key);
            }
        }

        for (Iterator<String> keys = securityConfig.getKeys(); keys.hasNext();) {
            String key = keys.next();

            configurationService.setConfigurationWithoutEvent(key, securityConfig.getProperty(key));
        }

        configurationService.setConfigurationWithoutEvent("security.configured", true);
    }

    profileLoading = false;

    initSecurityConstraints();
}

From source file:org.apache.marmotta.platform.security.services.SecurityServiceImpl.java

private Configuration loadProfile(String profile, LinkedHashSet<String> profiles) {
    URL securityConfigUrl = this.getClass().getClassLoader()
            .getResource("security-profile." + profile + ".properties");
    if (securityConfigUrl != null) {
        try {/* www.  ja  v a  2s  .c  om*/
            Configuration securityConfig = null;
            securityConfig = new PropertiesConfiguration(securityConfigUrl);

            if (securityConfig.containsKey("security.profile.base")) {
                final String baseP = securityConfig.getString("security.profile.base");
                if (profiles.contains(baseP)) {
                    log.warn("Cycle in security configuration detected: {} -> {}", profiles, baseP);
                    return securityConfig;
                } else {
                    profiles.add(baseP);
                    final Configuration baseProfile = loadProfile(baseP, profiles);

                    for (Iterator<String> keys = securityConfig.getKeys(); keys.hasNext();) {
                        String key = keys.next();

                        baseProfile.setProperty(key, securityConfig.getProperty(key));
                    }
                    return baseProfile;
                }
            } else {
                return securityConfig;
            }
        } catch (ConfigurationException e) {
            log.error("error parsing security-profile.{}.properties file at {}: {}",
                    new Object[] { profile, securityConfigUrl, e.getMessage() });
        }

    }
    return null;
}

From source file:org.apache.metron.helpers.topology.SettingsLoader.java

public static Map<String, JSONObject> loadKnownHosts(String config_path)
        throws ConfigurationException, ParseException {
    Configuration hosts = new PropertiesConfiguration(config_path);

    Iterator<String> keys = hosts.getKeys();
    Map<String, JSONObject> known_hosts = new HashMap<String, JSONObject>();
    JSONParser parser = new JSONParser();

    while (keys.hasNext()) {
        String key = keys.next().trim();
        JSONArray value = (JSONArray) parser.parse(hosts.getProperty(key).toString());
        known_hosts.put(key, (JSONObject) value.get(0));
    }//from ww  w . j a va  2s. com

    return known_hosts;
}

From source file:org.apache.servicecomb.config.archaius.sources.ApolloConfigurationSourceImpl.java

@Override
public boolean isValidSource(Configuration localConfiguration) {
    if (localConfiguration.getProperty(APOLLO_CONFIG_URL_KEY) == null) {
        LOGGER.warn("Apollo configuration source is not configured!");
        return false;
    }//from w ww. j  a  v  a2 s  . c om
    return true;
}

From source file:org.apache.servicecomb.config.archaius.sources.ConfigCenterConfigurationSourceImpl.java

@Override
public boolean isValidSource(Configuration localConfiguration) {
    if (localConfiguration.getProperty(CONFIG_CENTER_URL_KEY) == null) {
        LOGGER.warn("Config Center configuration source is not configured!");
        return false;
    }/*  ww w  .  ja v  a 2  s .c o  m*/
    return true;
}

From source file:org.apache.servicecomb.config.ConfigMapping.java

public static Map<String, Object> getConvertedMap(Configuration config) {
    if (configMap == null) {
        return new LinkedHashMap<>();
    }//from w  w w . j  a va  2  s. com
    Map<String, Object> retMap = new LinkedHashMap<>();
    for (Map.Entry<String, Object> entry : configMap.entrySet()) {
        String key = entry.getKey();
        Object configValue = config.getProperty(key);
        if (configValue != null) {
            if (entry.getValue() instanceof List) {
                @SuppressWarnings("unchecked")
                List<String> newKeys = (List<String>) entry.getValue();
                for (String newKey : newKeys) {
                    retMap.put(newKey, configValue);
                }
            } else {
                String newKey = (String) entry.getValue();
                retMap.put(newKey, configValue);
            }
        }
    }
    return retMap;
}