我们来讲讲Configuration类,从名字可以看出,这个类跟配置文件有关,它的继承关系如下:
这个类比较大,我们先从一个点切入,后面有需要会追加其他部分,我们的切入点是getDefaultUri函数,但如下:
public static URI getDefaultUri(Configuration conf) {
return URI.create(fixName(conf.get(FS_DEFAULT_NAME_KEY, DEFAULT_FS)));
}
这个函数用来创建一个URI类对象,我们先进入conf.get调用中,这个函数代码如下:
/**
* Get the value of the <code>name</code>. 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 <code>defaultValue</code> is returned.
*
* @param name property name, will be trimmed before get value.
* @param defaultValue default value.
* @return property value, or <code>defaultValue</code> 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;
}
这里面getprecationContext是什么类对象呢?这个对象的创建代码如下:
/**
* The global DeprecationContext.
*/
private static AtomicReference<DeprecationContext> deprecationContext =
new AtomicReference<DeprecationContext>(new DeprecationContext(null, defaultDeprecations));
可以看到,这个类对象是一个原子引用(详细请自己查阅相关资料,这里不再赘述),真正是DeprecationContext类对象,这个类到底有什么用呢?根据相关描述,这个类的对象是不可变的,类对象中存储了所有被废弃的key的集合。它的成员变量如下:
/**
* Stores the deprecated keys, the new keys which replace the deprecated keys
* and custom message(if any provided).
//存储废弃的key,代替废弃key的新key,以及相关描述,意思是说,这个map中存储了废弃的key以及代替该废弃key的新key,同时包 //括一些描述信息
*/
private final Map<String, DeprecatedKeyInfo> deprecatedKeyMap;
/**
* Stores a mapping from superseding keys to the keys which they deprecate.
//存储废弃的key以及取代废弃key的新key
*/
private final Map<String, String> reverseDeprecatedKeyMap;
构造函数如下:
/**
* Create a new DeprecationContext by copying a previous DeprecationContext
* and adding some deltas.
*//创建一个新的DeprecationContext类对象,将other对象中的数据拷贝到该对象中,同时添加一些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类对象
HashMap<String, DeprecatedKeyInfo> newDeprecatedKeyMap = new HashMap<String, DeprecatedKeyInfo>();
//创建HashMap类对象
HashMap<String, String> newReverseDeprecatedKeyMap = new HashMap<String, String>();
//如果other不为null,那么就将other对象成员变量deprecatedKeyMap中的key-value拷贝到newDeprecatedKeyMap中,同理将
//other对象成员变量reverseDeprecatedKeyMap的key-value拷贝到newReverseDeprecatedKeyMap中。
if (other != null) {
for (Entry<String, DeprecatedKeyInfo> entry : other.deprecatedKeyMap.entrySet()) {
newDeprecatedKeyMap.put(entry.getKey(), entry.getValue());
}
for (Entry<String, String> entry : other.reverseDeprecatedKeyMap.entrySet()) {
newReverseDeprecatedKeyMap.put(entry.getKey(), entry.getValue());
}
}
//开始遍历deltas
for (DeprecationDelta delta : deltas) {
//如果newDeprecatedKeyMap不包含delta中的key,那么就将delta中的数据添加到newDeprecatedKeyMap中,同时将key为新 //key,value为废弃key添加到newReverseDeprecatedKeyMap中.
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);
}
}
}
//至此,newDeprecatedKeyMap和newReverseDeprecatedKeyMap数据就赋值成功了,总结一下,newDeprecatedKeyMap中 //key为废弃的key,value为DeprecatedKeyInfo类对象,这个对象存储了新key和相应的关于key之间代替描述信息,
//而newReverseDeprecatedKeyMap这个对象中存储了key为新key,value为老key的信息。
//接下来分别创建两个UnmodifiableMap类对象,这个对象中的map对象分别为newDeprecatedKeyMap和 //newReverseDeprecatedKeyMap,同时UnmodifiableMap类对象实现了Map接口,同时里面的map变量时不可修改的。
this.deprecatedKeyMap = UnmodifiableMap.decorate(newDeprecatedKeyMap);
this.reverseDeprecatedKeyMap = UnmodifiableMap.decorate(newReverseDeprecatedKeyMap);
}
另外,defaultDeprecations赋值代码如下:
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("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),
new DeprecationDelta("dfs.nfs.exports.allowed.hosts",
CommonConfigurationKeys.NFS_EXPORTS_ALLOWED_HOSTS_KEY)
};