configuration.java

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

JAVA
483
字号
   * An error is thrown if the returned class does not implement the named   * interface.    */  public Class getClass(String propertyName, Class defaultValue,Class xface) {    try {      Class theClass = getClass(propertyName, defaultValue);      if (theClass != null && !xface.isAssignableFrom(theClass))        throw new RuntimeException(theClass+" not "+xface.getName());      return theClass;    } catch (Exception e) {      throw new RuntimeException(e);    }  }  /** Sets the value of the <code>name</code> property to the name of a class.   * First checks that the class implements the named interface.    */  public void setClass(String propertyName, Class theClass, Class xface) {    if (!xface.isAssignableFrom(theClass))      throw new RuntimeException(theClass+" not "+xface.getName());    set(propertyName, theClass.getName());  }  /** Returns a file name under a directory named in <i>dirsProp</i> with the   * given <i>path</i>.  If <i>dirsProp</i> contains multiple directories, then   * one is chosen based on <i>path</i>'s hash code.  If the selected directory   * does not exist, an attempt is made to create it.   */  public File getFile(String dirsProp, String path) throws IOException {    String[] dirs = getStrings(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).getAbsoluteFile();      File dir = file.getParentFile();      if (dir.exists() || dir.mkdirs()) {        return file;      }    }    throw new IOException("No valid local directories in property: "+dirsProp);  }  /** Returns the URL for the named resource. */  public URL getResource(String name) {    return classLoader.getResource(name);  }  /** Returns an input stream attached to the configuration resource with the   * given <code>name</code>.   */  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;    }  }  /** Returns a reader attached to the configuration resource with the   * given <code>name</code>.   */  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;    }  }  private synchronized Properties getProps() {    if (properties == null) {      Properties newProps = new Properties();      loadResources(newProps, defaultResources, false, false);      loadResources(newProps, finalResources, true, true);      properties = newProps;    }    return properties;  }  private void loadResources(Properties props,                             ArrayList resources,                             boolean reverse, boolean quiet) {    ListIterator i = resources.listIterator(reverse ? resources.size() : 0);    while (reverse ? i.hasPrevious() : i.hasNext()) {      loadResource(props, reverse ? i.previous() : i.next(), quiet);    }  }  private void loadResource(Properties properties, Object name, boolean quiet) {    try {      DocumentBuilder builder =        DocumentBuilderFactory.newInstance().newDocumentBuilder();      Document doc = null;      if (name instanceof String) {               // a CLASSPATH resource        URL url = getResource((String)name);        if (url != null) {          LOG.info("parsing " + url);          doc = builder.parse(url.toString());        }      } else if (name instanceof File) {          // a file resource        File file = (File)name;        if (file.exists()) {          LOG.info("parsing " + file);          doc = builder.parse(file);        }      }      if (doc == null) {        if (quiet)          return;        throw new RuntimeException(name + " not found");      }      Element root = doc.getDocumentElement();      if (!"configuration".equals(root.getTagName()))        LOG.severe("bad conf file: top-level element not <configuration>");      NodeList props = root.getChildNodes();      for (int i = 0; i < props.getLength(); i++) {        Node propNode = props.item(i);        if (!(propNode instanceof Element))          continue;        Element prop = (Element)propNode;        if (!"property".equals(prop.getTagName()))          LOG.warning("bad conf file: element not <property>");        NodeList fields = prop.getChildNodes();        String attr = null;        String value = null;        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()))            attr = ((Text)field.getFirstChild()).getData();          if ("value".equals(field.getTagName()) && field.hasChildNodes())            value = ((Text)field.getFirstChild()).getData();        }        if (attr != null && value != null)          properties.setProperty(attr, value);      }            } catch (Exception e) {      LOG.severe("error parsing conf file: " + e);      throw new RuntimeException(e);    }      }  /** Writes non-default properties in this configuration.*/  public void write(OutputStream out) throws IOException {    Properties properties = getProps();    try {      Document doc =        DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();      Element conf = doc.createElement("configuration");      doc.appendChild(conf);      conf.appendChild(doc.createTextNode("\n"));      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);        conf.appendChild(doc.createTextNode("\n"));      }          DOMSource source = new DOMSource(doc);      StreamResult result = new StreamResult(out);      TransformerFactory transFactory = TransformerFactory.newInstance();      Transformer transformer = transFactory.newTransformer();      transformer.transform(source, result);    } catch (Exception e) {      throw new RuntimeException(e);    }  }  public String toString() {    StringBuffer sb = new StringBuffer();    sb.append("Configuration: ");    sb.append("defaults: ");    toString(defaultResources, sb);    sb.append("final: ");    toString(finalResources, sb);    return sb.toString();  }  private void toString(ArrayList resources, StringBuffer sb) {    ListIterator i = resources.listIterator();    while (i.hasNext()) {      if (i.nextIndex() != 0) {        sb.append(" , ");      }      Object obj = i.next();      if (obj instanceof File) {        sb.append((File)obj);      } else {        sb.append((String)obj);      }    }  }  /** For debugging.  List non-default properties to the terminal and exit. */  public static void main(String[] args) throws Exception {    new Configuration().write(System.out);  }}

⌨️ 快捷键说明

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