001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002 *
003 * Copyright 2000-2013, 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.Iterator;
021 import java.util.List;
022
023 import org.qedeq.base.io.IoUtility;
024 import org.qedeq.base.io.Parameters;
025 import org.qedeq.base.io.Path;
026 import org.qedeq.kernel.se.common.Plugin;
027
028
029 /**
030 * This class gives a type save access to properties of the application.
031 *
032 * @author Michael Meyling
033 */
034 public class QedeqConfig {
035
036 /** Default location for newly created QEDEQ modules. */
037 private static final String DEFAULT_LOCAL_MODULES_DIRECTORY
038 = "local";
039
040 /** Default location for locally buffered module files. */
041 private static final String DEFAULT_LOCAL_BUFFER
042 = "buffer";
043
044 /** Default location for generated module and document files. */
045 private static final String DEFAULT_GENERATED
046 = "generated";
047
048 /** Default log file path. */
049 private static final String DEFAULT_LOG_FILE
050 = "log/log.txt";
051
052 /** This class organizes the access to the config parameters. */
053 private final ConfigAccess configAccess;
054
055 /** Basis directory of application for all variable data. Basis for all relative paths. */
056 private final File basisDirectory;
057
058 /**
059 * Constructor.
060 *
061 * @param configFile Config file.
062 * @param description Config file description.
063 * @param basisDirectory Basis directory of application for all variable data. Basis for all
064 * new relative paths
065 * @throws IOException Config file couldn't be loaded.
066 */
067 public QedeqConfig(final File configFile, final String description, final File basisDirectory)
068 throws IOException {
069 configAccess = new ConfigAccess(configFile, description);
070 this.basisDirectory = basisDirectory.getCanonicalFile();
071 }
072
073 /**
074 * Store properties in config file.
075 *
076 * @throws IOException Writing failed.
077 */
078 public final void store() throws IOException {
079 configAccess.store();
080 }
081
082 /**
083 * Get local file directory to save generated files in.
084 *
085 * @return Generation directory.
086 */
087 public final File getGenerationDirectory() {
088 String location = getKeyValue("generationLocation");
089 if (location == null) {
090 location = QedeqConfig.DEFAULT_GENERATED;
091 }
092 return createAbsolutePath(location);
093 }
094
095 /**
096 * Set local file directory for generated files.
097 *
098 * @param location generation directory.
099 */
100 public final void setGenerationDirectory(final File location) {
101 final String relative = createRelativePath(location);
102 setKeyValue("generationLocation", relative);
103 }
104
105 /**
106 * Get local file directory for module buffering.
107 *
108 * @return Buffer directory.
109 */
110 public final File getBufferDirectory() {
111 String location = getKeyValue("bufferLocation");
112 if (location == null) {
113 location = QedeqConfig.DEFAULT_LOCAL_BUFFER;
114 }
115 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 public final void setBufferDirectory(final File location) {
126 final String relative = createRelativePath(location);
127 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 public final File getLocalModulesDirectory() {
136 String location = getKeyValue("localModulesDirectory");
137 if (location == null) {
138 location = QedeqConfig.DEFAULT_LOCAL_MODULES_DIRECTORY;
139 }
140 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 public final void setLocalModulesDirectory(final File location) {
151 final String relative = createRelativePath(location);
152 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 private final String getLogFileString() {
161 final String location = getKeyValue("logLocation");
162 if (location == null) {
163 return QedeqConfig.DEFAULT_LOG_FILE;
164 }
165 return location;
166 }
167
168 /**
169 * Get file location for log file.
170 *
171 * @return Log file path.
172 */
173 public final File getLogFile() {
174 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 public final String[] getModuleHistory() {
183 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 public final void saveModuleHistory(final List modules) {
192 configAccess.removeProperties(("moduleHistory."));
193 for (int i = 0; i < modules.size(); i++) {
194 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 public final String[] getPreviouslyLoadedModules() {
205 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 public final void setPreviouslyLoadedModules(final String[] moduleAddresses) {
214 configAccess.removeProperties("loadedModule.");
215 for (int i = 0; i < moduleAddresses.length; i++) {
216 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 public final File getBasisDirectory() {
226 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 public final File createAbsolutePath(final String path) {
236 File result = new File(path);
237 final Path ptest = new Path(path.replace(File.separatorChar, '/'), "");
238 if (ptest.isAbsolute()) {
239 try {
240 return result.getCanonicalFile();
241 } catch (Exception e) {
242 // we don't know if we can log something already
243 e.printStackTrace(System.out);
244 System.out.println("we try to continue with file " + result);
245 return result;
246 }
247 }
248 result = new File(getBasisDirectory(), path);
249 try {
250 result = result.getCanonicalFile();
251 } catch (IOException e) {
252 // we don't know if we can log something already
253 e.printStackTrace(System.out);
254 }
255 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 private final String createRelativePath(final File path) {
265 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 public boolean isAutoReloadLastSessionChecked() {
274 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 public final void setAutoReloadLastSessionChecked(final boolean mode) {
284 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 public final boolean isTraceOn() {
294 return "true".equals(getKeyValue("traceOn", "false"));
295 }
296
297 /**
298 * Set tracing on.
299 *
300 * @param traceOn Set trace on.
301 */
302 public final void setTraceOn(final boolean traceOn) {
303 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 public int getConnectionTimeout() {
312 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 public final void setConnectionTimeout(final int timeout) {
321 setKeyValue("connectionTimeout", timeout);
322 }
323
324 /**
325 * Get read timeout, especially for TCP/IP connections.
326 *
327 * @return Read timeout (in milliseconds).
328 */
329 public int getReadTimeout() {
330 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 public final void setReadTimeout(final int timeout) {
339 setKeyValue("readTimeout", timeout);
340 }
341
342 /**
343 * Set http proxy host.
344 *
345 * @param httpProxyHost Http proxy server.
346 */
347 public final void setHttpProxyHost(final String httpProxyHost) {
348 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 public final String getHttpProxyHost() {
360 final String def = System.getProperty("http.proxyHost");
361 if (def != null) {
362 return getKeyValue("http.proxyHost", def);
363 }
364 return getKeyValue("http.proxyHost");
365 }
366
367 /**
368 * Set http proxy port.
369 *
370 * @param httpProxyPort Http proxy port.
371 */
372 public final void setHttpProxyPort(final String httpProxyPort) {
373 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 public final String getHttpProxyPort() {
384 final String def = System.getProperty("http.proxyPort");
385 if (def != null) {
386 return getKeyValue("http.proxyPort", def);
387 }
388 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 public final void setHttpNonProxyHosts(final String httpNonProxyHosts) {
397 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 public final String getHttpNonProxyHosts() {
408 final String def = System.getProperty("http.nonProxyHosts");
409 if (def != null) {
410 return getKeyValue("http.nonProxyHosts", def);
411 }
412 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 protected synchronized String getKeyValue(final String key) {
422 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 protected synchronized String getKeyValue(final String key, final String defaultValue) {
434 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 protected synchronized void setKeyValue(final String key, final String value) {
444 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 protected synchronized int getKeyValue(final String key, final int defaultValue) {
456 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 protected synchronized void setKeyValue(final String key, final int value) {
466 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 protected synchronized boolean getKeyValue(final String key, final boolean defaultValue) {
478 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 protected void setKeyValue(final String key, final boolean value) {
488 setKeyValue(key, (value ? "true" : "false"));
489 }
490
491 /**
492 * Get plugin properties from configuration file.
493 *
494 * @param plugin We want to know properties for this plugin
495 * @return Map with properties for this plugin.
496 */
497 public Parameters getPluginEntries(final Plugin plugin) {
498 return new Parameters(configAccess.getProperties(plugin.getPluginId() + "$"));
499 }
500
501 /**
502 * Get value for given plugin key.
503 *
504 * @param plugin Setting for this plugin.
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 public String getPluginKeyValue(final Plugin plugin, final String key, final String defaultValue) {
511 return getKeyValue(plugin.getPluginId() + "$" + key, defaultValue);
512 }
513
514 /**
515 * Set value for given plugin key.
516 *
517 * @param plugin Setting for this plugin.
518 * @param key For this key.
519 * @param value Set this value.
520 */
521 public void setPluginKeyValue(final Plugin plugin, final String key, final String value) {
522 setKeyValue(plugin.getPluginId() + "$" + key, value);
523 }
524
525 /**
526 * Set value for given plugin key.
527 *
528 * @param plugin Setting for this plugin.
529 * @param parameters Parameters for this plugin.
530 */
531 public void setPluginKeyValues(final Plugin plugin, final Parameters parameters) {
532 final Iterator it = parameters.keySet().iterator();
533 while (it.hasNext()) {
534 final String key = (String) it.next();
535 setKeyValue(plugin.getPluginId() + "$" + key, parameters.getString(key));
536 }
537 }
538
539 /**
540 * Get value for given plugin key.
541 *
542 * @param plugin Setting for this plugin.
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 public int getPluginKeyValue(final Plugin plugin, final String key, final int defaultValue) {
549 return getKeyValue(plugin.getPluginId() + "$" + key, defaultValue);
550 }
551
552 /**
553 * Set value for given plugin key.
554 *
555 * @param plugin Setting for this plugin.
556 * @param key For this key.
557 * @param value Set this value.
558 */
559 public void setPluginKeyValue(final Plugin plugin, final String key, final int value) {
560 setKeyValue(plugin.getPluginId() + "$" + key, value);
561 }
562
563 /**
564 * Get value for given plugin key.
565 *
566 * @param plugin Setting for this plugin.
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 public boolean getPluginKeyValue(final Plugin plugin, final String key, final boolean defaultValue) {
573 return getKeyValue(plugin.getPluginId() + "$" + key, defaultValue);
574 }
575
576 /**
577 * Set value for given plugin key.
578 *
579 * @param plugin Setting for this plugin.
580 * @param key For this key.
581 * @param value Set this value.
582 */
583 public void setPluginKeyValue(final Plugin plugin, final String key, final boolean value) {
584 setKeyValue(plugin.getPluginId() + "$" + key, value);
585 }
586
587 }
|