KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > util > NutchConf


1 /* Copyright (c) 2003 The Nutch Organization. All rights reserved. */
2 /* Use subject to the conditions in http://www.nutch.org/LICENSE.txt. */
3
4 package net.nutch.util;
5
6 import java.util.*;
7 import java.net.URL JavaDoc;
8 import java.io.InputStream JavaDoc;
9 import java.io.InputStreamReader JavaDoc;
10 import java.io.Reader JavaDoc;
11 import java.util.logging.Logger JavaDoc;
12 import javax.xml.parsers.*;
13 import org.w3c.dom.*;
14
15 /** Provides access to Nutch configuration parameters.
16  *
17  * <p>Default values for all parameters are specified in a file named
18  * <tt>nutch-default.xml</tt> located on the classpath. Overrides for these
19  * defaults should be in an optional file named <tt>nutch-site.xml</tt>, also
20  * located on the classpath. Typically these files reside in the
21  * <tt>conf/</tt> subdirectory at the top-level of a Nutch installation.
22  */

23     
24 public class NutchConf {
25   private static final Logger JavaDoc 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   /** Adds a resource name to the chain of resources read. The first resource
37    * is always <tt>nutch-default.xml</tt>, and the last is always
38    * <tt>nutch-site.xml</tt>. New resources are inserted between these, so
39    * they can override defaults, but not site-specifics. */

40   public static synchronized void addConfResource(String JavaDoc name) {
41     resourceNames.add(resourceNames.size()-1, name); // add second to last
42
properties = null; // trigger reload
43
}
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 JavaDoc)i.next(), i.nextIndex()==resourceNames.size());
51       }
52     }
53     return properties;
54   }
55
56   /** Returns the value of the <code>name</code> property, or null if no
57    * such property exists. */

58   public static String JavaDoc get(String JavaDoc name) { return getProps().getProperty(name);}
59
60   /** Returns the value of the <code>name</code> property. If no such property
61    * exists, then <code>defaultValue</code> is returned.
62    */

63   public static String JavaDoc get(String JavaDoc name, String JavaDoc defaultValue) {
64      return getProps().getProperty(name, defaultValue);
65   }
66   
67   /** Returns the value of the <code>name</code> property as an integer. If no
68    * such property is specified, or if the specified value is not a valid
69    * integer, then <code>defaultValue</code> is returned.
70    */

71   public static int getInt(String JavaDoc name, int defaultValue) {
72     String JavaDoc valueString = get(name);
73     if (valueString == null)
74       return defaultValue;
75     try {
76       return Integer.parseInt(valueString);
77     } catch (NumberFormatException JavaDoc e) {
78       return defaultValue;
79     }
80   }
81
82   /** Returns the value of the <code>name</code> property as a long. If no
83    * such property is specified, or if the specified value is not a valid
84    * long, then <code>defaultValue</code> is returned.
85    */

86   public static long getLong(String JavaDoc name, long defaultValue) {
87     String JavaDoc valueString = get(name);
88     if (valueString == null)
89       return defaultValue;
90     try {
91       return Long.parseLong(valueString);
92     } catch (NumberFormatException JavaDoc e) {
93       return defaultValue;
94     }
95   }
96
97   /** Returns the value of the <code>name</code> property as a float. If no
98    * such property is specified, or if the specified value is not a valid
99    * float, then <code>defaultValue</code> is returned.
100    */

101   public static float getFloat(String JavaDoc name, float defaultValue) {
102     String JavaDoc valueString = get(name);
103     if (valueString == null)
104       return defaultValue;
105     try {
106       return Float.parseFloat(valueString);
107     } catch (NumberFormatException JavaDoc e) {
108       return defaultValue;
109     }
110   }
111
112   /** Returns an input stream attached to the configuration resource with the
113    * given <code>name</code>.
114    */

115   public static InputStream JavaDoc getConfResourceAsInputStream(String JavaDoc name) {
116     try {
117       URL JavaDoc 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 JavaDoc e) {
128       return null;
129     }
130   }
131
132   /** Returns a reader attached to the configuration resource with the
133    * given <code>name</code>.
134    */

135   public static Reader JavaDoc getConfResourceAsReader(String JavaDoc name) {
136     try {
137       URL JavaDoc 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 JavaDoc(url.openStream());
147     } catch (Exception JavaDoc e) {
148       return null;
149     }
150   }
151
152   /** Returns the value of the <code>name</code> property as an boolean. If no
153    * such property is specified, or if the specified value is not a valid
154    * boolean, then <code>defaultValue</code> is returned. Valid boolean values
155    * are "true" and "false".
156    */

157   public static boolean getBoolean(String JavaDoc name, boolean defaultValue) {
158     String JavaDoc 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   /** Returns the value of the <code>name</code> property as an array of
167    * strings. If no such property is specified, then <code>null</code>
168    * is returned. Values are whitespace or comma delimted.
169    */

170   public static String JavaDoc[] getStrings(String JavaDoc name) {
171     String JavaDoc 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 JavaDoc[])values.toArray(new String JavaDoc[values.size()]);
180   }
181
182   private static void loadResource(String JavaDoc name, boolean quietFail) {
183     try {
184       URL JavaDoc 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 JavaDoc attr = null;
210         String JavaDoc 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 JavaDoc e) {
226       LOG.severe("error parsing conf file: " + e);
227     }
228     
229   }
230
231   /** For debugging. List all properties to the terminal and exits. */
232   public static void main(String JavaDoc[] args) {
233     getProps().list(System.out);
234   }
235 }
236
Popular Tags