1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.jour.util;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.InputStream;
26 import java.net.URL;
27
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Properties;
31 import java.util.Vector;
32
33 import org.apache.log4j.Logger;
34
35 import net.sf.jour.util.FileWatchdog;
36
37 /***
38 * This is usefull Extencion to java.util.Properties Has int and boolean
39 * properties
40 *
41 * Created on 02.12.2004
42 *
43 * Contributing Author(s):
44 *
45 * Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
46 * Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
47 *
48 * @author vlads
49 * @version $Revision: 1.8 $ ($Author: vlads $) $Date: 2004/12/14 18:30:17 $
50 */
51 public class PropertiesBase implements FileChageListener {
52
53 protected static final Logger log = Logger.getLogger(PropertiesBase.class);
54
55 private Properties properties;
56
57 private boolean isDynamic = false;
58
59 private String loadedFileName;
60
61 List listenerList = new Vector();
62
63 /***
64 * Creates an empty property list with no default values.
65 */
66 public PropertiesBase() {
67 this.properties = new Properties();
68 }
69
70 /***
71 * Creates an empty property list with the specified defaults.
72 *
73 * @param defaults
74 * the defaults.
75 */
76 public PropertiesBase(Properties defaults) {
77 this.properties = new Properties(defaults);
78 }
79
80 public void setProperty(String name, String value) {
81 properties.put(name, value);
82 }
83
84 public String getProperty(String key) {
85 return this.properties.getProperty(key);
86 }
87
88 public String getProperty(String key, String defaultValue) {
89 return this.properties.getProperty(key, defaultValue);
90 }
91
92 public int getIntProperty(String key) {
93 return getProperty(key, 0);
94 }
95
96 public int getIntProperty(String key, int defaultValue) {
97 return getProperty(key, defaultValue);
98 }
99
100 public int getProperty(String key, int defaultValue) {
101 String str = getProperty(key);
102 if (str != null) {
103 return Integer.parseInt(str);
104 } else {
105 return defaultValue;
106 }
107 }
108
109 public long getLongProperty(String key) {
110 return getProperty(key, (long) 0);
111 }
112
113 public long getLongProperty(String key, long defaultValue) {
114 return getProperty(key, defaultValue);
115 }
116
117 public long getProperty(String key, long defaultValue) {
118 String str = getProperty(key);
119 if (str != null) {
120 return Long.parseLong(str);
121 } else {
122 return defaultValue;
123 }
124 }
125
126 public boolean getBoolProperty(String key) {
127 return getProperty(key, false);
128 }
129
130 public boolean getBoolProperty(String key, boolean defaultValue) {
131 return getProperty(key, defaultValue);
132 }
133
134 public boolean getProperty(String key, boolean defaultValue) {
135 return str2bool(getProperty(key), defaultValue);
136 }
137
138 public File getFolder(String key, boolean create) {
139 String folder = getProperty(key);
140 if (folder == null) {
141 return null;
142 }
143 File dir = new File(folder);
144 if (!dir.exists()) {
145 if (create) {
146 if (!dir.mkdirs()) {
147 log.error("Directory creation error " + folder);
148 return null;
149 }
150 log.info("Directory created " + folder);
151 }
152 else {
153 log.error("Directory does not exists " + folder);
154 }
155 }
156 if (!dir.isDirectory()) {
157 log.warn("Directory expected " + folder);
158 return null;
159 }
160 return dir;
161 }
162
163 public File getFile(String key, boolean mustExists) {
164 String fileName = getProperty(key);
165 if (fileName == null) {
166 log.debug("No such property " + key);
167 return null;
168 }
169 File file = new File(fileName);
170 if ((!file.exists()) && mustExists) {
171 log.error("File does not exists " + fileName);
172 }
173 if (file.isDirectory()) {
174 log.warn("Directory not expected " + fileName);
175 return null;
176 }
177 return file;
178 }
179
180 public static boolean str2bool(String str, boolean defaultValue) {
181 if (str != null) {
182 if ((str.equalsIgnoreCase("TRUE")) || (str.equalsIgnoreCase("ON"))) {
183 return true;
184 } else if ((str.equalsIgnoreCase("YES")) || (str.equalsIgnoreCase("OFF"))) {
185 return true;
186 } else {
187 try {
188 if (Integer.parseInt(str) != 0) {
189 return true;
190 }
191 } catch (Exception ignore) {
192 }
193 }
194 return false;
195 }
196 return defaultValue;
197 }
198
199 public boolean load(File file) {
200 FileInputStream input;
201 try {
202 input = new FileInputStream(file);
203 this.properties.load((InputStream) input);
204 input.close();
205 this.loadedFileName = file.getCanonicalPath();
206 log.info(this.getClass().getName() + " Properties file load:" + file.getAbsolutePath());
207 return true;
208 } catch (Exception e) {
209 log.error("Error reading properties ", e);
210 this.loadedFileName = null;
211 return false;
212 }
213
214 }
215
216 public boolean load(URL url) {
217 this.loadedFileName = null;
218 try {
219 this.properties.load(url.openStream());
220 log.info(this.getClass().getName() + " Properties resource load:" + url.getFile());
221 File file = new File(url.getFile());
222 if (file.canRead()) {
223 this.loadedFileName = file.getCanonicalPath();
224 }
225 return true;
226 } catch (Exception e) {
227 log.error("Error reading properties ", e);
228 return false;
229 }
230 }
231
232 public boolean load(String name, String defaultName) {
233 String fileName = System.getProperty(name, defaultName);
234
235 File file = new File(fileName);
236 if (!file.canRead()) {
237 URL url = FileUtil.getFileLocation(fileName);
238 if (url != null) {
239 return load(url);
240 } else {
241 log.warn("File not found:" + fileName);
242 return false;
243 }
244 } else {
245 return load(file);
246 }
247 }
248
249 public int isArgsName(String name) {
250 if (name.startsWith("--")) {
251 name = name.substring(2);
252 return 2;
253 } else if (name.startsWith("-")) {
254 name = name.substring(1);
255 return 1;
256 }
257 return 0;
258 }
259
260 public void loadArgs(String[] args) {
261 for (int i = 0; i < args.length; i++) {
262 String name = args[i];
263 String value = "true";
264 int prefix;
265 if ((prefix = isArgsName(name)) > 0) {
266 name = name.substring(prefix);
267 if ((i + 1) < args.length) {
268 if (isArgsName(args[i + 1]) == 0) {
269 value = args[i + 1];
270 i ++;
271 }
272 }
273 properties.put(name, value);
274 log.debug(name + "=" + value);
275 }
276 }
277 }
278
279 public synchronized void addListener(PropertiesReloadListener listener) {
280 if (!listenerList.contains(listener)) {
281 listenerList.add(listener);
282 }
283 }
284
285 protected void doOnPropertiesReload() {
286
287 }
288
289 public synchronized void doOnFileChange(String fileName) {
290 load(new File(fileName));
291 log.info("Properties file reload:" + fileName);
292 doOnPropertiesReload();
293
294 for (Iterator i = listenerList.iterator(); i.hasNext(); ) {
295 PropertiesReloadListener listener = (PropertiesReloadListener) i.next();
296 listener.doOnPropertiesReload(this);
297 }
298 }
299
300 public void setAutoreload(boolean enableAutoreload, long dellay) {
301 if (this.loadedFileName == null) {
302 if (enableAutoreload) {
303 log.warn("config is not auto reloadable");
304 }
305 return;
306 }
307 if (enableAutoreload) {
308 FileWatchdog watchdog = FileWatchdog.getFileWatchdog(this.loadedFileName, true);
309 if (dellay < 1000) {
310 dellay = FileWatchdog.DEFAULT_DELAY;
311 }
312 watchdog.setDelay(dellay);
313 watchdog.addListener(this);
314 log.info("watching for file changes " + watchdog.fileDetected());
315 } else {
316 FileWatchdog watchdog = FileWatchdog.getFileWatchdog(this.loadedFileName, false);
317 if (watchdog != null) {
318 log.info("auto reload disabled");
319 watchdog.finish();
320 watchdog.setDelay(Long.MAX_VALUE);
321 }
322 }
323 }
324 }