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    import org.apache.oozie.service.Services;
022    import org.apache.oozie.servlet.ServicesLoader;
023    
024    /**
025     *
026     */
027    public class ConfigUtils {
028        private final static XLog LOG = XLog.getLog(ConfigUtils.class);
029    
030        /**
031         * Fetches a property using both a deprecated name and the new name. The deprecated property
032         * has precedence over the new name. If the deprecated name is used a warning is written to
033         * the log.
034         *
035         * @param conf configuration object.
036         * @param newName new property name.
037         * @param oldName deprecated property name.
038         * @param defaultValue default value.
039         * @return the property value, or the default value if not found under the deprecated name and the new name.
040         */
041        public static String getWithDeprecatedCheck(Configuration conf, String newName, String oldName,
042                                                    String defaultValue) {
043            String value = conf.get(oldName, null);
044            if (value == null) {
045                value = conf.get(newName, defaultValue);
046            }
047            else {
048                LOG.warn("Using a deprecated configuration property [{0}], should use [{1}].  " +
049                         "Please delete the deprecated property in order for the new property to take effect.",
050                         oldName, newName);
051            }
052            return value;
053        }
054    
055        /**
056         * Fetches a property using both a deprecated name and the new name. The deprecated property
057         * has precedence over the new name. If the deprecated name is used a warning is written to
058         * the log.
059         *
060         * @param conf configuration object.
061         * @param newName new property name.
062         * @param oldName deprecated property name.
063         * @param defaultValue default value.
064         * @return the property value, or the default value if not found under the deprecated name and the new name.
065         */
066        public static boolean getWithDeprecatedCheck(Configuration conf, String newName, String oldName,
067                                                     boolean defaultValue) {
068            String value = getWithDeprecatedCheck(conf, newName, oldName, Boolean.toString(defaultValue));
069            return Boolean.parseBoolean(value);
070        }
071    
072        /**
073         * Returns the HTTP or HTTPS URL for this Oozie server
074         * (http://HOSTNAME:HTTP_PORT/oozie or https://HOSTNAME:HTTPS_PORT/oozie)
075         *
076         * @param secure true to return the HTTPS URL or false to return the HTTP URL
077         * @return the HTTP or HTTPS URL for this Oozie server
078         */
079        public static String getOozieURL(boolean secure) {
080            StringBuilder sb = new StringBuilder();
081            if (secure) {
082                sb.append("https://");
083            }
084            else {
085                sb.append("http://");
086            }
087            sb.append(Services.get().getConf().get("oozie.http.hostname"));
088            sb.append(":");
089            if (secure) {
090                sb.append(Services.get().getConf().get("oozie.https.port"));
091            }
092            else {
093                sb.append(Services.get().getConf().get("oozie.http.port"));
094            }
095            sb.append("/oozie");
096            return sb.toString();
097        }
098    
099        /**
100         * Returns the HTTP or HTTPS URL for this Oozie server depending on which is actually configured
101         *
102         * @return the HTTP or HTTPS URL for this Oozie server
103         */
104        public static String getOozieEffectiveUrl() {
105            return getOozieURL(ServicesLoader.isSSLEnabled());
106        }
107    }