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 * http://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 /** The configuration to which lookups are delegated. */
36 private final ImmutableConfiguration configuration;
37
38 /**
39 * Creates a new instance of {@code ConfigurationLookup} and sets the associated {@code ImmutableConfiguration}.
40 *
41 * @param config the configuration to use for lookups (must not be <strong>null</strong>)
42 * @throws IllegalArgumentException if the configuration is <strong>null</strong>
43 */
44 public ConfigurationLookup(final ImmutableConfiguration config) {
45 if (config == null) {
46 throw new IllegalArgumentException("Configuration must not be null!");
47 }
48 configuration = config;
49 }
50
51 /**
52 * Gets the {@code ImmutableConfiguration} used by this object.
53 *
54 * @return the associated {@code ImmutableConfiguration}
55 */
56 public ImmutableConfiguration getConfiguration() {
57 return configuration;
58 }
59
60 /**
61 * {@inheritDoc} This implementation calls {@code getProperty()} on the associated configuration. The return value is
62 * directly returned. Note that this may be a complex object, for example a collection or an array.
63 */
64 @Override
65 public Object lookup(final String variable) {
66 return getConfiguration().getProperty(variable);
67 }
68 }