org/sonews/daemon/Config.java
author chris <chris@marvin>
Wed Jul 01 10:48:22 2009 +0200 (2009-07-01)
changeset 2 1090e2141798
permissions -rw-r--r--
sonews/0.5.1 fixes merged
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 org.sonews.util.Log;
chris@1
    22
import java.sql.SQLException;
chris@1
    23
import org.sonews.daemon.storage.Database;
chris@1
    24
import org.sonews.util.AbstractConfig;
chris@1
    25
import org.sonews.util.TimeoutMap;
chris@1
    26
chris@1
    27
/**
chris@1
    28
 * Provides access to the program wide configuration that is stored within
chris@1
    29
 * the server's database.
chris@1
    30
 * @author Christian Lins
chris@1
    31
 * @since sonews/0.5.0
chris@1
    32
 */
chris@1
    33
public final class Config extends AbstractConfig
chris@1
    34
{
chris@1
    35
chris@1
    36
  /** Config key constant. Value is the maximum article size in kilobytes. */
chris@1
    37
  public static final String ARTICLE_MAXSIZE   = "sonews.article.maxsize";
chris@1
    38
  
chris@1
    39
  /** Config key constant. Value: Amount of news that are feeded per run. */
chris@1
    40
  public static final String FEED_NEWSPERRUN   = "sonews.feed.newsperrun";
chris@1
    41
  public static final String FEED_PULLINTERVAL = "sonews.feed.pullinterval";
chris@1
    42
  public static final String HOSTNAME          = "sonews.hostname";
chris@1
    43
  public static final String PORT              = "sonews.port";
chris@1
    44
  public static final String TIMEOUT           = "sonews.timeout";
chris@1
    45
  public static final String MLPOLL_DELETEUNKNOWN = "sonews.mlpoll.deleteunknown";
chris@1
    46
  public static final String MLPOLL_HOST       = "sonews.mlpoll.host";
chris@1
    47
  public static final String MLPOLL_PASSWORD   = "sonews.mlpoll.password";
chris@1
    48
  public static final String MLPOLL_USER       = "sonews.mlpoll.user";
chris@1
    49
  public static final String MLSEND_ADDRESS    = "sonews.mlsend.address";
chris@1
    50
  public static final String MLSEND_RW_FROM    = "sonews.mlsend.rewrite.from";
chris@1
    51
  public static final String MLSEND_RW_SENDER  = "sonews.mlsend.rewrite.sender";
chris@1
    52
  public static final String MLSEND_HOST       = "sonews.mlsend.host";
chris@1
    53
  public static final String MLSEND_PASSWORD   = "sonews.mlsend.password";
chris@1
    54
  public static final String MLSEND_PORT       = "sonews.mlsend.port";
chris@1
    55
  public static final String MLSEND_USER       = "sonews.mlsend.user";
chris@1
    56
  
chris@1
    57
  public static final String[] AVAILABLE_KEYS = {
chris@1
    58
    Config.ARTICLE_MAXSIZE,
chris@1
    59
    Config.FEED_NEWSPERRUN,
chris@1
    60
    Config.FEED_PULLINTERVAL,
chris@1
    61
    Config.HOSTNAME,
chris@1
    62
    Config.MLPOLL_DELETEUNKNOWN,
chris@1
    63
    Config.MLPOLL_HOST,
chris@1
    64
    Config.MLPOLL_PASSWORD,
chris@1
    65
    Config.MLPOLL_USER,
chris@1
    66
    Config.MLSEND_ADDRESS,
chris@1
    67
    Config.MLSEND_HOST,
chris@1
    68
    Config.MLSEND_PASSWORD,
chris@1
    69
    Config.MLSEND_PORT,
chris@1
    70
    Config.MLSEND_RW_FROM,
chris@1
    71
    Config.MLSEND_RW_SENDER,
chris@1
    72
    Config.MLSEND_USER,
chris@1
    73
    Config.PORT,
chris@1
    74
    Config.TIMEOUT
chris@1
    75
  };
chris@1
    76
chris@1
    77
  private static Config instance = new Config();
chris@1
    78
  
chris@1
    79
  public static Config getInstance()
chris@1
    80
  {
chris@1
    81
    return instance;
chris@1
    82
  }
chris@1
    83
  
chris@1
    84
  private final TimeoutMap<String, String> values 
chris@1
    85
    = new TimeoutMap<String, String>();
chris@1
    86
  
chris@1
    87
  private Config()
chris@1
    88
  {
chris@1
    89
    super();
chris@1
    90
  }
chris@1
    91
  
chris@1
    92
  /**
chris@1
    93
   * Returns the config value for the given key or the defaultValue if the
chris@1
    94
   * key is not found in config.
chris@1
    95
   * @param key
chris@1
    96
   * @param defaultValue
chris@1
    97
   * @return
chris@1
    98
   */
chris@1
    99
  public String get(String key, String defaultValue)
chris@1
   100
  {
chris@1
   101
    try
chris@1
   102
    {
chris@1
   103
      String configValue = values.get(key);
chris@1
   104
      if(configValue == null)
chris@1
   105
      {
chris@1
   106
        configValue = Database.getInstance().getConfigValue(key);
chris@1
   107
        if(configValue == null)
chris@1
   108
        {
chris@1
   109
          return defaultValue;
chris@1
   110
        }
chris@1
   111
        else
chris@1
   112
        {
chris@1
   113
          values.put(key, configValue);
chris@1
   114
          return configValue;
chris@1
   115
        }
chris@1
   116
      }
chris@1
   117
      else
chris@1
   118
      {
chris@1
   119
        return configValue;
chris@1
   120
      }
chris@1
   121
    }
chris@1
   122
    catch(SQLException ex)
chris@1
   123
    {
chris@1
   124
      Log.msg(ex.getMessage(), false);
chris@1
   125
      return defaultValue;
chris@1
   126
    }
chris@1
   127
  }
chris@1
   128
  
chris@1
   129
  /**
chris@1
   130
   * Sets the config value which is identified by the given key.
chris@1
   131
   * @param key
chris@1
   132
   * @param value
chris@1
   133
   */
chris@1
   134
  public void set(String key, String value)
chris@1
   135
  {
chris@1
   136
    values.put(key, value);
chris@1
   137
    
chris@1
   138
    try
chris@1
   139
    {
chris@1
   140
      // Write values to database
chris@1
   141
      Database.getInstance().setConfigValue(key, value);
chris@1
   142
    }
chris@1
   143
    catch(SQLException ex)
chris@1
   144
    {
chris@1
   145
      ex.printStackTrace();
chris@1
   146
    }
chris@1
   147
  }
chris@1
   148
  
chris@1
   149
}