01: /**
02: * Configuration.java
03: * Created on 12.03.2003, 0:31:22 Alex
04: * Package: net.sf.memoranda.util
05: *
06: * @author Alex V. Alishevskikh, alex@openmechanics.net
07: * Copyright (c) 2003 Memoranda Team. http://memoranda.sf.net
08: */package net.sf.memoranda.util;
09:
10: import java.io.File;
11: import java.io.FileInputStream;
12: import java.io.FileOutputStream;
13:
14: import net.sf.memoranda.ui.ExceptionDialog;
15:
16: /**
17: *
18: */
19: /*$Id: Configuration.java,v 1.5 2004/10/11 08:48:21 alexeya Exp $*/
20: public class Configuration {
21:
22: static LoadableProperties config = new LoadableProperties();
23: static String configPath = getConfigPath();
24:
25: static {
26: try {
27: config.load(new FileInputStream(configPath));
28: } catch (Exception e) {
29: File f = new File(configPath);
30: new File(f.getParent()).mkdirs();
31: /*DEBUG*/System.out.println("New configuration created: "
32: + configPath);
33: try {
34: config
35: .load(Configuration.class
36: .getResourceAsStream("resources/memoranda.default.properties"));
37: saveConfig();
38: } catch (Exception e2) {
39: new ExceptionDialog(
40: e2,
41: "Failed to load default configuration from resources.",
42: "");
43: config = null;
44: }
45: }
46: }
47:
48: static String getConfigPath() {
49: String p = Util.getEnvDir() + "memoranda.properties";
50: if (new File(p).exists())
51: return p;
52: String p1 = Util.getEnvDir() + "jnotes2.properties";
53: if (new File(p1).exists()) {
54: /*DEBUG*/System.out.println(p + " not found.\n" + p1
55: + " used instead.");
56: return p1;
57: }
58: return p;
59: }
60:
61: public static void saveConfig() {
62: try {
63: config.save(new FileOutputStream(configPath));
64: } catch (Exception e) {
65: new ExceptionDialog(e,
66: "Failed to save a configuration file:<br>"
67: + configPath, "");
68: }
69: }
70:
71: public static Object get(String key) {
72: if ((config.get(key)) == null) {
73: /*DEBUG*///System.out.println("Configuration: Key '"+key+"' not found.");
74: return "";
75: }
76: return config.get(key);
77: }
78:
79: public static void put(String key, Object value) {
80: config.put(key, value);
81: }
82: }
|