trunk/com/so/news/Config.java
author chris <chris@marvin>
Tue, 20 Jan 2009 10:21:03 +0100
changeset 0 f907866f0e4b
permissions -rw-r--r--
Initial import.
chris@0
     1
/*
chris@0
     2
 *   StarOffice News Server
chris@0
     3
 *   see AUTHORS for the list of contributors
chris@0
     4
 *
chris@0
     5
 *   This program is free software: you can redistribute it and/or modify
chris@0
     6
 *   it under the terms of the GNU General Public License as published by
chris@0
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@0
     8
 *   (at your option) any later version.
chris@0
     9
 *
chris@0
    10
 *   This program is distributed in the hope that it will be useful,
chris@0
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@0
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@0
    13
 *   GNU General Public License for more details.
chris@0
    14
 *
chris@0
    15
 *   You should have received a copy of the GNU General Public License
chris@0
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@0
    17
 */
chris@0
    18
chris@0
    19
package com.so.news;
chris@0
    20
chris@0
    21
import java.io.FileInputStream;
chris@0
    22
import java.io.FileNotFoundException;
chris@0
    23
import java.io.FileOutputStream;
chris@0
    24
import java.io.IOException;
chris@0
    25
import java.util.Properties;
chris@0
    26
chris@0
    27
/**
chris@0
    28
 * Manages the n3tpd configuration.
chris@0
    29
 * @author Christian Lins
chris@0
    30
 */
chris@0
    31
