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