chris@3: /* chris@3: * SONEWS News Server chris@3: * see AUTHORS for the list of contributors chris@3: * chris@3: * This program is free software: you can redistribute it and/or modify chris@3: * it under the terms of the GNU General Public License as published by chris@3: * the Free Software Foundation, either version 3 of the License, or chris@3: * (at your option) any later version. chris@3: * chris@3: * This program is distributed in the hope that it will be useful, chris@3: * but WITHOUT ANY WARRANTY; without even the implied warranty of chris@3: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the chris@3: * GNU General Public License for more details. chris@3: * chris@3: * You should have received a copy of the GNU General Public License chris@3: * along with this program. If not, see . chris@3: */ chris@3: chris@3: package org.sonews.config; chris@3: chris@3: import java.io.FileInputStream; chris@3: import java.io.FileNotFoundException; chris@3: import java.io.FileOutputStream; chris@3: import java.io.IOException; chris@3: import java.util.Properties; chris@3: chris@3: /** chris@3: * Manages the bootstrap configuration. It MUST contain all config values chris@3: * that are needed to establish a database connection. chris@3: * For further configuration values use the Config class instead as that class chris@3: * stores its values within the database. chris@3: * @author Christian Lins chris@3: * @since sonews/0.5.0 chris@3: */ chris@3: class FileConfig extends AbstractConfig chris@3: { chris@3: chris@3: private static final Properties defaultConfig = new Properties(); chris@3: chris@3: private static FileConfig instance = null; chris@3: chris@3: static chris@3: { chris@3: // Set some default values chris@3: defaultConfig.setProperty(Config.STORAGE_DATABASE, "jdbc:mysql://localhost/sonews"); chris@3: defaultConfig.setProperty(Config.STORAGE_DBMSDRIVER, "com.mysql.jdbc.Driver"); chris@3: defaultConfig.setProperty(Config.STORAGE_USER, "sonews_user"); chris@3: defaultConfig.setProperty(Config.STORAGE_PASSWORD, "mysecret"); chris@3: defaultConfig.setProperty(Config.DEBUG, "false"); chris@3: } chris@3: chris@3: /** chris@3: * Note: this method is not thread-safe chris@3: * @return A Config instance chris@3: */ chris@3: public static synchronized FileConfig getInstance() chris@3: { chris@3: if(instance == null) chris@3: { chris@3: instance = new FileConfig(); chris@3: } chris@3: return instance; chris@3: } chris@3: chris@3: // Every config instance is initialized with the default values. chris@3: private final Properties settings = (Properties)defaultConfig.clone(); chris@3: chris@3: /** chris@3: * Config is a singelton class with only one instance at time. chris@3: * So the constructor is private to prevent the creation of more chris@3: * then one Config instance. chris@3: * @see Config.getInstance() to retrieve an instance of Config chris@3: */ chris@3: private FileConfig() chris@3: { chris@3: try chris@3: { chris@3: // Load settings from file chris@3: load(); chris@3: } chris@3: catch(IOException ex) chris@3: { chris@3: ex.printStackTrace(); chris@3: } chris@3: } chris@3: chris@3: /** chris@3: * Loads the configuration from the config file. By default this is done chris@3: * by the (private) constructor but it can be useful to reload the config chris@3: * by invoking this method. chris@3: * @throws IOException chris@3: */ chris@3: public void load() chris@3: throws IOException chris@3: { chris@3: FileInputStream in = null; chris@3: chris@3: try chris@3: { chris@3: in = new FileInputStream( chris@3: Config.inst().get(Config.LEVEL_CLI, Config.CONFIGFILE, "sonews.conf")); chris@3: settings.load(in); chris@3: } chris@3: catch (FileNotFoundException e) chris@3: { chris@3: // MUST NOT use Log otherwise endless loop chris@3: System.err.println(e.getMessage()); chris@3: save(); chris@3: } chris@3: finally chris@3: { chris@3: if(in != null) chris@3: in.close(); chris@3: } chris@3: } chris@3: chris@3: /** chris@3: * Saves this Config to the config file. By default this is done chris@3: * at program end. chris@3: * @throws FileNotFoundException chris@3: * @throws IOException chris@3: */ chris@3: public void save() throws FileNotFoundException, IOException chris@3: { chris@3: FileOutputStream out = null; chris@3: try chris@3: { chris@3: out = new FileOutputStream( chris@3: Config.inst().get(Config.LEVEL_CLI, Config.CONFIGFILE, "sonews.conf")); chris@3: settings.store(out, "SONEWS Config File"); chris@3: out.flush(); chris@3: } chris@3: catch(IOException ex) chris@3: { chris@3: throw ex; chris@3: } chris@3: finally chris@3: { chris@3: if(out != null) chris@3: out.close(); chris@3: } chris@3: } chris@3: chris@3: /** chris@3: * Returns the value that is stored within this config chris@3: * identified by the given key. If the key cannot be found chris@3: * the default value is returned. chris@3: * @param key Key to identify the value. chris@3: * @param def The default value that is returned if the key chris@3: * is not found in this Config. chris@3: * @return chris@3: */ chris@3: @Override chris@3: public String get(String key, String def) chris@3: { chris@3: return settings.getProperty(key, def); chris@3: } chris@3: chris@3: /** chris@3: * Sets the value for a given key. chris@3: * @param key chris@3: * @param value chris@3: */ chris@3: public void set(final String key, final String value) chris@3: { chris@3: settings.setProperty(key, value); chris@3: } chris@3: chris@3: }