public class Config
chris@0
    32
{
chris@0
    33
  /** The filename of the logfile */
chris@0
    34
  public static final String CONFIG_N3TPD_LOGFILE = "n3tpd.logfile";
chris@0
    35
  
chris@0
    36
  /** The filename of the config file that is loaded on startup */
chris@0
    37
  public static final String FILE                 = "n3tpd.conf";
chris@0
    38
chris@0
    39
  private static final Properties defaultConfig = new Properties();
chris@0
    40
  
chris@0
    41
  private static Config instance = null;
chris@0
    42
  
chris@0
    43
  static
chris@0
    44
  {
chris@0
    45
    // Set some default values
chris@0
    46
    defaultConfig.setProperty("n3tpd.article.lifetime", "300"); // 300 days
chris@0
    47
    defaultConfig.setProperty("n3tpd.article.maxsize", "100");  // 100 kbyte
chris@0
    48
    defaultConfig.setProperty("n3tpd.port", "119");
chris@0
    49
    defaultConfig.setProperty("n3tpd.auxport", "8080");
chris@0
    50
    defaultConfig.setProperty("n3tpd.server.backlog", "10");
chris@0
    51
    defaultConfig.setProperty("n3tpd.hostname", "localhost");
chris@0
    52
    defaultConfig.setProperty("n3tpd.storage.database", "jdbc:mysql://localhost/n3tpd_data");
chris@0
    53
    defaultConfig.setProperty("n3tpd.storage.dbmsdriver", "com.mysql.jdbc.Driver");
chris@0
    54
    defaultConfig.setProperty("n3tpd.storage.user", "n3tpd_user");
chris@0
    55
    defaultConfig.setProperty("n3tpd.storage.password", "mysecret");
chris@0
    56
    
chris@0
    57
    instance = new Config();
chris@0
    58
  }
chris@0
    59
  
chris@0
    60
  /**
chris@0
    61
   * @return A Config instance
chris@0
    62
   */
chris@0
    63
  public static Config getInstance()
chris@0
    64
  {
chris@0
    65
    return instance;
chris@0
    66
  }
chris@0
    67
chris@0
    68
  // Every config instance is initialized with the default values.
chris@0
    69
  private Properties settings = (Properties)defaultConfig.clone();
chris@0
    70
chris@0
    71
  /**
chris@0
    72
   * Config is a singelton class with only one instance at time.
chris@0
    73
   * So the constructor is private to prevent the creation of more
chris@0
    74
   * then one Config instance.
chris@0
    75
   * @see Config.getInstance() to retrieve an instance of Config
chris@0
    76
   */
chris@0
    77
  private Config()
chris@0
    78
  {
chris@0
    79
    try
chris@0
    80
    {
chris@0
    81
      // Load settings from file
chris@0
    82
      load();
chris@0
    83
    }
chris@0
    84
    catch(IOException e)
chris@0
    85
    {
chris@0
    86
      e.printStackTrace();
chris@0
    87
    }
chris@0
    88
  }
chris@0
    89
chris@0
    90
  /**
chris@0
    91
   * Loads the configuration from the config file. By default this is done
chris@0
    92
   * by the (private) constructor but it can be useful to reload the config
chris@0
    93
   * by invoking this method.
chris@0
    94
   * @throws IOException
chris@0
    95
   */
chris@0
    96
  public void load() throws IOException
chris@0
    97
  {
chris@0
    98
    try
chris@0
    99
    {
chris@0
   100
      settings.load(new FileInputStream(FILE));
chris@0
   101
    }
chris@0
   102
    catch (FileNotFoundException e)
chris@0
   103
    {
chris@0
   104
      save();
chris@0
   105
    }
chris@0
   106
  }
chris@0
   107
chris@0
   108
  /**
chris@0
   109
   * Saves this Config to the config file. By default this is done
chris@0
   110
   * at program end.
chris@0
   111
   * @throws FileNotFoundException
chris@0
   112
   * @throws IOException
chris@0
   113
   */
chris@0
   114
  public void save() throws FileNotFoundException, IOException
chris@0
   115
  {
chris@0
   116
    settings.store(new FileOutputStream(FILE), "N3TPD Config File");
chris@0
   117
  }
chris@0
   118
  
chris@0
   119
  /**
chris@0
   120
   * Returns the value that is stored within this config
chris@0
   121
   * identified by the given key. If the key cannot be found
chris@0
   122
   * the default value is returned.
chris@0
   123
   * @param key Key to identify the value.
chris@0
   124
   * @param def The default value that is returned if the key
chris@0
   125
   * is not found in this Config.
chris@0
   126
   * @return
chris@0
   127
   */
chris@0
   128
  public String get(String key, String def)
chris@0
   129
  {
chris@0
   130
    return settings.getProperty(key, def);
chris@0
   131
  }
chris@0
   132
chris@0
   133
  /**
chris@0
   134
   * Returns the value that is stored within this config
chris@0
   135
   * identified by the given key. If the key cannot be found
chris@0
   136
   * the default value is returned.
chris@0
   137
   * @param key Key to identify the value.
chris@0
   138
   * @param def The default value that is returned if the key
chris@0
   139
   * is not found in this Config.
chris@0
   140
   * @return
chris@0
   141
   */
chris@0
   142
  public int get(String key, int def)
chris@0
   143
  {
chris@0
   144
    try
chris@0
   145
    {
chris@0
   146
      String val = get(key);
chris@0
   147
      return Integer.parseInt(val);
chris@0
   148
    }
chris@0
   149
    catch(Exception e)
chris@0
   150
    {
chris@0
   151
      return def;
chris@0
   152
    }
chris@0
   153
  }
chris@0
   154
  
chris@0
   155
  /**
chris@0
   156
   * Returns the value that is stored within this config
chris@0
   157
   * identified by the given key. If the key cannot be found
chris@0
   158
   * the default value is returned.
chris@0
   159
   * @param key Key to identify the value.
chris@0
   160
   * @param def The default value that is returned if the key
chris@0
   161
   * is not found in this Config.
chris@0
   162
   * @return
chris@0
   163
   */
chris@0
   164
  public long get(String key, long def)
chris@0
   165
  {
chris@0
   166
    try
chris@0
   167
    {
chris@0
   168
      String val = get(key);
chris@0
   169
      return Long.parseLong(val);
chris@0
   170
    }
chris@0
   171
    catch(Exception e)
chris@0
   172
    {
chris@0
   173
      return def;
chris@0
   174
    }
chris@0
   175
  }
chris@0
   176
chris@0
   177
  /**
chris@0
   178
   * Returns the value for the given key or null if the
chris@0
   179
   * key is not found in this Config.
chris@0
   180
   * @param key
chris@0
   181
   * @return
chris@0
   182
   */
chris@0
   183
  private String get(String key)
chris@0
   184
  {
chris@0
   185
    return settings.getProperty(key);
chris@0
   186
  }
chris@0
   187
chris@0
   188
  /**
chris@0
   189
   * Sets the value for a given key.
chris@0
   190
   * @param key
chris@0
   191
   * @param value
chris@0
   192
   */
chris@0
   193
  public void set(String key, String value)
chris@0
   194
  {
chris@0
   195
    settings.setProperty(key, value);
chris@0
   196
  }
chris@0
   197
chris@0
   198
}