org/sonews/daemon/Config.java
changeset 2 1090e2141798
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/sonews/daemon/Config.java	Wed Jul 01 10:48:22 2009 +0200
     1.3 @@ -0,0 +1,149 @@
     1.4 +/*
     1.5 + *   SONEWS News Server
     1.6 + *   see AUTHORS for the list of contributors
     1.7 + *
     1.8 + *   This program is free software: you can redistribute it and/or modify
     1.9 + *   it under the terms of the GNU General Public License as published by
    1.10 + *   the Free Software Foundation, either version 3 of the License, or
    1.11 + *   (at your option) any later version.
    1.12 + *
    1.13 + *   This program is distributed in the hope that it will be useful,
    1.14 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + *   GNU General Public License for more details.
    1.17 + *
    1.18 + *   You should have received a copy of the GNU General Public License
    1.19 + *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +
    1.22 +package org.sonews.daemon;
    1.23 +
    1.24 +import org.sonews.util.Log;
    1.25 +import java.sql.SQLException;
    1.26 +import org.sonews.daemon.storage.Database;
    1.27 +import org.sonews.util.AbstractConfig;
    1.28 +import org.sonews.util.TimeoutMap;
    1.29 +
    1.30 +/**
    1.31 + * Provides access to the program wide configuration that is stored within
    1.32 + * the server's database.
    1.33 + * @author Christian Lins
    1.34 + * @since sonews/0.5.0
    1.35 + */
    1.36 +public final class Config extends AbstractConfig
    1.37 +{
    1.38 +
    1.39 +  /** Config key constant. Value is the maximum article size in kilobytes. */
    1.40 +  public static final String ARTICLE_MAXSIZE   = "sonews.article.maxsize";
    1.41 +  
    1.42 +  /** Config key constant. Value: Amount of news that are feeded per run. */
    1.43 +  public static final String FEED_NEWSPERRUN   = "sonews.feed.newsperrun";
    1.44 +  public static final String FEED_PULLINTERVAL = "sonews.feed.pullinterval";
    1.45 +  public static final String HOSTNAME          = "sonews.hostname";
    1.46 +  public static final String PORT              = "sonews.port";
    1.47 +  public static final String TIMEOUT           = "sonews.timeout";
    1.48 +  public static final String MLPOLL_DELETEUNKNOWN = "sonews.mlpoll.deleteunknown";
    1.49 +  public static final String MLPOLL_HOST       = "sonews.mlpoll.host";
    1.50 +  public static final String MLPOLL_PASSWORD   = "sonews.mlpoll.password";
    1.51 +  public static final String MLPOLL_USER       = "sonews.mlpoll.user";
    1.52 +  public static final String MLSEND_ADDRESS    = "sonews.mlsend.address";
    1.53 +  public static final String MLSEND_RW_FROM    = "sonews.mlsend.rewrite.from";
    1.54 +  public static final String MLSEND_RW_SENDER  = "sonews.mlsend.rewrite.sender";
    1.55 +  public static final String MLSEND_HOST       = "sonews.mlsend.host";
    1.56 +  public static final String MLSEND_PASSWORD   = "sonews.mlsend.password";
    1.57 +  public static final String MLSEND_PORT       = "sonews.mlsend.port";
    1.58 +  public static final String MLSEND_USER       = "sonews.mlsend.user";
    1.59 +  
    1.60 +  public static final String[] AVAILABLE_KEYS = {
    1.61 +    Config.ARTICLE_MAXSIZE,
    1.62 +    Config.FEED_NEWSPERRUN,
    1.63 +    Config.FEED_PULLINTERVAL,
    1.64 +    Config.HOSTNAME,
    1.65 +    Config.MLPOLL_DELETEUNKNOWN,
    1.66 +    Config.MLPOLL_HOST,
    1.67 +    Config.MLPOLL_PASSWORD,
    1.68 +    Config.MLPOLL_USER,
    1.69 +    Config.MLSEND_ADDRESS,
    1.70 +    Config.MLSEND_HOST,
    1.71 +    Config.MLSEND_PASSWORD,
    1.72 +    Config.MLSEND_PORT,
    1.73 +    Config.MLSEND_RW_FROM,
    1.74 +    Config.MLSEND_RW_SENDER,
    1.75 +    Config.MLSEND_USER,
    1.76 +    Config.PORT,
    1.77 +    Config.TIMEOUT
    1.78 +  };
    1.79 +
    1.80 +  private static Config instance = new Config();
    1.81 +  
    1.82 +  public static Config getInstance()
    1.83 +  {
    1.84 +    return instance;
    1.85 +  }
    1.86 +  
    1.87 +  private final TimeoutMap<String, String> values 
    1.88 +    = new TimeoutMap<String, String>();
    1.89 +  
    1.90 +  private Config()
    1.91 +  {
    1.92 +    super();
    1.93 +  }
    1.94 +  
    1.95 +  /**
    1.96 +   * Returns the config value for the given key or the defaultValue if the
    1.97 +   * key is not found in config.
    1.98 +   * @param key
    1.99 +   * @param defaultValue
   1.100 +   * @return
   1.101 +   */
   1.102 +  public String get(String key, String defaultValue)
   1.103 +  {
   1.104 +    try
   1.105 +    {
   1.106 +      String configValue = values.get(key);
   1.107 +      if(configValue == null)
   1.108 +      {
   1.109 +        configValue = Database.getInstance().getConfigValue(key);
   1.110 +        if(configValue == null)
   1.111 +        {
   1.112 +          return defaultValue;
   1.113 +        }
   1.114 +        else
   1.115 +        {
   1.116 +          values.put(key, configValue);
   1.117 +          return configValue;
   1.118 +        }
   1.119 +      }
   1.120 +      else
   1.121 +      {
   1.122 +        return configValue;
   1.123 +      }
   1.124 +    }
   1.125 +    catch(SQLException ex)
   1.126 +    {
   1.127 +      Log.msg(ex.getMessage(), false);
   1.128 +      return defaultValue;
   1.129 +    }
   1.130 +  }
   1.131 +  
   1.132 +  /**
   1.133 +   * Sets the config value which is identified by the given key.
   1.134 +   * @param key
   1.135 +   * @param value
   1.136 +   */
   1.137 +  public void set(String key, String value)
   1.138 +  {
   1.139 +    values.put(key, value);
   1.140 +    
   1.141 +    try
   1.142 +    {
   1.143 +      // Write values to database
   1.144 +      Database.getInstance().setConfigValue(key, value);
   1.145 +    }
   1.146 +    catch(SQLException ex)
   1.147 +    {
   1.148 +      ex.printStackTrace();
   1.149 +    }
   1.150 +  }
   1.151 +  
   1.152 +}