org/sonews/daemon/BootstrapConfig.java
author chris <chris@marvin>
Fri Jun 26 16:48:50 2009 +0200 (2009-06-26)
changeset 1 6fceb66e1ad7
permissions -rw-r--r--
Hooray... sonews/0.5.0 final

HG: Enter commit message. Lines beginning with 'HG:' are removed.
HG: Remove all lines to abort the collapse operation.
chris@1
     1
/*
chris@1
     2
 *   SONEWS News Server
chris@1
     3
 *   see AUTHORS for the list of contributors
chris@1
     4
 *
chris@1
     5
 *   This program is free software: you can redistribute it and/or modify
chris@1
     6
 *   it under the terms of the GNU General Public License as published by
chris@1
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@1
     8
 *   (at your option) any later version.
chris@1
     9
 *
chris@1
    10
 *   This program is distributed in the hope that it will be useful,
chris@1
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@1
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@1
    13
 *   GNU General Public License for more details.
chris@1
    14
 *
chris@1
    15
 *   You should have received a copy of the GNU General Public License
chris@1
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@1
    17
 */
chris@1
    18
chris@1
    19
package org.sonews.daemon;
chris@1
    20
chris@1
    21
import java.io.FileInputStream;
chris@1
    22
import java.io.FileNotFoundException;
chris@1
    23
import java.io.FileOutputStream;
chris@1
    24
import java.io.IOException;
chris@1
    25
import java.util.Properties;
chris@1
    26
import org.sonews.util.AbstractConfig;
chris@1
    27
chris@1
    28
/**
chris@1
    29
 * Manages the bootstrap configuration. It MUST contain all config values
chris@1
    30
 * that are needed to establish a database connection.
chris@1
    31
 * For further configuration values use the Config class instead as that class
chris@1
    32
 * stores its values within the database.
chris@1
    33
 * @author Christian Lins
chris@1
    34
 * @since sonews/0.5.0
chris@1
    35
 */
chris@1
    36
