Search in sources :

Example 26 with Configuration

use of org.apache.commons.configuration.Configuration in project ninja by ninjaframework.

the class MessagesImpl method get.

@Override
public Optional<String> get(String key, Optional<String> language, Object... params) {
    Configuration configuration = getLanguageConfigurationForLocale(language);
    String value = configuration.getString(key);
    if (value != null) {
        MessageFormat messageFormat = getMessageFormatForLocale(value, language);
        return Optional.of(messageFormat.format(params));
    } else {
        return Optional.empty();
    }
}
Also used : Configuration(org.apache.commons.configuration.Configuration) CompositeConfiguration(org.apache.commons.configuration.CompositeConfiguration) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) MessageFormat(java.text.MessageFormat)

Example 27 with Configuration

use of org.apache.commons.configuration.Configuration in project ninja by ninjaframework.

the class MessagesImpl method loadAllMessageFilesForRegisteredLanguages.

/**
     * Does all the loading of message files.
     * 
     * Only registered messages in application.conf are loaded.
     * 
     */
private Map<String, Configuration> loadAllMessageFilesForRegisteredLanguages() {
    Map<String, Configuration> langToKeyAndValuesMappingMutable = Maps.newHashMap();
    // Load default messages:
    Configuration defaultLanguage = loadLanguageConfiguration("conf/messages.properties");
    // Everything else does not make much sense.
    if (defaultLanguage == null) {
        throw new RuntimeException("Did not find conf/messages.properties. Please add a default language file.");
    } else {
        langToKeyAndValuesMappingMutable.put("", defaultLanguage);
    }
    // Get the languages from the application configuration.
    String[] applicationLangs = ninjaProperties.getStringArray(NinjaConstant.applicationLanguages);
    // We'll use the default messages.properties file.
    if (applicationLangs == null) {
        return ImmutableMap.copyOf(langToKeyAndValuesMappingMutable);
    }
    // Load each language into the HashMap containing the languages:
    for (String lang : applicationLangs) {
        // First step: Load complete language eg. en-US
        Configuration configuration = loadLanguageConfiguration(String.format("conf/messages_%s.properties", lang));
        Configuration configurationLangOnly = null;
        // Overwritten by the default languages.
        if (lang.contains("-")) {
            // get the lang
            String langOnly = lang.split("-")[0];
            // And load the configuraion
            configurationLangOnly = loadLanguageConfiguration(String.format("conf/messages_%s.properties", langOnly));
        }
        // it should be there propably.
        if (configuration == null) {
            logger.info("Did not find conf/messages_{}.properties but it was specified in application.conf. Using default language instead.", lang);
        } else {
            // add new language, but combine with default language if stuff
            // is missing...
            CompositeConfiguration compositeConfiguration = new CompositeConfiguration();
            // Add eg. "en-US"
            compositeConfiguration.addConfiguration(configuration);
            // Add eg. "en"
            if (configurationLangOnly != null) {
                compositeConfiguration.addConfiguration(configurationLangOnly);
            }
            // Add messages.conf (default pack)
            compositeConfiguration.addConfiguration(defaultLanguage);
            // and add the composed configuration to the hashmap with the
            // mapping.
            langToKeyAndValuesMappingMutable.put(lang, (Configuration) compositeConfiguration);
        }
    }
    return ImmutableMap.copyOf(langToKeyAndValuesMappingMutable);
}
Also used : Configuration(org.apache.commons.configuration.Configuration) CompositeConfiguration(org.apache.commons.configuration.CompositeConfiguration) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) CompositeConfiguration(org.apache.commons.configuration.CompositeConfiguration)

Example 28 with Configuration

use of org.apache.commons.configuration.Configuration in project ninja by ninjaframework.

the class MessagesImpl method getLanguageConfigurationForLocale.

/**
     * Retrieves the matching language file from an arbitrary one or two part
     * locale String ("en-US", or "en" or "de"...).
     * <p>
     * 
     * @param language
     *            A two or one letter language code such as "en-US" or "en" or
     *            "en-US,en;q=0.8,de;q=0.6".
     * @return The matching configuration from the hashmap. Or the default
     *         mapping if no one has been found.
     */
