1 | |
package us.daveread.basicquery.util; |
2 | |
|
3 | |
import java.io.BufferedReader; |
4 | |
import java.io.File; |
5 | |
import java.io.FileInputStream; |
6 | |
import java.io.FileOutputStream; |
7 | |
import java.io.FileReader; |
8 | |
import java.io.FileWriter; |
9 | |
import java.io.PrintWriter; |
10 | |
import java.util.Properties; |
11 | |
|
12 | |
import org.apache.log4j.Logger; |
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class Configuration extends Properties { |
34 | |
|
35 | |
|
36 | |
|
37 | |
private static final long serialVersionUID = 8439104437125048420L; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
private static Configuration config; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | 1 | private static final Logger LOGGER = Logger.getLogger(Configuration.class); |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
private static final String PROPERTIES_DIRECTORY = "BasicQuery"; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
private static final String FILENAME_PROPERTIES = "BasicQuery.Properties"; |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
private static final String PROP_SYSTEM_USERHOMEDIR = "user.home"; |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
private static String userHomeDirectory; |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | 1 | private Configuration() { |
73 | 1 | userHomeDirectory = System.getProperty(PROP_SYSTEM_USERHOMEDIR); |
74 | |
|
75 | 1 | if (userHomeDirectory == null) { |
76 | 0 | userHomeDirectory = PROPERTIES_DIRECTORY; |
77 | |
} else { |
78 | 1 | userHomeDirectory += "/" + PROPERTIES_DIRECTORY; |
79 | 1 | new File(userHomeDirectory).mkdirs(); |
80 | |
} |
81 | 1 | } |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
public static synchronized Configuration instance() { |
89 | 3 | if (config == null) { |
90 | 1 | config = new Configuration(); |
91 | |
} |
92 | |
|
93 | |
|
94 | 3 | config.setupProperties(); |
95 | |
|
96 | 3 | return config; |
97 | |
} |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public void store() { |
103 | |
File configFile; |
104 | |
|
105 | 1 | configFile = getFile(FILENAME_PROPERTIES); |
106 | |
|
107 | |
try { |
108 | 1 | store(new FileOutputStream(configFile, false), "BasicQuery Configuration"); |
109 | 0 | } catch (Throwable any) { |
110 | 0 | LOGGER.error("Unable to store properties file (" |
111 | |
+ configFile.getAbsolutePath() + ")", any); |
112 | 1 | } |
113 | 1 | } |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
private void setupProperties() { |
119 | |
File propFile; |
120 | |
|
121 | 3 | propFile = getFile(FILENAME_PROPERTIES); |
122 | |
|
123 | |
try { |
124 | 3 | config.load(new FileInputStream(propFile)); |
125 | 0 | } catch (Throwable any) { |
126 | 0 | LOGGER.error("Unable to load properties file (" |
127 | |
+ propFile.getAbsolutePath() + ")", any); |
128 | 3 | } |
129 | 3 | } |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
public File getFile(String fileName) { |
143 | |
File configFile; |
144 | |
|
145 | 8 | configFile = new File(getFilePath(fileName)); |
146 | 8 | if (!configFile.exists()) { |
147 | 3 | userDefaultFile(fileName); |
148 | |
} |
149 | |
|
150 | 8 | return configFile; |
151 | |
} |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
private String getFilePath(String fileName) { |
163 | 8 | return userHomeDirectory + "/" + fileName; |
164 | |
} |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
private void userDefaultFile(String fileName) { |
175 | |
File realFile, defaultFile; |
176 | |
|
177 | 3 | realFile = new File(userHomeDirectory + "/" + fileName); |
178 | 3 | if (!realFile.exists()) { |
179 | 3 | defaultFile = new File(fileName + ".txt"); |
180 | 3 | if (defaultFile.exists()) { |
181 | 1 | copyFile(defaultFile, realFile); |
182 | |
} |
183 | |
} |
184 | 3 | } |
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
private static void copyFile(File source, File dest) { |
195 | |
BufferedReader in; |
196 | |
PrintWriter out; |
197 | |
String data; |
198 | |
|
199 | 1 | in = null; |
200 | 1 | out = null; |
201 | |
|
202 | |
try { |
203 | 1 | in = new BufferedReader(new FileReader(source)); |
204 | 1 | out = new PrintWriter(new FileWriter(dest)); |
205 | |
|
206 | 3 | while ((data = in.readLine()) != null) { |
207 | 2 | out.println(data); |
208 | |
} |
209 | 0 | } catch (Throwable any) { |
210 | 0 | LOGGER.warn("Unable to copy default file (" + source.getAbsolutePath() |
211 | |
+ ") to configuration file (" + dest.getAbsolutePath() + ")", any); |
212 | |
} finally { |
213 | 1 | if (in != null) { |
214 | |
try { |
215 | 1 | in.close(); |
216 | 0 | } catch (Throwable any) { |
217 | 0 | LOGGER.warn("Unable to close the input file: " + source, any); |
218 | 1 | } |
219 | |
} |
220 | 1 | if (out != null) { |
221 | |
try { |
222 | 1 | out.close(); |
223 | 0 | } catch (Throwable any) { |
224 | 0 | LOGGER.warn("Unable to close the output file: " + dest, any); |
225 | 1 | } |
226 | |
} |
227 | |
} |
228 | 1 | } |
229 | |
} |