org.apache.commons.configuration2.interpol.Lookup Java Examples

The following examples show how to use org.apache.commons.configuration2.interpol.Lookup. 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: TestBasicBuilderParameters.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether default lookups can be set.
 */
@Test
public void testSetDefaultLookups()
{
    final Lookup look = EasyMock.createMock(Lookup.class);
    final Collection<Lookup> looks = Collections.singleton(look);
    assertSame("Wrong result", params, params.setDefaultLookups(looks));
    final Collection<?> col =
            (Collection<?>) params.getParameters().get("defaultLookups");
    assertNotSame("No copy was created", col, looks);
    assertEquals("Wrong number of lookups", 1, col.size());
    assertSame("Wrong lookup", look, col.iterator().next());
    final Collection<?> col2 =
            (Collection<?>) params.getParameters().get("defaultLookups");
    assertNotSame("No copy in parameters", col, col2);
}
 
Example #2
Source File: TestAbstractConfigurationBasicFeatures.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether prefix lookups can be added to an existing
 * {@code ConfigurationInterpolator}.
 */
@Test
public void testSetPrefixLookupsExistingInterpolator()
{
    final Lookup look = EasyMock.createMock(Lookup.class);
    EasyMock.replay(look);
    final AbstractConfiguration config =
            new TestConfigurationImpl(new PropertiesConfiguration());
    final int count = config.getInterpolator().getLookups().size();
    final Map<String, Lookup> lookups = new HashMap<>();
    lookups.put("test", look);
    config.setPrefixLookups(lookups);
    final Map<String, Lookup> lookups2 = config.getInterpolator().getLookups();
    assertEquals("Not added", count + 1, lookups2.size());
    assertSame("Not found", look, lookups2.get("test"));
}
 
Example #3
Source File: TestAbstractConfigurationBasicFeatures.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether default lookups can be added to an already existing
 * {@code ConfigurationInterpolator}.
 */
@Test
public void testSetDefaultLookupsExistingInterpolator()
{
    final Lookup look = EasyMock.createMock(Lookup.class);
    EasyMock.replay(look);
    final AbstractConfiguration config =
            new TestConfigurationImpl(new PropertiesConfiguration());
    config.getInterpolator().addDefaultLookup(
            new ConfigurationLookup(new PropertiesConfiguration()));
    config.setDefaultLookups(Collections.singleton(look));
    final List<Lookup> lookups = config.getInterpolator().getDefaultLookups();
    assertEquals("Wrong number of default lookups", 3, lookups.size());
    assertSame("Wrong lookup at 1", look, lookups.get(1));
    assertTrue("Wrong lookup at 2: " + lookups,
            lookups.get(2) instanceof ConfigurationLookup);
}
 
Example #4
Source File: TestAbstractConfigurationBasicFeatures.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether default lookups can be added if not
 * {@code ConfigurationInterpolator} exists yet.
 */
@Test
public void testSetDefaultLookupsNoInterpolator()
{
    final Lookup look = EasyMock.createMock(Lookup.class);
    EasyMock.replay(look);
    final AbstractConfiguration config =
            new TestConfigurationImpl(new PropertiesConfiguration());
    config.setInterpolator(null);
    config.setDefaultLookups(Collections.singleton(look));
    final List<Lookup> lookups = config.getInterpolator().getDefaultLookups();
    assertEquals("Wrong number of default lookups", 2, lookups.size());
    assertSame("Wrong lookup at 0", look, lookups.get(0));
    assertTrue("Wrong lookup at 1",
            lookups.get(1) instanceof ConfigurationLookup);
}
 
