QedeqConfig.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2011,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.kernel.se.config;
017 
018 import java.io.File;
019 import java.io.IOException;
020 import java.util.List;
021 import java.util.Map;
022 
023 import org.qedeq.base.io.IoUtility;
024 import org.qedeq.kernel.se.common.Plugin;
025 
026 
027 /**
028  * This class gives a type save access to properties of the application.
029  *
030  @author  Michael Meyling
031  */
032 public class QedeqConfig {
033 
034     /** Default location for newly created QEDEQ modules. */
035     private static final String DEFAULT_LOCAL_MODULES_DIRECTORY
036         "local";
037 
038     /** Default location for locally buffered module files. */
039     private static final String DEFAULT_LOCAL_BUFFER
040         "buffer";
041 
042     /** Default location for generated module and document files. */
043     private static final String DEFAULT_GENERATED
044         "generated";
045 
046     /** Default log file path. */
047     private static final String DEFAULT_LOG_FILE
048         "log/log.txt";
049 
050     /** This class organizes the access to the config parameters. */
051     private final ConfigAccess configAccess;
052 
053     /** Basis directory of application for all variable data. Basis for all relative paths. */
054     private final File basisDirectory;
055 
056     /**
057      * Constructor.
058      *
059      @param   configFile       Config file.
060      @param   description      Config file description.
061      @param   basisDirectory   Basis directory of application for all variable data. Basis for all
062      *                           relative paths
063      @throws  IOException      Config file couldn't be loaded.
064      */
065     public QedeqConfig(final File configFile, final String description, final File basisDirectory)
066             throws IOException {
067         configAccess = new ConfigAccess(configFile, description);
068         this.basisDirectory = basisDirectory;
069     }
070 
071     /**
072      * Store properties in config file.
073      *
074      @throws  IOException Writing failed.
075      */
076     public final void store() throws IOException {
077         configAccess.store();
078     }
079 
080     /**
081      * Get local file directory to save generated files in.
082      *
083      @return  Generation directory.
084      */
085     public final File getGenerationDirectory() {
086         String location = getKeyValue("generationLocation");
087         if (location == null) {
088             location = QedeqConfig.DEFAULT_GENERATED;
089         }
090         return createAbsolutePath(location);
091     }
092 
093     /**
094      * Set local file directory for generated files.
095      *
096      @param   location    generation directory.
097      */
098     public final void setGenerationDirectory(final File location) {
099         final String relative = createRelativePath(location);
100         setKeyValue("generationLocation", relative);
101     }
102 
103     /**
104      * Get local file directory for module buffering.
105      *
106      @return  Buffer directory.
107      */
108     public final File getBufferDirectory() {
109         String location = getKeyValue("bufferLocation");
110         if (location == null) {
111             location = QedeqConfig.DEFAULT_LOCAL_BUFFER;
112         }
113         return createAbsolutePath(location);
114     }
115 
116 
117     /**
118      * Set local file directory for module buffering.
119      * After changing this location the buffer should eventually be cleared.
120      *
121      @param   location    buffer directory.
122      */
123     public final void setBufferDirectory(final File location) {
124         final String relative = createRelativePath(location);
125         setKeyValue("bufferLocation", relative);
126     }
127 
128     /**
129      * Get directory for newly created QEDEQ module files.
130      *
131      @return  Directory for newly created QEDEQ modules.
132      */
133     public final File getLocalModulesDirectory() {
134         String location = getKeyValue("localModulesDirectory");
135         if (location == null) {
136             location = QedeqConfig.DEFAULT_LOCAL_MODULES_DIRECTORY;
137         }
138         return createAbsolutePath(location);
139     }
140 
141 
142     /**
143      * Set directory for newly created module files.
144      * After changing this location the buffer should eventually be cleared.
145      *
146      @param   location    Buffer directory.
147      */
148     public final void setLocalModulesDirectory(final File location) {
149         final String relative = createRelativePath(location);
150         setKeyValue("localModulesDirectory", relative);
151     }
152 
153     /**
154      * Get local file location for log file.
155      *
156      @return  Log file path.
157      */
158     public final String getLogFile() {
159         final String location = getKeyValue("logLocation");
160         if (location == null) {
161             return QedeqConfig.DEFAULT_LOG_FILE;
162         }
163         return location;
164     }
165 
166     /**
167      * Get history of modules, which were tried to load.
168      *
169      @return  list of modules.
170      */
171     public final String[] getModuleHistory() {
172         return configAccess.getStringProperties("moduleHistory.");
173     }
174 
175     /**
176      * Save history of modules, which were tried to load.
177      *
178      @param  modules  list of modules.
179      */
180     public final void saveModuleHistory(final List modules) {
181         configAccess.removeProperties(("moduleHistory."));
182         for (int i = 0; i < modules.size(); i++) {
183             setKeyValue("moduleHistory." (i + 101),
184             modules.get(i).toString());
185         }
186     }
187 
188     /**
189      * Get list of previously checked modules.
190      *
191      @return  list of modules.
192      */
193     public final String[] getPreviouslyCheckedModules() {
194         return configAccess.getStringProperties("checkedModule.");
195     }
196 
197     /**
198      * Set successfully list of successfully loaded QEDEQ modules.
199      *
200      @param   moduleAddresses     This modules were successfully checked.
201      */
202     public final void setLoadedModules(final String[] moduleAddresses) {
203         configAccess.removeProperties("checkedModule.");
204         for (int i = 0; i < moduleAddresses.length; i++) {
205             setKeyValue("checkedModule." (i + 1), moduleAddresses[i]);
206         }
207     }
208 
209     /**
210      * Get basis directory of this application.
211      *
212      @return  Basis directory of application for all variable data. Basis for all relative paths.
213      */
214     public final File getBasisDirectory() {
215         return basisDirectory;
216     }
217 
218     /**
219      * Get file path starting from basis directory of this application.
220      *
221      @param   path    Go to this path starting from basis directory.
222      @return  File path resolved against basis application directory.
223      */
224     public final File createAbsolutePath(final String path) {
225         File result = new File(getBasisDirectory(), path);
226         try {
227             result = result.getCanonicalFile();
228         catch (IOException e) {
229             // we don't know if we can log something already
230             e.printStackTrace();
231         }
232         return result;
233     }
234 
235     /**
236      * Create relative file path starting from basis directory of this application.
237      *
238      @param   path    Reach this path starting from basis directory.
239      @return  File path relative to basis application directory.
240      */
241     private final String createRelativePath(final File path) {
242         return IoUtility.createRelativePath(getBasisDirectory(), path);
243     }
244 
245     /**
246      * Get auto reload of last session successfully loaded modules.
247      *
248      @return auto reload enabled?
249      */
250     public boolean isAutoReloadLastSessionChecked() {
251         return "true".equals(
252             getKeyValue("sessionAutoReload""true"));
253     }
254 
255     /**
256      * Set auto reload checked modules of last session mode.
257      *
258      @param  mode     enable auto reload?
259      */
260     public final void setAutoReloadLastSessionChecked(final boolean mode) {
261         setKeyValue("sessionAutoReload"(mode ? "true" "false"));
262     }
263 
264     /**
265      * Is tracing on? If not, only business and fatal messages are logged.
266      * Otherwise all events are logged according to the log level settings.
267      *
268      @return  Is tracing on?
269      */
270     public final boolean isTraceOn() {
271         return "true".equals(getKeyValue("traceOn""false"));
272     }
273 
274     /**
275      * Set tracing on.
276      *
277      @param  traceOn     Set trace on.
278      */
279     public final void setTraceOn(final boolean traceOn) {
280         setKeyValue("traceOn"(traceOn ? "true" "false"));
281     }
282 
283     /**
284      * Get connection timeout, especially for TCP/IP connections.
285      *
286      @return  Connection timeout (in milliseconds).
287      */
288     public int getConnectTimeout() {
289         return getKeyValue("connectionTimeout"2000);
290     }
291 
292     /**
293      * Set connection timeout, especially for TCP/IP connections.
294      *
295      @param   timeout Connection timeout, especially for TCP/IP connections. In milliseconds.
296      */
297     public final void setConnectionTimeout(final int timeout) {
298         setKeyValue("connectionTimeout", timeout);
299     }
300 
301     /**
302      * Get read timeout, especially for TCP/IP connections.
303      *
304      @return  Read timeout (in milliseconds).
305      */
306     public int getReadTimeout() {
307         return getKeyValue("readTimeout"1000);
308     }
309 
310     /**
311      * Set read timeout, especially for TCP/IP connections.
312      *
313      @param   timeout Read timeout, especially for TCP/IP connections. In milliseconds.
314      */
315     public final void setReadTimeout(final int timeout) {
316         setKeyValue("readTimeout", timeout);
317     }
318 
319     /**
320      * Set http proxy host.
321      *
322      @param  httpProxyHost    Http proxy server.
323      */
324     public final void setHttpProxyHost(final String httpProxyHost) {
325         setKeyValue("http.proxyHost", httpProxyHost);
326     }
327 
328     /**
329      * Get http proxy host. It might be a good idea to ignore this value, if the application
330      * was started via Java Webstart.
331      *
332      @return  Http proxy host.
333      */
334     public final String getHttpProxyHost() {
335         final String def = System.getProperty("http.proxyHost");
336         if (def != null) {
337             return getKeyValue("http.proxyHost", def);
338         }
339         return getKeyValue("http.proxyHost");
340     }
341 
342     /**
343      * Set http proxy port.
344      *
345      @param  httpProxyPort    Http proxy port.
346      */
347     public final void setHttpProxyPort(final String httpProxyPort) {
348         setKeyValue("http.proxyPort", httpProxyPort);
349     }
350 
351     /**
352      * Get http proxy port. It might be a good idea to ignore this value, if the application
353      * was started via Java Webstart.
354      *
355      @return  Http proxy port.
356      */
357     public final String getHttpProxyPort() {
358         final String def = System.getProperty("http.proxyPort");
359         if (def != null) {
360             return getKeyValue("http.proxyPort", def);
361         }
362         return getKeyValue("http.proxyPort");
363     }
364 
365     /**
366      * Set http non proxy hosts.
367      *
368      @param  httpNonProxyHosts    Http non proxy hosts.
369      */
370     public final void setHttpNonProxyHosts(final String httpNonProxyHosts) {
371         setKeyValue("http.nonProxyHosts", httpNonProxyHosts);
372     }
373 
374     /**
375      * Get non http proxy hosts. It might be a good idea to ignore this value, if the application
376      * was started via Java Webstart.
377      *
378      @return  Http non proxy hosts.
379      */
380     public final String getHttpNonProxyHosts() {
381         final String def = System.getProperty("http.nonProxyHosts");
382         if (def != null) {
383             return getKeyValue("http.nonProxyHosts", def);
384         }
385         return getKeyValue("http.nonProxyHosts");
386     }
387 
388     /**
389      * Get value for given key.
390      *
391      @param   key     Get value for this key.
392      @return  Value, maybe <code>null</code>.
393      */
394     protected synchronized String getKeyValue(final String key) {
395         return configAccess.getString(key);
396     }
397 
398     /**
399      * Get value for given key.
400      *
401      @param   key             Get value for this key.
402      @param   defaultValue    Default value..
403      @return  Value. If value for key is originally <code>null</code> <code>defaultValue</code>
404      *          is returned..
405      */
406     protected synchronized String getKeyValue(final String key, final String defaultValue) {
407         return configAccess.getString(key, defaultValue);
408     }
409 
410     /**
411      * Set value for given key.
412      *
413      @param   key     For this key.
414      @param   value   Set this value.
415      */
416     protected synchronized void setKeyValue(final String key, final String value) {
417         configAccess.setString(key, value);
418     }
419 
420     /**
421      * Get value for given key.
422      *
423      @param   key             Get value for this key.
424      @param   defaultValue    Default value..
425      @return  Value. If value for key is originally <code>null</code> <code>defaultValue</code>
426      *          is returned..
427      */
428     protected synchronized int getKeyValue(final String key, final int defaultValue) {
429         return configAccess.getInteger(key, defaultValue);
430     }
431 
432     /**
433      * Set value for given key.
434      *
435      @param   key     For this key.
436      @param   value   Set this value.
437      */
438     protected synchronized void setKeyValue(final String key, final int value) {
439         configAccess.setInteger(key, value);
440     }
441 
442     /**
443      * Get value for given key.
444      *
445      @param   key             Get value for this key.
446      @param   defaultValue    Default value..
447      @return  Value. If value for key is originally <code>null</code> <code>defaultValue</code>
448      *          is returned.
449      */
450     protected synchronized boolean getKeyValue(final String key, final boolean defaultValue) {
451         return "true".equals(getKeyValue(key, (defaultValue ? "true" "false")));
452     }
453 
454     /**
455      * Set value for given key.
456      *
457      @param   key     For this key.
458      @param   value   Set this value.
459      */
460     protected void setKeyValue(final String key, final boolean value) {
461         setKeyValue(key, (value ? "true" "false"));
462     }
463 
464     /**
465      * Get plugin properties from configuration file.
466      *
467      @param   plugin  We want to know properties for this plugin
468      @return  Map with properties for this plugin.
469      */
470     public Map getPluginEntries(final Plugin plugin) {
471         return configAccess.getProperties(plugin.getPluginId() "$");
472     }
473 
474     /**
475      * Get value for given plugin key.
476      *
477      @param   plugin  Setting for this plugin.
478      @param   key             Get value for this key.
479      @param   defaultValue    Default value..
480      @return  Value. If value for key is originally <code>null</code> <code>defaultValue</code>
481      *          is returned.
482      */
483     public String getPluginKeyValue(final Plugin plugin, final String key, final String defaultValue) {
484         return getKeyValue(plugin.getPluginId() "$" + key, defaultValue);
485     }
486 
487     /**
488      * Set value for given plugin key.
489      *
490      @param   plugin  Setting for this plugin.
491      @param   key     For this key.
492      @param   value   Set this value.
493      */
494     public void setPluginKeyValue(final Plugin plugin, final String key, final String value) {
495         setKeyValue(plugin.getPluginId() "$" + key, value);
496     }
497 
498     /**
499      * Get value for given plugin key.
500      *
501      @param   plugin  Setting for this plugin.
502      @param   key             Get value for this key.
503      @param   defaultValue    Default value..
504      @return  Value. If value for key is originally <code>null</code> <code>defaultValue</code>
505      *          is returned.
506      */
507     public int getPluginKeyValue(final Plugin plugin, final String key, final int defaultValue) {
508         return getKeyValue(plugin.getPluginId() "$" + key, defaultValue);
509     }
510 
511     /**
512      * Set value for given plugin key.
513      *
514      @param   plugin  Setting for this plugin.
515      @param   key     For this key.
516      @param   value   Set this value.
517      */
518     public void setPluginKeyValue(final Plugin plugin, final String key, final int value) {
519         setKeyValue(plugin.getPluginId() "$" + key, value);
520     }
521 
522     /**
523      * Get value for given plugin key.
524      *
525      @param   plugin  Setting for this plugin.
526      @param   key             Get value for this key.
527      @param   defaultValue    Default value..
528      @return  Value. If value for key is originally <code>null</code> <code>defaultValue</code>
529      *          is returned.
530      */
531     public boolean getPluginKeyValue(final Plugin plugin, final String key, final boolean defaultValue) {
532         return getKeyValue(plugin.getPluginId() "$" + key, defaultValue);
533     }
534 
535     /**
536      * Set value for given plugin key.
537      *
538      @param   plugin  Setting for this plugin.
539      @param   key     For this key.
540      @param   value   Set this value.
541      */
542     public void setPluginKeyValue(final Plugin plugin, final String key, final boolean value) {
543         setKeyValue(plugin.getPluginId() "$" + key, value);
544     }
545 
546 }