org/sonews/config/BackendConfig.java
author cli
Fri, 25 Dec 2009 15:42:46 +0100
changeset 25 dd05c3f2fa24
parent 15 f2293e8566f5
permissions -rw-r--r--
Fix for too early disconnects on slow client connections. (#563)
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
cli@15
    21
import java.util.logging.Level;
chris@3
    22
import org.sonews.util.Log;
chris@3
    23
import org.sonews.storage.StorageBackendException;
chris@3
    24
import org.sonews.storage.StorageManager;
chris@3
    25
import org.sonews.util.TimeoutMap;
chris@3
    26
chris@3
    27
/**
chris@3
    28
 * Provides access to the program wide configuration that is stored within
chris@3
    29
 * the server's database.
chris@3
    30
 * @author Christian Lins
chris@3
    31
 * @since sonews/0.5.0
chris@3
    32
 */
chris@3
    33
class BackendConfig extends AbstractConfig
chris@3
    34
{
chris@3
    35
chris@3
    36
  private static BackendConfig instance = new BackendConfig();
chris@3
    37
  
chris@3
    38
  public static BackendConfig getInstance()
chris@3
    39
  {
chris@3
    40
    return instance;
chris@3
    41
  }
chris@3
    42
  
chris@3
    43
  private final TimeoutMap<String, String> values 
chris@3
    44
    = new TimeoutMap<String, String>();
chris@3
    45
  
chris@3
    46
  private BackendConfig()
chris@3
    47
  {
chris@3
    48
    super();
chris@3
    49
  }
chris@3
    50
  
chris@3
    51
  /**
chris@3
    52
   * Returns the config value for the given key or the defaultValue if the
chris@3
    53
   * key is not found in config.
chris@3
    54
   * @param key
chris@3
    55
   * @param defaultValue
chris@3
    56
   * @return
chris@3
    57
   */
chris@3
    58
  @Override
chris@3
    59
  public String get(String key, String defaultValue)
chris@3
    60
  {
chris@3
    61
    try
chris@3
    62
    {
chris@3
    63
      String configValue = values.get(key);
chris@3
    64
      if(configValue == null)
chris@3
    65
      {
cli@12
    66
        if(StorageManager.current() == null)
cli@12
    67
        {
cli@15
    68
          Log.get().warning("BackendConfig not available, using default.");
cli@12
    69
          return defaultValue;
cli@12
    70
        }
cli@12
    71
chris@3
    72
        configValue = StorageManager.current().getConfigValue(key);
chris@3
    73
        if(configValue == null)
chris@3
    74
        {
chris@3
    75
          return defaultValue;
chris@3
    76
        }
chris@3
    77
        else
chris@3
    78
        {
chris@3
    79
          values.put(key, configValue);
chris@3
    80
          return configValue;
chris@3
    81
        }
chris@3
    82
      }
chris@3
    83
      else
chris@3
    84
      {
chris@3
    85
        return configValue;
chris@3
    86
      }
chris@3
    87
    }
chris@3
    88
    catch(StorageBackendException ex)
chris@3
    89
    {
cli@15
    90
      Log.get().log(Level.SEVERE, "Storage backend problem", ex);
chris@3
    91
      return defaultValue;
chris@3
    92
    }
chris@3
    93
  }
chris@3
    94
  
chris@3
    95
  /**
chris@3
    96
   * Sets the config value which is identified by the given key.
chris@3
    97
   * @param key
chris@3
    98
   * @param value
chris@3
    99
   */
chris@3
   100
  public void set(String key, String value)
chris@3
   101
  {
chris@3
   102
    values.put(key, value);
chris@3
   103
    
chris@3
   104
    try
chris@3
   105
    {
chris@3
   106
      // Write values to database
chris@3
   107
      StorageManager.current().setConfigValue(key, value);
chris@3
   108
    }
chris@3
   109
    catch(StorageBackendException ex)
chris@3
   110
    {
chris@3
   111
      ex.printStackTrace();
chris@3
   112
    }
chris@3
   113
  }
chris@3
   114
  
chris@3
   115
}