publicinterfaceConfigurable {/** Set the configuration to be used by this object. */void setConf(Configuration conf);
/** Return the configuration used by this object. */
Configuration getConf();
}
static{
//print deprecation warning if hadoop-site.xml is found in classpath
ClassLoader cL = Thread.currentThread().getContextClassLoader();
if (cL == null) {
cL = Configuration.class.getClassLoader();
}
if(cL.getResource("hadoop-site.xml")!=null) {
LOG.warn("DEPRECATED: hadoop-site.xml found in the classpath. " +
"Usage of hadoop-site.xml is deprecated. Instead use core-site.xml, "
+ "mapred-site.xml and hdfs-site.xml to override properties of " +
"core-default.xml, mapred-default.xml and hdfs-default.xml " +
"respectively");
}
addDefaultResource("core-default.xml");
addDefaultResource("core-site.xml");
}
首先,这个方法是synchronized修饰的。然后,这个REGISTRY在我们前面已经列出来了,是一个static final WeakHashMap<Configuration,Object>,后面我们看Configuration的构造方法的时候,会看到每产生一个Configuration对象,都会put到REGISTRY中去。由于REGISTRY是static final属性,所以全局都只有同一个REGISTRY。而这个defaultResources,则是static final CopyOnWriteArrayList<String>。代码的最后一行,是一个reloadConfiguration方法。继续看这个reloadConfiguration是个什么鬼:
/**
* Reload configuration from previously added resources.
*
* This method will clear all the configuration read from the added
* resources, and final parameters. This will make the resources to
* be read again before accessing the values. Values that are added
* via set methods will overlay values read from the resources.
*/publicsynchronizedvoidreloadConfiguration() {
properties = null; // trigger reload
finalParameters.clear(); // clear site-limits
}
特意把注释给添加上。这个方法的作用,注释里面已经说得很清楚了,就是 This method will clear all the configuration read from the added resources, and final parameters.
4.三个构造方法
static代码块执行完以后,开始执行构造方法:
/** A new configuration. */publicConfiguration() {
this(true);
}
/** A new configuration where the behavior of reading from the default
* resources can be turned off.
*
* If the parameter {@code loadDefaults} is false, the new instance
* will not load resources from the default files.
* @param loadDefaults specifies whether to load from the default files
*/publicConfiguration(boolean loadDefaults) {
this.loadDefaults = loadDefaults;
updatingResource = new HashMap<String, String[]>();
synchronized(Configuration.class) {
REGISTRY.put(this, null);
}
}
/**
* A new configuration with the same settings cloned from another.
*
* @param other the configuration from which to clone settings.
*/@SuppressWarnings("unchecked")
publicConfiguration(Configuration other) {
this.resources = (ArrayList<Resource>) other.resources.clone();
synchronized(other) {
if (other.properties != null) {
this.properties = (Properties)other.properties.clone();
}
if (other.overlay!=null) {
this.overlay = (Properties)other.overlay.clone();
}
this.updatingResource = new HashMap<String, String[]>(other.updatingResource);
this.finalParameters = new HashSet<String>(other.finalParameters);
}
synchronized(Configuration.class) {
REGISTRY.put(this, null);
}
this.classLoader = other.classLoader;
this.loadDefaults = other.loadDefaults;
setQuietMode(other.getQuietMode());
}