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();
39 private static FileConfig instance = null;
42 // Set some default values
43 defaultConfig.setProperty(Config.STORAGE_DATABASE, "jdbc:mysql://localhost/sonews");
44 defaultConfig.setProperty(Config.STORAGE_DBMSDRIVER, "com.mysql.jdbc.Driver");
45 defaultConfig.setProperty(Config.STORAGE_USER, "sonews_user");
46 defaultConfig.setProperty(Config.STORAGE_PASSWORD, "mysecret");
47 defaultConfig.setProperty(Config.DEBUG, "false");
51 * Note: this method is not thread-safe
52 * @return A Config instance
54 public static synchronized FileConfig getInstance()
56 if (instance == null) {
57 instance = new FileConfig();
61 // Every config instance is initialized with the default values.
62 private final Properties settings = (Properties) defaultConfig.clone();
65 * Config is a singelton class with only one instance at time.
66 * So the constructor is private to prevent the creation of more
67 * then one Config instance.
68 * @see Config.getInstance() to retrieve an instance of Config
73 // Load settings from file
75 } catch (IOException ex) {
81 * Loads the configuration from the config file. By default this is done
82 * by the (private) constructor but it can be useful to reload the config
83 * by invoking this method.
89 FileInputStream in = null;
92 in = new FileInputStream(
93 Config.inst().get(Config.LEVEL_CLI, Config.CONFIGFILE, "sonews.conf"));
95 } catch (FileNotFoundException e) {
96 // MUST NOT use Log otherwise endless loop
97 System.err.println(e.getMessage());
107 * Saves this Config to the config file. By default this is done
109 * @throws FileNotFoundException
110 * @throws IOException
112 public void save() throws FileNotFoundException, IOException
114 FileOutputStream out = null;
116 out = new FileOutputStream(
117 Config.inst().get(Config.LEVEL_CLI, Config.CONFIGFILE, "sonews.conf"));
118 settings.store(out, "SONEWS Config File");
120 } catch (IOException ex) {
130 * Returns the value that is stored within this config
131 * identified by the given key. If the key cannot be found
132 * the default value is returned.
133 * @param key Key to identify the value.
134 * @param def The default value that is returned if the key
135 * is not found in this Config.
139 public String get(String key, String def)
141 return settings.getProperty(key, def);
145 * Sets the value for a given key.
150 public void set(final String key, final String value)
152 settings.setProperty(key, value);