chris@3: /*
chris@3: * SONEWS News Server
chris@3: * see AUTHORS for the list of contributors
chris@3: *
chris@3: * This program is free software: you can redistribute it and/or modify
chris@3: * it under the terms of the GNU General Public License as published by
chris@3: * the Free Software Foundation, either version 3 of the License, or
chris@3: * (at your option) any later version.
chris@3: *
chris@3: * This program is distributed in the hope that it will be useful,
chris@3: * but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@3: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
chris@3: * GNU General Public License for more details.
chris@3: *
chris@3: * You should have received a copy of the GNU General Public License
chris@3: * along with this program. If not, see .
chris@3: */
chris@3:
chris@3: package org.sonews.config;
chris@3:
chris@3: import org.sonews.util.Log;
chris@3: import org.sonews.storage.StorageBackendException;
chris@3: import org.sonews.storage.StorageManager;
chris@3: import org.sonews.util.TimeoutMap;
chris@3:
chris@3: /**
chris@3: * Provides access to the program wide configuration that is stored within
chris@3: * the server's database.
chris@3: * @author Christian Lins
chris@3: * @since sonews/0.5.0
chris@3: */
chris@3: class BackendConfig extends AbstractConfig
chris@3: {
chris@3:
chris@3: private static BackendConfig instance = new BackendConfig();
chris@3:
chris@3: public static BackendConfig getInstance()
chris@3: {
chris@3: return instance;
chris@3: }
chris@3:
chris@3: private final TimeoutMap values
chris@3: = new TimeoutMap();
chris@3:
chris@3: private BackendConfig()
chris@3: {
chris@3: super();
chris@3: }
chris@3:
chris@3: /**
chris@3: * Returns the config value for the given key or the defaultValue if the
chris@3: * key is not found in config.
chris@3: * @param key
chris@3: * @param defaultValue
chris@3: * @return
chris@3: */
chris@3: @Override
chris@3: public String get(String key, String defaultValue)
chris@3: {
chris@3: try
chris@3: {
chris@3: String configValue = values.get(key);
chris@3: if(configValue == null)
chris@3: {
chris@3: configValue = StorageManager.current().getConfigValue(key);
chris@3: if(configValue == null)
chris@3: {
chris@3: return defaultValue;
chris@3: }
chris@3: else
chris@3: {
chris@3: values.put(key, configValue);
chris@3: return configValue;
chris@3: }
chris@3: }
chris@3: else
chris@3: {
chris@3: return configValue;
chris@3: }
chris@3: }
chris@3: catch(StorageBackendException ex)
chris@3: {
chris@3: Log.msg(ex.getMessage(), false);
chris@3: return defaultValue;
chris@3: }
chris@3: }
chris@3:
chris@3: /**
chris@3: * Sets the config value which is identified by the given key.
chris@3: * @param key
chris@3: * @param value
chris@3: */
chris@3: public void set(String key, String value)
chris@3: {
chris@3: values.put(key, value);
chris@3:
chris@3: try
chris@3: {
chris@3: // Write values to database
chris@3: StorageManager.current().setConfigValue(key, value);
chris@3: }
chris@3: catch(StorageBackendException ex)
chris@3: {
chris@3: ex.printStackTrace();
chris@3: }
chris@3: }
chris@3:
chris@3: }