Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
90   587   63   1.96
30   221   0.7   46
46     1.37  
1    
 
  QedeqConfig       Line # 34 90 63 97.6% 0.97590363
 
  (139)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10    * This program is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    * GNU General Public License for more details.
14    */
15   
16    package org.qedeq.kernel.se.config;
17   
18    import java.io.File;
19    import java.io.IOException;
20    import java.util.Iterator;
21    import java.util.List;
22   
23    import org.qedeq.base.io.IoUtility;
24    import org.qedeq.base.io.Parameters;
25    import org.qedeq.base.io.Path;
26    import org.qedeq.kernel.se.common.Service;
27   
28   
29    /**
30    * This class gives a type save access to properties of the application.
31    *
32    * @author Michael Meyling
33    */
 
34    public class QedeqConfig {
35   
36    /** Default location for newly created QEDEQ modules. */
37    private static final String DEFAULT_LOCAL_MODULES_DIRECTORY
38    = "local";
39   
40    /** Default location for locally buffered module files. */
41    private static final String DEFAULT_LOCAL_BUFFER
42    = "buffer";
43   
44    /** Default location for generated module and document files. */
45    private static final String DEFAULT_GENERATED
46    = "generated";
47   
48    /** Default log file path. */
49    private static final String DEFAULT_LOG_FILE
50    = "log/log.txt";
51   
52    /** This class organizes the access to the config parameters. */
53    private final ConfigAccess configAccess;
54   
55    /** Basis directory of application for all variable data. Basis for all relative paths. */
56    private final File basisDirectory;
57   
58    /**
59    * Constructor.
60    *
61    * @param configFile Config file.
62    * @param description Config file description.
63    * @param basisDirectory Basis directory of application for all variable data. Basis for all
64    * new relative paths
65    * @throws IOException Config file couldn't be loaded.
66    */
 
67  544 toggle public QedeqConfig(final File configFile, final String description, final File basisDirectory)
68    throws IOException {
69  544 configAccess = new ConfigAccess(configFile, description);
70  544 this.basisDirectory = basisDirectory.getCanonicalFile();
71    }
72   
73    /**
74    * Store properties in config file.
75    *
76    * @throws IOException Writing failed.
77    */
 
78  473 toggle public final void store() throws IOException {
79  473 configAccess.store();
80    }
81   
82    /**
83    * Get local file directory to save generated files in.
84    *
85    * @return Generation directory.
86    */
 
87  564 toggle public final File getGenerationDirectory() {
88  564 String location = getKeyValue("generationLocation");
89  564 if (location == null) {
90  561 location = QedeqConfig.DEFAULT_GENERATED;
91    }
92  564 return createAbsolutePath(location);
93    }
94   
95    /**
96    * Set local file directory for generated files.
97    *
98    * @param location generation directory.
99    */
 
100  2 toggle public final void setGenerationDirectory(final File location) {
101  2 final String relative = createRelativePath(location);
102  2 setKeyValue("generationLocation", relative);
103    }
104   
105    /**
106    * Get local file directory for module buffering.
107    *
108    * @return Buffer directory.
109    */
 
110  1262 toggle public final File getBufferDirectory() {
111  1262 String location = getKeyValue("bufferLocation");
112  1262 if (location == null) {
113  1259 location = QedeqConfig.DEFAULT_LOCAL_BUFFER;
114    }
115  1262 return createAbsolutePath(location);
116    }
117   
118   
119    /**
120    * Set local file directory for module buffering.
121    * After changing this location the buffer should eventually be cleared.
122    *
123    * @param location buffer directory.
124    */
 
125  2 toggle public final void setBufferDirectory(final File location) {
126  2 final String relative = createRelativePath(location);
127  2 setKeyValue("bufferLocation", relative);
128    }
129   
130    /**
131    * Get directory for newly created QEDEQ module files.
132    *
133    * @return Directory for newly created QEDEQ modules.
134    */
 
135  4 toggle public final File getLocalModulesDirectory() {
136  4 String location = getKeyValue("localModulesDirectory");
137  4 if (location == null) {
138  1 location = QedeqConfig.DEFAULT_LOCAL_MODULES_DIRECTORY;
139    }
140  4 return createAbsolutePath(location);
141    }
142   
143   
144    /**
145    * Set directory for newly created module files.
146    * After changing this location the buffer should eventually be cleared.
147    *
148    * @param location Buffer directory.
149    */
 
150  2 toggle public final void setLocalModulesDirectory(final File location) {
151  2 final String relative = createRelativePath(location);
152  2 setKeyValue("localModulesDirectory", relative);
153    }
154   
155    /**
156    * Get relative file location for log file.
157    *
158    * @return Log file path relative to basis directory.
159    */
 
160  499 toggle private final String getLogFileString() {
161  499 final String location = getKeyValue("logLocation");
162  499 if (location == null) {
163  498 return QedeqConfig.DEFAULT_LOG_FILE;
164    }
165  1 return location;
166    }
167   
168    /**
169    * Get file location for log file.
170    *
171    * @return Log file path.
172    */
 
173  499 toggle public final File getLogFile() {
174  499 return new File(getBasisDirectory(), getLogFileString());
175    }
176   
177    /**
178    * Get history of modules, which were tried to load.
179    *
180    * @return list of modules.
181    */
 
182  3 toggle public final String[] getModuleHistory() {
183  3 return configAccess.getStringProperties("moduleHistory.");
184    }
185   
186    /**
187    * Save history of modules, which were tried to load.
188    *
189    * @param modules list of modules.
190    */
 
191  1 toggle public final void saveModuleHistory(final List modules) {
192  1 configAccess.removeProperties(("moduleHistory."));
193  2 for (int i = 0; i < modules.size(); i++) {
194  1 setKeyValue("moduleHistory." + (i + 101),
195    modules.get(i).toString());
196    }
197    }
198   
199    /**
200    * Get list of previously loaded modules.
201    *
202    * @return list of modules.
203    */
 
204  3 toggle public final String[] getPreviouslyLoadedModules() {
205  3 return configAccess.getStringProperties("loadedModule.");
206    }
207   
208    /**
209    * Set list of previously successfully loaded QEDEQ modules.
210    *
211    * @param moduleAddresses This modules were successfully loaded.
212    */
 
213  472 toggle public final void setPreviouslyLoadedModules(final String[] moduleAddresses) {
214  472 configAccess.removeProperties("loadedModule.");
215  1042 for (int i = 0; i < moduleAddresses.length; i++) {
216  570 setKeyValue("loadedModule." + (i + 1), moduleAddresses[i]);
217    }
218    }
219   
220    /**
221    * Get basis directory of this application.
222    *
223    * @return Basis directory of application for all variable data. Basis for all relative paths.
224    */
 
225  2343 toggle public final File getBasisDirectory() {
226  2343 return basisDirectory;
227    }
228   
229    /**
230    * Get file path starting from basis directory of this application.
231    *
232    * @param path Go to this path starting from basis directory.
233    * @return File path resolved against basis application directory as an absolute path.
234    */
 
235  1831 toggle public final File createAbsolutePath(final String path) {
236  1831 File result = new File(path);
237  1831 final Path ptest = new Path(path.replace(File.separatorChar, '/'), "");
238  1831 if (ptest.isAbsolute()) {
239  1 try {
240  1 return result.getCanonicalFile();
241    } catch (Exception e) {
242    // we don't know if we can log something already
243  0 e.printStackTrace(System.out);
244  0 System.out.println("we try to continue with file " + result);
245  0 return result;
246    }
247    }
248  1830 result = new File(getBasisDirectory(), path);
249  1830 try {
250  1830 result = result.getCanonicalFile();
251    } catch (IOException e) {
252    // we don't know if we can log something already
253  0 e.printStackTrace(System.out);
254    }
255  1830 return result;
256    }
257   
258    /**
259    * Create relative file path starting from basis directory of this application.
260    *
261    * @param path Reach this path starting from basis directory.
262    * @return File path relative to basis application directory.
263    */
 
264  6 toggle private final String createRelativePath(final File path) {
265  6 return IoUtility.createRelativePath(getBasisDirectory(), path);
266    }
267   
268    /**
269    * Get auto reload of last session successfully loaded modules.
270    *
271    * @return auto reload enabled?
272    */
 
273  475 toggle public boolean isAutoReloadLastSessionChecked() {
274  475 return "true".equals(
275    getKeyValue("sessionAutoReload", "true"));
276    }
277   
278    /**
279    * Set auto reload checked modules of last session mode.
280    *
281    * @param mode enable auto reload?
282    */
 
283  501 toggle public final void setAutoReloadLastSessionChecked(final boolean mode) {
284  501 setKeyValue("sessionAutoReload", (mode ? "true" : "false"));
285    }
286   
287    /**
288    * Is tracing on? If not, only business and fatal messages are logged.
289    * Otherwise all events are logged according to the log level settings.
290    *
291    * @return Is tracing on?
292    */
 
293  501 toggle public final boolean isTraceOn() {
294  501 return "true".equals(getKeyValue("traceOn", "false"));
295    }
296   
297    /**
298    * Set tracing on.
299    *
300    * @param traceOn Set trace on.
301    */
 
302  2 toggle public final void setTraceOn(final boolean traceOn) {
303  2 setKeyValue("traceOn", (traceOn ? "true" : "false"));
304    }
305   
306    /**
307    * Get connection timeout, especially for TCP/IP connections.
308    *
309    * @return Connection timeout (in milliseconds).
310    */
 
311  12 toggle public int getConnectionTimeout() {
312  12 return getKeyValue("connectionTimeout", 2000);
313    }
314   
315    /**
316    * Set connection timeout, especially for TCP/IP connections.
317    *
318    * @param timeout Connection timeout, especially for TCP/IP connections. In milliseconds.
319    */
 
320  3 toggle public final void setConnectionTimeout(final int timeout) {
321  3 setKeyValue("connectionTimeout", timeout);
322    }
323   
324    /**
325    * Get read timeout, especially for TCP/IP connections.
326    *
327    * @return Read timeout (in milliseconds).
328    */
 
329  10 toggle public int getReadTimeout() {
330  10 return getKeyValue("readTimeout", 1000);
331    }
332   
333    /**
334    * Set read timeout, especially for TCP/IP connections.
335    *
336    * @param timeout Read timeout, especially for TCP/IP connections. In milliseconds.
337    */
 
338  2 toggle public final void setReadTimeout(final int timeout) {
339  2 setKeyValue("readTimeout", timeout);
340    }
341   
342    /**
343    * Set http proxy host.
344    *
345    * @param httpProxyHost Http proxy server.
346    */
 
347  3 toggle public final void setHttpProxyHost(final String httpProxyHost) {
348  3 setKeyValue("http.proxyHost", httpProxyHost);
349    }
350   
351    /**
352    * Get http proxy host. It might be a good idea to ignore this value, if the application
353    * was started via Java Webstart. If none is defined we take the value of the system property
354    * "http.proxyHost".
355   
356    *
357    * @return Http proxy host. Might be <code>null</code>.
358    */
 
359  13 toggle public final String getHttpProxyHost() {
360  13 final String def = System.getProperty("http.proxyHost");
361  13 if (def != null) {
362  4 return getKeyValue("http.proxyHost", def);
363    }
364  9 return getKeyValue("http.proxyHost");
365    }
366   
367    /**
368    * Set http proxy port.
369    *
370    * @param httpProxyPort Http proxy port.
371    */
 
372  2 toggle public final void setHttpProxyPort(final String httpProxyPort) {
373  2 setKeyValue("http.proxyPort", httpProxyPort);
374    }
375   
376    /**
377    * Get http proxy port. It might be a good idea to ignore this value, if the application
378    * was started via Java Webstart. If none is defined we take the value of the system property
379    * "http.proxyPort".
380    *
381    * @return Http proxy port. Might be <code>null</code>.
382    */
 
383  11 toggle public final String getHttpProxyPort() {
384  11 final String def = System.getProperty("http.proxyPort");
385  11 if (def != null) {
386  2 return getKeyValue("http.proxyPort", def);
387    }
388  9 return getKeyValue("http.proxyPort");
389    }
390   
391    /**
392    * Set http non proxy hosts.
393    *
394    * @param httpNonProxyHosts Http non proxy hosts. Might be <code>null</code>.
395    */
 
396  2 toggle public final void setHttpNonProxyHosts(final String httpNonProxyHosts) {
397  2 setKeyValue("http.nonProxyHosts", httpNonProxyHosts);
398    }
399   
400    /**
401    * Get non http proxy hosts. It might be a good idea to ignore this value, if the application
402    * was started via Java Webstart. If none is defined we take the value of the system property
403    * "http.nonProxyHosts".
404    *
405    * @return Http non proxy hosts. Might be <code>null</code>.
406    */
 
407  11 toggle public final String getHttpNonProxyHosts() {
408  11 final String def = System.getProperty("http.nonProxyHosts");
409  11 if (def != null) {
410  2 return getKeyValue("http.nonProxyHosts", def);
411    }
412  9 return getKeyValue("http.nonProxyHosts");
413    }
414   
415    /**
416    * Get value for given key.
417    *
418    * @param key Get value for this key.
419    * @return Value, maybe <code>null</code>.
420    */
 
421  2360 toggle protected synchronized String getKeyValue(final String key) {
422  2360 return configAccess.getString(key);
423    }
424   
425    /**
426    * Get value for given key.
427    *
428    * @param key Get value for this key.
429    * @param defaultValue Default value..
430    * @return Value. If value for key is originally <code>null</code> <code>defaultValue</code>
431    * is returned..
432    */
 
433  993 toggle protected synchronized String getKeyValue(final String key, final String defaultValue) {
434  993 return configAccess.getString(key, defaultValue);
435    }
436   
437    /**
438    * Set value for given key.
439    *
440    * @param key For this key.
441    * @param value Set this value.
442    */
 
443  17148 toggle protected synchronized void setKeyValue(final String key, final String value) {
444  17148 configAccess.setString(key, value);
445    }
446   
447    /**
448    * Get value for given key.
449    *
450    * @param key Get value for this key.
451    * @param defaultValue Default value..
452    * @return Value. If value for key is originally <code>null</code> <code>defaultValue</code>
453    * is returned..
454    */
 
455  26 toggle protected synchronized int getKeyValue(final String key, final int defaultValue) {
456  26 return configAccess.getInteger(key, defaultValue);
457    }
458   
459    /**
460    * Set value for given key.
461    *
462    * @param key For this key.
463    * @param value Set this value.
464    */
 
465  6 toggle protected synchronized void setKeyValue(final String key, final int value) {
466  6 configAccess.setInteger(key, value);
467    }
468   
469    /**
470    * Get value for given key.
471    *
472    * @param key Get value for this key.
473    * @param defaultValue Default value..
474    * @return Value. If value for key is originally <code>null</code> <code>defaultValue</code>
475    * is returned.
476    */
 
477  6 toggle protected synchronized boolean getKeyValue(final String key, final boolean defaultValue) {
478  6 return "true".equals(getKeyValue(key, (defaultValue ? "true" : "false")));
479    }
480   
481    /**
482    * Set value for given key.
483    *
484    * @param key For this key.
485    * @param value Set this value.
486    */
 
487  3 toggle protected void setKeyValue(final String key, final boolean value) {
488  3 setKeyValue(key, (value ? "true" : "false"));
489    }
490   
491    /**
492    * Get service properties from configuration file.
493    *
494    * @param service We want to know properties for this service
495    * @return Map with properties for this service.
496    */
 
497  6669 toggle public Parameters getServiceEntries(final Service service) {
498  6669 return new Parameters(configAccess.getProperties(service.getServiceId() + "$"));
499    }
500   
501    /**
502    * Get value for given service key.
503    *
504    * @param service Setting for this service.
505    * @param key Get value for this key.
506    * @param defaultValue Default value..
507    * @return Value. If value for key is originally <code>null</code> <code>defaultValue</code>
508    * is returned.
509    */
 
510  1 toggle public String getServiceKeyValue(final Service service, final String key, final String defaultValue) {
511  1 return getKeyValue(service.getServiceId() + "$" + key, defaultValue);
512    }
513   
514    /**
515    * Set value for given service key.
516    *
517    * @param service Setting for this service.
518    * @param key For this key.
519    * @param value Set this value.
520    */
 
521  477 toggle public void setServiceKeyValue(final Service service, final String key, final String value) {
522  477 setKeyValue(service.getServiceId() + "$" + key, value);
523    }
524   
525    /**
526    * Set value for given service key.
527    *
528    * @param service Setting for this service.
529    * @param parameters Parameters for this service.
530    */
 
531  4714 toggle public void setServiceKeyValues(final Service service, final Parameters parameters) {
532  4714 final Iterator it = parameters.keySet().iterator();
533  20294 while (it.hasNext()) {
534  15580 final String key = (String) it.next();
535  15580 setKeyValue(service.getServiceId() + "$" + key, parameters.getString(key));
536    }
537    }
538   
539    /**
540    * Get value for given service key.
541    *
542    * @param service Setting for this service.
543    * @param key Get value for this key.
544    * @param defaultValue Default value..
545    * @return Value. If value for key is originally <code>null</code> <code>defaultValue</code>
546    * is returned.
547    */
 
548  1 toggle public int getServiceKeyValue(final Service service, final String key, final int defaultValue) {
549  1 return getKeyValue(service.getServiceId() + "$" + key, defaultValue);
550    }
551   
552    /**
553    * Set value for given service key.
554    *
555    * @param service Setting for this service.
556    * @param key For this key.
557    * @param value Set this value.
558    */
 
559  1 toggle public void setServiceKeyValue(final Service service, final String key, final int value) {
560  1 setKeyValue(service.getServiceId() + "$" + key, value);
561    }
562   
563    /**
564    * Get value for given service key.
565    *
566    * @param service Setting for this service.
567    * @param key Get value for this key.
568    * @param defaultValue Default value..
569    * @return Value. If value for key is originally <code>null</code> <code>defaultValue</code>
570    * is returned.
571    */
 
572  1 toggle public boolean getServiceKeyValue(final Service service, final String key, final boolean defaultValue) {
573  1 return getKeyValue(service.getServiceId() + "$" + key, defaultValue);
574    }
575   
576    /**
577    * Set value for given service key.
578    *
579    * @param service Setting for this service.
580    * @param key For this key.
581    * @param value Set this value.
582    */
 
583  1 toggle public void setServiceKeyValue(final Service service, final String key, final boolean value) {
584  1 setKeyValue(service.getServiceId() + "$" + key, value);
585    }
586   
587    }