src/org/sonews/config/BackendConfig.java
author cli
Mon, 12 Sep 2011 20:20:00 +0200
changeset 52 d97b223eab4e
parent 49 8df94bfd3e2f
permissions -rwxr-xr-x
Change binary package name sonews-server to server
chris@3
     1
/*
chris@3
     2
 *   SONEWS News Server
chris@3
     3
 *   see AUTHORS for the list of contributors
chris@3
     4
 *
chris@3
     5
 *   This program is free software: you can redistribute it and/or modify
chris@3
     6
 *   it under the terms of the GNU General Public License as published by
chris@3
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@3
     8
 *   (at your option) any later version.
chris@3
     9
 *
chris@3
    10
 *   This program is distributed in the hope that it will be useful,
chris@3
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@3
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@3
    13
 *   GNU General Public License for more details.
chris@3
    14
 *
chris@3
    15
 *   You should have received a copy of the GNU General Public License
chris@3
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@3
    17
 */
chris@3
    18
package org.sonews.config;
chris@3
    19
cli@15
    20
import java.util.logging.Level;
chris@3
    21
import org.sonews.util.Log;
chris@3
    22
import org.sonews.storage.StorageBackendException;
chris@3
    23
import org.sonews.storage.StorageManager;
chris@3
    24
import org.sonews.util.TimeoutMap;
chris@3
    25
chris@3
    26
/**
chris@3
    27
 * Provides access to the program wide configuration that is stored within
chris@3
    28
 * the server's database.
chris@3
    29
 * @author Christian Lins
chris@3
    30
 * @since sonews/0.5.0
chris@3
    31
 */
cli@49
    32
class BackendConfig extends AbstractConfig {
chris@3
    33
cli@37
    34
	private static BackendConfig instance = new BackendConfig();
cli@12
    35
cli@49
    36
	public static BackendConfig getInstance() {
cli@37
    37
		return instance;
cli@37
    38
	}
cli@37
    39
	private final TimeoutMap<String, String> values = new TimeoutMap<String, String>();
cli@37
    40
cli@49
    41
	private BackendConfig() {
cli@37
    42
		super();
cli@37
    43
	}
cli@37
    44
cli@37
    45
	/**
cli@37
    46
	 * Returns the config value for the given key or the defaultValue if the
cli@37
    47
	 * key is not found in config.
cli@37
    48
	 * @param key
cli@37
    49
	 * @param defaultValue
cli@37
    50
	 * @return
cli@37
    51
	 */
cli@37
    52
	@Override
cli@49
    53
	public String get(String key, String defaultValue) {
cli@37
    54
		try {
cli@37
    55
			String configValue = values.get(key);
cli@37
    56
			if (configValue == null) {
cli@37
    57
				if (StorageManager.current() == null) {
cli@37
    58
					Log.get().warning("BackendConfig not available, using default.");
cli@37
    59
					return defaultValue;
cli@37
    60
				}
cli@37
    61
cli@37
    62
				configValue = StorageManager.current().getConfigValue(key);
cli@37
    63
				if (configValue == null) {
cli@37
    64
					return defaultValue;
cli@37
    65
				} else {
cli@37
    66
					values.put(key, configValue);
cli@37
    67
					return configValue;
cli@37
    68
				}
cli@37
    69
			} else {
cli@37
    70
				return configValue;
cli@37
    71
			}
cli@37
    72
		} catch (StorageBackendException ex) {
cli@37
    73
			Log.get().log(Level.SEVERE, "Storage backend problem", ex);
cli@37
    74
			return defaultValue;
cli@37
    75
		}
cli@37
    76
	}
cli@37
    77
cli@37
    78
	/**
cli@37
    79
	 * Sets the config value which is identified by the given key.
cli@37
    80
	 * @param key
cli@37
    81
	 * @param value
cli@37
    82
	 */
cli@49
    83
	public void set(String key, String value) {
cli@37
    84
		values.put(key, value);
cli@37
    85
cli@37
    86
		try {
cli@37
    87
			// Write values to database
cli@37
    88
			StorageManager.current().setConfigValue(key, value);
cli@37
    89
		} catch (StorageBackendException ex) {
cli@37
    90
			ex.printStackTrace();
cli@37
    91
		}
cli@37
    92
	}
chris@3
    93
}