org/sonews/daemon/BootstrapConfig.java
changeset 1 6fceb66e1ad7
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/sonews/daemon/BootstrapConfig.java	Fri Jun 26 16:48:50 2009 +0200
     1.3 @@ -0,0 +1,194 @@
     1.4 +/*
     1.5 + *   SONEWS News Server
     1.6 + *   see AUTHORS for the list of contributors
     1.7 + *
     1.8 + *   This program is free software: you can redistribute it and/or modify
     1.9 + *   it under the terms of the GNU General Public License as published by
    1.10 + *   the Free Software Foundation, either version 3 of the License, or
    1.11 + *   (at your option) any later version.
    1.12 + *
    1.13 + *   This program is distributed in the hope that it will be useful,
    1.14 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + *   GNU General Public License for more details.
    1.17 + *
    1.18 + *   You should have received a copy of the GNU General Public License
    1.19 + *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +
    1.22 +package org.sonews.daemon;
    1.23 +
    1.24 +import java.io.FileInputStream;
    1.25 +import java.io.FileNotFoundException;
    1.26 +import java.io.FileOutputStream;
    1.27 +import java.io.IOException;
    1.28 +import java.util.Properties;
    1.29 +import org.sonews.util.AbstractConfig;
    1.30 +
    1.31 +/**
    1.32 + * Manages the bootstrap configuration. It MUST contain all config values
    1.33 + * that are needed to establish a database connection.
    1.34 + * For further configuration values use the Config class instead as that class
    1.35 + * stores its values within the database.
    1.36 + * @author Christian Lins
    1.37 + * @since sonews/0.5.0
    1.38 + */
    1.39 +public final class BootstrapConfig extends AbstractConfig
    1.40 +{
    1.41 +  
    1.42 +  /** Key constant. If value is "true" every I/O is written to logfile 
    1.43 +   * (which is a lot!) 
    1.44 +   */
    1.45 +  public static final String DEBUG              = "sonews.debug";
    1.46 +  
    1.47 +  /** Key constant. Value is classname of the JDBC driver */
    1.48 +  public static final String STORAGE_DBMSDRIVER = "sonews.storage.dbmsdriver";
    1.49 +  
    1.50 +  /** Key constant. Value is JDBC connect String to the database. */
    1.51 +  public static final String STORAGE_DATABASE   = "sonews.storage.database";
    1.52 +  
    1.53 +  /** Key constant. Value is the username for the DBMS. */
    1.54 +  public static final String STORAGE_USER       = "sonews.storage.user";
    1.55 +  
    1.56 +  /** Key constant. Value is the password for the DBMS. */
    1.57 +  public static final String STORAGE_PASSWORD   = "sonews.storage.password";
    1.58 +  
    1.59 +  /** Key constant. Value is the name of the host which is allowed to use the
    1.60 +   *  XDAEMON command; default: "localhost" */
    1.61 +  public static final String XDAEMON_HOST       = "sonews.xdaemon.host";
    1.62 +
    1.63 +  /** The config key for the filename of the logfile */
    1.64 +  public static final String LOGFILE = "sonews.log";
    1.65 +  
    1.66 +  /** The filename of the config file that is loaded on startup */
    1.67 +  public static volatile String FILE               = "sonews.conf";
    1.68 +
    1.69 +  private static final Properties defaultConfig = new Properties();
    1.70 +  
    1.71 +  private static BootstrapConfig instance = null;
    1.72 +  
    1.73 +  static
    1.74 +  {
    1.75 +    // Set some default values
    1.76 +    defaultConfig.setProperty(STORAGE_DATABASE, "jdbc:mysql://localhost/sonews");
    1.77 +    defaultConfig.setProperty(STORAGE_DBMSDRIVER, "com.mysql.jdbc.Driver");
    1.78 +    defaultConfig.setProperty(STORAGE_USER, "sonews_user");
    1.79 +    defaultConfig.setProperty(STORAGE_PASSWORD, "mysecret");
    1.80 +    defaultConfig.setProperty(DEBUG, "false");
    1.81 +  }
    1.82 +  
    1.83 +  /**
    1.84 +   * Note: this method is not thread-safe
    1.85 +   * @return A Config instance
    1.86 +   */
    1.87 +  public static synchronized BootstrapConfig getInstance()
    1.88 +  {
    1.89 +    if(instance == null)
    1.90 +    {
    1.91 +      instance = new BootstrapConfig();
    1.92 +    }
    1.93 +    return instance;
    1.94 +  }
    1.95 +
    1.96 +  // Every config instance is initialized with the default values.
    1.97 +  private final Properties settings = (Properties)defaultConfig.clone();
    1.98 +
    1.99 +  /**
   1.100 +   * Config is a singelton class with only one instance at time.
   1.101 +   * So the constructor is private to prevent the creation of more
   1.102 +   * then one Config instance.
   1.103 +   * @see Config.getInstance() to retrieve an instance of Config
   1.104 +   */
   1.105 +  private BootstrapConfig()
   1.106 +  {
   1.107 +    try
   1.108 +    {
   1.109 +      // Load settings from file
   1.110 +      load();
   1.111 +    }
   1.112 +    catch(IOException ex)
   1.113 +    {
   1.114 +      ex.printStackTrace();
   1.115 +    }
   1.116 +  }
   1.117 +
   1.118 +  /**
   1.119 +   * Loads the configuration from the config file. By default this is done
   1.120 +   * by the (private) constructor but it can be useful to reload the config
   1.121 +   * by invoking this method.
   1.122 +   * @throws IOException
   1.123 +   */
   1.124 +  public void load() 
   1.125 +    throws IOException
   1.126 +  {
   1.127 +    FileInputStream in = null;
   1.128 +    
   1.129 +    try
   1.130 +    {
   1.131 +      in = new FileInputStream(FILE);
   1.132 +      settings.load(in);
   1.133 +    }
   1.134 +    catch (FileNotFoundException e)
   1.135 +    {
   1.136 +      // MUST NOT use Log otherwise endless loop
   1.137 +      System.err.println(e.getMessage());
   1.138 +      save();
   1.139 +    }
   1.140 +    finally
   1.141 +    {
   1.142 +      if(in != null)
   1.143 +        in.close();
   1.144 +    }
   1.145 +  }
   1.146 +
   1.147 +  /**
   1.148 +   * Saves this Config to the config file. By default this is done
   1.149 +   * at program end.
   1.150 +   * @throws FileNotFoundException
   1.151 +   * @throws IOException
   1.152 +   */
   1.153 +  public void save() throws FileNotFoundException, IOException
   1.154 +  {
   1.155 +    FileOutputStream out = null;
   1.156 +    try
   1.157 +    {
   1.158 +      out = new FileOutputStream(FILE);
   1.159 +      settings.store(out, "SONEWS Config File");
   1.160 +      out.flush();
   1.161 +    }
   1.162 +    catch(IOException ex)
   1.163 +    {
   1.164 +      throw ex;
   1.165 +    }
   1.166 +    finally
   1.167 +    {
   1.168 +      if(out != null)
   1.169 +        out.close();
   1.170 +    }
   1.171 +  }
   1.172 +  
   1.173 +  /**
   1.174 +   * Returns the value that is stored within this config
   1.175 +   * identified by the given key. If the key cannot be found
   1.176 +   * the default value is returned.
   1.177 +   * @param key Key to identify the value.
   1.178 +   * @param def The default value that is returned if the key
   1.179 +   * is not found in this Config.
   1.180 +   * @return
   1.181 +   */
   1.182 +  public String get(String key, String def)
   1.183 +  {
   1.184 +    return settings.getProperty(key, def);
   1.185 +  }
   1.186 +
   1.187 +  /**
   1.188 +   * Sets the value for a given key.
   1.189 +   * @param key
   1.190 +   * @param value
   1.191 +   */
   1.192 +  public void set(final String key, final String value)
   1.193 +  {
   1.194 +    settings.setProperty(key, value);
   1.195 +  }
   1.196 +
   1.197 +}