Search in sources :

Example 1 with XMLConfiguration

use of org.apache.commons.configuration2.XMLConfiguration in project engine by craftercms.

the class ConfigUtils method readXmlConfiguration.

public static XMLConfiguration readXmlConfiguration(Resource resource, char listDelimiter, Map<String, Lookup> prefixLookups) throws ConfigurationException {
    Parameters params = new Parameters();
    FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<>(XMLConfiguration.class);
    try {
        XMLBuilderParameters xmlParams = params.xml().setURL(resource.getURL()).setListDelimiterHandler(new DefaultListDelimiterHandler(listDelimiter));
        if (MapUtils.isNotEmpty(prefixLookups)) {
            xmlParams = xmlParams.setPrefixLookups(prefixLookups);
        }
        builder.configure(xmlParams);
    } catch (IOException e) {
        throw new ConfigurationException("Unable to get URL of resource " + resource, e);
    }
    return builder.getConfiguration();
}
Also used : XMLConfiguration(org.apache.commons.configuration2.XMLConfiguration) DefaultListDelimiterHandler(org.apache.commons.configuration2.convert.DefaultListDelimiterHandler) Parameters(org.apache.commons.configuration2.builder.fluent.Parameters) XMLBuilderParameters(org.apache.commons.configuration2.builder.fluent.XMLBuilderParameters) FileBasedConfigurationBuilder(org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder) XMLBuilderParameters(org.apache.commons.configuration2.builder.fluent.XMLBuilderParameters) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException) IOException(java.io.IOException)

Example 2 with XMLConfiguration

use of org.apache.commons.configuration2.XMLConfiguration in project engine by craftercms.

the class MultiResourceConfigurationBuilder method getConfiguration.

@Override
public HierarchicalConfiguration getConfiguration() throws ConfigurationException {
    List<HierarchicalConfiguration> configs = new ArrayList<>();
    // Last configurations should be loaded and added first so that they have greater priority.
    logger.info("Loading XML configurations in the order in which the properties will be resolved");
    for (int i = configPaths.length - 1; i >= 0; i--) {
        try {
            Resource resource = resourceLoader.getResource(configPaths[i]);
            if (resource.exists()) {
                Map<String, Lookup> prefixLookups = null;
                if (configDecryptor != null) {
                    prefixLookups = Collections.singletonMap(encryptedValuePrefix, new DecryptionLookup(configDecryptor));
                }
                XMLConfiguration config = ConfigUtils.readXmlConfiguration(resource, configListDelimiter, prefixLookups);
                logger.info("XML configuration loaded from " + resource);
                configs.add(config);
            }
        } catch (Exception e) {
            throw new ConfigurationException("Unable to load configuration at " + configPaths[i], e);
        }
    }
    if (configs.size() > 1) {
        CombinedConfiguration combinedConfig = new CombinedConfiguration(new OverrideCombiner());
        for (Configuration config : configs) {
            combinedConfig.addConfiguration(config);
        }
        return combinedConfig;
    } else if (configs.size() == 1) {
        return configs.get(0);
    } else {
        return null;
    }
}
Also used : XMLConfiguration(org.apache.commons.configuration2.XMLConfiguration) HierarchicalConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration) Configuration(org.apache.commons.configuration2.Configuration) CombinedConfiguration(org.apache.commons.configuration2.CombinedConfiguration) ArrayList(java.util.ArrayList) Resource(org.springframework.core.io.Resource) DecryptionLookup(org.craftercms.commons.config.DecryptionLookup) CombinedConfiguration(org.apache.commons.configuration2.CombinedConfiguration) HierarchicalConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException) OverrideCombiner(org.apache.commons.configuration2.tree.OverrideCombiner) XMLConfiguration(org.apache.commons.configuration2.XMLConfiguration) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException) DecryptionLookup(org.craftercms.commons.config.DecryptionLookup) Lookup(org.apache.commons.configuration2.interpol.Lookup)

Example 3 with XMLConfiguration

use of org.apache.commons.configuration2.XMLConfiguration in project sponge by softelnet.

the class DefaultConfigurationManager method createRootConfig.