Example #5
Source File: BasicBuilderParameters.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether the passed in map with parameters contains a valid
 * collection with default lookups. This method works like
 * {@link #fetchAndCheckPrefixLookups(Map)}, but tests the default lookups
 * collection.
 *
 * @param params the map with parameters
 * @return the collection with default lookups (may be <b>null</b>)
 * @throws IllegalArgumentException if invalid data is found
 */
private static Collection<? extends Lookup> fetchAndCheckDefaultLookups(
        final Map<String, Object> params)
{
    final Collection<?> col =
            fetchParameter(params, PROP_DEFAULT_LOOKUPS, Collection.class);
    if (col == null)
    {
        return null;
    }

    for (final Object o : col)
    {
        if (!(o instanceof Lookup))
        {
            throw new IllegalArgumentException(
                    "Collection with default lookups contains invalid data: "
                            + col);
        }
    }
    return fetchDefaultLookups(params);
}
 
Example #6
Source File: BasicBuilderParameters.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether the passed in map with parameters contains a map with
 * prefix lookups. This method is used if the parameters map is from an
 * insecure source and we cannot be sure that it contains valid data.
 * Therefore, we have to map that the key for the prefix lookups actually
 * points to a map containing keys and values of expected data types.
 *
 * @param params the parameters map
 * @return the obtained map with prefix lookups
 * @throws IllegalArgumentException if the map contains invalid data
 */
private static Map<String, ? extends Lookup> fetchAndCheckPrefixLookups(
        final Map<String, Object> params)
{
    final Map<?, ?> prefixes =
            fetchParameter(params, PROP_PREFIX_LOOKUPS, Map.class);
    if (prefixes == null)
    {
        return null;
    }

    for (final Map.Entry<?, ?> e : prefixes.entrySet())
    {
        if (!(e.getKey() instanceof String)
                || !(e.getValue() instanceof Lookup))
        {
            throw new IllegalArgumentException(
                    "Map with prefix lookups contains invalid data: "
                            + prefixes);
        }
    }
    return fetchPrefixLookups(params);
}
 
Example #7
Source File: BasicBuilderParameters.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Creates defensive copies for collection structures when constructing the
 * map with parameters. It should not be possible to modify this object's
 * internal state when having access to the parameters map.
 *
 * @param params the map with parameters to be passed to the caller
 */
private static void createDefensiveCopies(final HashMap<String, Object> params)
{
    final Map<String, ? extends Lookup> prefixLookups =
            fetchPrefixLookups(params);
    if (prefixLookups != null)
    {
        params.put(PROP_PREFIX_LOOKUPS, new HashMap<>(
                prefixLookups));
    }
    final Collection<? extends Lookup> defLookups = fetchDefaultLookups(params);
    if (defLookups != null)
    {
        params.put(PROP_DEFAULT_LOOKUPS, new ArrayList<>(defLookups));
    }
}
 
Example #8
Source File: AbstractConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Finds a {@code ConfigurationLookup} pointing to the specified
 * configuration in the default lookups for the specified
 * {@code ConfigurationInterpolator}.
 *
 * @param ci the {@code ConfigurationInterpolator} in question
 * @param targetConf the target configuration of the searched lookup
 * @return the found {@code Lookup} object or <b>null</b>
 */
private static Lookup findConfigurationLookup(final ConfigurationInterpolator ci,
        final ImmutableConfiguration targetConf)
{
    for (final Lookup l : ci.getDefaultLookups())
    {
        if (l instanceof ConfigurationLookup)
        {
            if (targetConf == ((ConfigurationLookup) l).getConfiguration())
            {
                return l;
            }
        }
    }
    return null;
}
 
Example #9
Source File: TestBasicBuilderParameters.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether a custom {@code ConfigurationInterpolator} overrides
 * settings for custom lookups.
 */
@Test
public void testSetLookupsAndInterpolator()
{
    final Lookup look1 = EasyMock.createMock(Lookup.class);
    final Lookup look2 = EasyMock.createMock(Lookup.class);
    final ConfigurationInterpolator parent =
            EasyMock.createMock(ConfigurationInterpolator.class);
    final ConfigurationInterpolator ci =
            EasyMock.createMock(ConfigurationInterpolator.class);
    params.setDefaultLookups(Collections.singleton(look1));
    params.setPrefixLookups(Collections.singletonMap("test", look2));
    params.setInterpolator(ci);
    params.setParentInterpolator(parent);
    final Map<String, Object> map = params.getParameters();
    assertFalse("Got prefix lookups", map.containsKey("prefixLookups"));
    assertFalse("Got default lookups", map.containsKey("defaultLookups"));
    assertFalse("Got a parent interpolator",
            map.containsKey("parentInterpolator"));
}
 
Example #10
Source File: AbstractConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Adds all {@code Lookup} objects in the given collection as default
 * lookups (i.e. lookups without a variable prefix) to the
 * {@code ConfigurationInterpolator} object of this configuration. In
 * addition, it adds a specialized default {@code Lookup} object which
 * queries this {@code Configuration}. The set of {@code Lookup} objects
 * with prefixes 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 the collection with default {@code Lookup} objects to be
 *        added
 * @since 2.0
 */
public void setDefaultLookups(final Collection<? extends Lookup> lookups)
{
    boolean success;
    do
    {
        final ConfigurationInterpolator ciOld = getInterpolator();
        final ConfigurationInterpolator ciNew =
                ciOld != null ? ciOld : new ConfigurationInterpolator();
        Lookup confLookup = findConfigurationLookup(ciNew);
        if (confLookup == null)
        {
            confLookup = new ConfigurationLookup(this);
        }
        else
        {
            ciNew.removeDefaultLookup(confLookup);
        }
        ciNew.addDefaultLookups(lookups);
        ciNew.addDefaultLookup(confLookup);
        success = interpolator.compareAndSet(ciOld, ciNew);
    } while (!success);
}
 
Example #11
Source File: TestBaseConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether a {@code ConfigurationInterpolator} can be created and
 * installed.
 */
@Test
public void testInstallInterpolator()
{
    final Lookup prefixLookup = EasyMock.createMock(Lookup.class);
    final Lookup defLookup = EasyMock.createMock(Lookup.class);
    EasyMock.replay(prefixLookup, defLookup);
    final Map<String, Lookup> prefixLookups = new HashMap<>();
    prefixLookups.put("test", prefixLookup);
    final List<Lookup> defLookups = new ArrayList<>();
    defLookups.add(defLookup);
    config.installInterpolator(prefixLookups, defLookups);
    final ConfigurationInterpolator interpolator = config.getInterpolator();
    assertEquals("Wrong prefix lookups", prefixLookups,
            interpolator.getLookups());
    final List<Lookup> defLookups2 = interpolator.getDefaultLookups();
    assertEquals("Wrong number of default lookups", 2, defLookups2.size());
    assertSame("Wrong default lookup 1", defLookup, defLookups2.get(0));
    final String var = "testVariable";
    final Object value = 42;
    config.addProperty(var, value);
    assertEquals("Wrong lookup result", value,
            defLookups2.get(1).lookup(var));
}
 
Example #12
Source File: TestBasicBuilderParameters.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether null values are handled by setDefaultLookups().
 */
@Test
public void testSetDefaultLookupsNull()
{
    params.setDefaultLookups(new ArrayList<Lookup>());
    params.setDefaultLookups(null);
    assertFalse("Found key",
            params.getParameters().containsKey("defaultLookups"));
}
 
Example #13
Source File: DynamicCombinedConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@code ConfigurationInterpolator} instance for performing local
 * variable substitutions. This implementation returns an object which
 * shares the prefix lookups from this configuration's
 * {@code ConfigurationInterpolator}, but does not define any other lookups.
 *
 * @return the {@code ConfigurationInterpolator}
 */
