1 2 3 4 package net.nutch.util; 5 6 import java.util.*; 7 import java.net.URL ; 8 import java.io.InputStream ; 9 import java.io.InputStreamReader ; 10 import java.io.Reader ; 11 import java.util.logging.Logger ; 12 import javax.xml.parsers.*; 13 import org.w3c.dom.*; 14 15 23 24 public class NutchConf { 25 private static final Logger LOG = 26 LogFormatter.getLogger("net.nutch.util.NutchConf"); 27 28 private static List resourceNames = new ArrayList(); 29 private static Properties properties; 30 31 static { 32 resourceNames.add("nutch-default.xml"); 33 resourceNames.add("nutch-site.xml"); 34 } 35 36 40 public static synchronized void addConfResource(String name) { 41 resourceNames.add(resourceNames.size()-1, name); properties = null; } 44 45 private static synchronized Properties getProps() { 46 if (properties == null) { 47 properties = new Properties(); 48 ListIterator i = resourceNames.listIterator(); 49 while (i.hasNext()) { 50 loadResource((String )i.next(), i.nextIndex()==resourceNames.size()); 51 } 52 } 53 return properties; 54 } 55 56 58 public static String get(String name) { return getProps().getProperty(name);} 59 60 63 public static String get(String name, String defaultValue) { 64 return getProps().getProperty(name, defaultValue); 65 } 66 67 71 public static int getInt(String name, int defaultValue) { 72 String valueString = get(name); 73 if (valueString == null) 74 return defaultValue; 75 try { 76 return Integer.parseInt(valueString); 77 } catch (NumberFormatException e) { 78 return defaultValue; 79 } 80 } 81 82 86 public static long getLong(String name, long defaultValue) { 87 String valueString = get(name); 88 if (valueString == null) 89 return defaultValue; 90 try { 91 return Long.parseLong(valueString); 92 } catch (NumberFormatException e) { 93 return defaultValue; 94 } 95 } 96 97 101 public static float getFloat(String name, float defaultValue) { 102 String valueString = get(name); 103 if (valueString == null) 104 return defaultValue; 105 try { 106 return Float.parseFloat(valueString); 107 } catch (NumberFormatException e) { 108 return defaultValue; 109 } 110 } 111 112 115 public static InputStream getConfResourceAsInputStream(String name) { 116 try { 117 URL url= NutchConf.class.getClassLoader().getResource(name); 118 119 if (url == null) { 120 LOG.info(name + " not found"); 121 return null; 122 } else { 123 LOG.info("found resource " + name + " at " + url); 124 } 125 126 return url.openStream(); 127 } catch (Exception e) { 128 return null; 129 } 130 } 131 132 135 public static Reader getConfResourceAsReader(String name) { 136 try { 137 URL url= NutchConf.class.getClassLoader().getResource(name); 138 139 if (url == null) { 140 LOG.info(name + " not found"); 141 return null; 142 } else { 143 LOG.info("found resource " + name + " at " + url); 144 } 145 146 return new InputStreamReader (url.openStream()); 147 } catch (Exception e) { 148 return null; 149 } 150 } 151 152 157 public static boolean getBoolean(String name, boolean defaultValue) { 158 String valueString = get(name); 159 if ("true".equals(valueString)) 160 return true; 161 else if ("false".equals(valueString)) 162 return false; 163 else return defaultValue; 164 } 165 166 170 public static String [] getStrings(String name) { 171 String valueString = get(name); 172 if (valueString == null) 173 return null; 174 StringTokenizer tokenizer = new StringTokenizer (valueString,", \t\n\r\f"); 175 List values = new ArrayList(); 176 while (tokenizer.hasMoreTokens()) { 177 values.add(tokenizer.nextToken()); 178 } 179 return (String [])values.toArray(new String [values.size()]); 180 } 181 182 private static void loadResource(String name, boolean quietFail) { 183 try { 184 URL url = NutchConf.class.getClassLoader().getResource(name); 185 186 if (url == null) { 187 if (!quietFail) 188 LOG.severe(name + " not found"); 189 return; 190 } else { 191 LOG.info("loading " + url); 192 } 193 194 Document doc = 195 DocumentBuilderFactory.newInstance().newDocumentBuilder() 196 .parse(url.toString()); 197 Element root = doc.getDocumentElement(); 198 if (!"nutch-conf".equals(root.getTagName())) 199 LOG.severe("bad conf file: top-level element not <nutch-conf>"); 200 NodeList props = root.getChildNodes(); 201 for (int i = 0; i < props.getLength(); i++) { 202 Node propNode = props.item(i); 203 if (!(propNode instanceof Element)) 204 continue; 205 Element prop = (Element)propNode; 206 if (!"property".equals(prop.getTagName())) 207 LOG.warning("bad conf file: element not <property>"); 208 NodeList fields = prop.getChildNodes(); 209 String attr = null; 210 String value = null; 211 for (int j = 0; j < fields.getLength(); j++) { 212 Node fieldNode = fields.item(j); 213 if (!(fieldNode instanceof Element)) 214 continue; 215 Element field = (Element)fieldNode; 216 if ("name".equals(field.getTagName())) 217 attr = ((Text)field.getFirstChild()).getData(); 218 if ("value".equals(field.getTagName()) && field.hasChildNodes()) 219 value = ((Text)field.getFirstChild()).getData(); 220 } 221 if (attr != null && value != null) 222 properties.setProperty(attr, value); 223 } 224 225 } catch (Exception e) { 226 LOG.severe("error parsing conf file: " + e); 227 } 228 229 } 230 231 232 public static void main(String [] args) { 233 getProps().list(System.out); 234 } 235 } 236 | Popular Tags |