| 1 | /* This file is part of the project "Hilbert II" - http://www.qedeq.org |
| 2 | * |
| 3 | * Copyright 2000-2014, 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 inclusive path. */ |
| 49 | private static final String DEFAULT_LOG_FILE |
| 50 | = "log/log.txt"; |
| 51 | |
| 52 | /** This class organizes the access to the configuration 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 | public QedeqConfig(final File configFile, final String description, final File basisDirectory) |
| 68 | throws IOException { |
| 69 | configAccess = new ConfigAccess(configFile, description); |
| 70 | this.basisDirectory = basisDirectory.getCanonicalFile(); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Store properties in configuration file. |
| 75 | * |
| 76 | * @throws IOException Writing failed. |
| 77 | */ |
| 78 | public final void store() throws IOException { |
| 79 | configAccess.store(); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Get local file directory to save generated files in. |
| 84 | * |
| 85 | * @return Generation directory. |
| 86 | */ |
| 87 | public final File getGenerationDirectory() { |
| 88 | String location = getKeyValue("generationLocation"); |
| 89 | if (location == null) { |
| 90 | location = QedeqConfig.DEFAULT_GENERATED; |
| 91 | } |
| 92 | return createAbsolutePath(location); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Set local file directory for generated files. |
| 97 | * |
| 98 | * @param location generation directory. |
| 99 | */ |
| 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 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 | public Parameters getServiceEntries(final Service service) { |
| 498 | 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 | public String getServiceKeyValue(final Service service, final String key, final String defaultValue) { |
| 511 | 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 | public void setServiceKeyValue(final Service service, final String key, final String value) { |
| 522 | 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 | public void setServiceKeyValues(final Service service, final Parameters parameters) { |
| 532 | final Iterator it = parameters.keySet().iterator(); |
| 533 | while (it.hasNext()) { |
| 534 | final String key = (String) it.next(); |
| 535 | 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 | public int getServiceKeyValue(final Service service, final String key, final int defaultValue) { |
| 549 | 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 | public void setServiceKeyValue(final Service service, final String key, final int value) { |
| 560 | 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 | public boolean getServiceKeyValue(final Service service, final String key, final boolean defaultValue) { |
| 573 | 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 | public void setServiceKeyValue(final Service service, final String key, final boolean value) { |
| 584 | setKeyValue(service.getServiceId() + "$" + key, value); |
| 585 | } |
| 586 | |
| 587 | } |