private ConfigurationInterpolator initLocalInterpolator()
{
    return new ConfigurationInterpolator()
    {
        @Override
        protected Lookup fetchLookupForPrefix(final String prefix)
        {
            return ConfigurationInterpolator
                    .nullSafeLookup(getInterpolator().getLookups().get(
                            prefix));
        }
    };
}
 
Example #14
Source File: TestBasicBuilderParameters.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether a specification object for interpolation can be obtained.
 */
@Test
public void testFetchInterpolatorSpecification()
{
    final ConfigurationInterpolator parent =
            EasyMock.createMock(ConfigurationInterpolator.class);
    final Lookup l1 = EasyMock.createMock(Lookup.class);
    final Lookup l2 = EasyMock.createMock(Lookup.class);
    final Lookup l3 = EasyMock.createMock(Lookup.class);
    final Map<String, Lookup> prefixLookups = new HashMap<>();
    prefixLookups.put("p1", l1);
    prefixLookups.put("p2", l2);
    final Collection<Lookup> defLookups = Collections.singleton(l3);
    params.setParentInterpolator(parent);
    params.setPrefixLookups(prefixLookups);
    params.setDefaultLookups(defLookups);
    final Map<String, Object> map = params.getParameters();
    final InterpolatorSpecification spec =
            BasicBuilderParameters.fetchInterpolatorSpecification(map);
    assertSame("Wrong parent", parent, spec.getParentInterpolator());
    assertEquals("Wrong prefix lookups", prefixLookups,
            spec.getPrefixLookups());
    assertEquals("Wrong number of default lookups", 1, spec
            .getDefaultLookups().size());
    assertTrue("Wrong default lookup", spec.getDefaultLookups()
            .contains(l3));
}
 
Example #15
Source File: TestBasicBuilderParameters.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether null values are handled by setPrefixLookups().
 */
@Test
public void testSetPrefixLookupsNull()
{
    params.setPrefixLookups(new HashMap<String, Lookup>());
    params.setPrefixLookups(null);
    assertFalse("Found key",
            params.getParameters().containsKey("prefixLookups"));
}
 
Example #16
Source File: TestBasicBuilderParameters.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether prefix lookups can be set.
 */
