configuration.java

来自「Hadoop是一个用于运行应用程序在大型集群的廉价硬件设备上的框架。Hadoop」· Java 代码 · 共 483 行 · 第 1/2 页

JAVA
483
字号
/** * Copyright 2005 The Apache Software Foundation * * Licensed 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.util.*;import java.net.URL;import java.io.*;import java.util.logging.Logger;import javax.xml.parsers.*;import org.w3c.dom.*;import javax.xml.transform.TransformerFactory;import javax.xml.transform.Transformer;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import org.apache.hadoop.util.LogFormatter;/** Provides access to configuration parameters.  Configurations are specified * by resources.  A resource contains a set of name/value pairs. * * <p>Each resources is named by either a String or by a File.  If named by a * String, then the classpath is examined for a file with that name.  If a * File, then the filesystem is examined directly, without referring to the * CLASSPATH. * * <p>Configuration resources are of two types: default and * final.  Default values are loaded first and final values are loaded last, and * thus override default values. * * <p>Hadoop's default resource is the String "hadoop-default.xml" and its * final resource is the String "hadoop-site.xml".  Other tools built on Hadoop * may specify additional resources. */public class Configuration {  private static final Logger LOG =    LogFormatter.getLogger("org.apache.hadoop.conf.Configuration");  private ArrayList defaultResources = new ArrayList();  private ArrayList finalResources = new ArrayList();  private Properties properties;  private ClassLoader classLoader =     Thread.currentThread().getContextClassLoader();  /** A new configuration. */  public Configuration() {    defaultResources.add("hadoop-default.xml");    finalResources.add("hadoop-site.xml");  }  /** A new configuration with the same settings cloned from another. */  public Configuration(Configuration other) {    this.defaultResources = (ArrayList)other.defaultResources.clone();    this.finalResources = (ArrayList)other.finalResources.clone();    if (other.properties != null)      this.properties = (Properties)other.properties.clone();  }  /** Add a default resource. */  public void addDefaultResource(String name) {    addResource(defaultResources, name);  }  /** Add a default resource. */  public void addDefaultResource(File file) {    addResource(defaultResources, file);  }  /** Add a final resource. */  public void addFinalResource(String name) {    addResource(finalResources, name);  }  /** Add a final resource. */  public void addFinalResource(File file) {    addResource(finalResources, file);  }  private synchronized void addResource(ArrayList resources, Object resource) {    resources.add(resource);                      // add to resources    properties = null;                            // trigger reload  }    /**   * Returns the value of the <code>name</code> property, or null if no such   * property exists.   */  public Object getObject(String name) { return getProps().get(name);}  /** Sets the value of the <code>name</code> property. */  public void setObject(String name, Object value) {    getProps().put(name, value);  }  /** Returns the value of the <code>name</code> property.  If no such property   * exists, then <code>defaultValue</code> is returned.   */  public Object get(String name, Object defaultValue) {    Object res = getObject(name);    if (res != null) return res;    else return defaultValue;  }    /** Returns the value of the <code>name</code> property, or null if no   * such property exists. */  public String get(String name) { return getProps().getProperty(name);}  /** Sets the value of the <code>name</code> property. */  public void set(String name, Object value) {    getProps().setProperty(name, value.toString());  }    /** Returns the value of the <code>name</code> property.  If no such property   * exists, then <code>defaultValue</code> is returned.   */  public String get(String name, String defaultValue) {     return getProps().getProperty(name, defaultValue);  }    /** Returns the value of the <code>name</code> property as an integer.  If no   * such property is specified, or if the specified value is not a valid   * integer, then <code>defaultValue</code> is returned.   */  public int getInt(String name, int defaultValue) {    String valueString = get(name);    if (valueString == null)      return defaultValue;    try {      return Integer.parseInt(valueString);    } catch (NumberFormatException e) {      return defaultValue;    }  }  /** Sets the value of the <code>name</code> property to an integer. */  public void setInt(String name, int value) {    set(name, Integer.toString(value));  }  /** Returns the value of the <code>name</code> property as a long.  If no   * such property is specified, or if the specified value is not a valid   * long, then <code>defaultValue</code> is returned.   */  public long getLong(String name, long defaultValue) {    String valueString = get(name);    if (valueString == null)      return defaultValue;    try {      return Long.parseLong(valueString);    } catch (NumberFormatException e) {      return defaultValue;    }  }  /** Sets the value of the <code>name</code> property to a long. */  public void setLong(String name, long value) {    set(name, Long.toString(value));  }  /** Returns the value of the <code>name</code> property as a float.  If no   * such property is specified, or if the specified value is not a valid   * float, then <code>defaultValue</code> is returned.   */  public float getFloat(String name, float defaultValue) {    String valueString = get(name);    if (valueString == null)      return defaultValue;    try {      return Float.parseFloat(valueString);    } catch (NumberFormatException e) {      return defaultValue;    }  }  /** Returns the value of the <code>name</code> property as an boolean.  If no   * such property is specified, or if the specified value is not a valid   * boolean, then <code>defaultValue</code> is returned.  Valid boolean values   * are "true" and "false".   */  public boolean getBoolean(String name, boolean defaultValue) {    String valueString = get(name);    if ("true".equals(valueString))      return true;    else if ("false".equals(valueString))      return false;    else return defaultValue;  }  /** Sets the value of the <code>name</code> property to an integer. */  public void setBoolean(String name, boolean value) {    set(name, Boolean.toString(value));  }  /** Returns the value of the <code>name</code> property as an array of   * strings.  If no such property is specified, then <code>null</code>   * is returned.  Values are whitespace or comma delimted.   */  public String[] getStrings(String name) {    String valueString = get(name);    if (valueString == null)      return null;    StringTokenizer tokenizer = new StringTokenizer (valueString,", \t\n\r\f");    List values = new ArrayList();    while (tokenizer.hasMoreTokens()) {      values.add(tokenizer.nextToken());    }    return (String[])values.toArray(new String[values.size()]);  }  /** Returns the value of the <code>name</code> property as a Class.  If no   * such property is specified, then <code>defaultValue</code> is returned.   */  public Class getClass(String name, Class defaultValue) {    String valueString = get(name);    if (valueString == null)      return defaultValue;    try {      return Class.forName(valueString);    } catch (ClassNotFoundException e) {      throw new RuntimeException(e);    }  }  /** Returns the value of the <code>name</code> property as a Class.  If no   * such property is specified, then <code>defaultValue</code> is returned.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?