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