@Test
public void testSetPrefixLookups()
{
    final Lookup look = EasyMock.createMock(Lookup.class);
    final Map<String, Lookup> lookups = Collections.singletonMap("test", look);
    assertSame("Wrong result", params, params.setPrefixLookups(lookups));
    final Map<?, ?> map = (Map<?, ?>) params.getParameters().get("prefixLookups");
    assertNotSame("No copy was created", lookups, map);
    assertEquals("Wrong lookup", look, map.get("test"));
    assertEquals("Wrong number of lookups", 1, map.size());
    final Map<?, ?> map2 = (Map<?, ?>) params.getParameters().get("prefixLookups");
    assertNotSame("No copy in parameters", map, map2);
}
 
Example #17
Source File: TestBasicBuilderParameters.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests fetchInterpolatorSpecification() if the map with prefix lookups
 * contains an invalid key.
 */
@Test(expected = IllegalArgumentException.class)
public void testFetchInterpolatorSpecificationInvalidMapKey()
{
    final Map<String, Object> map = new HashMap<>();
    final Map<Object, Object> prefix = new HashMap<>();
    prefix.put(42, EasyMock.createMock(Lookup.class));
    map.put("prefixLookups", prefix);
    BasicBuilderParameters.fetchInterpolatorSpecification(map);
}
 
Example #18
Source File: TestAbstractConfigurationBasicFeatures.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether a new {@code ConfigurationInterpolator} can be installed
 * without providing custom lookups.
 */
@Test
public void testInstallInterpolatorNull()
{
    final AbstractConfiguration config =
            new TestConfigurationImpl(new PropertiesConfiguration());
    config.installInterpolator(null, null);
    assertTrue("Got prefix lookups", config.getInterpolator().getLookups()
            .isEmpty());
    final List<Lookup> defLookups = config.getInterpolator().getDefaultLookups();
    assertEquals("Wrong number of default lookups", 1, defLookups.size());
    assertTrue("Wrong default lookup",
            defLookups.get(0) instanceof ConfigurationLookup);
}
 
Example #19
Source File: TestBasicBuilderParameters.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether the map with prefix lookups is cloned, too.
 */
@Test
public void testClonePrefixLookups()
{
    final Lookup look = EasyMock.createMock(Lookup.class);
    final Map<String, Lookup> lookups = Collections.singletonMap("test", look);
    params.setPrefixLookups(lookups);
    final BasicBuilderParameters clone = params.clone();
    Map<?, ?> map = (Map<?, ?>) params.getParameters().get("prefixLookups");
    map.clear();
    map = (Map<?, ?>) clone.getParameters().get("prefixLookups");
    assertEquals("Wrong number of lookups", 1, map.size());
    assertSame("Wrong lookup", look, map.get("test"));
}
 
Example #20
Source File: TestBasicBuilderParameters.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether the collection with default lookups can be cloned, too.
 */
@Test
public void testCloneDefaultLookups()
{
    final Lookup look = EasyMock.createMock(Lookup.class);
    final Collection<Lookup> looks = Collections.singleton(look);
    params.setDefaultLookups(looks);
    final BasicBuilderParameters clone = params.clone();
    Collection<?> defLooks =
            (Collection<?>) params.getParameters().get("defaultLookups");
    defLooks.clear();
    defLooks = (Collection<?>) clone.getParameters().get("defaultLookups");
    assertEquals("Wrong number of default lookups", 1, defLooks.size());
    assertTrue("Wrong default lookup", defLooks.contains(look));
}
 
Example #21
Source File: TestAbstractConfigurationBasicFeatures.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether prefix lookups can be added if no
 * {@code ConfigurationInterpolator} exists yet.
 */
@Test
public void testSetPrefixLookupsNoInterpolator()
{
    final Lookup look = EasyMock.createMock(Lookup.class);
    EasyMock.replay(look);
    final AbstractConfiguration config =
            new TestConfigurationImpl(new PropertiesConfiguration());
    config.setInterpolator(null);
    config.setPrefixLookups(Collections.singletonMap("test", look));
    final Map<String, Lookup> lookups = config.getInterpolator().getLookups();
    assertEquals("Wrong number of lookups", 1, lookups.size());
    assertSame("Not found", look, lookups.get("test"));
}
 
Example #22
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 #23
Source File: BasicBuilderParameters.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Obtains the collection with default lookups from the parameters map.
 *
 * @param params the map with parameters
 * @return the collection with default lookups (may be <b>null</b>)
 */
