src/org/sonews/config/BackendConfig.java
author František Kučera <franta-hg@frantovo.cz>
Thu Oct 20 10:50:58 2011 +0200 (2011-10-20)
changeset 103 a788bf0e1080
parent 37 74139325d305
permissions -rwxr-xr-x
Drupal: zpráva od uživatele se před uložením prožene přes XSLT případně Tidy.
     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 package org.sonews.config;
    19 
    20 import java.util.logging.Level;
    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 	private static BackendConfig instance = new BackendConfig();
    35 
    36 	public static BackendConfig getInstance() {
    37 		return instance;
    38 	}
    39 	private final TimeoutMap<String, String> values = new TimeoutMap<String, String>();
    40 
    41 	private BackendConfig() {
    42 		super();
    43 	}
    44 
    45 	/**
    46 	 * Returns the config value for the given key or the defaultValue if the
    47 	 * key is not found in config.
    48 	 * @param key
    49 	 * @param defaultValue
    50 	 * @return
    51 	 */
    52 	@Override
    53 	public String get(String key, String defaultValue) {
    54 		try {
    55 			String configValue = values.get(key);
    56 			if (configValue == null) {
    57 				if (StorageManager.current() == null) {
    58 					Log.get().warning("BackendConfig not available, using default.");
    59 					return defaultValue;
    60 				}
    61 
    62 				configValue = StorageManager.current().getConfigValue(key);
    63 				if (configValue == null) {
    64 					return defaultValue;
    65 				} else {
    66 					values.put(key, configValue);
    67 					return configValue;
    68 				}
    69 			} else {
    70 				return configValue;
    71 			}
    72 		} catch (StorageBackendException ex) {
    73 			Log.get().log(Level.SEVERE, "Storage backend problem", ex);
    74 			return defaultValue;
    75 		}
    76 	}
    77 
    78 	/**
    79 	 * Sets the config value which is identified by the given key.
    80 	 * @param key
    81 	 * @param value
    82 	 */
    83 	public void set(String key, String value) {
    84 		values.put(key, value);
    85 
    86 		try {
    87 			// Write values to database
    88 			StorageManager.current().setConfigValue(key, value);
    89 		} catch (StorageBackendException ex) {
    90 			ex.printStackTrace();
    91 		}
    92 	}
    93 }