src/org/sonews/config/Config.java
author cli
Tue, 13 Sep 2011 19:08:15 +0200
changeset 56 c713ca00a4a7
parent 49 8df94bfd3e2f
child 58 b2df305a13ce
permissions -rwxr-xr-x
Remove binary files in lib/
     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 package org.sonews.config;
    19 
    20 /**
    21  * Configuration facade class.
    22  * @author Christian Lins
    23  * @since sonews/1.0
    24  */
    25 public class Config extends AbstractConfig {
    26 
    27 	public static final int LEVEL_CLI = 1;
    28 	public static final int LEVEL_FILE = 2;
    29 	public static final int LEVEL_BACKEND = 3;
    30 	public static final String CONFIGFILE = "sonews.configfile";
    31 	/** BackendConfig key constant. Value is the maximum article size in kilobytes. */
    32 	public static final String ARTICLE_MAXSIZE = "sonews.article.maxsize";
    33 	/** BackendConfig key constant. Value: Amount of news that are feeded per run. */
    34 	public static final String EVENTLOG = "sonews.eventlog";
    35 	public static final String FEED_NEWSPERRUN = "sonews.feed.newsperrun";
    36 	public static final String FEED_PULLINTERVAL = "sonews.feed.pullinterval";
    37 	public static final String HOSTNAME = "sonews.hostname";
    38 	public static final String PORT = "sonews.port";
    39 	public static final String TIMEOUT = "sonews.timeout";
    40 	public static final String LOGLEVEL = "sonews.loglevel";
    41 	public static final String MLPOLL_DELETEUNKNOWN = "sonews.mlpoll.deleteunknown";
    42 	public static final String MLPOLL_HOST = "sonews.mlpoll.host";
    43 	public static final String MLPOLL_PASSWORD = "sonews.mlpoll.password";
    44 	public static final String MLPOLL_USER = "sonews.mlpoll.user";
    45 	public static final String MLSEND_ADDRESS = "sonews.mlsend.address";
    46 	public static final String MLSEND_RW_FROM = "sonews.mlsend.rewrite.from";
    47 	public static final String MLSEND_RW_SENDER = "sonews.mlsend.rewrite.sender";
    48 	public static final String MLSEND_HOST = "sonews.mlsend.host";
    49 	public static final String MLSEND_PASSWORD = "sonews.mlsend.password";
    50 	public static final String MLSEND_PORT = "sonews.mlsend.port";
    51 	public static final String MLSEND_USER = "sonews.mlsend.user";
    52 	/** Key constant. If value is "true" every I/O is written to logfile
    53 	 * (which is a lot!) */
    54 	public static final String DEBUG = "sonews.debug";
    55 	/** Key constant. Value is classname of the JDBC driver */
    56 	public static final String STORAGE_DBMSDRIVER = "sonews.storage.dbmsdriver";
    57 	/** Key constant. Value is JDBC connect String to the database. */
    58 	public static final String STORAGE_DATABASE = "sonews.storage.database";
    59 	/** Key constant. Value is the username for the DBMS. */
    60 	public static final String STORAGE_USER = "sonews.storage.user";
    61 	/** Key constant. Value is the password for the DBMS. */
    62 	public static final String STORAGE_PASSWORD = "sonews.storage.password";
    63 	public static final String STORAGE_PROVIDER = "sonews.storage.provider";
    64 	/** Key constant. Value is the name of the host which is allowed to use the
    65 	 *  XDAEMON command; default: "localhost" */
    66 	public static final String XDAEMON_HOST = "sonews.xdaemon.host";
    67 	/** The config key for the filename of the logfile */
    68 	public static final String LOGFILE = "sonews.log";
    69 	public static final String[] AVAILABLE_KEYS = {
    70 		ARTICLE_MAXSIZE,
    71 		EVENTLOG,
    72 		FEED_NEWSPERRUN,
    73 		FEED_PULLINTERVAL,
    74 		HOSTNAME,
    75 		MLPOLL_DELETEUNKNOWN,
    76 		MLPOLL_HOST,
    77 		MLPOLL_PASSWORD,
    78 		MLPOLL_USER,
    79 		MLSEND_ADDRESS,
    80 		MLSEND_HOST,
    81 		MLSEND_PASSWORD,
    82 		MLSEND_PORT,
    83 		MLSEND_RW_FROM,
    84 		MLSEND_RW_SENDER,
    85 		MLSEND_USER,
    86 		PORT,
    87 		TIMEOUT,
    88 		XDAEMON_HOST
    89 	};
    90 	private static Config instance = new Config();
    91 
    92 	public static Config inst() {
    93 		return instance;
    94 	}
    95 
    96 	private Config() {
    97 	}
    98 
    99 	@Override
   100 	public String get(String key, String def) {
   101 		String val = CommandLineConfig.getInstance().get(key, null);
   102 
   103 		if (val == null) {
   104 			val = FileConfig.getInstance().get(key, null);
   105 		}
   106 
   107 		if (val == null) {
   108 			val = BackendConfig.getInstance().get(key, def);
   109 		}
   110 
   111 		return val;
   112 	}
   113 
   114 	public String get(int maxLevel, String key, String def) {
   115 		String val = CommandLineConfig.getInstance().get(key, null);
   116 
   117 		if (val == null && maxLevel >= LEVEL_FILE) {
   118 			val = FileConfig.getInstance().get(key, null);
   119 			if (val == null && maxLevel >= LEVEL_BACKEND) {
   120 				val = BackendConfig.getInstance().get(key, def);
   121 			}
   122 		}
   123 
   124 		return val != null ? val : def;
   125 	}
   126 
   127 	@Override
   128 	public void set(String key, String val) {
   129 		set(LEVEL_BACKEND, key, val);
   130 	}
   131 
   132 	public void set(int level, String key, String val) {
   133 		switch (level) {
   134 			case LEVEL_CLI: {
   135 				CommandLineConfig.getInstance().set(key, val);
   136 				break;
   137 			}
   138 			case LEVEL_FILE: {
   139 				FileConfig.getInstance().set(key, val);
   140 				break;
   141 			}
   142 			case LEVEL_BACKEND: {
   143 				BackendConfig.getInstance().set(key, val);
   144 				break;
   145 			}
   146 		}
   147 	}
   148 }