Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.conf;
import java.io.BufferedInputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.lang.ref.WeakReference;
import java.net.InetSocketAddress;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.WeakHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.commons.collections.map.UnmodifiableMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.CommonConfigurationKeys;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableUtils;
import org.apache.hadoop.net.NetUtils;
import org.apache.hadoop.util.ReflectionUtils;
import org.apache.hadoop.util.StringInterner;
import org.apache.hadoop.util.StringUtils;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;
import com.google.common.base.Preconditions;
/**
* Provides access to configuration parameters.
*
*
Resources
*
*
Configurations are specified by resources. A resource contains a set of
* name/value pairs as XML data. Each resource is named by either a
* String or by a {@link Path}. If named by a String,
* then the classpath is examined for a file with that name. If named by a
* Path, then the local filesystem is examined directly, without
* referring to the classpath.
*
*
Unless explicitly turned off, Hadoop by default specifies two
* resources, loaded in-order from the classpath:
core-site.xml: Site-specific configuration for a given hadoop
* installation.
*
* Applications may add additional resources, which are loaded
* subsequent to these resources in the order they are added.
*
*
Final Parameters
*
*
Configuration parameters may be declared final.
* Once a resource declares a value final, no subsequently-loaded
* resource can alter that value.
* For example, one might define a final parameter with:
*
*
* When conf.get("tempdir") is called, then ${basedir}
* will be resolved to another property in this Configuration, while
* ${user.name} would then ordinarily be resolved to the value
* of the System property with that name.
*/
@InterfaceAudience.Public
@InterfaceStability.Stable
public class Configuration implements Iterable>,
Writable {
private static final Log LOG =
LogFactory.getLog(Configuration.class);
private boolean quietmode = true;
private static class Resource {
private final Object resource;
private final String name;
public Resource(Object resource) {
this(resource, resource.toString());
}
public Resource(Object resource, String name) {
this.resource = resource;
this.name = name;
}
public String getName(){
return name;
}
public Object getResource() {
return resource;
}
@Override
public String toString() {
return name;
}
}
/**
* List of configuration resources.
*/
private ArrayList resources = new ArrayList();
/**
* The value reported as the setting resource when a key is set
* by code rather than a file resource by dumpConfiguration.
*/
static final String UNKNOWN_RESOURCE = "Unknown";
/**
* List of configuration parameters marked final.
*/
private Set finalParameters = new HashSet();
private boolean loadDefaults = true;
/**
* Configuration objects
*/
private static final WeakHashMap REGISTRY =
new WeakHashMap();
/**
* List of default Resources. Resources are loaded in the order of the list
* entries
*/
private static final CopyOnWriteArrayList defaultResources =
new CopyOnWriteArrayList();
private static final Map>>>
CACHE_CLASSES = new WeakHashMap>>>();
/**
* Sentinel value to store negative cache results in {@link #CACHE_CLASSES}.
*/
private static final Class> NEGATIVE_CACHE_SENTINEL =
NegativeCacheSentinel.class;
/**
* Stores the mapping of key to the resource which modifies or loads
* the key most recently
*/
private HashMap updatingResource;
/**
* Class to keep the information about the keys which replace the deprecated
* ones.
*
* This class stores the new keys which replace the deprecated keys and also
* gives a provision to have a custom message for each of the deprecated key
* that is being replaced. It also provides method to get the appropriate
* warning message which can be logged whenever the deprecated key is used.
*/
private static class DeprecatedKeyInfo {
private final String[] newKeys;
private final String customMessage;
private final AtomicBoolean accessed = new AtomicBoolean(false);
DeprecatedKeyInfo(String[] newKeys, String customMessage) {
this.newKeys = newKeys;
this.customMessage = customMessage;
}
/**
* Method to provide the warning message. It gives the custom message if
* non-null, and default message otherwise.
* @param key the associated deprecated key.
* @return message that is to be logged when a deprecated key is used.
*/
private final String getWarningMessage(String key) {
String warningMessage;
if(customMessage == null) {
StringBuilder message = new StringBuilder(key);
String deprecatedKeySuffix = " is deprecated. Instead, use ";
message.append(deprecatedKeySuffix);
for (int i = 0; i < newKeys.length; i++) {
message.append(newKeys[i]);
if(i != newKeys.length-1) {
message.append(", ");
}
}
warningMessage = message.toString();
}
else {
warningMessage = customMessage;
}
return warningMessage;
}
boolean getAndSetAccessed() {
return accessed.getAndSet(true);
}
public void clearAccessed() {
accessed.set(false);
}
}
/**
* A pending addition to the global set of deprecated keys.
*/
public static class DeprecationDelta {
private final String key;
private final String[] newKeys;
private final String customMessage;
DeprecationDelta(String key, String[] newKeys, String customMessage) {
Preconditions.checkNotNull(key);
Preconditions.checkNotNull(newKeys);
Preconditions.checkArgument(newKeys.length > 0);
this.key = key;
this.newKeys = newKeys;
this.customMessage = customMessage;
}
public DeprecationDelta(String key, String newKey, String customMessage) {
this(key, new String[] { newKey }, customMessage);
}
public DeprecationDelta(String key, String newKey) {
this(key, new String[] { newKey }, null);
}
public String getKey() {
return key;
}
public String[] getNewKeys() {
return newKeys;
}
public String getCustomMessage() {
return customMessage;
}
}
/**
* The set of all keys which are deprecated.
*
* DeprecationContext objects are immutable.
*/
private static class DeprecationContext {
/**
* Stores the deprecated keys, the new keys which replace the deprecated keys
* and custom message(if any provided).
*/
private final Map deprecatedKeyMap;
/**
* Stores a mapping from superseding keys to the keys which they deprecate.
*/
private final Map reverseDeprecatedKeyMap;
/**
* Create a new DeprecationContext by copying a previous DeprecationContext
* and adding some deltas.
*
* @param other The previous deprecation context to copy, or null to start
* from nothing.
* @param deltas The deltas to apply.
*/
@SuppressWarnings("unchecked")
DeprecationContext(DeprecationContext other, DeprecationDelta[] deltas) {
HashMap newDeprecatedKeyMap =
new HashMap();
HashMap newReverseDeprecatedKeyMap =
new HashMap();
if (other != null) {
for (Entry entry :
other.deprecatedKeyMap.entrySet()) {
newDeprecatedKeyMap.put(entry.getKey(), entry.getValue());
}
for (Entry entry :
other.reverseDeprecatedKeyMap.entrySet()) {
newReverseDeprecatedKeyMap.put(entry.getKey(), entry.getValue());
}
}
for (DeprecationDelta delta : deltas) {
if (!newDeprecatedKeyMap.containsKey(delta.getKey())) {
DeprecatedKeyInfo newKeyInfo =
new DeprecatedKeyInfo(delta.getNewKeys(), delta.getCustomMessage());
newDeprecatedKeyMap.put(delta.key, newKeyInfo);
for (String newKey : delta.getNewKeys()) {
newReverseDeprecatedKeyMap.put(newKey, delta.key);
}
}
}
this.deprecatedKeyMap =
UnmodifiableMap.decorate(newDeprecatedKeyMap);
this.reverseDeprecatedKeyMap =
UnmodifiableMap.decorate(newReverseDeprecatedKeyMap);
}
Map getDeprecatedKeyMap() {
return deprecatedKeyMap;
}
Map getReverseDeprecatedKeyMap() {
return reverseDeprecatedKeyMap;
}
}
private static DeprecationDelta[] defaultDeprecations =
new DeprecationDelta[] {
new DeprecationDelta("topology.script.file.name",
CommonConfigurationKeys.NET_TOPOLOGY_SCRIPT_FILE_NAME_KEY),
new DeprecationDelta("topology.script.number.args",
CommonConfigurationKeys.NET_TOPOLOGY_SCRIPT_NUMBER_ARGS_KEY),
new DeprecationDelta("hadoop.configured.node.mapping",
CommonConfigurationKeys.NET_TOPOLOGY_CONFIGURED_NODE_MAPPING_KEY),
new DeprecationDelta("topology.node.switch.mapping.impl",
CommonConfigurationKeys.NET_TOPOLOGY_NODE_SWITCH_MAPPING_IMPL_KEY),
new DeprecationDelta("dfs.df.interval",
CommonConfigurationKeys.FS_DF_INTERVAL_KEY),
new DeprecationDelta("dfs.client.buffer.dir",
CommonConfigurationKeys.FS_CLIENT_BUFFER_DIR_KEY),
new DeprecationDelta("hadoop.native.lib",
CommonConfigurationKeys.IO_NATIVE_LIB_AVAILABLE_KEY),
new DeprecationDelta("fs.default.name",
CommonConfigurationKeys.FS_DEFAULT_NAME_KEY),
new DeprecationDelta("dfs.umaskmode",
CommonConfigurationKeys.FS_PERMISSIONS_UMASK_KEY)
};
/**
* The global DeprecationContext.
*/
private static AtomicReference deprecationContext =
new AtomicReference(
new DeprecationContext(null, defaultDeprecations));
/**
* Adds a set of deprecated keys to the global deprecations.
*
* This method is lockless. It works by means of creating a new
* DeprecationContext based on the old one, and then atomically swapping in
* the new context. If someone else updated the context in between us reading
* the old context and swapping in the new one, we try again until we win the
* race.
*
* @param deltas The deprecations to add.
*/
public static void addDeprecations(DeprecationDelta[] deltas) {
DeprecationContext prev, next;
do {
prev = deprecationContext.get();
next = new DeprecationContext(prev, deltas);
} while (!deprecationContext.compareAndSet(prev, next));
}
/**
* Adds the deprecated key to the global deprecation map.
* It does not override any existing entries in the deprecation map.
* This is to be used only by the developers in order to add deprecation of
* keys, and attempts to call this method after loading resources once,
* would lead to UnsupportedOperationException
*
* If a key is deprecated in favor of multiple keys, they are all treated as
* aliases of each other, and setting any one of them resets all the others
* to the new value.
*
* If you have multiple deprecation entries to add, it is more efficient to
* use #addDeprecations(DeprecationDelta[] deltas) instead.
*
* @param key
* @param newKeys
* @param customMessage
* @deprecated use {@link #addDeprecation(String key, String newKey,
String customMessage)} instead
*/
@Deprecated
public static void addDeprecation(String key, String[] newKeys,
String customMessage) {
addDeprecations(new DeprecationDelta[] {
new DeprecationDelta(key, newKeys, customMessage)
});
}
/**
* Adds the deprecated key to the global deprecation map.
* It does not override any existing entries in the deprecation map.
* This is to be used only by the developers in order to add deprecation of
* keys, and attempts to call this method after loading resources once,
* would lead to UnsupportedOperationException
*
* If you have multiple deprecation entries to add, it is more efficient to
* use #addDeprecations(DeprecationDelta[] deltas) instead.
*
* @param key
* @param newKey
* @param customMessage
*/
public static void addDeprecation(String key, String newKey,
String customMessage) {
addDeprecation(key, new String[] {newKey}, customMessage);
}
/**
* Adds the deprecated key to the global deprecation map when no custom
* message is provided.
* It does not override any existing entries in the deprecation map.
* This is to be used only by the developers in order to add deprecation of
* keys, and attempts to call this method after loading resources once,
* would lead to UnsupportedOperationException
*
* If a key is deprecated in favor of multiple keys, they are all treated as
* aliases of each other, and setting any one of them resets all the others
* to the new value.
*
* If you have multiple deprecation entries to add, it is more efficient to
* use #addDeprecations(DeprecationDelta[] deltas) instead.
*
* @param key Key that is to be deprecated
* @param newKeys list of keys that take up the values of deprecated key
* @deprecated use {@link #addDeprecation(String key, String newKey)} instead
*/
@Deprecated
public static void addDeprecation(String key, String[] newKeys) {
addDeprecation(key, newKeys, null);
}
/**
* Adds the deprecated key to the global deprecation map when no custom
* message is provided.
* It does not override any existing entries in the deprecation map.
* This is to be used only by the developers in order to add deprecation of
* keys, and attempts to call this method after loading resources once,
* would lead to UnsupportedOperationException
*
* If you have multiple deprecation entries to add, it is more efficient to
* use #addDeprecations(DeprecationDelta[] deltas) instead.
*
* @param key Key that is to be deprecated
* @param newKey key that takes up the value of deprecated key
*/
public static void addDeprecation(String key, String newKey) {
addDeprecation(key, new String[] {newKey}, null);
}
/**
* checks whether the given key is deprecated.
*
* @param key the parameter which is to be checked for deprecation
* @return true if the key is deprecated and
* false otherwise.
*/
public static boolean isDeprecated(String key) {
return deprecationContext.get().getDeprecatedKeyMap().containsKey(key);
}
/**
* Returns the alternate name for a key if the property name is deprecated
* or if deprecates a property name.
*
* @param name property name.
* @return alternate name.
*/
private String[] getAlternateNames(String name) {
String altNames[] = null;
DeprecationContext cur = deprecationContext.get();
DeprecatedKeyInfo keyInfo = cur.getDeprecatedKeyMap().get(name);
if (keyInfo == null) {
altNames = (cur.getReverseDeprecatedKeyMap().get(name) != null ) ?
new String [] {cur.getReverseDeprecatedKeyMap().get(name)} : null;
if(altNames != null && altNames.length > 0) {
//To help look for other new configs for this deprecated config
keyInfo = cur.getDeprecatedKeyMap().get(altNames[0]);
}
}
if(keyInfo != null && keyInfo.newKeys.length > 0) {
List list = new ArrayList();
if(altNames != null) {
list.addAll(Arrays.asList(altNames));
}
list.addAll(Arrays.asList(keyInfo.newKeys));
altNames = list.toArray(new String[list.size()]);
}
return altNames;
}
/**
* Checks for the presence of the property name in the
* deprecation map. Returns the first of the list of new keys if present
* in the deprecation map or the name itself. If the property
* is not presently set but the property map contains an entry for the
* deprecated key, the value of the deprecated key is set as the value for
* the provided property name.
*
* @param name the property name
* @return the first property in the list of properties mapping
* the name or the name itself.
*/
private String[] handleDeprecation(DeprecationContext deprecations,
String name) {
ArrayList names = new ArrayList();
if (isDeprecated(name)) {
DeprecatedKeyInfo keyInfo = deprecations.getDeprecatedKeyMap().get(name);
warnOnceIfDeprecated(deprecations, name);
for (String newKey : keyInfo.newKeys) {
if(newKey != null) {
names.add(newKey);
}
}
}
if(names.size() == 0) {
names.add(name);
}
for(String n : names) {
String deprecatedKey = deprecations.getReverseDeprecatedKeyMap().get(n);
if (deprecatedKey != null && !getOverlay().containsKey(n) &&
getOverlay().containsKey(deprecatedKey)) {
getProps().setProperty(n, getOverlay().getProperty(deprecatedKey));
getOverlay().setProperty(n, getOverlay().getProperty(deprecatedKey));
}
}
return names.toArray(new String[names.size()]);
}
private void handleDeprecation() {
LOG.debug("Handling deprecation for all properties in config...");
DeprecationContext deprecations = deprecationContext.get();
Set
property, null if
* no such property exists. If the key is deprecated, it returns the value of
* the first key which replaces the deprecated key and is not null
*
* Values are processed for variable expansion
* before being returned.
*
* @param name the property name.
* @return the value of the name or its replacing property,
* or null if no such property exists.
*/
public String get(String name) {
String[] names = handleDeprecation(deprecationContext.get(), name);
String result = null;
for(String n : names) {
result = substituteVars(getProps().getProperty(n));
}
return result;
}
/**
* Get the value of the name property as a trimmed String,
* null if no such property exists.
* If the key is deprecated, it returns the value of
* the first key which replaces the deprecated key and is not null
*
* Values are processed for variable expansion
* before being returned.
*
* @param name the property name.
* @return the value of the name or its replacing property,
* or null if no such property exists.
*/
public String getTrimmed(String name) {
String value = get(name);
if (null == value) {
return null;
} else {
return value.trim();
}
}
/**
* Get the value of the name property as a trimmed String,
* defaultVal if no such property exists.
* See @{Configuration#getTrimmed} for more details.
*
* @param name the property name.
* @param defaultVal the property default value.
* @return the value of the name or defaultVal
* if it is not set.
*/
public String getTrimmed(String name, String defaultValue) {
String ret = getTrimmed(name);
return ret == null ? defaultValue : ret;
}
/**
* Get the value of the name property, without doing
* variable expansion.If the key is
* deprecated, it returns the value of the first key which replaces
* the deprecated key and is not null.
*
* @param name the property name.
* @return the value of the name property or
* its replacing property and null if no such property exists.
*/
public String getRaw(String name) {
String[] names = handleDeprecation(deprecationContext.get(), name);
String result = null;
for(String n : names) {
result = getProps().getProperty(n);
}
return result;
}
/**
* Set the value of the name property. If
* name is deprecated or there is a deprecated name associated to it,
* it sets the value to both names.
*
* @param name property name.
* @param value property value.
*/
public void set(String name, String value) {
set(name, value, null);
}
/**
* Set the value of the name property. If
* name is deprecated or there is a deprecated name associated to it,
* it sets the value to both names.
*
* @param name property name.
* @param value property value.
* @param source the place that this configuration value came from
* (For debugging).
*/
public void set(String name, String value, String source) {
Preconditions.checkArgument(
name != null,
"Property name must not be null");
Preconditions.checkArgument(
value != null,
"Property value must not be null");
DeprecationContext deprecations = deprecationContext.get();
if (deprecations.getDeprecatedKeyMap().isEmpty()) {
getProps();
}
getOverlay().setProperty(name, value);
getProps().setProperty(name, value);
if(source == null) {
updatingResource.put(name, new String[] {"programatically"});
} else {
updatingResource.put(name, new String[] {source});
}
String[] altNames = getAlternateNames(name);
if (altNames != null && altNames.length > 0) {
String altSource = "because " + name + " is deprecated";
for(String altName : altNames) {
if(!altName.equals(name)) {
getOverlay().setProperty(altName, value);
getProps().setProperty(altName, value);
updatingResource.put(altName, new String[] {altSource});
}
}
}
warnOnceIfDeprecated(deprecations, name);
}
private void warnOnceIfDeprecated(DeprecationContext deprecations, String name) {
DeprecatedKeyInfo keyInfo = deprecations.getDeprecatedKeyMap().get(name);
if (keyInfo != null && !keyInfo.getAndSetAccessed()) {
LOG.warn(keyInfo.getWarningMessage(name));
}
}
/**
* Unset a previously set property.
*/
public synchronized void unset(String name) {
String[] altNames = getAlternateNames(name);
getOverlay().remove(name);
getProps().remove(name);
if (altNames !=null && altNames.length > 0) {
for(String altName : altNames) {
getOverlay().remove(altName);
getProps().remove(altName);
}
}
}
/**
* Sets a property if it is currently unset.
* @param name the property name
* @param value the new value
*/
public synchronized void setIfUnset(String name, String value) {
if (get(name) == null) {
set(name, value);
}
}
private synchronized Properties getOverlay() {
if (overlay==null){
overlay=new Properties();
}
return overlay;
}
/**
* Get the value of the name. If the key is deprecated,
* it returns the value of the first key which replaces the deprecated key
* and is not null.
* If no such property exists,
* then defaultValue is returned.
*
* @param name property name.
* @param defaultValue default value.
* @return property value, or defaultValue if the property
* doesn't exist.
*/
public String get(String name, String defaultValue) {
String[] names = handleDeprecation(deprecationContext.get(), name);
String result = null;
for(String n : names) {
result = substituteVars(getProps().getProperty(n, defaultValue));
}
return result;
}
/**
* Get the value of the name property as an int.
*
* If no such property exists, the provided default value is returned,
* or if the specified value is not a valid int,
* then an error is thrown.
*
* @param name property name.
* @param defaultValue default value.
* @throws NumberFormatException when the value is invalid
* @return property value as an int,
* or defaultValue.
*/
public int getInt(String name, int defaultValue) {
String valueString = getTrimmed(name);
if (valueString == null)
return defaultValue;
String hexString = getHexDigits(valueString);
if (hexString != null) {
return Integer.parseInt(hexString, 16);
}
return Integer.parseInt(valueString);
}
/**
* Get the value of the name property as a set of comma-delimited
* int values.
*
* If no such property exists, an empty array is returned.
*
* @param name property name
* @return property value interpreted as an array of comma-delimited
* int values
*/
public int[] getInts(String name) {
String[] strings = getTrimmedStrings(name);
int[] ints = new int[strings.length];
for (int i = 0; i < strings.length; i++) {
ints[i] = Integer.parseInt(strings[i]);
}
return ints;
}
/**
* Set the value of the name property to an int.
*
* @param name property name.
* @param value int value of the property.
*/
public void setInt(String name, int value) {
set(name, Integer.toString(value));
}
/**
* Get the value of the name property as a long.
* If no such property exists, the provided default value is returned,
* or if the specified value is not a valid long,
* then an error is thrown.
*
* @param name property name.
* @param defaultValue default value.
* @throws NumberFormatException when the value is invalid
* @return property value as a long,
* or defaultValue.
*/
public long getLong(String name, long defaultValue) {
String valueString = getTrimmed(name);
if (valueString == null)
return defaultValue;
String hexString = getHexDigits(valueString);
if (hexString != null) {
return Long.parseLong(hexString, 16);
}
return Long.parseLong(valueString);
}
/**
* Get the value of the name property as a long or
* human readable format. If no such property exists, the provided default
* value is returned, or if the specified value is not a valid
* long or human readable format, then an error is thrown. You
* can use the following suffix (case insensitive): k(kilo), m(mega), g(giga),
* t(tera), p(peta), e(exa)
*
* @param name property name.
* @param defaultValue default value.
* @throws NumberFormatException when the value is invalid
* @return property value as a long,
* or defaultValue.
*/
public long getLongBytes(String name, long defaultValue) {
String valueString = getTrimmed(name);
if (valueString == null)
return defaultValue;
return StringUtils.TraditionalBinaryPrefix.string2long(valueString);
}
private String getHexDigits(String value) {
boolean negative = false;
String str = value;
String hexString = null;
if (value.startsWith("-")) {
negative = true;
str = value.substring(1);
}
if (str.startsWith("0x") || str.startsWith("0X")) {
hexString = str.substring(2);
if (negative) {
hexString = "-" + hexString;
}
return hexString;
}
return null;
}
/**
* Set the value of the name property to a long.
*
* @param name property name.
* @param value long value of the property.
*/
public void setLong(String name, long value) {
set(name, Long.toString(value));
}
/**
* Get the value of the name property as a float.
* If no such property exists, the provided default value is returned,
* or if the specified value is not a valid float,
* then an error is thrown.
*
* @param name property name.
* @param defaultValue default value.
* @throws NumberFormatException when the value is invalid
* @return property value as a float,
* or defaultValue.
*/
public float getFloat(String name, float defaultValue) {
String valueString = getTrimmed(name);
if (valueString == null)
return defaultValue;
return Float.parseFloat(valueString);
}
/**
* Set the value of the name property to a float.
*
* @param name property name.
* @param value property value.
*/
public void setFloat(String name, float value) {
set(name,Float.toString(value));
}
/**
* Get the value of the name property as a double.
* If no such property exists, the provided default value is returned,
* or if the specified value is not a valid double,
* then an error is thrown.
*
* @param name property name.
* @param defaultValue default value.
* @throws NumberFormatException when the value is invalid
* @return property value as a double,
* or defaultValue.
*/
public double getDouble(String name, double defaultValue) {
String valueString = getTrimmed(name);
if (valueString == null)
return defaultValue;
return Double.parseDouble(valueString);
}
/**
* Set the value of the name property to a double.
*
* @param name property name.
* @param value property value.
*/
public void setDouble(String name, double value) {
set(name,Double.toString(value));
}
/**
* Get the value of the name property as a boolean.
* If no such property is specified, or if the specified value is not a valid
* boolean, then defaultValue is returned.
*
* @param name property name.
* @param defaultValue default value.
* @return property value as a boolean,
* or defaultValue.
*/
public boolean getBoolean(String name, boolean defaultValue) {
String valueString = getTrimmed(name);
if (null == valueString || "".equals(valueString)) {
return defaultValue;
}
valueString = valueString.toLowerCase();
if ("true".equals(valueString))
return true;
else if ("false".equals(valueString))
return false;
else return defaultValue;
}
/**
* Set the value of the name property to a boolean.
*
* @param name property name.
* @param value boolean value of the property.
*/
public void setBoolean(String name, boolean value) {
set(name, Boolean.toString(value));
}
/**
* Set the given property, if it is currently unset.
* @param name property name
* @param value new value
*/
public void setBooleanIfUnset(String name, boolean value) {
setIfUnset(name, Boolean.toString(value));
}
/**
* Set the value of the name property to the given type. This
* is equivalent to set(<name>, value.toString()).
* @param name property name
* @param value new value
*/
public > void setEnum(String name, T value) {
set(name, value.toString());
}
/**
* Return value matching this enumerated type.
* @param name Property name
* @param defaultValue Value returned if no mapping exists
* @throws IllegalArgumentException If mapping is illegal for the type
* provided
*/
public > T getEnum(String name, T defaultValue) {
final String val = get(name);
return null == val
? defaultValue
: Enum.valueOf(defaultValue.getDeclaringClass(), val);
}
/**
* Get the value of the name property as a Pattern.
* If no such property is specified, or if the specified value is not a valid
* Pattern, then DefaultValue is returned.
*
* @param name property name
* @param defaultValue default value
* @return property value as a compiled Pattern, or defaultValue
*/
public Pattern getPattern(String name, Pattern defaultValue) {
String valString = get(name);
if (null == valString || "".equals(valString)) {
return defaultValue;
}
try {
return Pattern.compile(valString);
} catch (PatternSyntaxException pse) {
LOG.warn("Regular expression '" + valString + "' for property '" +
name + "' not valid. Using default", pse);
return defaultValue;
}
}
/**
* Set the given property to Pattern.
* If the pattern is passed as null, sets the empty pattern which results in
* further calls to getPattern(...) returning the default value.
*
* @param name property name
* @param pattern new value
*/
public void setPattern(String name, Pattern pattern) {
if (null == pattern) {
set(name, null);
} else {
set(name, pattern.pattern());
}
}
/**
* Gets information about why a property was set. Typically this is the
* path to the resource objects (file, URL, etc.) the property came from, but
* it can also indicate that it was set programatically, or because of the
* command line.
*
* @param name - The property name to get the source of.
* @return null - If the property or its source wasn't found. Otherwise,
* returns a list of the sources of the resource. The older sources are
* the first ones in the list. So for example if a configuration is set from
* the command line, and then written out to a file that is read back in the
* first entry would indicate that it was set from the command line, while
* the second one would indicate the file that the new configuration was read
* in from.
*/
@InterfaceStability.Unstable
public synchronized String[] getPropertySources(String name) {
if (properties == null) {
// If properties is null, it means a resource was newly added
// but the props were cleared so as to load it upon future
// requests. So lets force a load by asking a properties list.
getProps();
}
// Return a null right away if our properties still
// haven't loaded or the resource mapping isn't defined
if (properties == null || updatingResource == null) {
return null;
} else {
String[] source = updatingResource.get(name);
if(source == null) {
return null;
} else {
return Arrays.copyOf(source, source.length);
}
}
}
/**
* A class that represents a set of positive integer ranges. It parses
* strings of the form: "2-3,5,7-" where ranges are separated by comma and
* the lower/upper bounds are separated by dash. Either the lower or upper
* bound may be omitted meaning all values up to or over. So the string
* above means 2, 3, 5, and 7, 8, 9, ...
*/
public static class IntegerRanges implements Iterable{
private static class Range {
int start;
int end;
}
private static class RangeNumberIterator implements Iterator {
Iterator internal;
int at;
int end;
public RangeNumberIterator(List ranges) {
if (ranges != null) {
internal = ranges.iterator();
}
at = -1;
end = -2;
}
@Override
public boolean hasNext() {
if (at <= end) {
return true;
} else if (internal != null){
return internal.hasNext();
}
return false;
}
@Override
public Integer next() {
if (at <= end) {
at++;
return at - 1;
} else if (internal != null){
Range found = internal.next();
if (found != null) {
at = found.start;
end = found.end;
at++;
return at - 1;
}
}
return null;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
List ranges = new ArrayList();
public IntegerRanges() {
}
public IntegerRanges(String newValue) {
StringTokenizer itr = new StringTokenizer(newValue, ",");
while (itr.hasMoreTokens()) {
String rng = itr.nextToken().trim();
String[] parts = rng.split("-", 3);
if (parts.length < 1 || parts.length > 2) {
throw new IllegalArgumentException("integer range badly formed: " +
rng);
}
Range r = new Range();
r.start = convertToInt(parts[0], 0);
if (parts.length == 2) {
r.end = convertToInt(parts[1], Integer.MAX_VALUE);
} else {
r.end = r.start;
}
if (r.start > r.end) {
throw new IllegalArgumentException("IntegerRange from " + r.start +
" to " + r.end + " is invalid");
}
ranges.add(r);
}
}
/**
* Convert a string to an int treating empty strings as the default value.
* @param value the string value
* @param defaultValue the value for if the string is empty
* @return the desired integer
*/
private static int convertToInt(String value, int defaultValue) {
String trim = value.trim();
if (trim.length() == 0) {
return defaultValue;
}
return Integer.parseInt(trim);
}
/**
* Is the given value in the set of ranges
* @param value the value to check
* @return is the value in the ranges?
*/
public boolean isIncluded(int value) {
for(Range r: ranges) {
if (r.start <= value && value <= r.end) {
return true;
}
}
return false;
}
/**
* @return true if there are no values in this range, else false.
*/
public boolean isEmpty() {
return ranges == null || ranges.isEmpty();
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
boolean first = true;
for(Range r: ranges) {
if (first) {
first = false;
} else {
result.append(',');
}
result.append(r.start);
result.append('-');
result.append(r.end);
}
return result.toString();
}
@Override
public Iterator iterator() {
return new RangeNumberIterator(ranges);
}
}
/**
* Parse the given attribute as a set of integer ranges
* @param name the attribute name
* @param defaultValue the default value if it is not set
* @return a new set of ranges from the configured value
*/
public IntegerRanges getRange(String name, String defaultValue) {
return new IntegerRanges(get(name, defaultValue));
}
/**
* Get the comma delimited values of the name property as
* a collection of Strings.
* If no such property is specified then empty collection is returned.
*
* This is an optimized version of {@link #getStrings(String)}
*
* @param name property name.
* @return property value as a collection of Strings.
*/
public Collection getStringCollection(String name) {
String valueString = get(name);
return StringUtils.getStringCollection(valueString);
}
/**
* Get the comma delimited values of the name property as
* an array of Strings.
* If no such property is specified then null is returned.
*
* @param name property name.
* @return property value as an array of Strings,
* or null.
*/
public String[] getStrings(String name) {
String valueString = get(name);
return StringUtils.getStrings(valueString);
}
/**
* Get the comma delimited values of the name property as
* an array of Strings.
* If no such property is specified then default value is returned.
*
* @param name property name.
* @param defaultValue The default value
* @return property value as an array of Strings,
* or default value.
*/
public String[] getStrings(String name, String... defaultValue) {
String valueString = get(name);
if (valueString == null) {
return defaultValue;
} else {
return StringUtils.getStrings(valueString);
}
}
/**
* Get the comma delimited values of the name property as
* a collection of Strings, trimmed of the leading and trailing whitespace.
* If no such property is specified then empty Collection is returned.
*
* @param name property name.
* @return property value as a collection of Strings, or empty Collection
*/
public Collection getTrimmedStringCollection(String name) {
String valueString = get(name);
if (null == valueString) {
Collection empty = new ArrayList();
return empty;
}
return StringUtils.getTrimmedStringCollection(valueString);
}
/**
* Get the comma delimited values of the name property as
* an array of Strings, trimmed of the leading and trailing whitespace.
* If no such property is specified then an empty array is returned.
*
* @param name property name.
* @return property value as an array of trimmed Strings,
* or empty array.
*/
public String[] getTrimmedStrings(String name) {
String valueString = get(name);
return StringUtils.getTrimmedStrings(valueString);
}
/**
* Get the comma delimited values of the name property as
* an array of Strings, trimmed of the leading and trailing whitespace.
* If no such property is specified then default value is returned.
*
* @param name property name.
* @param defaultValue The default value
* @return property value as an array of trimmed Strings,
* or default value.
*/
public String[] getTrimmedStrings(String name, String... defaultValue) {
String valueString = get(name);
if (null == valueString) {
return defaultValue;
} else {
return StringUtils.getTrimmedStrings(valueString);
}
}
/**
* Set the array of string values for the name property as
* as comma delimited values.
*
* @param name property name.
* @param values The values
*/
public void setStrings(String name, String... values) {
set(name, StringUtils.arrayToString(values));
}
/**
* Get the socket address for name property as a
* InetSocketAddress.
* @param name property name.
* @param defaultAddress the default value
* @param defaultPort the default port
* @return InetSocketAddress
*/
public InetSocketAddress getSocketAddr(
String name, String defaultAddress, int defaultPort) {
final String address = get(name, defaultAddress);
return NetUtils.createSocketAddr(address, defaultPort, name);
}
/**
* Set the socket address for the name property as
* a host:port.
*/
public void setSocketAddr(String name, InetSocketAddress addr) {
set(name, NetUtils.getHostPortString(addr));
}
/**
* Set the socket address a client can use to connect for the
* name property as a host:port. The wildcard
* address is replaced with the local host's address.
* @param name property name.
* @param addr InetSocketAddress of a listener to store in the given property
* @return InetSocketAddress for clients to connect
*/
public InetSocketAddress updateConnectAddr(String name,
InetSocketAddress addr) {
final InetSocketAddress connectAddr = NetUtils.getConnectAddress(addr);
setSocketAddr(name, connectAddr);
return connectAddr;
}
/**
* Load a class by name.
*
* @param name the class name.
* @return the class object.
* @throws ClassNotFoundException if the class is not found.
*/
public Class> getClassByName(String name) throws ClassNotFoundException {
Class> ret = getClassByNameOrNull(name);
if (ret == null) {
throw new ClassNotFoundException("Class " + name + " not found");
}
return ret;
}
/**
* Load a class by name, returning null rather than throwing an exception
* if it couldn't be loaded. This is to avoid the overhead of creating
* an exception.
*
* @param name the class name
* @return the class object, or null if it could not be found.
*/
public Class> getClassByNameOrNull(String name) {
Map>> map;
synchronized (CACHE_CLASSES) {
map = CACHE_CLASSES.get(classLoader);
if (map == null) {
map = Collections.synchronizedMap(
new WeakHashMap>>());
CACHE_CLASSES.put(classLoader, map);
}
}
Class> clazz = null;
WeakReference> ref = map.get(name);
if (ref != null) {
clazz = ref.get();
}
if (clazz == null) {
try {
clazz = Class.forName(name, true, classLoader);
} catch (ClassNotFoundException e) {
// Leave a marker that the class isn't found
map.put(name, new WeakReference>(NEGATIVE_CACHE_SENTINEL));
return null;
}
// two putters can race here, but they'll put the same class
map.put(name, new WeakReference>(clazz));
return clazz;
} else if (clazz == NEGATIVE_CACHE_SENTINEL) {
return null; // not found
} else {
// cache hit
return clazz;
}
}
/**
* Get the value of the name property
* as an array of Class.
* The value of the property specifies a list of comma separated class names.
* If no such property is specified, then defaultValue is
* returned.
*
* @param name the property name.
* @param defaultValue default value.
* @return property value as a Class[],
* or defaultValue.
*/
public Class>[] getClasses(String name, Class> ... defaultValue) {
String[] classnames = getTrimmedStrings(name);
if (classnames == null)
return defaultValue;
try {
Class>[] classes = new Class>[classnames.length];
for(int i = 0; i < classnames.length; i++) {
classes[i] = getClassByName(classnames[i]);
}
return classes;
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
/**
* Get the value of the name property as a Class.
* If no such property is specified, then defaultValue is
* returned.
*
* @param name the class name.
* @param defaultValue default value.
* @return property value as a Class,
* or defaultValue.
*/
public Class> getClass(String name, Class> defaultValue) {
String valueString = getTrimmed(name);
if (valueString == null)
return defaultValue;
try {
return getClassByName(valueString);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
/**
* Get the value of the name property as a Class
* implementing the interface specified by xface.
*
* If no such property is specified, then defaultValue is
* returned.
*
* An exception is thrown if the returned class does not implement the named
* interface.
*
* @param name the class name.
* @param defaultValue default value.
* @param xface the interface implemented by the named class.
* @return property value as a Class,
* or defaultValue.
*/
public Class extends U> getClass(String name,
Class extends U> defaultValue,
Class xface) {
try {
Class> theClass = getClass(name, defaultValue);
if (theClass != null && !xface.isAssignableFrom(theClass))
throw new RuntimeException(theClass+" not "+xface.getName());
else if (theClass != null)
return theClass.asSubclass(xface);
else
return null;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Get the value of the name property as a List
* of objects implementing the interface specified by xface.
*
* An exception is thrown if any of the classes does not exist, or if it does
* not implement the named interface.
*
* @param name the property name.
* @param xface the interface implemented by the classes named by
* name.
* @return a List of objects implementing xface.
*/
@SuppressWarnings("unchecked")
public List getInstances(String name, Class xface) {
List ret = new ArrayList();
Class>[] classes = getClasses(name);
for (Class> cl: classes) {
if (!xface.isAssignableFrom(cl)) {
throw new RuntimeException(cl + " does not implement " + xface);
}
ret.add((U)ReflectionUtils.newInstance(cl, this));
}
return ret;
}
/**
* Set the value of the name property to the name of a
* theClass implementing the given interface xface.
*
* An exception is thrown if theClass does not implement the
* interface xface.
*
* @param name property name.
* @param theClass property value.
* @param xface the interface implemented by the named class.
*/
public void setClass(String name, Class> theClass, Class> xface) {
if (!xface.isAssignableFrom(theClass))
throw new RuntimeException(theClass+" not "+xface.getName());
set(name, theClass.getName());
}
/**
* Get a local file under a directory named by dirsProp with
* the given path. If dirsProp contains multiple directories,
* then one is chosen based on path's hash code. If the selected
* directory does not exist, an attempt is made to create it.
*
* @param dirsProp directory in which to locate the file.
* @param path file-path.
* @return local file under the directory with the given path.
*/
public Path getLocalPath(String dirsProp, String path)
throws IOException {
String[] dirs = getTrimmedStrings(dirsProp);
int hashCode = path.hashCode();
FileSystem fs = FileSystem.getLocal(this);
for (int i = 0; i < dirs.length; i++) { // try each local dir
int index = (hashCode+i & Integer.MAX_VALUE) % dirs.length;
Path file = new Path(dirs[index], path);
Path dir = file.getParent();
if (fs.mkdirs(dir) || fs.exists(dir)) {
return file;
}
}
LOG.warn("Could not make " + path +
" in local directories from " + dirsProp);
for(int i=0; i < dirs.length; i++) {
int index = (hashCode+i & Integer.MAX_VALUE) % dirs.length;
LOG.warn(dirsProp + "[" + index + "]=" + dirs[index]);
}
throw new IOException("No valid local directories in property: "+dirsProp);
}
/**
* Get a local file name under a directory named in dirsProp with
* the given path. If dirsProp contains multiple directories,
* then one is chosen based on path's hash code. If the selected
* directory does not exist, an attempt is made to create it.
*
* @param dirsProp directory in which to locate the file.
* @param path file-path.
* @return local file under the directory with the given path.
*/
public File getFile(String dirsProp, String path)
throws IOException {
String[] dirs = getTrimmedStrings(dirsProp);
int hashCode = path.hashCode();
for (int i = 0; i < dirs.length; i++) { // try each local dir
int index = (hashCode+i & Integer.MAX_VALUE) % dirs.length;
File file = new File(dirs[index], path);
File dir = file.getParentFile();
if (dir.exists() || dir.mkdirs()) {
return file;
}
}
throw new IOException("No valid local directories in property: "+dirsProp);
}
/**
* Get the {@link URL} for the named resource.
*
* @param name resource name.
* @return the url for the named resource.
*/
public URL getResource(String name) {
return classLoader.getResource(name);
}
/**
* Get an input stream attached to the configuration resource with the
* given name.
*
* @param name configuration resource name.
* @return an input stream attached to the resource.
*/
public InputStream getConfResourceAsInputStream(String name) {
try {
URL url= getResource(name);
if (url == null) {
LOG.info(name + " not found");
return null;
} else {
LOG.info("found resource " + name + " at " + url);
}
return url.openStream();
} catch (Exception e) {
return null;
}
}
/**
* Get a {@link Reader} attached to the configuration resource with the
* given name.
*
* @param name configuration resource name.
* @return a reader attached to the resource.
*/
public Reader getConfResourceAsReader(String name) {
try {
URL url= getResource(name);
if (url == null) {
LOG.info(name + " not found");
return null;
} else {
LOG.info("found resource " + name + " at " + url);
}
return new InputStreamReader(url.openStream());
} catch (Exception e) {
return null;
}
}
protected synchronized Properties getProps() {
if (properties == null) {
properties = new Properties();
HashMap backup =
new HashMap(updatingResource);
loadResources(properties, resources, quietmode);
if (overlay!= null) {
properties.putAll(overlay);
for (Map.Entry item: overlay.entrySet()) {
String key = (String)item.getKey();
updatingResource.put(key, backup.get(key));
}
}
}
return properties;
}
/**
* Return the number of keys in the configuration.
*
* @return number of keys in the configuration.
*/
public int size() {
return getProps().size();
}
/**
* Clears all keys from the configuration.
*/
public void clear() {
getProps().clear();
getOverlay().clear();
}
/**
* Get an {@link Iterator} to go through the list of String
* key-value pairs in the configuration.
*
* @return an iterator over the entries.
*/
public Iterator> iterator() {
// Get a copy of just the string to string pairs. After the old object
// methods that allow non-strings to be put into configurations are removed,
// we could replace properties with a Map and get rid of this
// code.
Map result = new HashMap();
for(Map.Entry item: getProps().entrySet()) {
if (item.getKey() instanceof String &&
item.getValue() instanceof String) {
result.put((String) item.getKey(), (String) item.getValue());
}
}
return result.entrySet().iterator();
}
private Document parse(DocumentBuilder builder, URL url)
throws IOException, SAXException {
if (!quietmode) {
LOG.info("parsing URL " + url);
}
if (url == null) {
return null;
}
return parse(builder, url.openStream(), url.toString());
}
private Document parse(DocumentBuilder builder, InputStream is,
String systemId) throws IOException, SAXException {
if (!quietmode) {
LOG.info("parsing input stream " + is);
}
if (is == null) {
return null;
}
try {
return (systemId == null) ? builder.parse(is) : builder.parse(is,
systemId);
} finally {
is.close();
}
}
private void loadResources(Properties properties,
ArrayList resources,
boolean quiet) {
if(loadDefaults) {
for (String resource : defaultResources) {
loadResource(properties, new Resource(resource), quiet);
}
//support the hadoop-site.xml as a deprecated case
if(getResource("hadoop-site.xml")!=null) {
loadResource(properties, new Resource("hadoop-site.xml"), quiet);
}
}
for (int i = 0; i < resources.size(); i++) {
Resource ret = loadResource(properties, resources.get(i), quiet);
if (ret != null) {
resources.set(i, ret);
}
}
}
private Resource loadResource(Properties properties, Resource wrapper, boolean quiet) {
String name = UNKNOWN_RESOURCE;
try {
Object resource = wrapper.getResource();
name = wrapper.getName();
DocumentBuilderFactory docBuilderFactory
= DocumentBuilderFactory.newInstance();
//ignore all comments inside the xml file
docBuilderFactory.setIgnoringComments(true);
//allow includes in the xml file
docBuilderFactory.setNamespaceAware(true);
try {
docBuilderFactory.setXIncludeAware(true);
} catch (UnsupportedOperationException e) {
LOG.error("Failed to set setXIncludeAware(true) for parser "
+ docBuilderFactory
+ ":" + e,
e);
}
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
Document doc = null;
Element root = null;
boolean returnCachedProperties = false;
if (resource instanceof URL) { // an URL resource
doc = parse(builder, (URL)resource);
} else if (resource instanceof String) { // a CLASSPATH resource
URL url = getResource((String)resource);
doc = parse(builder, url);
} else if (resource instanceof Path) { // a file resource
// Can't use FileSystem API or we get an infinite loop
// since FileSystem uses Configuration API. Use java.io.File instead.
File file = new File(((Path)resource).toUri().getPath())
.getAbsoluteFile();
if (file.exists()) {
if (!quiet) {
LOG.info("parsing File " + file);
}
doc = parse(builder, new BufferedInputStream(
new FileInputStream(file)), ((Path)resource).toString());
}
} else if (resource instanceof InputStream) {
doc = parse(builder, (InputStream) resource, null);
returnCachedProperties = true;
} else if (resource instanceof Properties) {
overlay(properties, (Properties)resource);
} else if (resource instanceof Element) {
root = (Element)resource;
}
if (doc == null && root == null) {
if (quiet)
return null;
throw new RuntimeException(resource + " not found");
}
if (root == null) {
root = doc.getDocumentElement();
}
Properties toAddTo = properties;
if(returnCachedProperties) {
toAddTo = new Properties();
}
if (!"configuration".equals(root.getTagName()))
LOG.fatal("bad conf file: top-level element not ");
NodeList props = root.getChildNodes();
DeprecationContext deprecations = deprecationContext.get();
for (int i = 0; i < props.getLength(); i++) {
Node propNode = props.item(i);
if (!(propNode instanceof Element))
continue;
Element prop = (Element)propNode;
if ("configuration".equals(prop.getTagName())) {
loadResource(toAddTo, new Resource(prop, name), quiet);
continue;
}
if (!"property".equals(prop.getTagName()))
LOG.warn("bad conf file: element not ");
NodeList fields = prop.getChildNodes();
String attr = null;
String value = null;
boolean finalParameter = false;
LinkedList source = new LinkedList();
for (int j = 0; j < fields.getLength(); j++) {
Node fieldNode = fields.item(j);
if (!(fieldNode instanceof Element))
continue;
Element field = (Element)fieldNode;
if ("name".equals(field.getTagName()) && field.hasChildNodes())
attr = StringInterner.weakIntern(
((Text)field.getFirstChild()).getData().trim());
if ("value".equals(field.getTagName()) && field.hasChildNodes())
value = StringInterner.weakIntern(
((Text)field.getFirstChild()).getData());
if ("final".equals(field.getTagName()) && field.hasChildNodes())
finalParameter = "true".equals(((Text)field.getFirstChild()).getData());
if ("source".equals(field.getTagName()) && field.hasChildNodes())
source.add(StringInterner.weakIntern(
((Text)field.getFirstChild()).getData()));
}
source.add(name);
// Ignore this parameter if it has already been marked as 'final'
if (attr != null) {
if (deprecations.getDeprecatedKeyMap().containsKey(attr)) {
DeprecatedKeyInfo keyInfo =
deprecations.getDeprecatedKeyMap().get(attr);
keyInfo.clearAccessed();
for (String key:keyInfo.newKeys) {
// update new keys with deprecated key's value
loadProperty(toAddTo, name, key, value, finalParameter,
source.toArray(new String[source.size()]));
}
}
else {
loadProperty(toAddTo, name, attr, value, finalParameter,
source.toArray(new String[source.size()]));
}
}
}
if (returnCachedProperties) {
overlay(properties, toAddTo);
return new Resource(toAddTo, name);
}
return null;
} catch (IOException e) {
LOG.fatal("error parsing conf " + name, e);
throw new RuntimeException(e);
} catch (DOMException e) {
LOG.fatal("error parsing conf " + name, e);
throw new RuntimeException(e);
} catch (SAXException e) {
LOG.fatal("error parsing conf " + name, e);
throw new RuntimeException(e);
} catch (ParserConfigurationException e) {
LOG.fatal("error parsing conf " + name , e);
throw new RuntimeException(e);
}
}
private void overlay(Properties to, Properties from) {
for (Entry entry: from.entrySet()) {
to.put(entry.getKey(), entry.getValue());
}
}
private void loadProperty(Properties properties, String name, String attr,
String value, boolean finalParameter, String[] source) {
if (value != null) {
if (!finalParameters.contains(attr)) {
properties.setProperty(attr, value);
updatingResource.put(attr, source);
} else if (!value.equals(properties.getProperty(attr))) {
LOG.warn(name+":an attempt to override final parameter: "+attr
+"; Ignoring.");
}
}
if (finalParameter) {
finalParameters.add(attr);
}
}
/**
* Write out the non-default properties in this configuration to the given
* {@link OutputStream}.
*
* @param out the output stream to write to.
*/
public void writeXml(OutputStream out) throws IOException {
writeXml(new OutputStreamWriter(out));
}
/**
* Write out the non-default properties in this configuration to the given
* {@link Writer}.
*
* @param out the writer to write to.
*/
public void writeXml(Writer out) throws IOException {
Document doc = asXmlDocument();
try {
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(out);
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
// Important to not hold Configuration log while writing result, since
// 'out' may be an HDFS stream which needs to lock this configuration
// from another thread.
transformer.transform(source, result);
} catch (TransformerException te) {
throw new IOException(te);
}
}
/**
* Return the XML DOM corresponding to this Configuration.
*/
private synchronized Document asXmlDocument() throws IOException {
Document doc;
try {
doc =
DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
} catch (ParserConfigurationException pe) {
throw new IOException(pe);
}
Element conf = doc.createElement("configuration");
doc.appendChild(conf);
conf.appendChild(doc.createTextNode("\n"));
handleDeprecation(); //ensure properties is set and deprecation is handled
for (Enumeration e = properties.keys(); e.hasMoreElements();) {
String name = (String)e.nextElement();
Object object = properties.get(name);
String value = null;
if (object instanceof String) {
value = (String) object;
}else {
continue;
}
Element propNode = doc.createElement("property");
conf.appendChild(propNode);
Element nameNode = doc.createElement("name");
nameNode.appendChild(doc.createTextNode(name));
propNode.appendChild(nameNode);
Element valueNode = doc.createElement("value");
valueNode.appendChild(doc.createTextNode(value));
propNode.appendChild(valueNode);
if (updatingResource != null) {
String[] sources = updatingResource.get(name);
if(sources != null) {
for(String s : sources) {
Element sourceNode = doc.createElement("source");
sourceNode.appendChild(doc.createTextNode(s));
propNode.appendChild(sourceNode);
}
}
}
conf.appendChild(doc.createTextNode("\n"));
}
return doc;
}
/**
* Writes out all the parameters and their properties (final and resource) to
* the given {@link Writer}
* The format of the output would be
* { "properties" : [ {key1,value1,key1.isFinal,key1.resource}, {key2,value2,
* key2.isFinal,key2.resource}... ] }
* It does not output the parameters of the configuration object which is
* loaded from an input stream.
* @param out the Writer to write to
* @throws IOException
*/
public static void dumpConfiguration(Configuration config,
Writer out) throws IOException {
JsonFactory dumpFactory = new JsonFactory();
JsonGenerator dumpGenerator = dumpFactory.createJsonGenerator(out);
dumpGenerator.writeStartObject();
dumpGenerator.writeFieldName("properties");
dumpGenerator.writeStartArray();
dumpGenerator.flush();
synchronized (config) {
for (Map.Entry item: config.getProps().entrySet()) {
dumpGenerator.writeStartObject();
dumpGenerator.writeStringField("key", (String) item.getKey());
dumpGenerator.writeStringField("value",
config.get((String) item.getKey()));
dumpGenerator.writeBooleanField("isFinal",
config.finalParameters.contains(item.getKey()));
String[] resources = config.updatingResource.get(item.getKey());
String resource = UNKNOWN_RESOURCE;
if(resources != null && resources.length > 0) {
resource = resources[0];
}
dumpGenerator.writeStringField("resource", resource);
dumpGenerator.writeEndObject();
}
}
dumpGenerator.writeEndArray();
dumpGenerator.writeEndObject();
dumpGenerator.flush();
}
/**
* Get the {@link ClassLoader} for this job.
*
* @return the correct class loader.
*/
public ClassLoader getClassLoader() {
return classLoader;
}
/**
* Set the class loader that will be used to load the various objects.
*
* @param classLoader the new class loader.
*/
public void setClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Configuration: ");
if(loadDefaults) {
toString(defaultResources, sb);
if(resources.size()>0) {
sb.append(", ");
}
}
toString(resources, sb);
return sb.toString();
}
private void toString(List resources, StringBuilder sb) {
ListIterator i = resources.listIterator();
while (i.hasNext()) {
if (i.nextIndex() != 0) {
sb.append(", ");
}
sb.append(i.next());
}
}
/**
* Set the quietness-mode.
*
* In the quiet-mode, error and informational messages might not be logged.
*
* @param quietmode true to set quiet-mode on, false
* to turn it off.
*/
public synchronized void setQuietMode(boolean quietmode) {
this.quietmode = quietmode;
}
synchronized boolean getQuietMode() {
return this.quietmode;
}
/** For debugging. List non-default properties to the terminal and exit. */
public static void main(String[] args) throws Exception {
new Configuration().writeXml(System.out);
}
@Override
public void readFields(DataInput in) throws IOException {
clear();
int size = WritableUtils.readVInt(in);
for(int i=0; i < size; ++i) {
String key = org.apache.hadoop.io.Text.readString(in);
String value = org.apache.hadoop.io.Text.readString(in);
set(key, value);
String sources[] = WritableUtils.readCompressedStringArray(in);
updatingResource.put(key, sources);
}
}
//@Override
public void write(DataOutput out) throws IOException {
Properties props = getProps();
WritableUtils.writeVInt(out, props.size());
for(Map.Entry item: props.entrySet()) {
org.apache.hadoop.io.Text.writeString(out, (String) item.getKey());
org.apache.hadoop.io.Text.writeString(out, (String) item.getValue());
WritableUtils.writeCompressedStringArray(out,
updatingResource.get(item.getKey()));
}
}
/**
* get keys matching the the regex
* @param regex
* @return Map with matching keys
*/
public Map getValByRegex(String regex) {
Pattern p = Pattern.compile(regex);
Map result = new HashMap();
Matcher m;
for(Map.Entry item: getProps().entrySet()) {
if (item.getKey() instanceof String &&
item.getValue() instanceof String) {
m = p.matcher((String)item.getKey());
if(m.find()) { // match
result.put((String) item.getKey(), (String) item.getValue());
}
}
}
return result;
}
/**
* A unique class which is used as a sentinel value in the caching
* for getClassByName. {@link Configuration#getClassByNameOrNull(String)}
*/
private static abstract class NegativeCacheSentinel {}
public static void dumpDeprecatedKeys() {
DeprecationContext deprecations = deprecationContext.get();
for (Map.Entry entry :
deprecations.getDeprecatedKeyMap().entrySet()) {
StringBuilder newKeys = new StringBuilder();
for (String newKey : entry.getValue().newKeys) {
newKeys.append(newKey).append("\t");
}
System.out.println(entry.getKey() + "\t" + newKeys.toString());
}
}
}