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.
     1 /*
     2  *   StarOffice 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 com.so.news;
    20 
    21 import java.io.FileInputStream;
    22 import java.io.FileNotFoundException;
    23 import java.io.FileOutputStream;
    24 import java.io.IOException;
    25 import java.util.Properties;
    26 
    27 /**
    28  * Manages the n3tpd configuration.
    29  * @author Christian Lins
    30  */
    31 public class Config
    32 {
    33   /** The filename of the logfile */
    34   public static final String CONFIG_N3TPD_LOGFILE = "n3tpd.logfile";
    35   
    36   /** The filename of the config file that is loaded on startup */
    37   public static final String FILE                 = "n3tpd.conf";
    38 
    39   private static final Properties defaultConfig = new Properties();
    40   
    41   private static Config instance = null;
    42   
    43   static
    44   {
    45     // Set some default values
    46     defaultConfig.setProperty("n3tpd.article.lifetime", "300"); // 300 days
    47     defaultConfig.setProperty("n3tpd.article.maxsize", "100");  // 100 kbyte
    48     defaultConfig.setProperty("n3tpd.port", "119");
    49     defaultConfig.setProperty("n3tpd.auxport", "8080");
    50     defaultConfig.setProperty("n3tpd.server.backlog", "10");
    51     defaultConfig.setProperty("n3tpd.hostname", "localhost");
    52     defaultConfig.setProperty("n3tpd.storage.database", "jdbc:mysql://localhost/n3tpd_data");
    53     defaultConfig.setProperty("n3tpd.storage.dbmsdriver", "com.mysql.jdbc.Driver");
    54     defaultConfig.setProperty("n3tpd.storage.user", "n3tpd_user");
    55     defaultConfig.setProperty("n3tpd.storage.password", "mysecret");
    56     
    57     instance = new Config();
    58   }
    59   
    60   /**
    61    * @return A Config instance
    62    */
    63   public static Config getInstance()
    64   {
    65     return instance;
    66   }
    67 
    68   // Every config instance is initialized with the default values.
    69   private Properties settings = (Properties)defaultConfig.clone();
    70 
    71   /**
    72    * Config is a singelton class with only one instance at time.
    73    * So the constructor is private to prevent the creation of more
    74    * then one Config instance.
    75    * @see Config.getInstance() to retrieve an instance of Config
    76    */
    77   private Config()
    78   {
    79     try
    80     {
    81       // Load settings from file
    82       load();
    83     }
    84     catch(IOException e)
    85     {
    86       e.printStackTrace();
    87     }
    88   }
    89 
    90   /**
    91    * Loads the configuration from the config file. By default this is done
    92    * by the (private) constructor but it can be useful to reload the config
    93    * by invoking this method.
    94    * @throws IOException
    95    */
    96   public void load() throws IOException
    97   {
    98     try
    99     {
   100       settings.load(new FileInputStream(FILE));
   101     }
   102     catch (FileNotFoundException e)
   103     {
   104       save();
   105     }
   106   }
   107 
   108   /**
   109    * Saves this Config to the config file. By default this is done
   110    * at program end.
   111    * @throws FileNotFoundException
   112    * @throws IOException
   113    */
   114   public void save() throws FileNotFoundException, IOException
   115   {
   116     settings.store(new FileOutputStream(FILE), "N3TPD Config File");
   117   }
   118   
   119   /**
   120    * Returns the value that is stored within this config
   121    * identified by the given key. If the key cannot be found
   122    * the default value is returned.
   123    * @param key Key to identify the value.
   124    * @param def The default value that is returned if the key
   125    * is not found in this Config.
   126    * @return
   127    */
   128   public String get(String key, String def)
   129   {
   130     return settings.getProperty(key, def);
   131   }
   132 
   133   /**
   134    * Returns the value that is stored within this config
   135    * identified by the given key. If the key cannot be found
   136    * the default value is returned.
   137    * @param key Key to identify the value.
   138    * @param def The default value that is returned if the key
   139    * is not found in this Config.
   140    * @return
   141    */
   142   public int get(String key, int def)
   143   {
   144     try
   145     {
   146       String val = get(key);
   147       return Integer.parseInt(val);
   148     }
   149     catch(Exception e)
   150     {
   151       return def;
   152     }
   153   }
   154   
   155   /**
   156    * Returns the value that is stored within this config
   157    * identified by the given key. If the key cannot be found
   158    * the default value is returned.
   159    * @param key Key to identify the value.
   160    * @param def The default value that is returned if the key
   161    * is not found in this Config.
   162    * @return
   163    */
   164   public long get(String key, long def)
   165   {
   166     try
   167     {
   168       String val = get(key);
   169       return Long.parseLong(val);
   170     }
   171     catch(Exception e)
   172     {
   173       return def;
   174     }
   175   }
   176 
   177   /**
   178    * Returns the value for the given key or null if the
   179    * key is not found in this Config.
   180    * @param key
   181    * @return
   182    */
   183   private String get(String key)
   184   {
   185     return settings.getProperty(key);
   186   }
   187 
   188   /**
   189    * Sets the value for a given key.
   190    * @param key
   191    * @param value
   192    */
   193   public void set(String key, String value)
   194   {
   195     settings.setProperty(key, value);
   196   }
   197 
   198 }