src/org/sonews/config/FileConfig.java
author cli
Tue, 07 Jun 2011 09:23:34 +0200
changeset 43 7d0e65712a95
parent 37 74139325d305
permissions -rwxr-xr-x
Adapt config sample to use hsqldb
chris@3
     1
/*
chris@3
     2
 *   SONEWS News Server
chris@3
     3
 *   see AUTHORS for the list of contributors
chris@3
     4
 *
chris@3
     5
 *   This program is free software: you can redistribute it and/or modify
chris@3
     6
 *   it under the terms of the GNU General Public License as published by
chris@3
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@3
     8
 *   (at your option) any later version.
chris@3
     9
 *
chris@3
    10
 *   This program is distributed in the hope that it will be useful,
chris@3
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@3
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@3
    13
 *   GNU General Public License for more details.
chris@3
    14
 *
chris@3
    15
 *   You should have received a copy of the GNU General Public License
chris@3
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@3
    17
 */
chris@3
    18
chris@3
    19
package org.sonews.config;
chris@3
    20
chris@3
    21
import java.io.FileInputStream;
chris@3
    22
import java.io.FileNotFoundException;
chris@3
    23
import java.io.FileOutputStream;
chris@3
    24
import java.io.IOException;
chris@3
    25
import java.util.Properties;
chris@3
    26
chris@3
    27
/**
chris@3
    28
 * Manages the bootstrap configuration. It MUST contain all config values
chris@3
    29
 * that are needed to establish a database connection.
chris@3
    30
 * For further configuration values use the Config class instead as that class
chris@3
    31
 * stores its values within the database.
chris@3
    32
 * @author Christian Lins
chris@3
    33
 * @since sonews/0.5.0
chris@3
    34
 */
chris@3
    35
class FileConfig extends AbstractConfig
chris@3
    36
{
chris@3
    37
cli@37
    38
	private static final Properties defaultConfig = new Properties();
cli@37
    39
	private static FileConfig instance = null;
chris@3
    40
cli@37
    41
	static {
cli@37
    42
		// Set some default values
cli@37
    43
		defaultConfig.setProperty(Config.STORAGE_DATABASE, "jdbc:mysql://localhost/sonews");
cli@37
    44
		defaultConfig.setProperty(Config.STORAGE_DBMSDRIVER, "com.mysql.jdbc.Driver");
cli@37
    45
		defaultConfig.setProperty(Config.STORAGE_USER, "sonews_user");
cli@37
    46
		defaultConfig.setProperty(Config.STORAGE_PASSWORD, "mysecret");
cli@37
    47
		defaultConfig.setProperty(Config.DEBUG, "false");
cli@37
    48
	}
chris@3
    49
cli@37
    50
	/**
cli@37
    51
	 * Note: this method is not thread-safe
cli@37
    52
	 * @return A Config instance
cli@37
    53
	 */
cli@37
    54
	public static synchronized FileConfig getInstance()
cli@37
    55
	{
cli@37
    56
		if (instance == null) {
cli@37
    57
			instance = new FileConfig();
cli@37
    58
		}
cli@37
    59
		return instance;
cli@37
    60
	}
cli@37
    61
	// Every config instance is initialized with the default values.
cli@37
    62
	private final Properties settings = (Properties) defaultConfig.clone();
chris@3
    63
cli@37
    64
	/**
cli@37
    65
	 * Config is a singelton class with only one instance at time.
cli@37
    66
	 * So the constructor is private to prevent the creation of more
cli@37
    67
	 * then one Config instance.
cli@37
    68
	 * @see Config.getInstance() to retrieve an instance of Config
cli@37
    69
	 */
cli@37
    70
	private FileConfig()
cli@37
    71
	{
cli@37
    72
		try {
cli@37
    73
			// Load settings from file
cli@37
    74
			load();
cli@37
    75
		} catch (IOException ex) {
cli@37
    76
			ex.printStackTrace();
cli@37
    77
		}
cli@37
    78
	}
chris@3
    79
cli@37
    80
	/**
cli@37
    81
	 * Loads the configuration from the config file. By default this is done
cli@37
    82
	 * by the (private) constructor but it can be useful to reload the config
cli@37
    83
	 * by invoking this method.
cli@37
    84
	 * @throws IOException
cli@37
    85
	 */
cli@37
    86
	public void load()
cli@37
    87
		throws IOException
cli@37
    88
	{
cli@37
    89
		FileInputStream in = null;
chris@3
    90
cli@37
    91
		try {
cli@37
    92
			in = new FileInputStream(
cli@37
    93
				Config.inst().get(Config.LEVEL_CLI, Config.CONFIGFILE, "sonews.conf"));
cli@37
    94
			settings.load(in);
cli@37
    95
		} catch (FileNotFoundException e) {
cli@37
    96
			// MUST NOT use Log otherwise endless loop
cli@37
    97
			System.err.println(e.getMessage());
cli@37
    98
			save();
cli@37
    99
		} finally {
cli@37
   100
			if (in != null) {
cli@37
   101
				in.close();
cli@37
   102
			}
cli@37
   103
		}
cli@37
   104
	}
chris@3
   105
cli@37
   106
	/**
cli@37
   107
	 * Saves this Config to the config file. By default this is done
cli@37
   108
	 * at program end.
cli@37
   109
	 * @throws FileNotFoundException
cli@37
   110
	 * @throws IOException
cli@37
   111
	 */
cli@37
   112
	public void save() throws FileNotFoundException, IOException
cli@37
   113
	{
cli@37
   114
		FileOutputStream out = null;
cli@37
   115
		try {
cli@37
   116
			out = new FileOutputStream(
cli@37
   117
				Config.inst().get(Config.LEVEL_CLI, Config.CONFIGFILE, "sonews.conf"));
cli@37
   118
			settings.store(out, "SONEWS Config File");
cli@37
   119
			out.flush();
cli@37
   120
		} catch (IOException ex) {
cli@37
   121
			throw ex;
cli@37
   122
		} finally {
cli@37
   123
			if (out != null) {
cli@37
   124
				out.close();
cli@37
   125
			}
cli@37
   126
		}
cli@37
   127
	}
cli@37
   128
cli@37
   129
	/**
cli@37
   130
	 * Returns the value that is stored within this config
cli@37
   131
	 * identified by the given key. If the key cannot be found
cli@37
   132
	 * the default value is returned.
cli@37
   133
	 * @param key Key to identify the value.
cli@37
   134
	 * @param def The default value that is returned if the key
cli@37
   135
	 * is not found in this Config.
cli@37
   136
	 * @return
cli@37
   137
	 */
cli@37
   138
	@Override
cli@37
   139
	public String get(String key, String def)
cli@37
   140
	{
cli@37
   141
		return settings.getProperty(key, def);
cli@37
   142
	}
cli@37
   143
cli@37
   144
	/**
cli@37
   145
	 * Sets the value for a given key.
cli@37
   146
	 * @param key
cli@37
   147
	 * @param value
cli@37
   148
	 */
cli@37
   149
	@Override
cli@37
   150
	public void set(final String key, final String value)
cli@37
   151
	{
cli@37
   152
		settings.setProperty(key, value);
cli@37
   153
	}
chris@3
   154
}