private Configuration getLanguageConfigurationForLocale(Optional<String> language) {
    // if language is null we return the default language.
    if (!language.isPresent()) {
        return langToKeyAndValuesMapping.get("");
    }
    // Check if we get a registered mapping for the language input string.
    // At that point the language may be either language-country or only country.
    // extract multiple languages from Accept-Language header
    String[] languages = language.get().split(",");
    for (String l : languages) {
        l = l.trim();
        // Ignore the relative quality factor in Accept-Language header
        if (l.contains(";")) {
            l = l.split(";")[0];
        }
        Configuration configuration = langToKeyAndValuesMapping.get(l);
        if (configuration != null) {
            return configuration;
        }
        // search only for the language "en".
        if (l.contains("-")) {
            String[] array = l.split("-");
            String languageWithoutCountry = array[0];
            // Modify country code to upper case for IE and Firefox
            if (array.length > 1) {
                String country = array[1];
                String languageWithUpperCaseCountry = languageWithoutCountry + "-" + country.toUpperCase();
                configuration = langToKeyAndValuesMapping.get(languageWithUpperCaseCountry);
                if (configuration != null) {
                    return configuration;
                }
            }
            configuration = langToKeyAndValuesMapping.get(languageWithoutCountry);
            if (configuration != null) {
                return configuration;
            }
        }
    }
    // Oops. Nothing found. We return the default language (by convention guaranteed to work).
    return langToKeyAndValuesMapping.get("");
}
Also used : Configuration(org.apache.commons.configuration.Configuration) CompositeConfiguration(org.apache.commons.configuration.CompositeConfiguration) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration)

Example 29 with Configuration

use of org.apache.commons.configuration.Configuration in project blueprints by tinkerpop.

the class GraphFactoryTest method testOpenInMemoryTinkerGraphViaApacheConfig.

public void testOpenInMemoryTinkerGraphViaApacheConfig() {
    final Configuration conf = new BaseConfiguration();
    conf.setProperty("blueprints.graph", "com.tinkerpop.blueprints.impls.tg.TinkerGraph");
    final Graph g = GraphFactory.open(conf);
    assertNotNull(g);
    assertTrue(g instanceof TinkerGraph);
}
Also used : BaseConfiguration(org.apache.commons.configuration.BaseConfiguration) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) Configuration(org.apache.commons.configuration.Configuration) BaseConfiguration(org.apache.commons.configuration.BaseConfiguration) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph)

Example 30 with Configuration

use of org.apache.commons.configuration.Configuration in project opennms by OpenNMS.

the class DefaultOtrsConfigDao method getProperties.

/**
	 * Retrieves the properties defined in the otrs.properties file.
	 * 
	 * @return a
	 *         <code>java.util.Properties object containing otrs plugin defined properties
	 * @throws IOException 
	 */
private Configuration getProperties() {
    Configuration config = new PropertiesConfiguration();
    String propsFile = null;
    try {
        propsFile = new File(new File(System.getProperty("opennms.home"), "etc"), "otrs.properties").getCanonicalPath();
        LOG.debug("loading properties from: {}", propsFile);
        config = new PropertiesConfiguration(propsFile);
    } catch (final ConfigurationException e) {
        LOG.error("Unable to load properties from {}", propsFile, e);
    } catch (final IOException e) {
        LOG.error("Exception when trying to find OTRS configuration properties from {}", propsFile, e);
    }
    return config;
}
Also used : Configuration(org.apache.commons.configuration.Configuration) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) ConfigurationException(org.apache.commons.configuration.ConfigurationException) IOException(java.io.IOException) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) File(java.io.File)

Aggregations

Configuration (org.apache.commons.configuration.Configuration)169 Test (org.junit.Test)42 PropertiesConfiguration (org.apache.commons.configuration.PropertiesConfiguration)39 ZapXmlConfiguration (org.zaproxy.zap.utils.ZapXmlConfiguration)23 Test (org.testng.annotations.Test)22 File (java.io.File)13 AbstractConfiguration (org.apache.commons.configuration.AbstractConfiguration)13 MidpointConfiguration (com.evolveum.midpoint.common.configuration.api.MidpointConfiguration)11 Properties (java.util.Properties)10 AtlasException (org.apache.atlas.AtlasException)9 ZkUtils (kafka.utils.ZkUtils)8 IndexLoadingConfigMetadata (com.linkedin.pinot.common.metadata.segment.IndexLoadingConfigMetadata)7 HashMap (java.util.HashMap)7 CompositeConfiguration (org.apache.commons.configuration.CompositeConfiguration)7 ArrayList (java.util.ArrayList)6 AtlasClient (org.apache.atlas.AtlasClient)6 BeforeClass (org.testng.annotations.BeforeClass)6 IOException (java.io.IOException)5 SimpleEntry (java.util.AbstractMap.SimpleEntry)5 ConfigurationException (org.apache.commons.configuration.ConfigurationException)5