1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.configuration2;
18
19 import org.apache.commons.configuration2.interpol.Lookup;
20
21 /**
22 * <p>
23 * A specialized implementation of the {@code Lookup} interface which uses a {@code Configuration} object to resolve
24 * variables.
25 * </p>
26 * <p>
27 * This class is passed an {@link ImmutableConfiguration} object at construction time. In its implementation of the
28 * {@code lookup()} method it simply queries this configuration for the passed in variable name. So the keys passed to
29 * {@code lookup()} are mapped directly to configuration properties.
30 * </p>
31 *
32 * @since 2.0
33 */
34 public class ConfigurationLookup implements Lookup {
35
36 /** The configuration to which lookups are delegated. */
37 private final ImmutableConfiguration configuration;
38
39 /**
40 * Creates a new instance of {@code ConfigurationLookup} and sets the associated {@code ImmutableConfiguration}.
41 *
42 * @param config the configuration to use for lookups (must not be <strong>null</strong>)
43 * @throws IllegalArgumentException if the configuration is <strong>null</strong>
44 */
45 public ConfigurationLookup(final ImmutableConfiguration config) {
46 if (config == null) {
47 throw new IllegalArgumentException("Configuration must not be null.");
48 }
49 configuration = config;
50 }
51
52 /**
53 * Gets the {@code ImmutableConfiguration} used by this object.
54 *
55 * @return the associated {@code ImmutableConfiguration}
56 */
57 public ImmutableConfiguration getConfiguration() {
58 return configuration;
59 }
60
61 /**
62 * {@inheritDoc} This implementation calls {@code getProperty()} on the associated configuration. The return value is
63 * directly returned. Note that this may be a complex object, for example a collection or an array.
64 */
65 @Override
66 public Object lookup(final String variable) {
67 return getConfiguration().getProperty(variable);
68 }
69 }