chris@1: /* chris@1: * SONEWS News Server chris@1: * see AUTHORS for the list of contributors chris@1: * chris@1: * This program is free software: you can redistribute it and/or modify chris@1: * it under the terms of the GNU General Public License as published by chris@1: * the Free Software Foundation, either version 3 of the License, or chris@1: * (at your option) any later version. chris@1: * chris@1: * This program is distributed in the hope that it will be useful, chris@1: * but WITHOUT ANY WARRANTY; without even the implied warranty of chris@1: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the chris@1: * GNU General Public License for more details. chris@1: * chris@1: * You should have received a copy of the GNU General Public License chris@1: * along with this program. If not, see . chris@1: */ chris@1: chris@1: package org.sonews.daemon; chris@1: chris@1: import org.sonews.util.Log; chris@1: import java.sql.SQLException; chris@1: import org.sonews.daemon.storage.Database; chris@1: import org.sonews.util.AbstractConfig; chris@1: import org.sonews.util.TimeoutMap; chris@1: chris@1: /** chris@1: * Provides access to the program wide configuration that is stored within chris@1: * the server's database. chris@1: * @author Christian Lins chris@1: * @since sonews/0.5.0 chris@1: */ chris@1: public final class Config extends AbstractConfig chris@1: { chris@1: chris@1: /** Config key constant. Value is the maximum article size in kilobytes. */ chris@1: public static final String ARTICLE_MAXSIZE = "sonews.article.maxsize"; chris@1: chris@1: /** Config key constant. Value: Amount of news that are feeded per run. */ chris@1: public static final String FEED_NEWSPERRUN = "sonews.feed.newsperrun"; chris@1: public static final String FEED_PULLINTERVAL = "sonews.feed.pullinterval"; chris@1: public static final String HOSTNAME = "sonews.hostname"; chris@1: public static final String PORT = "sonews.port"; chris@1: public static final String TIMEOUT = "sonews.timeout"; chris@1: public static final String MLPOLL_DELETEUNKNOWN = "sonews.mlpoll.deleteunknown"; chris@1: public static final String MLPOLL_HOST = "sonews.mlpoll.host"; chris@1: public static final String MLPOLL_PASSWORD = "sonews.mlpoll.password"; chris@1: public static final String MLPOLL_USER = "sonews.mlpoll.user"; chris@1: public static final String MLSEND_ADDRESS = "sonews.mlsend.address"; chris@1: public static final String MLSEND_RW_FROM = "sonews.mlsend.rewrite.from"; chris@1: public static final String MLSEND_RW_SENDER = "sonews.mlsend.rewrite.sender"; chris@1: public static final String MLSEND_HOST = "sonews.mlsend.host"; chris@1: public static final String MLSEND_PASSWORD = "sonews.mlsend.password"; chris@1: public static final String MLSEND_PORT = "sonews.mlsend.port"; chris@1: public static final String MLSEND_USER = "sonews.mlsend.user"; chris@1: chris@1: public static final String[] AVAILABLE_KEYS = { chris@1: Config.ARTICLE_MAXSIZE, chris@1: Config.FEED_NEWSPERRUN, chris@1: Config.FEED_PULLINTERVAL, chris@1: Config.HOSTNAME, chris@1: Config.MLPOLL_DELETEUNKNOWN, chris@1: Config.MLPOLL_HOST, chris@1: Config.MLPOLL_PASSWORD, chris@1: Config.MLPOLL_USER, chris@1: Config.MLSEND_ADDRESS, chris@1: Config.MLSEND_HOST, chris@1: Config.MLSEND_PASSWORD, chris@1: Config.MLSEND_PORT, chris@1: Config.MLSEND_RW_FROM, chris@1: Config.MLSEND_RW_SENDER, chris@1: Config.MLSEND_USER, chris@1: Config.PORT, chris@1: Config.TIMEOUT chris@1: }; chris@1: chris@1: private static Config instance = new Config(); chris@1: chris@1: public static Config getInstance() chris@1: { chris@1: return instance; chris@1: } chris@1: chris@1: private final TimeoutMap values chris@1: = new TimeoutMap(); chris@1: chris@1: private Config() chris@1: { chris@1: super(); chris@1: } chris@1: chris@1: /** chris@1: * Returns the config value for the given key or the defaultValue if the chris@1: * key is not found in config. chris@1: * @param key chris@1: * @param defaultValue chris@1: * @return chris@1: */ chris@1: public String get(String key, String defaultValue) chris@1: { chris@1: try chris@1: { chris@1: String configValue = values.get(key); chris@1: if(configValue == null) chris@1: { chris@1: configValue = Database.getInstance().getConfigValue(key); chris@1: if(configValue == null) chris@1: { chris@1: return defaultValue; chris@1: } chris@1: else chris@1: { chris@1: values.put(key, configValue); chris@1: return configValue; chris@1: } chris@1: } chris@1: else chris@1: { chris@1: return configValue; chris@1: } chris@1: } chris@1: catch(SQLException ex) chris@1: { chris@1: Log.msg(ex.getMessage(), false); chris@1: return defaultValue; chris@1: } chris@1: } chris@1: chris@1: /** chris@1: * Sets the config value which is identified by the given key. chris@1: * @param key chris@1: * @param value chris@1: */ chris@1: public void set(String key, String value) chris@1: { chris@1: values.put(key, value); chris@1: chris@1: try chris@1: { chris@1: // Write values to database chris@1: Database.getInstance().setConfigValue(key, value); chris@1: } chris@1: catch(SQLException ex) chris@1: { chris@1: ex.printStackTrace(); chris@1: } chris@1: } chris@1: chris@1: }