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()
     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         if(StorageManager.current() == null)
    66         {
    67           Log.msg("Warning: BackendConfig not available, using default.", false);
    68           return defaultValue;
    69         }
    70 
    71         configValue = StorageManager.current().getConfigValue(key);
    72         if(configValue == null)
    73         {
    74           return defaultValue;
    75         }
    76         else
    77         {
    78           values.put(key, configValue);
    79           return configValue;
    80         }
    81       }
    82       else
    83       {
    84         return configValue;
    85       }
    86     }
    87     catch(StorageBackendException ex)
    88     {
    89       Log.msg(ex.getMessage(), false);
    90       return defaultValue;
    91     }
    92   }
    93   
    94   /**
    95    * Sets the config value which is identified by the given key.
    96    * @param key
    97    * @param value
    98    */
    99   public void set(String key, String value)
   100   {
   101     values.put(key, value);
   102     
   103     try
   104     {
   105       // Write values to database
   106       StorageManager.current().setConfigValue(key, value);
   107     }
   108     catch(StorageBackendException ex)
   109     {
   110       ex.printStackTrace();
   111     }
   112   }
   113   
   114 }