src/org/sonews/config/FileConfig.java
author František Kučera <franta-hg@frantovo.cz>
Fri Oct 14 00:45:06 2011 +0200 (2011-10-14)
changeset 76 b5690fc25af6
parent 35 ed84c8bdd87b
permissions -rwxr-xr-x
Drupal: XSL šablona pro formátování XHTML části zprávy.
     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 	private static FileConfig instance = null;
    40 
    41 	static {
    42 		// Set some default values
    43 		defaultConfig.setProperty(Config.STORAGE_DATABASE, "jdbc:mysql://localhost/sonews");
    44 		defaultConfig.setProperty(Config.STORAGE_DBMSDRIVER, "com.mysql.jdbc.Driver");
    45 		defaultConfig.setProperty(Config.STORAGE_USER, "sonews_user");
    46 		defaultConfig.setProperty(Config.STORAGE_PASSWORD, "mysecret");
    47 		defaultConfig.setProperty(Config.DEBUG, "false");
    48 	}
    49 
    50 	/**
    51 	 * Note: this method is not thread-safe
    52 	 * @return A Config instance
    53 	 */
    54 	public static synchronized FileConfig getInstance()
    55 	{
    56 		if (instance == null) {
    57 			instance = new FileConfig();
    58 		}
    59 		return instance;
    60 	}
    61 	// Every config instance is initialized with the default values.
    62 	private final Properties settings = (Properties) defaultConfig.clone();
    63 
    64 	/**
    65 	 * Config is a singelton class with only one instance at time.
    66 	 * So the constructor is private to prevent the creation of more
    67 	 * then one Config instance.
    68 	 * @see Config.getInstance() to retrieve an instance of Config
    69 	 */
    70 	private FileConfig()
    71 	{
    72 		try {
    73 			// Load settings from file
    74 			load();
    75 		} catch (IOException ex) {
    76 			ex.printStackTrace();
    77 		}
    78 	}
    79 
    80 	/**
    81 	 * Loads the configuration from the config file. By default this is done
    82 	 * by the (private) constructor but it can be useful to reload the config
    83 	 * by invoking this method.
    84 	 * @throws IOException
    85 	 */
    86 	public void load()
    87 		throws IOException
    88 	{
    89 		FileInputStream in = null;
    90 
    91 		try {
    92 			in = new FileInputStream(
    93 				Config.inst().get(Config.LEVEL_CLI, Config.CONFIGFILE, "sonews.conf"));
    94 			settings.load(in);
    95 		} catch (FileNotFoundException e) {
    96 			// MUST NOT use Log otherwise endless loop
    97 			System.err.println(e.getMessage());
    98 			save();
    99 		} finally {
   100 			if (in != null) {
   101 				in.close();
   102 			}
   103 		}
   104 	}
   105 
   106 	/**
   107 	 * Saves this Config to the config file. By default this is done
   108 	 * at program end.
   109 	 * @throws FileNotFoundException
   110 	 * @throws IOException
   111 	 */
   112 	public void save() throws FileNotFoundException, IOException
   113 	{
   114 		FileOutputStream out = null;
   115 		try {
   116 			out = new FileOutputStream(
   117 				Config.inst().get(Config.LEVEL_CLI, Config.CONFIGFILE, "sonews.conf"));
   118 			settings.store(out, "SONEWS Config File");
   119 			out.flush();
   120 		} catch (IOException ex) {
   121 			throw ex;
   122 		} finally {
   123 			if (out != null) {
   124 				out.close();
   125 			}
   126 		}
   127 	}
   128 
   129 	/**
   130 	 * Returns the value that is stored within this config
   131 	 * identified by the given key. If the key cannot be found
   132 	 * the default value is returned.
   133 	 * @param key Key to identify the value.
   134 	 * @param def The default value that is returned if the key
   135 	 * is not found in this Config.
   136 	 * @return
   137 	 */
   138 	@Override
   139 	public String get(String key, String def)
   140 	{
   141 		return settings.getProperty(key, def);
   142 	}
   143 
   144 	/**
   145 	 * Sets the value for a given key.
   146 	 * @param key
   147 	 * @param value
   148 	 */
   149 	@Override
   150 	public void set(final String key, final String value)
   151 	{
   152 		settings.setProperty(key, value);
   153 	}
   154 }