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();
}
}
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);
}
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("");
}
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);
}
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;
}
Aggregations