src/org/sonews/config/BackendConfig.java
author cli
Wed, 04 May 2011 18:06:24 +0200
changeset 40 f8894df2d079
parent 37 74139325d305
child 49 8df94bfd3e2f
permissions -rwxr-xr-x
Refactoring.
     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 java.util.logging.Level;
    22 import org.sonews.util.Log;
    23 import org.sonews.storage.StorageBackendException;
    24 import org.sonews.storage.StorageManager;
    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 class BackendConfig extends AbstractConfig
    34 {
    35 
    36 	private static BackendConfig instance = new BackendConfig();
    37 
    38 	public static BackendConfig getInstance()
    39 	{
    40 		return instance;
    41 	}
    42 	private final TimeoutMap<String, String> values = new TimeoutMap<String, String>();
    43 
    44 	private BackendConfig()
    45 	{
    46 		super();
    47 	}
    48 
    49 	/**
    50 	 * Returns the config value for the given key or the defaultValue if the
    51 	 * key is not found in config.
    52 	 * @param key
    53 	 * @param defaultValue
    54 	 * @return
    55 	 */
    56 	@Override
    57 	public String get(String key, String defaultValue)
    58 	{
    59 		try {
    60 			String configValue = values.get(key);
    61 			if (configValue == null) {
    62 				if (StorageManager.current() == null) {
    63 					Log.get().warning("BackendConfig not available, using default.");
    64 					return defaultValue;
    65 				}
    66 
    67 				configValue = StorageManager.current().getConfigValue(key);
    68 				if (configValue == null) {
    69 					return defaultValue;
    70 				} else {
    71 					values.put(key, configValue);
    72 					return configValue;
    73 				}
    74 			} else {
    75 				return configValue;
    76 			}
    77 		} catch (StorageBackendException ex) {
    78 			Log.get().log(Level.SEVERE, "Storage backend problem", ex);
    79 			return defaultValue;
    80 		}
    81 	}
    82 
    83 	/**
    84 	 * Sets the config value which is identified by the given key.
    85 	 * @param key
    86 	 * @param value
    87 	 */
    88 	public void set(String key, String value)
    89 	{
    90 		values.put(key, value);
    91 
    92 		try {
    93 			// Write values to database
    94 			StorageManager.current().setConfigValue(key, value);
    95 		} catch (StorageBackendException ex) {
    96 			ex.printStackTrace();
    97 		}
    98 	}
    99 }