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