📄 configuration.java
字号:
} /** 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 getClassByName(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. * 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 local file 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 Path getLocalPath(String dirsProp, String path) throws IOException { String[] dirs = getStrings(dirsProp); int hashCode = path.hashCode(); FileSystem fs = FileSystem.getNamed("local", 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); } /** Returns a local 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); 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; if(overlay!=null) properties.putAll(overlay); } return properties; } /** @return Iterator< Map.Entry<String,String> > */ public Iterator entries() { return getProps().entrySet().iterator(); } 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 URL) { // an URL resource URL url = (URL)name; if (url != null) { LOG.info("parsing " + url); doc = builder.parse(url.toString()); } } else 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 Path) { // a file resource Path file = (Path)name; FileSystem fs = FileSystem.getNamed("local", this); if (fs.exists(file)) { LOG.info("parsing " + file); InputStream in = new BufferedInputStream(fs.openRaw(file)); try { doc = builder.parse(in); } finally { in.close(); } } } if (doc == null) { if (quiet) return; throw new RuntimeException(name + " not found"); } Element root = doc.getDocumentElement(); if (!"configuration".equals(root.getTagName())) LOG.fatal("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.warn("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.fatal("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); } } /** * Get the class loader 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; } 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(" , "); } sb.append(i.next()); } } /** 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -