org/sonews/config/FileConfig.java
author cli
Sun Aug 29 17:03:21 2010 +0200 (2010-08-29)
changeset 33 f9bf183447d1
parent 3 2fdc9cc89502
permissions -rw-r--r--
Article(javax.mail.Message) now has a safe method to read the byte body from the given message object (fixes #16).
     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 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 bootstrap configuration. It MUST contain all config values
    29  * that are needed to establish a database connection.
    30  * For further configuration values use the Config class instead as that class
    31  * stores its values within the database.
    32  * @author Christian Lins
    33  * @since sonews/0.5.0
    34  */
    35 class FileConfig extends AbstractConfig
    36 {
    37 
    38   private static final Properties defaultConfig = new Properties();
    39   
    40   private static FileConfig instance = null;
    41   
    42   static
    43   {
    44     // Set some default values
    45     defaultConfig.setProperty(Config.STORAGE_DATABASE, "jdbc:mysql://localhost/sonews");
    46     defaultConfig.setProperty(Config.STORAGE_DBMSDRIVER, "com.mysql.jdbc.Driver");
    47     defaultConfig.setProperty(Config.STORAGE_USER, "sonews_user");
    48     defaultConfig.setProperty(Config.STORAGE_PASSWORD, "mysecret");
    49     defaultConfig.setProperty(Config.DEBUG, "false");
    50   }
    51   
    52   /**
    53    * Note: this method is not thread-safe
    54    * @return A Config instance
    55    */
    56   public static synchronized FileConfig getInstance()
    57   {
    58     if(instance == null)
    59     {
    60       instance = new FileConfig();
    61     }
    62     return instance;
    63   }
    64 
    65   // Every config instance is initialized with the default values.
    66   private final Properties settings = (Properties)defaultConfig.clone();
    67 
    68   /**
    69    * Config is a singelton class with only one instance at time.
    70    * So the constructor is private to prevent the creation of more
    71    * then one Config instance.
    72    * @see Config.getInstance() to retrieve an instance of Config
    73    */
    74   private FileConfig()
    75   {
    76     try
    77     {
    78       // Load settings from file
    79       load();
    80     }
    81     catch(IOException ex)
    82     {
    83       ex.printStackTrace();
    84     }
    85   }
    86 
    87   /**
    88    * Loads the configuration from the config file. By default this is done
    89    * by the (private) constructor but it can be useful to reload the config
    90    * by invoking this method.
    91    * @throws IOException
    92    */
    93   public void load() 
    94     throws IOException
    95   {
    96     FileInputStream in = null;
    97     
    98     try
    99     {
   100       in = new FileInputStream(
   101         Config.inst().get(Config.LEVEL_CLI, Config.CONFIGFILE, "sonews.conf"));
   102       settings.load(in);
   103     }
   104     catch (FileNotFoundException e)
   105     {
   106       // MUST NOT use Log otherwise endless loop
   107       System.err.println(e.getMessage());
   108       save();
   109     }
   110     finally
   111     {
   112       if(in != null)
   113         in.close();
   114     }
   115   }
   116 
   117   /**
   118    * Saves this Config to the config file. By default this is done
   119    * at program end.
   120    * @throws FileNotFoundException
   121    * @throws IOException
   122    */
   123   public void save() throws FileNotFoundException, IOException
   124   {
   125     FileOutputStream out = null;
   126     try
   127     {
   128       out = new FileOutputStream(
   129         Config.inst().get(Config.LEVEL_CLI, Config.CONFIGFILE, "sonews.conf"));
   130       settings.store(out, "SONEWS Config File");
   131       out.flush();
   132     }
   133     catch(IOException ex)
   134     {
   135       throw ex;
   136     }
   137     finally
   138     {
   139       if(out != null)
   140         out.close();
   141     }
   142   }
   143   
   144   /**
   145    * Returns the value that is stored within this config
   146    * identified by the given key. If the key cannot be found
   147    * the default value is returned.
   148    * @param key Key to identify the value.
   149    * @param def The default value that is returned if the key
   150    * is not found in this Config.
   151    * @return
   152    */
   153   @Override
   154   public String get(String key, String def)
   155   {
   156     return settings.getProperty(key, def);
   157   }
   158 
   159   /**
   160    * Sets the value for a given key.
   161    * @param key
   162    * @param value
   163    */
   164   @Override
   165   public void set(final String key, final String value)
   166   {
   167     settings.setProperty(key, value);
   168   }
   169 
   170 }