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.daemon;
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;
26 import org.sonews.util.AbstractConfig;
29 * Manages the bootstrap configuration. It MUST contain all config values
30 * that are needed to establish a database connection.
31 * For further configuration values use the Config class instead as that class
32 * stores its values within the database.
33 * @author Christian Lins
36 public final class BootstrapConfig extends AbstractConfig
39 /** Key constant. If value is "true" every I/O is written to logfile
42 public static final String DEBUG = "sonews.debug";
44 /** Key constant. Value is classname of the JDBC driver */
45 public static final String STORAGE_DBMSDRIVER = "sonews.storage.dbmsdriver";
47 /** Key constant. Value is JDBC connect String to the database. */
48 public static final String STORAGE_DATABASE = "sonews.storage.database";
50 /** Key constant. Value is the username for the DBMS. */
51 public static final String STORAGE_USER = "sonews.storage.user";
53 /** Key constant. Value is the password for the DBMS. */
54 public static final String STORAGE_PASSWORD = "sonews.storage.password";
56 /** Key constant. Value is the name of the host which is allowed to use the
57 * XDAEMON command; default: "localhost" */
58 public static final String XDAEMON_HOST = "sonews.xdaemon.host";
60 /** The config key for the filename of the logfile */
61 public static final String LOGFILE = "sonews.log";
63 /** The filename of the config file that is loaded on startup */
64 public static volatile String FILE = "sonews.conf";
66 private static final Properties defaultConfig = new Properties();
68 private static BootstrapConfig instance = null;
72 // Set some default values
73 defaultConfig.setProperty(STORAGE_DATABASE, "jdbc:mysql://localhost/sonews");
74 defaultConfig.setProperty(STORAGE_DBMSDRIVER, "com.mysql.jdbc.Driver");
75 defaultConfig.setProperty(STORAGE_USER, "sonews_user");
76 defaultConfig.setProperty(STORAGE_PASSWORD, "mysecret");
77 defaultConfig.setProperty(DEBUG, "false");
81 * Note: this method is not thread-safe
82 * @return A Config instance
84 public static synchronized BootstrapConfig getInstance()
88 instance = new BootstrapConfig();
93 // Every config instance is initialized with the default values.
94 private final Properties settings = (Properties)defaultConfig.clone();
97 * Config is a singelton class with only one instance at time.
98 * So the constructor is private to prevent the creation of more
99 * then one Config instance.
100 * @see Config.getInstance() to retrieve an instance of Config
102 private BootstrapConfig()
106 // Load settings from file
109 catch(IOException ex)
111 ex.printStackTrace();
116 * Loads the configuration from the config file. By default this is done
117 * by the (private) constructor but it can be useful to reload the config
118 * by invoking this method.
119 * @throws IOException
124 FileInputStream in = null;
128 in = new FileInputStream(FILE);
131 catch (FileNotFoundException e)
133 // MUST NOT use Log otherwise endless loop
134 System.err.println(e.getMessage());
145 * Saves this Config to the config file. By default this is done
147 * @throws FileNotFoundException
148 * @throws IOException
150 public void save() throws FileNotFoundException, IOException
152 FileOutputStream out = null;
155 out = new FileOutputStream(FILE);
156 settings.store(out, "SONEWS Config File");
159 catch(IOException ex)
171 * Returns the value that is stored within this config
172 * identified by the given key. If the key cannot be found
173 * the default value is returned.
174 * @param key Key to identify the value.
175 * @param def The default value that is returned if the key
176 * is not found in this Config.
179 public String get(String key, String def)
181 return settings.getProperty(key, def);
185 * Sets the value for a given key.
189 public void set(final String key, final String value)
191 settings.setProperty(key, value);