1
2
3
4
5
6
7
8
9
10
11
12
13
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
31
32
33
34 public class QedeqConfig {
35
36
37 private static final String DEFAULT_LOCAL_MODULES_DIRECTORY
38 = "local";
39
40
41 private static final String DEFAULT_LOCAL_BUFFER
42 = "buffer";
43
44
45 private static final String DEFAULT_GENERATED
46 = "generated";
47
48
49 private static final String DEFAULT_LOG_FILE
50 = "log/log.txt";
51
52
53 private final ConfigAccess configAccess;
54
55
56 private final File basisDirectory;
57
58
59
60
61
62
63
64
65
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
75
76
77
78 public final void store() throws IOException {
79 configAccess.store();
80 }
81
82
83
84
85
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
97
98
99
100 public final void setGenerationDirectory(final File location) {
101 final String relative = createRelativePath(location);
102 setKeyValue("generationLocation", relative);
103 }
104
105
106
107
108
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
121
122
123
124
125 public final void setBufferDirectory(final File location) {
126 final String relative = createRelativePath(location);
127 setKeyValue("bufferLocation", relative);
128 }
129
130
131
132
133
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
146
147
148
149
150 public final void setLocalModulesDirectory(final File location) {
151 final String relative = createRelativePath(location);
152 setKeyValue("localModulesDirectory", relative);
153 }
154
155
156
157
158
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
170
171
172
173 public final File getLogFile() {
174 return new File(getBasisDirectory(), getLogFileString());
175 }
176
177
178
179
180
181
182 public final String[] getModuleHistory() {
183 return configAccess.getStringProperties("moduleHistory.");
184 }
185
186
187
188
189
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
201
202
203
204 public final String[] getPreviouslyLoadedModules() {
205 return configAccess.getStringProperties("loadedModule.");
206 }
207
208
209
210
211
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
222
223
224
225 public final File getBasisDirectory() {
226 return basisDirectory;
227 }
228
229
230
231
232
233
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
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
253 e.printStackTrace(System.out);
254 }
255 return result;
256 }
257
258
259
260
261
262
263
264 private final String createRelativePath(final File path) {
265 return IoUtility.createRelativePath(getBasisDirectory(), path);
266 }
267
268
269
270
271
272
273 public boolean isAutoReloadLastSessionChecked() {
274 return "true".equals(
275 getKeyValue("sessionAutoReload", "true"));
276 }
277
278
279
280
281
282
283 public final void setAutoReloadLastSessionChecked(final boolean mode) {
284 setKeyValue("sessionAutoReload", (mode ? "true" : "false"));
285 }
286
287
288
289
290
291
292
293 public final boolean isTraceOn() {
294 return "true".equals(getKeyValue("traceOn", "false"));
295 }
296
297
298
299
300
301
302 public final void setTraceOn(final boolean traceOn) {
303 setKeyValue("traceOn", (traceOn ? "true" : "false"));
304 }
305
306
307
308
309
310
311 public int getConnectionTimeout() {
312 return getKeyValue("connectionTimeout", 2000);
313 }
314
315
316
317
318
319
320 public final void setConnectionTimeout(final int timeout) {
321 setKeyValue("connectionTimeout", timeout);
322 }
323
324
325
326
327
328
329 public int getReadTimeout() {
330 return getKeyValue("readTimeout", 1000);
331 }
332
333
334
335
336
337
338 public final void setReadTimeout(final int timeout) {
339 setKeyValue("readTimeout", timeout);
340 }
341
342
343
344
345
346
347 public final void setHttpProxyHost(final String httpProxyHost) {
348 setKeyValue("http.proxyHost", httpProxyHost);
349 }
350
351
352
353
354
355
356
357
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
369
370
371
372 public final void setHttpProxyPort(final String httpProxyPort) {
373 setKeyValue("http.proxyPort", httpProxyPort);
374 }
375
376
377
378
379
380
381
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
393
394
395
396 public final void setHttpNonProxyHosts(final String httpNonProxyHosts) {
397 setKeyValue("http.nonProxyHosts", httpNonProxyHosts);
398 }
399
400
401
402
403
404
405
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
417
418
419
420
421 protected synchronized String getKeyValue(final String key) {
422 return configAccess.getString(key);
423 }
424
425
426
427
428
429
430
431
432
433 protected synchronized String getKeyValue(final String key, final String defaultValue) {
434 return configAccess.getString(key, defaultValue);
435 }
436
437
438
439
440
441
442
443 protected synchronized void setKeyValue(final String key, final String value) {
444 configAccess.setString(key, value);
445 }
446
447
448
449
450
451
452
453
454
455 protected synchronized int getKeyValue(final String key, final int defaultValue) {
456 return configAccess.getInteger(key, defaultValue);
457 }
458
459
460
461
462
463
464
465 protected synchronized void setKeyValue(final String key, final int value) {
466 configAccess.setInteger(key, value);
467 }
468
469
470
471
472
473
474
475
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
483
484
485
486
487 protected void setKeyValue(final String key, final boolean value) {
488 setKeyValue(key, (value ? "true" : "false"));
489 }
490
491
492
493
494
495
496
497 public Parameters getServiceEntries(final Service service) {
498 return new Parameters(configAccess.getProperties(service.getServiceId() + "$"));
499 }
500
501
502
503
504
505
506
507
508
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
516
517
518
519
520
521 public void setServiceKeyValue(final Service service, final String key, final String value) {
522 setKeyValue(service.getServiceId() + "$" + key, value);
523 }
524
525
526
527
528
529
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
541
542
543
544
545
546
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
554
555
556
557
558
559 public void setServiceKeyValue(final Service service, final String key, final int value) {
560 setKeyValue(service.getServiceId() + "$" + key, value);
561 }
562
563
564
565
566
567
568
569
570
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
578
579
580
581
582
583 public void setServiceKeyValue(final Service service, final String key, final boolean value) {
584 setKeyValue(service.getServiceId() + "$" + key, value);
585 }
586
587 }