Initial import.
2 * StarOffice News Server
3 * see AUTHORS for the list of contributors
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 import java.io.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.util.Properties;
28 * Manages the n3tpd configuration.
29 * @author Christian Lins
33 /** The filename of the logfile */
34 public static final String CONFIG_N3TPD_LOGFILE = "n3tpd.logfile";
36 /** The filename of the config file that is loaded on startup */
37 public static final String FILE = "n3tpd.conf";
39 private static final Properties defaultConfig = new Properties();
41 private static Config instance = null;
45 // Set some default values
46 defaultConfig.setProperty("n3tpd.article.lifetime", "300"); // 300 days
47 defaultConfig.setProperty("n3tpd.article.maxsize", "100"); // 100 kbyte
48 defaultConfig.setProperty("n3tpd.port", "119");
49 defaultConfig.setProperty("n3tpd.auxport", "8080");
50 defaultConfig.setProperty("n3tpd.server.backlog", "10");
51 defaultConfig.setProperty("n3tpd.hostname", "localhost");
52 defaultConfig.setProperty("n3tpd.storage.database", "jdbc:mysql://localhost/n3tpd_data");
53 defaultConfig.setProperty("n3tpd.storage.dbmsdriver", "com.mysql.jdbc.Driver");
54 defaultConfig.setProperty("n3tpd.storage.user", "n3tpd_user");
55 defaultConfig.setProperty("n3tpd.storage.password", "mysecret");
57 instance = new Config();
61 * @return A Config instance
63 public static Config getInstance()
68 // Every config instance is initialized with the default values.
69 private Properties settings = (Properties)defaultConfig.clone();
72 * Config is a singelton class with only one instance at time.
73 * So the constructor is private to prevent the creation of more
74 * then one Config instance.
75 * @see Config.getInstance() to retrieve an instance of Config
81 // Load settings from file
91 * Loads the configuration from the config file. By default this is done
92 * by the (private) constructor but it can be useful to reload the config
93 * by invoking this method.
96 public void load() throws IOException
100 settings.load(new FileInputStream(FILE));
102 catch (FileNotFoundException e)
109 * Saves this Config to the config file. By default this is done
111 * @throws FileNotFoundException
112 * @throws IOException
114 public void save() throws FileNotFoundException, IOException
116 settings.store(new FileOutputStream(FILE), "N3TPD Config File");
120 * Returns the value that is stored within this config
121 * identified by the given key. If the key cannot be found
122 * the default value is returned.
123 * @param key Key to identify the value.
124 * @param def The default value that is returned if the key
125 * is not found in this Config.
128 public String get(String key, String def)
130 return settings.getProperty(key, def);
134 * Returns the value that is stored within this config
135 * identified by the given key. If the key cannot be found
136 * the default value is returned.
137 * @param key Key to identify the value.
138 * @param def The default value that is returned if the key
139 * is not found in this Config.
142 public int get(String key, int def)
146 String val = get(key);
147 return Integer.parseInt(val);
156 * Returns the value that is stored within this config
157 * identified by the given key. If the key cannot be found
158 * the default value is returned.
159 * @param key Key to identify the value.
160 * @param def The default value that is returned if the key
161 * is not found in this Config.
164 public long get(String key, long def)
168 String val = get(key);
169 return Long.parseLong(val);
178 * Returns the value for the given key or null if the
179 * key is not found in this Config.
183 private String get(String key)
185 return settings.getProperty(key);
189 * Sets the value for a given key.
193 public void set(String key, String value)
195 settings.setProperty(key, value);