public final class BootstrapConfig extends AbstractConfig
chris@1
    37
{
chris@1
    38
  
chris@1
    39
  /** Key constant. If value is "true" every I/O is written to logfile 
chris@1
    40
   * (which is a lot!) 
chris@1
    41
   */
chris@1
    42
  public static final String DEBUG              = "sonews.debug";
chris@1
    43
  
chris@1
    44
  /** Key constant. Value is classname of the JDBC driver */
chris@1
    45
  public static final String STORAGE_DBMSDRIVER = "sonews.storage.dbmsdriver";
chris@1
    46
  
chris@1
    47
  /** Key constant. Value is JDBC connect String to the database. */
chris@1
    48
  public static final String STORAGE_DATABASE   = "sonews.storage.database";
chris@1
    49
  
chris@1
    50
  /** Key constant. Value is the username for the DBMS. */
chris@1
    51
  public static final String STORAGE_USER       = "sonews.storage.user";
chris@1
    52
  
chris@1
    53
  /** Key constant. Value is the password for the DBMS. */
chris@1
    54
  public static final String STORAGE_PASSWORD   = "sonews.storage.password";
chris@1
    55
  
chris@1
    56
  /** Key constant. Value is the name of the host which is allowed to use the
chris@1
    57
   *  XDAEMON command; default: "localhost" */
chris@1
    58
  public static final String XDAEMON_HOST       = "sonews.xdaemon.host";
chris@1
    59
chris@1
    60
  /** The config key for the filename of the logfile */
chris@1
    61
  public static final String LOGFILE = "sonews.log";
chris@1
    62
  
chris@1
    63
  /** The filename of the config file that is loaded on startup */
chris@1
    64
  public static volatile String FILE               = "sonews.conf";
chris@1
    65
chris@1
    66
  private static final Properties defaultConfig = new Properties();
chris@1
    67
  
chris@1
    68
  private static BootstrapConfig instance = null;
chris@1
    69
  
chris@1
    70
  static
chris@1
    71
  {
chris@1
    72
    // Set some default values
chris@1
    73
    defaultConfig.setProperty(STORAGE_DATABASE, "jdbc:mysql://localhost/sonews");
chris@1
    74
    defaultConfig.setProperty(STORAGE_DBMSDRIVER, "com.mysql.jdbc.Driver");
chris@1
    75
    defaultConfig.setProperty(STORAGE_USER, "sonews_user");
chris@1
    76
    defaultConfig.setProperty(STORAGE_PASSWORD, "mysecret");
chris@1
    77
    defaultConfig.setProperty(DEBUG, "false");
chris@1
    78
  }
chris@1
    79
  
chris@1
    80
  /**
chris@1
    81
   * Note: this method is not thread-safe
chris@1
    82
   * @return A Config instance
chris@1
    83
   */
chris@1
    84
  public static synchronized BootstrapConfig getInstance()
chris@1
    85
  {
chris@1
    86
    if(instance == null)
chris@1
    87
    {
chris@1
    88
      instance = new BootstrapConfig();
chris@1
    89
    }
chris@1
    90
    return instance;
chris@1
    91
  }
chris@1
    92
chris@1
    93
  // Every config instance is initialized with the default values.
chris@1
    94
  private final Properties settings = (Properties)defaultConfig.clone();
chris@1
    95
chris@1
    96
  /**
chris@1
    97
   * Config is a singelton class with only one instance at time.
chris@1
    98
   * So the constructor is private to prevent the creation of more
chris@1
    99
   * then one Config instance.
chris@1
   100
   * @see Config.getInstance() to retrieve an instance of Config
chris@1
   101
   */
chris@1
   102
  private BootstrapConfig()
chris@1
   103
  {
chris@1
   104
    try
chris@1
   105
    {
chris@1
   106
      // Load settings from file
chris@1
   107
      load();
chris@1
   108
    }
chris@1
   109
    catch(IOException ex)
chris@1
   110
    {
chris@1
   111
      ex.printStackTrace();
chris@1
   112
    }
chris@1
   113
  }
chris@1
   114
chris@1
   115
  /**
chris@1
   116
   * Loads the configuration from the config file. By default this is done
chris@1
   117
   * by the (private) constructor but it can be useful to reload the config
chris@1
   118
   * by invoking this method.
chris@1
   119
   * @throws IOException
chris@1
   120
   */
chris@1
   121
  public void load() 
chris@1
   122
    throws IOException
chris@1
   123
  {
chris@1
   124
    FileInputStream in = null;
chris@1
   125
    
chris@1
   126
    try
chris@1
   127
    {
chris@1
   128
      in = new FileInputStream(FILE);
chris@1
   129
      settings.load(in);
chris@1
   130
    }
chris@1
   131
    catch (FileNotFoundException e)
chris@1
   132
    {
chris@1
   133
      // MUST NOT use Log otherwise endless loop
chris@1
   134
      System.err.println(e.getMessage());
chris@1
   135
      save();
chris@1
   136
    }
chris@1
   137
    finally
chris@1
   138
    {
chris@1
   139
      if(in != null)
chris@1
   140
        in.close();
chris@1
   141
    }
chris@1
   142
  }
chris@1
   143
chris@1
   144
  /**
chris@1
   145
   * Saves this Config to the config file. By default this is done
chris@1
   146
   * at program end.
chris@1
   147
   * @throws FileNotFoundException
chris@1
   148
   * @throws IOException
chris@1
   149
   */
chris@1
   150
  public void save() throws FileNotFoundException, IOException
chris@1
   151
  {
chris@1
   152
    FileOutputStream out = null;
chris@1
   153
    try
chris@1
   154
    {
chris@1
   155
      out = new FileOutputStream(FILE);
chris@1
   156
      settings.store(out, "SONEWS Config File");
chris@1
   157
      out.flush();
chris@1
   158
    }
chris@1
   159
    catch(IOException ex)
chris@1
   160
    {
chris@1
   161
      throw ex;
chris@1
   162
    }
chris@1
   163
    finally
chris@1
   164
    {
chris@1
   165
      if(out != null)
chris@1
   166
        out.close();
chris@1
   167
    }
chris@1
   168
  }
chris@1
   169
  
chris@1
   170
  /**
chris@1
   171
   * Returns the value that is stored within this config
chris@1
   172
   * identified by the given key. If the key cannot be found
chris@1
   173
   * the default value is returned.
chris@1
   174
   * @param key Key to identify the value.
chris@1
   175
   * @param def The default value that is returned if the key
chris@1
   176
   * is not found in this Config.
chris@1
   177
   * @return
chris@1
   178
   */
chris@1
   179
  public String get(String key, String def)
chris@1
   180
  {
chris@1
   181
    return settings.getProperty(key, def);
chris@1
   182
  }
chris@1
   183
chris@1
   184
  /**
chris@1
   185
   * Sets the value for a given key.
chris@1
   186
   * @param key
chris@1
   187
   * @param value
chris@1
   188
   */
chris@1
   189
  public void set(final String key, final String value)
chris@1
   190
  {
chris@1
   191
    settings.setProperty(key, value);
chris@1
   192
  }
chris@1
   193
chris@1
   194
}