org/sonews/daemon/BootstrapConfig.java
author chris <chris@marvin>
Fri, 26 Jun 2009 16:48:50 +0200
changeset 1 6fceb66e1ad7
permissions -rw-r--r--
Hooray... sonews/0.5.0 final

HG: Enter commit message. Lines beginning with 'HG:' are removed.
HG: Remove all lines to abort the collapse operation.
     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.daemon;
    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 import org.sonews.util.AbstractConfig;
    27 
    28 /**
    29  * Manages the bootstrap configuration. It MUST contain all config values
    30  * that are needed to establish a database connection.
    31  * For further configuration values use the Config class instead as that class
    32  * stores its values within the database.
    33  * @author Christian Lins
    34  * @since sonews/0.5.0
    35  */
    36 public final class BootstrapConfig extends AbstractConfig
    37 {
    38   
    39   /** Key constant. If value is "true" every I/O is written to logfile 
    40    * (which is a lot!) 
    41    */
    42   public static final String DEBUG              = "sonews.debug";
    43   
    44   /** Key constant. Value is classname of the JDBC driver */
    45   public static final String STORAGE_DBMSDRIVER = "sonews.storage.dbmsdriver";
    46   
    47   /** Key constant. Value is JDBC connect String to the database. */
    48   public static final String STORAGE_DATABASE   = "sonews.storage.database";
    49   
    50   /** Key constant. Value is the username for the DBMS. */
    51   public static final String STORAGE_USER       = "sonews.storage.user";
    52   
    53   /** Key constant. Value is the password for the DBMS. */
    54   public static final String STORAGE_PASSWORD   = "sonews.storage.password";
    55   
    56   /** Key constant. Value is the name of the host which is allowed to use the
    57    *  XDAEMON command; default: "localhost" */
    58   public static final String XDAEMON_HOST       = "sonews.xdaemon.host";
    59 
    60   /** The config key for the filename of the logfile */
    61   public static final String LOGFILE = "sonews.log";
    62   
    63   /** The filename of the config file that is loaded on startup */
    64   public static volatile String FILE               = "sonews.conf";
    65 
    66   private static final Properties defaultConfig = new Properties();
    67   
    68   private static BootstrapConfig instance = null;
    69   
    70   static
    71   {
    72     // Set some default values
    73     defaultConfig.setProperty(STORAGE_DATABASE, "jdbc:mysql://localhost/sonews");
    74     defaultConfig.setProperty(STORAGE_DBMSDRIVER, "com.mysql.jdbc.Driver");
    75     defaultConfig.setProperty(STORAGE_USER, "sonews_user");
    76     defaultConfig.setProperty(STORAGE_PASSWORD, "mysecret");
    77     defaultConfig.setProperty(DEBUG, "false");
    78   }
    79   
    80   /**
    81    * Note: this method is not thread-safe
    82    * @return A Config instance
    83    */
    84   public static synchronized BootstrapConfig getInstance()
    85   {
    86     if(instance == null)
    87     {
    88       instance = new BootstrapConfig();
    89     }
    90     return instance;
    91   }
    92 
    93   // Every config instance is initialized with the default values.
    94   private final Properties settings = (Properties)defaultConfig.clone();
    95 
    96   /**
    97    * Config is a singelton class with only one instance at time.
    98    * So the constructor is private to prevent the creation of more
    99    * then one Config instance.
   100    * @see Config.getInstance() to retrieve an instance of Config
   101    */
   102   private BootstrapConfig()
   103   {
   104     try
   105     {
   106       // Load settings from file
   107       load();
   108     }
   109     catch(IOException ex)
   110     {
   111       ex.printStackTrace();
   112     }
   113   }
   114 
   115   /**
   116    * Loads the configuration from the config file. By default this is done
   117    * by the (private) constructor but it can be useful to reload the config
   118    * by invoking this method.
   119    * @throws IOException
   120    */
   121   public void load() 
   122     throws IOException
   123   {
   124     FileInputStream in = null;
   125     
   126     try
   127     {
   128       in = new FileInputStream(FILE);
   129       settings.load(in);
   130     }
   131     catch (FileNotFoundException e)
   132     {
   133       // MUST NOT use Log otherwise endless loop
   134       System.err.println(e.getMessage());
   135       save();
   136     }
   137     finally
   138     {
   139       if(in != null)
   140         in.close();
   141     }
   142   }
   143 
   144   /**
   145    * Saves this Config to the config file. By default this is done
   146    * at program end.
   147    * @throws FileNotFoundException
   148    * @throws IOException
   149    */
   150   public void save() throws FileNotFoundException, IOException
   151   {
   152     FileOutputStream out = null;
   153     try
   154     {
   155       out = new FileOutputStream(FILE);
   156       settings.store(out, "SONEWS Config File");
   157       out.flush();
   158     }
   159     catch(IOException ex)
   160     {
   161       throw ex;
   162     }
   163     finally
   164     {
   165       if(out != null)
   166         out.close();
   167     }
   168   }
   169   
   170   /**
   171    * Returns the value that is stored within this config
   172    * identified by the given key. If the key cannot be found
   173    * the default value is returned.
   174    * @param key Key to identify the value.
   175    * @param def The default value that is returned if the key
   176    * is not found in this Config.
   177    * @return
   178    */
   179   public String get(String key, String def)
   180   {
   181     return settings.getProperty(key, def);
   182   }
   183 
   184   /**
   185    * Sets the value for a given key.
   186    * @param key
   187    * @param value
   188    */
   189   public void set(final String key, final String value)
   190   {
   191     settings.setProperty(key, value);
   192   }
   193 
   194 }