org/sonews/config/BackendConfig.java
author chris <chris@marvin>
Wed, 22 Jul 2009 14:04:05 +0200
changeset 3 2fdc9cc89502
child 12 bb6990c0dd1a
permissions -rw-r--r--
sonews/1.0.0
     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.config;
    20 
    21 import org.sonews.util.Log;
    22 import org.sonews.storage.StorageBackendException;
    23 import org.sonews.storage.StorageManager;
    24 import org.sonews.util.TimeoutMap;
    25 
    26 /**
    27  * Provides access to the program wide configuration that is stored within
    28  * the server's database.
    29  * @author Christian Lins
    30  * @since sonews/0.5.0
    31  */
    32 class BackendConfig extends AbstractConfig
    33 {
    34 
    35   private static BackendConfig instance = new BackendConfig();
    36   
    37   public static BackendConfig getInstance()
    38   {
    39     return instance;
    40   }
    41   
    42   private final TimeoutMap<String, String> values 
    43     = new TimeoutMap<String, String>();
    44   
    45   private BackendConfig()
    46   {
    47     super();
    48   }
    49   
    50   /**
    51    * Returns the config value for the given key or the defaultValue if the
    52    * key is not found in config.
    53    * @param key
    54    * @param defaultValue
    55    * @return
    56    */
    57   @Override
    58   public String get(String key, String defaultValue)
    59   {
    60     try
    61     {
    62       String configValue = values.get(key);
    63       if(configValue == null)
    64       {
    65         configValue = StorageManager.current().getConfigValue(key);
    66         if(configValue == null)
    67         {
    68           return defaultValue;
    69         }
    70         else
    71         {
    72           values.put(key, configValue);
    73           return configValue;
    74         }
    75       }
    76       else
    77       {
    78         return configValue;
    79       }
    80     }
    81     catch(StorageBackendException ex)
    82     {
    83       Log.msg(ex.getMessage(), false);
    84       return defaultValue;
    85     }
    86   }
    87   
    88   /**
    89    * Sets the config value which is identified by the given key.
    90    * @param key
    91    * @param value
    92    */
    93   public void set(String key, String value)
    94   {
    95     values.put(key, value);
    96     
    97     try
    98     {
    99       // Write values to database
   100       StorageManager.current().setConfigValue(key, value);
   101     }
   102     catch(StorageBackendException ex)
   103     {
   104       ex.printStackTrace();
   105     }
   106   }
   107   
   108 }