private static Collection<? extends Lookup> fetchDefaultLookups(
        final Map<String, Object> params)
{
    // This is safe to cast because we either have full control over the map
    // and thus know the types of the contained values or have checked
    // the content before
    @SuppressWarnings("unchecked")
    final
    Collection<? extends Lookup> defLookups =
            (Collection<? extends Lookup>) params.get(PROP_DEFAULT_LOOKUPS);
    return defLookups;
}
 
Example #24
Source File: BasicBuilderParameters.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Obtains the map with prefix lookups from the parameters map.
 *
 * @param params the map with parameters
 * @return the map with prefix lookups (may be <b>null</b>)
 */
private static Map<String, ? extends Lookup> fetchPrefixLookups(
        final Map<String, Object> params)
{
    // This is safe to cast because we either have full control over the map
    // and thus know the types of the contained values or have checked
    // the content before
    @SuppressWarnings("unchecked")
    final
    Map<String, ? extends Lookup> prefixLookups =
            (Map<String, ? extends Lookup>) params.get(PROP_PREFIX_LOOKUPS);
    return prefixLookups;
}
 
Example #25
Source File: BasicBuilderParameters.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc} A defensive copy of the passed in collection is created. A
 * <b>null</b> argument causes all default lookups to be removed from the
 * internal parameters map.
 */
@Override
public BasicBuilderParameters setDefaultLookups(
        final Collection<? extends Lookup> lookups)
{
    if (lookups == null)
    {
        properties.remove(PROP_DEFAULT_LOOKUPS);
        return this;
    }
    return setProperty(PROP_DEFAULT_LOOKUPS, new ArrayList<>(
            lookups));
}
 
Example #26
Source File: BasicBuilderParameters.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc} A defensive copy of the passed in map is created. A
 * <b>null</b> argument causes all prefix lookups to be removed from the
 * internal parameters map.
 */
@Override
public BasicBuilderParameters setPrefixLookups(
        final Map<String, ? extends Lookup> lookups)
{
    if (lookups == null)
    {
        properties.remove(PROP_PREFIX_LOOKUPS);
        return this;
    }
    return setProperty(PROP_PREFIX_LOOKUPS,
            new HashMap<>(lookups));
}
 
Example #27
Source File: AbstractConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc} This implementation creates a new
 * {@code ConfigurationInterpolator} instance and initializes it with the
 * given {@code Lookup} objects. In addition, it adds a specialized default
 * {@code Lookup} object which queries this {@code Configuration}.
 *
 * @since 2.0
 */
@Override
public final void installInterpolator(
        final Map<String, ? extends Lookup> prefixLookups,
        final Collection<? extends Lookup> defLookups)
{
    final InterpolatorSpecification spec =
            new InterpolatorSpecification.Builder()
                    .withPrefixLookups(prefixLookups)
                    .withDefaultLookups(defLookups)
                    .withDefaultLookup(new ConfigurationLookup(this))
                    .create();
    setInterpolator(ConfigurationInterpolator.fromSpecification(spec));
}
 
Example #28
Source File: DelegatedPropertiesConfiguration.java    From james-project with Apache License 2.0 4 votes vote down vote up
@Override
public void installInterpolator(Map<String, ? extends Lookup> prefixLookups, Collection<? extends Lookup> defLookups) {
    configuration.installInterpolator(prefixLookups, defLookups);
}
 
Example #29
Source File: AbstractConfiguration.java    From commons-configuration with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a clone of the {@code ConfigurationInterpolator} used by this
 * instance. This method can be called by {@code clone()} implementations of
 * derived classes. Normally, the {@code ConfigurationInterpolator} of a
 * configuration instance must not be shared with other instances because it
 * contains a specific {@code Lookup} object pointing to the owning
 * configuration. This has to be taken into account when cloning a
 * configuration. This method creates a new
 * {@code ConfigurationInterpolator} for this configuration instance which
 * contains all lookup objects from the original
 * {@code ConfigurationInterpolator} except for the configuration specific
 * lookup pointing to the passed in original configuration. This one is
 * replaced by a corresponding {@code Lookup} referring to this
 * configuration.
 *
 * @param orgConfig the original configuration from which this one was
 *        cloned
 * @since 2.0
 */
protected void cloneInterpolator(final AbstractConfiguration orgConfig)
{
    interpolator = new AtomicReference<>();
    final ConfigurationInterpolator orgInterpolator = orgConfig.getInterpolator();
    final List<Lookup> defaultLookups = orgInterpolator.getDefaultLookups();
    final Lookup lookup = findConfigurationLookup(orgInterpolator, orgConfig);
    if (lookup != null)
    {
        defaultLookups.remove(lookup);
    }

    installInterpolator(orgInterpolator.getLookups(), defaultLookups);
}
 
Example #30
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);
}