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