1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/org/sonews/config/BackendConfig.java Sun Aug 29 17:43:58 2010 +0200
1.3 @@ -0,0 +1,115 @@
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 java.util.logging.Level;
1.25 +import org.sonews.util.Log;
1.26 +import org.sonews.storage.StorageBackendException;
1.27 +import org.sonews.storage.StorageManager;
1.28 +import org.sonews.util.TimeoutMap;
1.29 +
1.30 +/**
1.31 + * Provides access to the program wide configuration that is stored within
1.32 + * the server's database.
1.33 + * @author Christian Lins
1.34 + * @since sonews/0.5.0
1.35 + */
1.36 +class BackendConfig extends AbstractConfig
1.37 +{
1.38 +
1.39 + private static BackendConfig instance = new BackendConfig();
1.40 +
1.41 + public static BackendConfig getInstance()
1.42 + {
1.43 + return instance;
1.44 + }
1.45 +
1.46 + private final TimeoutMap<String, String> values
1.47 + = new TimeoutMap<String, String>();
1.48 +
1.49 + private BackendConfig()
1.50 + {
1.51 + super();
1.52 + }
1.53 +
1.54 + /**
1.55 + * Returns the config value for the given key or the defaultValue if the
1.56 + * key is not found in config.
1.57 + * @param key
1.58 + * @param defaultValue
1.59 + * @return
1.60 + */
1.61 + @Override
1.62 + public String get(String key, String defaultValue)
1.63 + {
1.64 + try
1.65 + {
1.66 + String configValue = values.get(key);
1.67 + if(configValue == null)
1.68 + {
1.69 + if(StorageManager.current() == null)
1.70 + {
1.71 + Log.get().warning("BackendConfig not available, using default.");
1.72 + return defaultValue;
1.73 + }
1.74 +
1.75 + configValue = StorageManager.current().getConfigValue(key);
1.76 + if(configValue == null)
1.77 + {
1.78 + return defaultValue;
1.79 + }
1.80 + else
1.81 + {
1.82 + values.put(key, configValue);
1.83 + return configValue;
1.84 + }
1.85 + }
1.86 + else
1.87 + {
1.88 + return configValue;
1.89 + }
1.90 + }
1.91 + catch(StorageBackendException ex)
1.92 + {
1.93 + Log.get().log(Level.SEVERE, "Storage backend problem", ex);
1.94 + return defaultValue;
1.95 + }
1.96 + }
1.97 +
1.98 + /**
1.99 + * Sets the config value which is identified by the given key.
1.100 + * @param key
1.101 + * @param value
1.102 + */
1.103 + public void set(String key, String value)
1.104 + {
1.105 + values.put(key, value);
1.106 +
1.107 + try
1.108 + {
1.109 + // Write values to database
1.110 + StorageManager.current().setConfigValue(key, value);
1.111 + }
1.112 + catch(StorageBackendException ex)
1.113 + {
1.114 + ex.printStackTrace();
1.115 + }
1.116 + }
1.117 +
1.118 +}