src/org/sonews/config/Config.java
author cli
Sat Sep 10 18:18:05 2011 +0200 (2011-09-10)
changeset 45 7e24949b87b0
parent 37 74139325d305
child 49 8df94bfd3e2f
permissions -rwxr-xr-x
HSQLDB backend support completed, but untested.
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 
    19 package org.sonews.config;
    20 
    21 /**
    22  * Configuration facade class.
    23  * @author Christian Lins
    24  * @since sonews/1.0
    25  */
    26 public class Config extends AbstractConfig
    27 {
    28 
    29 	public static final int LEVEL_CLI = 1;
    30 	public static final int LEVEL_FILE = 2;
    31 	public static final int LEVEL_BACKEND = 3;
    32 	public static final String CONFIGFILE = "sonews.configfile";
    33 	/** BackendConfig key constant. Value is the maximum article size in kilobytes. */
    34 	public static final String ARTICLE_MAXSIZE = "sonews.article.maxsize";
    35 	/** BackendConfig key constant. Value: Amount of news that are feeded per run. */
    36 	public static final String EVENTLOG = "sonews.eventlog";
    37 	public static final String FEED_NEWSPERRUN = "sonews.feed.newsperrun";
    38 	public static final String FEED_PULLINTERVAL = "sonews.feed.pullinterval";
    39 	public static final String HOSTNAME = "sonews.hostname";
    40 	public static final String PORT = "sonews.port";
    41 	public static final String TIMEOUT = "sonews.timeout";
    42 	public static final String LOGLEVEL = "sonews.loglevel";
    43 	public static final String MLPOLL_DELETEUNKNOWN = "sonews.mlpoll.deleteunknown";
    44 	public static final String MLPOLL_HOST = "sonews.mlpoll.host";
    45 	public static final String MLPOLL_PASSWORD = "sonews.mlpoll.password";
    46 	public static final String MLPOLL_USER = "sonews.mlpoll.user";
    47 	public static final String MLSEND_ADDRESS = "sonews.mlsend.address";
    48 	public static final String MLSEND_RW_FROM = "sonews.mlsend.rewrite.from";
    49 	public static final String MLSEND_RW_SENDER = "sonews.mlsend.rewrite.sender";
    50 	public static final String MLSEND_HOST = "sonews.mlsend.host";
    51 	public static final String MLSEND_PASSWORD = "sonews.mlsend.password";
    52 	public static final String MLSEND_PORT = "sonews.mlsend.port";
    53 	public static final String MLSEND_USER = "sonews.mlsend.user";
    54 	/** Key constant. If value is "true" every I/O is written to logfile
    55 	 * (which is a lot!) */
    56 	public static final String DEBUG = "sonews.debug";
    57 	/** Key constant. Value is classname of the JDBC driver */
    58 	public static final String STORAGE_DBMSDRIVER = "sonews.storage.dbmsdriver";
    59 	/** Key constant. Value is JDBC connect String to the database. */
    60 	public static final String STORAGE_DATABASE = "sonews.storage.database";
    61 	/** Key constant. Value is the username for the DBMS. */
    62 	public static final String STORAGE_USER = "sonews.storage.user";
    63 	/** Key constant. Value is the password for the DBMS. */
    64 	public static final String STORAGE_PASSWORD = "sonews.storage.password";
    65 	public static final String STORAGE_PROVIDER = "sonews.storage.provider";
    66 	/** Key constant. Value is the name of the host which is allowed to use the
    67 	 *  XDAEMON command; default: "localhost" */
    68 	public static final String XDAEMON_HOST = "sonews.xdaemon.host";
    69 	/** The config key for the filename of the logfile */
    70 	public static final String LOGFILE = "sonews.log";
    71 	public static final String[] AVAILABLE_KEYS = {
    72 		ARTICLE_MAXSIZE,
    73 		EVENTLOG,
    74 		FEED_NEWSPERRUN,
    75 		FEED_PULLINTERVAL,
    76 		HOSTNAME,
    77 		MLPOLL_DELETEUNKNOWN,
    78 		MLPOLL_HOST,
    79 		MLPOLL_PASSWORD,
    80 		MLPOLL_USER,
    81 		MLSEND_ADDRESS,
    82 		MLSEND_HOST,
    83 		MLSEND_PASSWORD,
    84 		MLSEND_PORT,
    85 		MLSEND_RW_FROM,
    86 		MLSEND_RW_SENDER,
    87 		MLSEND_USER,
    88 		PORT,
    89 		TIMEOUT,
    90 		XDAEMON_HOST
    91 	};
    92 	private static Config instance = new Config();
    93 
    94 	public static Config inst()
    95 	{
    96 		return instance;
    97 	}
    98 
    99 	private Config()
   100 	{
   101 	}
   102 
   103 	@Override
   104 	public String get(String key, String def)
   105 	{
   106 		String val = CommandLineConfig.getInstance().get(key, null);
   107 
   108 		if (val == null) {
   109 			val = FileConfig.getInstance().get(key, null);
   110 		}
   111 
   112 		if (val == null) {
   113 			val = BackendConfig.getInstance().get(key, def);
   114 		}
   115 
   116 		return val;
   117 	}
   118 
   119 	public String get(int maxLevel, String key, String def)
   120 	{
   121 		String val = CommandLineConfig.getInstance().get(key, null);
   122 
   123 		if (val == null && maxLevel >= LEVEL_FILE) {
   124 			val = FileConfig.getInstance().get(key, null);
   125 			if (val == null && maxLevel >= LEVEL_BACKEND) {
   126 				val = BackendConfig.getInstance().get(key, def);
   127 			}
   128 		}
   129 
   130 		return val != null ? val : def;
   131 	}
   132 
   133 	@Override
   134 	public void set(String key, String val)
   135 	{
   136 		set(LEVEL_BACKEND, key, val);
   137 	}
   138 
   139 	public void set(int level, String key, String val)
   140 	{
   141 		switch (level) {
   142 			case LEVEL_CLI: {
   143 				CommandLineConfig.getInstance().set(key, val);
   144 				break;
   145 			}
   146 			case LEVEL_FILE: {
   147 				FileConfig.getInstance().set(key, val);
   148 				break;
   149 			}
   150 			case LEVEL_BACKEND: {
   151 				BackendConfig.getInstance().set(key, val);
   152 				break;
   153 			}
   154 		}
   155 	}
   156 }