Add some checks to prevent #13 happen.
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/>.
19 package org.sonews.config;
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 bootstrap configuration. It MUST contain all config values
29 * that are needed to establish a database connection.
30 * For further configuration values use the Config class instead as that class
31 * stores its values within the database.
32 * @author Christian Lins
35 class FileConfig extends AbstractConfig
38 private static final Properties defaultConfig = new Properties();
40 private static FileConfig instance = null;
44 // Set some default values
45 defaultConfig.setProperty(Config.STORAGE_DATABASE, "jdbc:mysql://localhost/sonews");
46 defaultConfig.setProperty(Config.STORAGE_DBMSDRIVER, "com.mysql.jdbc.Driver");
47 defaultConfig.setProperty(Config.STORAGE_USER, "sonews_user");
48 defaultConfig.setProperty(Config.STORAGE_PASSWORD, "mysecret");
49 defaultConfig.setProperty(Config.DEBUG, "false");
53 * Note: this method is not thread-safe
54 * @return A Config instance
56 public static synchronized FileConfig getInstance()
60 instance = new FileConfig();
65 // Every config instance is initialized with the default values.
66 private final Properties settings = (Properties)defaultConfig.clone();
69 * Config is a singelton class with only one instance at time.
70 * So the constructor is private to prevent the creation of more
71 * then one Config instance.
72 * @see Config.getInstance() to retrieve an instance of Config
78 // Load settings from file
88 * Loads the configuration from the config file. By default this is done
89 * by the (private) constructor but it can be useful to reload the config
90 * by invoking this method.
96 FileInputStream in = null;
100 in = new FileInputStream(
101 Config.inst().get(Config.LEVEL_CLI, Config.CONFIGFILE, "sonews.conf"));
104 catch (FileNotFoundException e)
106 // MUST NOT use Log otherwise endless loop
107 System.err.println(e.getMessage());
118 * Saves this Config to the config file. By default this is done
120 * @throws FileNotFoundException
121 * @throws IOException
123 public void save() throws FileNotFoundException, IOException
125 FileOutputStream out = null;
128 out = new FileOutputStream(
129 Config.inst().get(Config.LEVEL_CLI, Config.CONFIGFILE, "sonews.conf"));
130 settings.store(out, "SONEWS Config File");
133 catch(IOException ex)
145 * Returns the value that is stored within this config
146 * identified by the given key. If the key cannot be found
147 * the default value is returned.
148 * @param key Key to identify the value.
149 * @param def The default value that is returned if the key
150 * is not found in this Config.
154 public String get(String key, String def)
156 return settings.getProperty(key, def);
160 * Sets the value for a given key.
165 public void set(final String key, final String value)
167 settings.setProperty(key, value);