src/org/sonews/config/BackendConfig.java
author cli
Tue, 07 Jun 2011 11:55:22 +0200
changeset 44 5d7d1adf387f
parent 37 74139325d305
child 49 8df94bfd3e2f
permissions -rwxr-xr-x
Work on hsqldb support
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
chris@3
    19
package org.sonews.config;
chris@3
    20
cli@15
    21
import java.util.logging.Level;
chris@3
    22
import org.sonews.util.Log;
chris@3
    23
import org.sonews.storage.StorageBackendException;
chris@3
    24
import org.sonews.storage.StorageManager;
chris@3
    25
import org.sonews.util.TimeoutMap;
chris@3
    26
chris@3
    27
/**
chris@3
    28
 * Provides access to the program wide configuration that is stored within
chris@3
    29
 * the server's database.
chris@3
    30
 * @author Christian Lins
chris@3
    31
 * @since sonews/0.5.0
chris@3
    32
 */
chris@3
    33
class BackendConfig extends AbstractConfig
chris@3
    34
{
chris@3
    35
cli@37
    36
	private static BackendConfig instance = new BackendConfig();
cli@12
    37
cli@37
    38
	public static BackendConfig getInstance()
cli@37
    39
	{
cli@37
    40
		return instance;
cli@37
    41
	}
cli@37
    42
	private final TimeoutMap<String, String> values = new TimeoutMap<String, String>();
cli@37
    43
cli@37
    44
	private BackendConfig()
cli@37
    45
	{
cli@37
    46
		super();
cli@37
    47
	}
cli@37
    48
cli@37
    49
	/**
cli@37
    50
	 * Returns the config value for the given key or the defaultValue if the
cli@37
    51
	 * key is not found in config.
cli@37
    52
	 * @param key
cli@37
    53
	 * @param defaultValue
cli@37
    54
	 * @return
cli@37
    55
	 */
cli@37
    56
	@Override
cli@37
    57
	public String get(String key, String defaultValue)
cli@37
    58
	{
cli@37
    59
		try {
cli@37
    60
			String configValue = values.get(key);
cli@37
    61
			if (configValue == null) {
cli@37
    62
				if (StorageManager.current() == null) {
cli@37
    63
					Log.get().warning("BackendConfig not available, using default.");
cli@37
    64
					return defaultValue;
cli@37
    65
				}
cli@37
    66
cli@37
    67
				configValue = StorageManager.current().getConfigValue(key);
cli@37
    68
				if (configValue == null) {
cli@37
    69
					return defaultValue;
cli@37
    70
				} else {
cli@37
    71
					values.put(key, configValue);
cli@37
    72
					return configValue;
cli@37
    73
				}
cli@37
    74
			} else {
cli@37
    75
				return configValue;
cli@37
    76
			}
cli@37
    77
		} catch (StorageBackendException ex) {
cli@37
    78
			Log.get().log(Level.SEVERE, "Storage backend problem", ex);
cli@37
    79
			return defaultValue;
cli@37
    80
		}
cli@37
    81
	}
cli@37
    82
cli@37
    83
	/**
cli@37
    84
	 * Sets the config value which is identified by the given key.
cli@37
    85
	 * @param key
cli@37
    86
	 * @param value
cli@37
    87
	 */
cli@37
    88
	public void set(String key, String value)
cli@37
    89
	{
cli@37
    90
		values.put(key, value);
cli@37
    91
cli@37
    92
		try {
cli@37
    93
			// Write values to database
cli@37
    94
			StorageManager.current().setConfigValue(key, value);
cli@37
    95
		} catch (StorageBackendException ex) {
cli@37
    96
			ex.printStackTrace();
cli@37
    97
		}
cli@37
    98
	}
chris@3
    99
}