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 com.google.common.base.Preconditions; 022import org.apache.hadoop.conf.Configuration; 023import org.apache.hadoop.mapreduce.MRJobConfig; 024import org.apache.oozie.client.OozieClient; 025import org.apache.oozie.service.ConfigurationService; 026import org.apache.oozie.service.StatusTransitService; 027import org.apache.oozie.servlet.ServicesLoader; 028 029/** 030 * 031 */ 032public class ConfigUtils { 033 private final static XLog LOG = XLog.getLog(ConfigUtils.class); 034 public static final String OOZIE_HTTPS_ENABLED = "oozie.https.enabled"; 035 public static final String OOZIE_HTTP_HOSTNAME = "oozie.http.hostname"; 036 public static final String OOZIE_HTTPS_PORT = "oozie.https.port"; 037 public static final String OOZIE_HTTP_PORT = "oozie.http.port"; 038 039 public static boolean BOOLEAN_DEFAULT = false; 040 public static String STRING_DEFAULT = ""; 041 public static int INT_DEFAULT = 0; 042 public static float FLOAT_DEFAULT = 0f; 043 public static long LONG_DEFAULT = 0l; 044 045 /** 046 * Fetches a property using both a deprecated name and the new name. The deprecated property 047 * has precedence over the new name. If the deprecated name is used a warning is written to 048 * the log. 049 * 050 * @param conf configuration object. 051 * @param newName new property name. 052 * @param oldName deprecated property name. 053 * @param defaultValue default value. 054 * @return the property value, or the default value if not found under the deprecated name and the new name. 055 */ 056 public static String getWithDeprecatedCheck(Configuration conf, String newName, String oldName, 057 String defaultValue) { 058 String value = conf.get(oldName, null); 059 if (value == null) { 060 value = conf.get(newName, defaultValue); 061 } 062 else { 063 LOG.warn("Using a deprecated configuration property [{0}], should use [{1}]. " + 064 "Please delete the deprecated property in order for the new property to take effect.", 065 oldName, newName); 066 } 067 return value; 068 } 069 070 /** 071 * Fetches a property using both a deprecated name and the new name. The deprecated property 072 * has precedence over the new name. If the deprecated name is used a warning is written to 073 * the log. 074 * 075 * @param conf configuration object. 076 * @param newName new property name. 077 * @param oldName deprecated property name. 078 * @param defaultValue default value. 079 * @return the property value, or the default value if not found under the deprecated name and the new name. 080 */ 081 public static boolean getWithDeprecatedCheck(Configuration conf, String newName, String oldName, 082 boolean defaultValue) { 083 String value = getWithDeprecatedCheck(conf, newName, oldName, Boolean.toString(defaultValue)); 084 return Boolean.parseBoolean(value); 085 } 086 087 /** 088 * Returns the HTTP or HTTPS URL for this Oozie server 089 * (http://HOSTNAME:HTTP_PORT/oozie or https://HOSTNAME:HTTPS_PORT/oozie) 090 * 091 * @param secure true to return the HTTPS URL or false to return the HTTP URL 092 * @return the HTTP or HTTPS URL for this Oozie server 093 */ 094 public static String getOozieURL(boolean secure) { 095 StringBuilder sb = new StringBuilder(); 096 if (secure) { 097 sb.append("https://"); 098 } 099 else { 100 sb.append("http://"); 101 } 102 sb.append(ConfigurationService.get(OOZIE_HTTP_HOSTNAME)); 103 sb.append(":"); 104 if (secure) { 105 sb.append(ConfigurationService.get(OOZIE_HTTPS_PORT)); 106 } 107 else { 108 sb.append(ConfigurationService.get(OOZIE_HTTP_PORT)); 109 } 110 sb.append("/oozie"); 111 return sb.toString(); 112 } 113 114 /** 115 * Returns the HTTP or HTTPS URL for this Oozie server depending on which is actually configured 116 * 117 * @return the HTTP or HTTPS URL for this Oozie server 118 */ 119 public static String getOozieEffectiveUrl() { 120 return getOozieURL(ServicesLoader.isSSLEnabled() || ConfigurationService.getBoolean(OOZIE_HTTPS_ENABLED)); 121 } 122 123 public static boolean isBackwardSupportForCoordStatus() { 124 return ConfigurationService.getBoolean(StatusTransitService.CONF_BACKWARD_SUPPORT_FOR_COORD_STATUS); 125 } 126 127 /** 128 * Check {@link Configuration} whether it contains disallowed properties. Given configuration property values of 129 * {@code oozie-site.xml#oozie.configuration.check-and-set.DISALLOWED.PROPERTY_KEY}, {@code newValue} and {@code performWrite} 130 * values, one of the following will happen: 131 * <ul> 132 * <li>{@code base} doesn't contain any disallowed property: NOP</li> 133 * <li>{@code base} contains a disallowed property but its value is the same as {@code newValue}: NOP</li> 134 * <li>{@code base} contains a conflicting disallowed property and configuration tells we should also set, but 135 * {@code performWrite=false}: NOP</li> 136 * <li>{@code base} contains a conflicting disallowed property and configuration tells we should also set, and 137 * {@code performWrite=true}: {@code base} will be overwritten by {@code key=newValue}</li> 138 * <li>{@code base} contains a conflicting disallowed property and configuration tells we should not set: 139 * {@code Exception toThrow} is thrown</li> 140 * </ul> 141 * @param base the {@link Configuration} to check 142 * @param newValue the new value to assign if {@code performWrite=true} and if configuration value 143 * {@code oozie.configuration.check-and-set.*} is set 144 * @param toThrow the {@link Exception} to throw when {@code oozie.configuration.check-and-set.*} is unset 145 * @param performWrite 146 * @param <E> {@link Exception} type 147 * @throws E the {@link Exception} to throw when {@code oozie.configuration.check-and-set.*} is unset 148 */ 149 public static <E extends Exception> void checkAndSetDisallowedProperties(final Configuration base, 150 final String newValue, 151 final E toThrow, 152 final boolean performWrite) throws E { 153 Preconditions.checkNotNull(base, "base"); 154 Preconditions.checkNotNull(newValue, "newValue"); 155 Preconditions.checkNotNull(toThrow, "toThrow"); 156 157 for (final String defaultDisallowedProperty : PropertiesUtils.DEFAULT_DISALLOWED_PROPERTIES) { 158 checkAndSetConfigValue(base, defaultDisallowedProperty, newValue, toThrow, performWrite); 159 } 160 } 161 162 private static <E extends Exception> void checkAndSetConfigValue(final Configuration base, 163 final String key, 164 final String newValue, 165 final E toThrow, 166 final boolean performWrite) throws E { 167 final boolean shouldCheckAndSet = ConfigurationService.getBoolean("oozie.configuration.check-and-set." + key, false); 168 final boolean isPresent = base.get(key) != null; 169 if (isPresent && !base.get(key).equals(newValue)) { 170 LOG.trace("Base configuration contains config property [{0}={1}] that is different from new value [{2}]", 171 key, 172 base.get(key), 173 newValue); 174 if (shouldCheckAndSet && performWrite) { 175 LOG.trace("Setting [{0}] to [{1}]", key, newValue); 176 base.set(key, newValue); 177 } 178 else if (!shouldCheckAndSet) { 179 LOG.error("Cannot set [{0}] to [{1}]. {2}", key, newValue, toThrow.getMessage()); 180 throw toThrow; 181 } 182 } 183 } 184}