chris@0: /* chris@0: * StarOffice News Server chris@0: * see AUTHORS for the list of contributors chris@0: * chris@0: * This program is free software: you can redistribute it and/or modify chris@0: * it under the terms of the GNU General Public License as published by chris@0: * the Free Software Foundation, either version 3 of the License, or chris@0: * (at your option) any later version. chris@0: * chris@0: * This program is distributed in the hope that it will be useful, chris@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of chris@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the chris@0: * GNU General Public License for more details. chris@0: * chris@0: * You should have received a copy of the GNU General Public License chris@0: * along with this program. If not, see . chris@0: */ chris@0: chris@0: package com.so.news; chris@0: chris@0: import java.io.FileInputStream; chris@0: import java.io.FileNotFoundException; chris@0: import java.io.FileOutputStream; chris@0: import java.io.IOException; chris@0: import java.util.Properties; chris@0: chris@0: /** chris@0: * Manages the n3tpd configuration. chris@0: * @author Christian Lins chris@0: */ chris@0: public class Config chris@0: { chris@0: /** The filename of the logfile */ chris@0: public static final String CONFIG_N3TPD_LOGFILE = "n3tpd.logfile"; chris@0: chris@0: /** The filename of the config file that is loaded on startup */ chris@0: public static final String FILE = "n3tpd.conf"; chris@0: chris@0: private static final Properties defaultConfig = new Properties(); chris@0: chris@0: private static Config instance = null; chris@0: chris@0: static chris@0: { chris@0: // Set some default values chris@0: defaultConfig.setProperty("n3tpd.article.lifetime", "300"); // 300 days chris@0: defaultConfig.setProperty("n3tpd.article.maxsize", "100"); // 100 kbyte chris@0: defaultConfig.setProperty("n3tpd.port", "119"); chris@0: defaultConfig.setProperty("n3tpd.auxport", "8080"); chris@0: defaultConfig.setProperty("n3tpd.server.backlog", "10"); chris@0: defaultConfig.setProperty("n3tpd.hostname", "localhost"); chris@0: defaultConfig.setProperty("n3tpd.storage.database", "jdbc:mysql://localhost/n3tpd_data"); chris@0: defaultConfig.setProperty("n3tpd.storage.dbmsdriver", "com.mysql.jdbc.Driver"); chris@0: defaultConfig.setProperty("n3tpd.storage.user", "n3tpd_user"); chris@0: defaultConfig.setProperty("n3tpd.storage.password", "mysecret"); chris@0: chris@0: instance = new Config(); chris@0: } chris@0: chris@0: /** chris@0: * @return A Config instance chris@0: */ chris@0: public static Config getInstance() chris@0: { chris@0: return instance; chris@0: } chris@0: chris@0: // Every config instance is initialized with the default values. chris@0: private Properties settings = (Properties)defaultConfig.clone(); chris@0: chris@0: /** chris@0: * Config is a singelton class with only one instance at time. chris@0: * So the constructor is private to prevent the creation of more chris@0: * then one Config instance. chris@0: * @see Config.getInstance() to retrieve an instance of Config chris@0: */ chris@0: private Config() chris@0: { chris@0: try chris@0: { chris@0: // Load settings from file chris@0: load(); chris@0: } chris@0: catch(IOException e) chris@0: { chris@0: e.printStackTrace(); chris@0: } chris@0: } chris@0: chris@0: /** chris@0: * Loads the configuration from the config file. By default this is done chris@0: * by the (private) constructor but it can be useful to reload the config chris@0: * by invoking this method. chris@0: * @throws IOException chris@0: */ chris@0: public void load() throws IOException chris@0: { chris@0: try chris@0: { chris@0: settings.load(new FileInputStream(FILE)); chris@0: } chris@0: catch (FileNotFoundException e) chris@0: { chris@0: save(); chris@0: } chris@0: } chris@0: chris@0: /** chris@0: * Saves this Config to the config file. By default this is done chris@0: * at program end. chris@0: * @throws FileNotFoundException chris@0: * @throws IOException chris@0: */ chris@0: public void save() throws FileNotFoundException, IOException chris@0: { chris@0: settings.store(new FileOutputStream(FILE), "N3TPD Config File"); chris@0: } chris@0: chris@0: /** chris@0: * Returns the value that is stored within this config chris@0: * identified by the given key. If the key cannot be found chris@0: * the default value is returned. chris@0: * @param key Key to identify the value. chris@0: * @param def The default value that is returned if the key chris@0: * is not found in this Config. chris@0: * @return chris@0: */ chris@0: public String get(String key, String def) chris@0: { chris@0: return settings.getProperty(key, def); chris@0: } chris@0: chris@0: /** chris@0: * Returns the value that is stored within this config chris@0: * identified by the given key. If the key cannot be found chris@0: * the default value is returned. chris@0: * @param key Key to identify the value. chris@0: * @param def The default value that is returned if the key chris@0: * is not found in this Config. chris@0: * @return chris@0: */ chris@0: public int get(String key, int def) chris@0: { chris@0: try chris@0: { chris@0: String val = get(key); chris@0: return Integer.parseInt(val); chris@0: } chris@0: catch(Exception e) chris@0: { chris@0: return def; chris@0: } chris@0: } chris@0: chris@0: /** chris@0: * Returns the value that is stored within this config chris@0: * identified by the given key. If the key cannot be found chris@0: * the default value is returned. chris@0: * @param key Key to identify the value. chris@0: * @param def The default value that is returned if the key chris@0: * is not found in this Config. chris@0: * @return chris@0: */ chris@0: public long get(String key, long def) chris@0: { chris@0: try chris@0: { chris@0: String val = get(key); chris@0: return Long.parseLong(val); chris@0: } chris@0: catch(Exception e) chris@0: { chris@0: return def; chris@0: } chris@0: } chris@0: chris@0: /** chris@0: * Returns the value for the given key or null if the chris@0: * key is not found in this Config. chris@0: * @param key chris@0: * @return chris@0: */ chris@0: private String get(String key) chris@0: { chris@0: return settings.getProperty(key); chris@0: } chris@0: chris@0: /** chris@0: * Sets the value for a given key. chris@0: * @param key chris@0: * @param value chris@0: */ chris@0: public void set(String key, String value) chris@0: { chris@0: settings.setProperty(key, value); chris@0: } chris@0: chris@0: }