Index: trunk/lucene-search-2/src/org/wikimedia/lsearch/config/Configuration.java |
— | — | @@ -23,22 +23,13 @@ |
24 | 24 | */ |
25 | 25 | package org.wikimedia.lsearch.config; |
26 | 26 | |
27 | | -import java.io.BufferedReader; |
28 | 27 | import java.io.File; |
29 | 28 | import java.io.FileInputStream; |
30 | 29 | import java.io.FileNotFoundException; |
31 | 30 | import java.io.IOException; |
32 | | -import java.io.InputStreamReader; |
33 | | -import java.net.Inet4Address; |
34 | 31 | import java.net.MalformedURLException; |
35 | 32 | import java.net.URL; |
36 | | -import java.net.UnknownHostException; |
37 | | -import java.util.ArrayList; |
38 | | -import java.util.Hashtable; |
39 | 33 | import java.util.Properties; |
40 | | -import java.util.Vector; |
41 | | -import java.util.regex.Matcher; |
42 | | -import java.util.regex.Pattern; |
43 | 34 | |
44 | 35 | import org.apache.log4j.BasicConfigurator; |
45 | 36 | import org.apache.log4j.Level; |
— | — | @@ -54,16 +45,22 @@ |
55 | 46 | */ |
56 | 47 | public class Configuration { |
57 | 48 | private static Configuration instance; |
58 | | - private static String configfile = null; |
| 49 | + private static String configFile = null; |
59 | 50 | private static String globalConfigUrl = null; |
60 | 51 | private static boolean verbose = true; // print info and error messages |
61 | 52 | |
62 | 53 | protected final String CONF_FILE_NAME = "lsearch.conf"; |
| 54 | + protected final String TEST_CONF_FILE_NAME = "lsearch.conf.test"; |
63 | 55 | |
| 56 | + /* os independent path component */ |
| 57 | + private final static String ETC_FOLDER = System.getProperty("file.separator") + "etc"; |
| 58 | + private final static String TEST_DATA_FOLDER = System.getProperty("file.separator") + "test-data"; |
| 59 | + |
| 60 | + |
64 | 61 | public static final String PATH_SEP = System.getProperty("file.separator"); |
65 | 62 | |
66 | 63 | public static void setConfigFile(String file) { |
67 | | - configfile = file; |
| 64 | + configFile = file; |
68 | 65 | } |
69 | 66 | |
70 | 67 | /** Returns an instance of Configuration singleton class */ |
— | — | @@ -72,24 +69,29 @@ |
73 | 70 | instance = new Configuration(); |
74 | 71 | return instance; |
75 | 72 | } |
| 73 | + |
76 | 74 | private Configuration() { |
77 | | - if (configfile == null) { |
| 75 | + |
| 76 | + if (configFile == null) { |
78 | 77 | String home = System.getProperty("user.home"); |
79 | 78 | String [] paths; |
80 | | - String filename = System.getProperty("file.separator")+CONF_FILE_NAME; |
| 79 | + String filename = System.getProperty("file.separator") + CONF_FILE_NAME; |
| 80 | + String testFilename = System.getProperty("file.separator") + TEST_CONF_FILE_NAME; |
81 | 81 | if (home == null) { |
82 | 82 | paths = new String[] { |
83 | | - System.getProperty("user.dir")+filename, |
84 | | - "/etc"+filename }; |
| 83 | + System.getProperty("user.dir") + filename, |
| 84 | + ETC_FOLDER+filename }; |
85 | 85 | } else { |
86 | 86 | paths = new String[] { |
87 | | - home+System.getProperty("file.separator")+"."+CONF_FILE_NAME, |
88 | | - System.getProperty("user.dir")+filename, |
89 | | - "/etc"+filename }; |
| 87 | + home+System.getProperty("file.separator")+ "." + CONF_FILE_NAME, //home |
| 88 | + System.getProperty("user.dir") + filename, //where invoked |
| 89 | + ETC_FOLDER+filename, //in etc |
| 90 | + System.getProperty("user.dir") +TEST_DATA_FOLDER + testFilename //in test-data |
| 91 | + }; |
90 | 92 | } |
91 | | - openProps(paths); |
| 93 | + openPropertieFile(paths); |
92 | 94 | } else { |
93 | | - openProps(new String[] { configfile }); |
| 95 | + openPropertieFile(new String[] { configFile }); |
94 | 96 | } |
95 | 97 | |
96 | 98 | if (getBoolean("Logging", "debug")) { |
— | — | @@ -130,17 +132,24 @@ |
131 | 133 | } |
132 | 134 | private Properties props; |
133 | 135 | |
134 | | - private void openProps(String[] paths) { |
| 136 | + /* |
| 137 | + * trys to locate a properties file and open it. |
| 138 | + */ |
| 139 | + private void openPropertieFile(String[] paths) |
| 140 | + { |
135 | 141 | props = new Properties(); |
| 142 | + String path=""; |
| 143 | + |
136 | 144 | for(int i=0;i<paths.length;i++){ |
137 | 145 | try { |
138 | | - String path = paths[i]; |
| 146 | + path = paths[i]; |
139 | 147 | if(verbose) |
140 | 148 | System.out.println("Trying config file at path "+path); |
141 | 149 | props.load(new FileInputStream(new File(path))); |
142 | | - configfile = path; |
| 150 | + configFile = path; |
143 | 151 | return; |
144 | 152 | } catch (FileNotFoundException e3) { |
| 153 | + System.err.println("file not found at "+path); |
145 | 154 | // try the next one |
146 | 155 | } catch (IOException e3) { |
147 | 156 | System.err.println("Error: IO error reading config: " + e3.getMessage()); |
— | — | @@ -224,7 +233,4 @@ |
225 | 234 | public static void setGlobalConfigUrl(String globalConfigUrl) { |
226 | 235 | Configuration.globalConfigUrl = globalConfigUrl; |
227 | 236 | } |
228 | | - |
229 | | - |
230 | | - |
231 | 237 | } |