001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *      http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.oozie.util;
019    
020    import org.apache.hadoop.conf.Configuration;
021    
022    /**
023     *
024     */
025    public class ConfigUtils {
026        private final static XLog LOG = XLog.getLog(ConfigUtils.class);
027    
028        /**
029         * Fetches a property using both a deprecated name and the new name. The deprecated property
030         * has precedence over the new name. If the deprecated name is used a warning is written to
031         * the log.
032         *
033         * @param conf configuration object.
034         * @param newName new property name.
035         * @param oldName deprecated property name.
036         * @param defaultValue default value.
037         * @return the property value, or the default value if not found under the deprecated name and the new name.
038         */
039        public static String getWithDeprecatedCheck(Configuration conf, String newName, String oldName,
040                                                    String defaultValue) {
041            String value = conf.get(oldName, null);
042            if (value == null) {
043                value = conf.get(newName, defaultValue);
044            }
045            else {
046                LOG.warn("Using a deprecated configuration property [{0}], should use [{1}].  " +
047                         "Please delete the deprecated property in order for the new property to take effect.",
048                         oldName, newName);
049            }
050            return value;
051        }
052    
053        /**
054         * Fetches a property using both a deprecated name and the new name. The deprecated property
055         * has precedence over the new name. If the deprecated name is used a warning is written to
056         * the log.
057         *
058         * @param conf configuration object.
059         * @param newName new property name.
060         * @param oldName deprecated property name.
061         * @param defaultValue default value.
062         * @return the property value, or the default value if not found under the deprecated name and the new name.
063         */
064        public static boolean getWithDeprecatedCheck(Configuration conf, String newName, String oldName,
065                                                     boolean defaultValue) {
066            String value = getWithDeprecatedCheck(conf, newName, oldName, Boolean.toString(defaultValue));
067            return Boolean.parseBoolean(value);
068        }
069    }