org/sonews/config/BackendConfig.java
author chris <chris@marvin>
Thu Aug 06 18:34:10 2009 +0200 (2009-08-06)
changeset 5 ad210aa137a4
child 12 bb6990c0dd1a
permissions -rw-r--r--
Removing tags as they point to invalid changesets after history refactoring.
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 org.sonews.util.Log;
chris@3
    22
import org.sonews.storage.StorageBackendException;
chris@3
    23
import org.sonews.storage.StorageManager;
chris@3
    24
import org.sonews.util.TimeoutMap;
chris@3
    25
chris@3
    26
/**
chris@3
    27
 * Provides access to the program wide configuration that is stored within
chris@3
    28
 * the server's database.
chris@3
    29
 * @author Christian Lins
chris@3
    30
 * @since sonews/0.5.0
chris@3
    31
 */
chris@3
    32
class BackendConfig extends AbstractConfig
chris@3
    33
{
chris@3
    34
chris@3
    35
  private static BackendConfig instance = new BackendConfig();
chris@3
    36
  
chris@3
    37
  public static BackendConfig getInstance()
chris@3
    38
  {
chris@3
    39
    return instance;
chris@3
    40
  }
chris@3
    41
  
chris@3
    42
  private final TimeoutMap<String, String> values 
chris@3
    43
    = new TimeoutMap<String, String>();
chris@3
    44
  
chris@3
    45
  private BackendConfig()
chris@3
    46
  {
chris@3
    47
    super();
chris@3
    48
  }
chris@3
    49
  
chris@3
    50
  /**
chris@3
    51
   * Returns the config value for the given key or the defaultValue if the
chris@3
    52
   * key is not found in config.
chris@3
    53
   * @param key
chris@3
    54
   * @param defaultValue
chris@3
    55
   * @return
chris@3
    56
   */
chris@3
    57
  @Override
chris@3
    58
  public String get(String key, String defaultValue)
chris@3
    59
  {
chris@3
    60
    try
chris@3
    61
    {
chris@3
    62
      String configValue = values.get(key);
chris@3
    63
      if(configValue == null)
chris@3
    64
      {
chris@3
    65
        configValue = StorageManager.current().getConfigValue(key);
chris@3
    66
        if(configValue == null)
chris@3
    67
        {
chris@3
    68
          return defaultValue;
chris@3
    69
        }
chris@3
    70
        else
chris@3
    71
        {
chris@3
    72
          values.put(key, configValue);
chris@3
    73
          return configValue;
chris@3
    74
        }
chris@3
    75
      }
chris@3
    76
      else
chris@3
    77
      {
chris@3
    78
        return configValue;
chris@3
    79
      }
chris@3
    80
    }
chris@3
    81
    catch(StorageBackendException ex)
chris@3
    82
    {
chris@3
    83
      Log.msg(ex.getMessage(), false);
chris@3
    84
      return defaultValue;
chris@3
    85
    }
chris@3
    86
  }
chris@3
    87
  
chris@3
    88
  /**
chris@3
    89
   * Sets the config value which is identified by the given key.
chris@3
    90
   * @param key
chris@3
    91
   * @param value
chris@3
    92
   */
chris@3
    93
  public void set(String key, String value)
chris@3
    94
  {
chris@3
    95
    values.put(key, value);
chris@3
    96
    
chris@3
    97
    try
chris@3
    98
    {
chris@3
    99
      // Write values to database
chris@3
   100
      StorageManager.current().setConfigValue(key, value);
chris@3
   101
    }
chris@3
   102
    catch(StorageBackendException ex)
chris@3
   103
    {
chris@3
   104
      ex.printStackTrace();
chris@3
   105
    }
chris@3
   106
  }
chris@3
   107
  
chris@3
   108
}