1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/org/sonews/config/BackendConfig.java Wed Jul 22 14:04:05 2009 +0200
1.3 @@ -0,0 +1,108 @@
1.4 +/*
1.5 + * SONEWS News Server
1.6 + * see AUTHORS for the list of contributors
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, either version 3 of the License, or
1.11 + * (at your option) any later version.
1.12 + *
1.13 + * This program is distributed in the hope that it will be useful,
1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 + * GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License
1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 + */
1.21 +
1.22 +package org.sonews.config;
1.23 +
1.24 +import org.sonews.util.Log;
1.25 +import org.sonews.storage.StorageBackendException;
1.26 +import org.sonews.storage.StorageManager;
1.27 +import org.sonews.util.TimeoutMap;
1.28 +
1.29 +/**
1.30 + * Provides access to the program wide configuration that is stored within
1.31 + * the server's database.
1.32 + * @author Christian Lins
1.33 + * @since sonews/0.5.0
1.34 + */
1.35 +class BackendConfig extends AbstractConfig
1.36 +{
1.37 +
1.38 + private static BackendConfig instance = new BackendConfig();
1.39 +
1.40 + public static BackendConfig getInstance()
1.41 + {
1.42 + return instance;
1.43 + }
1.44 +
1.45 + private final TimeoutMap<String, String> values
1.46 + = new TimeoutMap<String, String>();
1.47 +
1.48 + private BackendConfig()
1.49 + {
1.50 + super();
1.51 + }
1.52 +
1.53 + /**
1.54 + * Returns the config value for the given key or the defaultValue if the
1.55 + * key is not found in config.
1.56 + * @param key
1.57 + * @param defaultValue
1.58 + * @return
1.59 + */
1.60 + @Override
1.61 + public String get(String key, String defaultValue)
1.62 + {
1.63 + try
1.64 + {
1.65 + String configValue = values.get(key);
1.66 + if(configValue == null)
1.67 + {
1.68 + configValue = StorageManager.current().getConfigValue(key);
1.69 + if(configValue == null)
1.70 + {
1.71 + return defaultValue;
1.72 + }
1.73 + else
1.74 + {
1.75 + values.put(key, configValue);
1.76 + return configValue;
1.77 + }
1.78 + }
1.79 + else
1.80 + {
1.81 + return configValue;
1.82 + }
1.83 + }
1.84 + catch(StorageBackendException ex)
1.85 + {
1.86 + Log.msg(ex.getMessage(), false);
1.87 + return defaultValue;
1.88 + }
1.89 + }
1.90 +
1.91 + /**
1.92 + * Sets the config value which is identified by the given key.
1.93 + * @param key
1.94 + * @param value
1.95 + */
1.96 + public void set(String key, String value)
1.97 + {
1.98 + values.put(key, value);
1.99 +
1.100 + try
1.101 + {
1.102 + // Write values to database
1.103 + StorageManager.current().setConfigValue(key, value);
1.104 + }
1.105 + catch(StorageBackendException ex)
1.106 + {
1.107 + ex.printStackTrace();
1.108 + }
1.109 + }
1.110 +
1.111 +}