protected CommonsConfiguration createRootConfig() {
    MergeCombiner combiner = new MergeCombiner();
    combiner.addListNode(PluginManagerConstants.CFG_PLUGIN);
    CombinedConfiguration cc = new CombinedConfiguration(combiner);
    // Try to add explicit configuration
    if (configurationFilename != null) {
        logger.info("Loading configuration file {}...", configurationFilename);
        Pair<XMLConfiguration, URL> configurationPair = createXmlConfiguration(configurationFilename);
        cc.addConfiguration(configurationPair.getLeft());
        configurationFileUrl = configurationPair.getRight();
    }
    // Add default configuration
    cc.addConfiguration(createXmlConfiguration(ConfigurationConstants.DEFAULT_CONFIG).getLeft());
    return new CommonsConfiguration(cc);
}
Also used : XMLConfiguration(org.apache.commons.configuration2.XMLConfiguration) MergeCombiner(org.apache.commons.configuration2.tree.MergeCombiner) CombinedConfiguration(org.apache.commons.configuration2.CombinedConfiguration) CommonsConfiguration(org.openksavi.sponge.core.config.CommonsConfiguration) URL(java.net.URL)

Example 4 with XMLConfiguration

use of org.apache.commons.configuration2.XMLConfiguration in project engine by craftercms.

the class ScriptFilterTest method createSiteContext.

private SiteContext createSiteContext(ContentStoreService storeService) throws Exception {
    SiteContext siteContext = spy(new SiteContext());
    ScriptFactory scriptFactory = createScriptFactory(siteContext);
    XMLConfiguration config = ConfigUtils.readXmlConfiguration(new ClassPathResource("config/site-config.xml"), ',', null);
    config.setListDelimiterHandler(new DefaultListDelimiterHandler(','));
    when(siteContext.getSiteName()).thenReturn("default");
    when(siteContext.getContext()).thenReturn(mock(Context.class));
    when(siteContext.getStoreService()).thenReturn(storeService);
    when(siteContext.getConfig()).thenReturn(config);
    when(siteContext.getScriptFactory()).thenReturn(scriptFactory);
    when(siteContext.getCacheTemplate()).thenReturn(cacheTemplate);
    return siteContext;
}
Also used : RequestContext(org.craftercms.commons.http.RequestContext) MockServletContext(org.springframework.mock.web.MockServletContext) SiteContext(org.craftercms.engine.service.context.SiteContext) Context(org.craftercms.core.service.Context) ServletContext(javax.servlet.ServletContext) XMLConfiguration(org.apache.commons.configuration2.XMLConfiguration) DefaultListDelimiterHandler(org.apache.commons.configuration2.convert.DefaultListDelimiterHandler) SiteContext(org.craftercms.engine.service.context.SiteContext) ScriptFactory(org.craftercms.engine.scripting.ScriptFactory) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 5 with XMLConfiguration

use of org.apache.commons.configuration2.XMLConfiguration in project engine by craftercms.

the class RejectIndexFilesItemFilterTest method setUpCurrentConfig.

private void setUpCurrentConfig() {
    XMLConfiguration config = mock(XMLConfiguration.class);
    when(config.getString(INDEX_FILE_NAME_CONFIG_KEY, DEFAULT_INDEX_FILE_NAME)).thenReturn(DEFAULT_INDEX_FILE_NAME);
    when(config.getBoolean(TARGETING_ENABLED_CONFIG_KEY, false)).thenReturn(true);
    SiteContext siteContext = spy(new SiteContext());
    when(siteContext.getSiteName()).thenReturn("test");
    when(siteContext.getConfig()).thenReturn(config);
    SiteContext.setCurrent(siteContext);
}
Also used : XMLConfiguration(org.apache.commons.configuration2.XMLConfiguration) SiteContext(org.craftercms.engine.service.context.SiteContext)

Aggregations

XMLConfiguration (org.apache.commons.configuration2.XMLConfiguration)12 Parameters (org.apache.commons.configuration2.builder.fluent.Parameters)5 ConfigurationException (org.apache.commons.configuration2.ex.ConfigurationException)5 ImmutableNode (org.apache.commons.configuration2.tree.ImmutableNode)4 SiteContext (org.craftercms.engine.service.context.SiteContext)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 DefaultListDelimiterHandler (org.apache.commons.configuration2.convert.DefaultListDelimiterHandler)3 Lookup (org.apache.commons.configuration2.interpol.Lookup)3 ArrayList (java.util.ArrayList)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 CombinedConfiguration (org.apache.commons.configuration2.CombinedConfiguration)2 HierarchicalConfiguration (org.apache.commons.configuration2.HierarchicalConfiguration)2 Context (org.craftercms.core.service.Context)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 Document (org.w3c.dom.Document)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 HostnameLookup (com.evolveum.midpoint.init.interpol.HostnameLookup)1 RandomLookup (com.evolveum.midpoint.init.interpol.RandomLookup)1