001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.configuration.interpol;
018    
019    import org.apache.commons.configuration.EnvironmentConfiguration;
020    import org.apache.commons.lang.text.StrLookup;
021    
022    /**
023     * <p>
024     * A specialized lookup implementation that allows access to environment
025     * variables.
026     * </p>
027     * <p>
028     * This implementation relies on {@link EnvironmentConfiguration} to resolve
029     * environment variables. It can be used for referencing environment variables
030     * in configuration files in an easy way, for instance:
031     *
032     * <pre>
033     * java.home = ${env:JAVA_HOME}
034     * </pre>
035     *
036     * </p>
037     * <p>
038     * <code>EnvironmentLookup</code> is one of the standard lookups that is
039     * registered per default for each configuration.
040     * </p>
041     *
042     * @author <a
043     *         href="http://commons.apache.org/configuration/team-list.html">Commons
044     *         Configuration team</a>
045     * @since 1.7
046     * @version $Id: EnvironmentLookup.java 833896 2009-11-08 17:38:19Z oheger $
047     */
048    public class EnvironmentLookup extends StrLookup
049    {
050        /** Stores the underlying <code>EnvironmentConfiguration</code>. */
051        private final EnvironmentConfiguration environmentConfig = new EnvironmentConfiguration();
052    
053        /**
054         * Performs a lookup for the specified variable. This implementation
055         * directly delegates to a <code>EnvironmentConfiguration</code>.
056         *
057         * @param key the key to lookup
058         * @return the value of this key or <b>null</b> if it cannot be resolved
059         */
060        public String lookup(String key)
061        {
062            return environmentConfig.getString(key);
063        }
064    }