1.1 --- a/trunk/com/so/news/Config.java Tue Jan 20 10:21:03 2009 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,198 +0,0 @@
1.4 -/*
1.5 - * StarOffice News Server
1.6 - * see AUTHORS for the list of contributors
1.7 - *
1.8 - * This program is free software: you can redistribute it and/or modify
1.9 - * it under the terms of the GNU General Public License as published by
1.10 - * the Free Software Foundation, either version 3 of the License, or
1.11 - * (at your option) any later version.
1.12 - *
1.13 - * This program is distributed in the hope that it will be useful,
1.14 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 - * GNU General Public License for more details.
1.17 - *
1.18 - * You should have received a copy of the GNU General Public License
1.19 - * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 - */
1.21 -
1.22 -package com.so.news;
1.23 -
1.24 -import java.io.FileInputStream;
1.25 -import java.io.FileNotFoundException;
1.26 -import java.io.FileOutputStream;
1.27 -import java.io.IOException;
1.28 -import java.util.Properties;
1.29 -
1.30 -/**
1.31 - * Manages the n3tpd configuration.
1.32 - * @author Christian Lins
1.33 - */
1.34 -public class Config
1.35 -{
1.36 - /** The filename of the logfile */
1.37 - public static final String CONFIG_N3TPD_LOGFILE = "n3tpd.logfile";
1.38 -
1.39 - /** The filename of the config file that is loaded on startup */
1.40 - public static final String FILE = "n3tpd.conf";
1.41 -
1.42 - private static final Properties defaultConfig = new Properties();
1.43 -
1.44 - private static Config instance = null;
1.45 -
1.46 - static
1.47 - {
1.48 - // Set some default values
1.49 - defaultConfig.setProperty("n3tpd.article.lifetime", "300"); // 300 days
1.50 - defaultConfig.setProperty("n3tpd.article.maxsize", "100"); // 100 kbyte
1.51 - defaultConfig.setProperty("n3tpd.port", "119");
1.52 - defaultConfig.setProperty("n3tpd.auxport", "8080");
1.53 - defaultConfig.setProperty("n3tpd.server.backlog", "10");
1.54 - defaultConfig.setProperty("n3tpd.hostname", "localhost");
1.55 - defaultConfig.setProperty("n3tpd.storage.database", "jdbc:mysql://localhost/n3tpd_data");
1.56 - defaultConfig.setProperty("n3tpd.storage.dbmsdriver", "com.mysql.jdbc.Driver");
1.57 - defaultConfig.setProperty("n3tpd.storage.user", "n3tpd_user");
1.58 - defaultConfig.setProperty("n3tpd.storage.password", "mysecret");
1.59 -
1.60 - instance = new Config();
1.61 - }
1.62 -
1.63 - /**
1.64 - * @return A Config instance
1.65 - */
1.66 - public static Config getInstance()
1.67 - {
1.68 - return instance;
1.69 - }
1.70 -
1.71 - // Every config instance is initialized with the default values.
1.72 - private Properties settings = (Properties)defaultConfig.clone();
1.73 -
1.74 - /**
1.75 - * Config is a singelton class with only one instance at time.
1.76 - * So the constructor is private to prevent the creation of more
1.77 - * then one Config instance.
1.78 - * @see Config.getInstance() to retrieve an instance of Config
1.79 - */
1.80 - private Config()
1.81 - {
1.82 - try
1.83 - {
1.84 - // Load settings from file
1.85 - load();
1.86 - }
1.87 - catch(IOException e)
1.88 - {
1.89 - e.printStackTrace();
1.90 - }
1.91 - }
1.92 -
1.93 - /**
1.94 - * Loads the configuration from the config file. By default this is done
1.95 - * by the (private) constructor but it can be useful to reload the config
1.96 - * by invoking this method.
1.97 - * @throws IOException
1.98 - */
1.99 - public void load() throws IOException
1.100 - {
1.101 - try
1.102 - {
1.103 - settings.load(new FileInputStream(FILE));
1.104 - }
1.105 - catch (FileNotFoundException e)
1.106 - {
1.107 - save();
1.108 - }
1.109 - }
1.110 -
1.111 - /**
1.112 - * Saves this Config to the config file. By default this is done
1.113 - * at program end.
1.114 - * @throws FileNotFoundException
1.115 - * @throws IOException
1.116 - */
1.117 - public void save() throws FileNotFoundException, IOException
1.118 - {
1.119 - settings.store(new FileOutputStream(FILE), "N3TPD Config File");
1.120 - }
1.121 -
1.122 - /**
1.123 - * Returns the value that is stored within this config
1.124 - * identified by the given key. If the key cannot be found
1.125 - * the default value is returned.
1.126 - * @param key Key to identify the value.
1.127 - * @param def The default value that is returned if the key
1.128 - * is not found in this Config.
1.129 - * @return
1.130 - */
1.131 - public String get(String key, String def)
1.132 - {
1.133 - return settings.getProperty(key, def);
1.134 - }
1.135 -
1.136 - /**
1.137 - * Returns the value that is stored within this config
1.138 - * identified by the given key. If the key cannot be found
1.139 - * the default value is returned.
1.140 - * @param key Key to identify the value.
1.141 - * @param def The default value that is returned if the key
1.142 - * is not found in this Config.
1.143 - * @return
1.144 - */
1.145 - public int get(String key, int def)
1.146 - {
1.147 - try
1.148 - {
1.149 - String val = get(key);
1.150 - return Integer.parseInt(val);
1.151 - }
1.152 - catch(Exception e)
1.153 - {
1.154 - return def;
1.155 - }
1.156 - }
1.157 -
1.158 - /**
1.159 - * Returns the value that is stored within this config
1.160 - * identified by the given key. If the key cannot be found
1.161 - * the default value is returned.
1.162 - * @param key Key to identify the value.
1.163 - * @param def The default value that is returned if the key
1.164 - * is not found in this Config.
1.165 - * @return
1.166 - */
1.167 - public long get(String key, long def)
1.168 - {
1.169 - try
1.170 - {
1.171 - String val = get(key);
1.172 - return Long.parseLong(val);
1.173 - }
1.174 - catch(Exception e)
1.175 - {
1.176 - return def;
1.177 - }
1.178 - }
1.179 -
1.180 - /**
1.181 - * Returns the value for the given key or null if the
1.182 - * key is not found in this Config.
1.183 - * @param key
1.184 - * @return
1.185 - */
1.186 - private String get(String key)
1.187 - {
1.188 - return settings.getProperty(key);
1.189 - }
1.190 -
1.191 - /**
1.192 - * Sets the value for a given key.
1.193 - * @param key
1.194 - * @param value
1.195 - */
1.196 - public void set(String key, String value)
1.197 - {
1.198 - settings.setProperty(key, value);
1.199 - }
1.200 -
1.201 -}