001: /*
002: * Copyright (c) 2004-2006, Jean-François Brazeau. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * 2. Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: *
014: * 3. The name of the author may not be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
018: * IMPLIEDWARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
019: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
021: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
022: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
023: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
024: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
025: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
026: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028: package jfb.tools.activitymgr.ui.util;
029:
030: import java.io.File;
031: import java.io.FileInputStream;
032: import java.io.FileOutputStream;
033: import java.io.IOException;
034: import java.util.Properties;
035:
036: /**
037: * Gestionnaire de la configuration.
038: */
039: public class CfgMgr {
040:
041: /** Constantes associées au paramétrage */
042: public static final String DATABASE_TYPE = "database.type";
043: public static final String JDBC_DRIVER = "jdbc.driver";
044: public static final String DATABASE_HOST = "database.host";
045: public static final String DATABASE_PORT = "database.port";
046: public static final String DATABASE_DATA_FILE = "datafile.name";
047: public static final String DATABASE_NAME = "database.name";
048: public static final String JDBC_URL = "jdbc.url";
049: public static final String JDBC_USER = "jdbc.user";
050: public static final String JDBC_PASSWORD = "jdbc.password";
051:
052: /** Nom du fichier de configuration */
053: private static final String CFG_FILE = "cfg/activitymgr.properties";
054:
055: /** Configuration */
056: private static Properties props = new Properties();
057:
058: /**
059: * Retourne la valeur associée à un paramètre.
060: * @param key le code de paramètre.
061: * @return la valeur du paramètre.
062: */
063: public static String get(String key) {
064: return props.getProperty(key);
065: }
066:
067: /**
068: * Définit la valeur d'un paramètre.
069: * @param key le code de paramètre.
070: * @param value la nouvelle valeur du paramètre.
071: */
072: public static void set(String key, String value) {
073: props.setProperty(key, value);
074: }
075:
076: /**
077: * Sauve le paramétrage.
078: * @throws IOException levé en cas d'incident I/O lors de l'accès en
079: * écriture sur le fichier de configuration.
080: */
081: public static void save() throws IOException {
082: FileOutputStream out = new FileOutputStream(CFG_FILE);
083: props.store(out, "Activity Manager configuration data");
084: out.close();
085: }
086:
087: /**
088: * Charge la configuration.
089: * @throws IOException levé en cas d'incident I/O lors de l'accès en
090: * lecture sur le fichier de configuration.
091: */
092: public static void load() throws IOException {
093: props.clear();
094: File cfgFile = new File(CFG_FILE);
095: if (cfgFile.exists()) {
096: FileInputStream in = new FileInputStream(CFG_FILE);
097: props.load(in);
098: in.close();
099: }
100: }
101:
102: /**
103: * Initie la configuration à partir d'un dioctionnaire spécifique.
104: * @param props le dictionnaire de propriétés.
105: */
106: public static void init(Properties props) {
107: CfgMgr.props = props;
108: }
109:
110: }
|