org/sonews/config/BackendConfig.java
author cli
Thu, 20 Aug 2009 16:57:38 +0200
changeset 14 efce4ec25564
parent 12 bb6990c0dd1a
child 15 f2293e8566f5
permissions -rw-r--r--
Fix #548: API change; changed parameter type of Storage.getGroupsForList()
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
      {
cli@12
    65
        if(StorageManager.current() == null)
cli@12
    66
        {
cli@12
    67
          Log.msg("Warning: BackendConfig not available, using default.", false);
cli@12
    68
          return defaultValue;
cli@12
    69
        }
cli@12
    70
chris@3
    71
        configValue = StorageManager.current().getConfigValue(key);
chris@3
    72
        if(configValue == null)
chris@3
    73
        {
chris@3
    74
          return defaultValue;
chris@3
    75
        }
chris@3
    76
        else
chris@3
    77
        {
chris@3
    78
          values.put(key, configValue);
chris@3
    79
          return configValue;
chris@3
    80
        }
chris@3
    81
      }
chris@3
    82
      else
chris@3
    83
      {
chris@3
    84
        return configValue;
chris@3
    85
      }
chris@3
    86
    }
chris@3
    87
    catch(StorageBackendException ex)
chris@3
    88
    {
chris@3
    89
      Log.msg(ex.getMessage(), false);
chris@3
    90
      return defaultValue;
chris@3
    91
    }
chris@3
    92
  }
chris@3
    93
  
chris@3
    94
  /**
chris@3
    95
   * Sets the config value which is identified by the given key.
chris@3
    96
   * @param key
chris@3
    97
   * @param value
chris@3
    98
   */
chris@3
    99
  public void set(String key, String value)
chris@3
   100
  {
chris@3
   101
    values.put(key, value);
chris@3
   102
    
chris@3
   103
    try
chris@3
   104
    {
chris@3
   105
      // Write values to database
chris@3
   106
      StorageManager.current().setConfigValue(key, value);
chris@3
   107
    }
chris@3
   108
    catch(StorageBackendException ex)
chris@3
   109
    {
chris@3
   110
      ex.printStackTrace();
chris@3
   111
    }
chris@3
   112
  }
chris@3
   113
  
chris@3
   114
}