Java Code Examples for org.apache.commons.configuration2.interpol.ConfigurationInterpolator#registerLookups()

The following examples show how to use org.apache.commons.configuration2.interpol.ConfigurationInterpolator#registerLookups() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CombinedConfigurationBuilder.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Processes custom {@link Lookup} objects that might be declared in the
 * definition configuration. Each {@code Lookup} object is registered at the
 * definition configuration and at the result configuration. It is also
 * added to all child configurations added to the resulting combined
 * configuration.
 *
 * @param defConfig the definition configuration
 * @param resultConfig the resulting configuration
 * @throws ConfigurationException if an error occurs
 */
protected void registerConfiguredLookups(
        final HierarchicalConfiguration<?> defConfig, final Configuration resultConfig)
        throws ConfigurationException
{
    final Map<String, Lookup> lookups = new HashMap<>();

    final List<? extends HierarchicalConfiguration<?>> nodes =
            defConfig.configurationsAt(KEY_CONFIGURATION_LOOKUPS);
    for (final HierarchicalConfiguration<?> config : nodes)
    {
        final XMLBeanDeclaration decl = new XMLBeanDeclaration(config);
        final String key = config.getString(KEY_LOOKUP_KEY);
        final Lookup lookup = (Lookup) fetchBeanHelper().createBean(decl);
        lookups.put(key, lookup);
    }

    if (!lookups.isEmpty())
    {
        final ConfigurationInterpolator defCI = defConfig.getInterpolator();
        if (defCI != null)
        {
            defCI.registerLookups(lookups);
        }
        resultConfig.getInterpolator().registerLookups(lookups);
    }
}
 
Example 2
Source File: CombinedConfigurationBuilder.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Creates and initializes a default {@code EntityResolver} if the
 * definition configuration contains a corresponding declaration.
 *
 * @param config the definition configuration
 * @param xmlParams the (already partly initialized) object with XML
 *        parameters; here the new resolver is to be stored
 * @throws ConfigurationException if an error occurs
 */
protected void configureEntityResolver(final HierarchicalConfiguration<?> config,
        final XMLBuilderParametersImpl xmlParams) throws ConfigurationException
{
    if (config.getMaxIndex(KEY_ENTITY_RESOLVER) == 0)
    {
        final XMLBeanDeclaration decl =
                new XMLBeanDeclaration(config, KEY_ENTITY_RESOLVER, true);
        final EntityResolver resolver =
                (EntityResolver) fetchBeanHelper().createBean(decl,
                        CatalogResolver.class);
        final FileSystem fileSystem = xmlParams.getFileHandler().getFileSystem();
        if (fileSystem != null)
        {
            BeanHelper.setProperty(resolver, "fileSystem", fileSystem);
        }
        final String basePath = xmlParams.getFileHandler().getBasePath();
        if (basePath != null)
        {
            BeanHelper.setProperty(resolver, "baseDir", basePath);
        }
        final ConfigurationInterpolator ci = new ConfigurationInterpolator();
        ci.registerLookups(fetchPrefixLookups());
        BeanHelper.setProperty(resolver, "interpolator", ci);

        xmlParams.setEntityResolver(resolver);
    }
}
 
Example 3
Source File: AbstractConfiguration.java    From commons-configuration with Apache License 2.0 3 votes vote down vote up
/**
 * Registers all {@code Lookup} objects in the given map at the current
 * {@code ConfigurationInterpolator} of this configuration. The set of
 * default lookup objects (for variables without a prefix) is not modified
 * by this method. If this configuration does not have a
 * {@code ConfigurationInterpolator}, a new instance is created. Note: This
 * method is mainly intended to be used for initializing a configuration
 * when it is created by a builder. Normal client code should better call
 * {@link #installInterpolator(Map, Collection)} to define the
 * {@code ConfigurationInterpolator} in a single step.
 *
 * @param lookups a map with new {@code Lookup} objects and their prefixes
 *        (may be <b>null</b>)
 * @since 2.0
 */
public void setPrefixLookups(final Map<String, ? extends Lookup> lookups)
{
    boolean success;
    do
    {
        // do this in a loop because the ConfigurationInterpolator
        // instance may be changed by another thread
        final ConfigurationInterpolator ciOld = getInterpolator();
        final ConfigurationInterpolator ciNew =
                ciOld != null ? ciOld : new ConfigurationInterpolator();
        ciNew.registerLookups(lookups);
        success = interpolator.compareAndSet(ciOld, ciNew);
    } while (!success);
}