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:
cli@15: import java.util.logging.Level;
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:
cli@37: private static BackendConfig instance = new BackendConfig();
cli@12:
cli@37: public static BackendConfig getInstance()
cli@37: {
cli@37: return instance;
cli@37: }
cli@37: private final TimeoutMap values = new TimeoutMap();
cli@37:
cli@37: private BackendConfig()
cli@37: {
cli@37: super();
cli@37: }
cli@37:
cli@37: /**
cli@37: * Returns the config value for the given key or the defaultValue if the
cli@37: * key is not found in config.
cli@37: * @param key
cli@37: * @param defaultValue
cli@37: * @return
cli@37: */
cli@37: @Override
cli@37: public String get(String key, String defaultValue)
cli@37: {
cli@37: try {
cli@37: String configValue = values.get(key);
cli@37: if (configValue == null) {
cli@37: if (StorageManager.current() == null) {
cli@37: Log.get().warning("BackendConfig not available, using default.");
cli@37: return defaultValue;
cli@37: }
cli@37:
cli@37: configValue = StorageManager.current().getConfigValue(key);
cli@37: if (configValue == null) {
cli@37: return defaultValue;
cli@37: } else {
cli@37: values.put(key, configValue);
cli@37: return configValue;
cli@37: }
cli@37: } else {
cli@37: return configValue;
cli@37: }
cli@37: } catch (StorageBackendException ex) {
cli@37: Log.get().log(Level.SEVERE, "Storage backend problem", ex);
cli@37: return defaultValue;
cli@37: }
cli@37: }
cli@37:
cli@37: /**
cli@37: * Sets the config value which is identified by the given key.
cli@37: * @param key
cli@37: * @param value
cli@37: */
cli@37: public void set(String key, String value)
cli@37: {
cli@37: values.put(key, value);
cli@37:
cli@37: try {
cli@37: // Write values to database
cli@37: StorageManager.current().setConfigValue(key, value);
cli@37: } catch (StorageBackendException ex) {
cli@37: ex.printStackTrace();
cli@37: }
cli@37: }
chris@3: }