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