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