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
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 
    19 package org.sonews.daemon;
    20 
    21 import org.sonews.util.Log;
    22 import java.sql.SQLException;
    23 import org.sonews.daemon.storage.Database;
    24 import org.sonews.util.AbstractConfig;
    25 import org.sonews.util.TimeoutMap;
    26 
    27 /**
    28  * Provides access to the program wide configuration that is stored within
    29  * the server's database.
    30  * @author Christian Lins
    31  * @since sonews/0.5.0
    32  */
    33 public final class Config extends AbstractConfig
    34 {
    35 
    36   /** Config key constant. Value is the maximum article size in kilobytes. */
    37   public static final String ARTICLE_MAXSIZE   = "sonews.article.maxsize";
    38   
    39   /** Config key constant. Value: Amount of news that are feeded per run. */
    40   public static final String FEED_NEWSPERRUN   = "sonews.feed.newsperrun";
    41   public static final String FEED_PULLINTERVAL = "sonews.feed.pullinterval";
    42   public static final String HOSTNAME          = "sonews.hostname";
    43   public static final String PORT              = "sonews.port";
    44   public static final String TIMEOUT           = "sonews.timeout";
    45   public static final String MLPOLL_DELETEUNKNOWN = "sonews.mlpoll.deleteunknown";
    46   public static final String MLPOLL_HOST       = "sonews.mlpoll.host";
    47   public static final String MLPOLL_PASSWORD   = "sonews.mlpoll.password";
    48   public static final String MLPOLL_USER       = "sonews.mlpoll.user";
    49   public static final String MLSEND_ADDRESS    = "sonews.mlsend.address";
    50   public static final String MLSEND_RW_FROM    = "sonews.mlsend.rewrite.from";
    51   public static final String MLSEND_RW_SENDER  = "sonews.mlsend.rewrite.sender";
    52   public static final String MLSEND_HOST       = "sonews.mlsend.host";
    53   public static final String MLSEND_PASSWORD   = "sonews.mlsend.password";
    54   public static final String MLSEND_PORT       = "sonews.mlsend.port";
    55   public static final String MLSEND_USER       = "sonews.mlsend.user";
    56   
    57   public static final String[] AVAILABLE_KEYS = {
    58     Config.ARTICLE_MAXSIZE,
    59     Config.FEED_NEWSPERRUN,
    60     Config.FEED_PULLINTERVAL,
    61     Config.HOSTNAME,
    62     Config.MLPOLL_DELETEUNKNOWN,
    63     Config.MLPOLL_HOST,
    64     Config.MLPOLL_PASSWORD,
    65     Config.MLPOLL_USER,
    66     Config.MLSEND_ADDRESS,
    67     Config.MLSEND_HOST,
    68     Config.MLSEND_PASSWORD,
    69     Config.MLSEND_PORT,
    70     Config.MLSEND_RW_FROM,
    71     Config.MLSEND_RW_SENDER,
    72     Config.MLSEND_USER,
    73     Config.PORT,
    74     Config.TIMEOUT
    75   };
    76 
    77   private static Config instance = new Config();
    78   
    79   public static Config getInstance()
    80   {
    81     return instance;
    82   }
    83   
    84   private final TimeoutMap<String, String> values 
    85     = new TimeoutMap<String, String>();
    86   
    87   private Config()
    88   {
    89     super();
    90   }
    91   
    92   /**
    93    * Returns the config value for the given key or the defaultValue if the
    94    * key is not found in config.
    95    * @param key
    96    * @param defaultValue
    97    * @return
    98    */
    99   public String get(String key, String defaultValue)
   100   {
   101     try
   102     {
   103       String configValue = values.get(key);
   104       if(configValue == null)
   105       {
   106         configValue = Database.getInstance().getConfigValue(key);
   107         if(configValue == null)
   108         {
   109           return defaultValue;
   110         }
   111         else
   112         {
   113           values.put(key, configValue);
   114           return configValue;
   115         }
   116       }
   117       else
   118       {
   119         return configValue;
   120       }
   121     }
   122     catch(SQLException ex)
   123     {
   124       Log.msg(ex.getMessage(), false);
   125       return defaultValue;
   126     }
   127   }
   128   
   129   /**
   130    * Sets the config value which is identified by the given key.
   131    * @param key
   132    * @param value
   133    */
   134   public void set(String key, String value)
   135   {
   136     values.put(key, value);
   137     
   138     try
   139     {
   140       // Write values to database
   141       Database.getInstance().setConfigValue(key, value);
   142     }
   143     catch(SQLException ex)
   144     {
   145       ex.printStackTrace();
   146     }
   147   }
   148   
   149 }