1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/org/sonews/config/FileConfig.java Wed Jul 22 14:04:05 2009 +0200
1.3 @@ -0,0 +1,169 @@
1.4 +/*
1.5 + * SONEWS News Server
1.6 + * see AUTHORS for the list of contributors
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, either version 3 of the License, or
1.11 + * (at your option) any later version.
1.12 + *
1.13 + * This program is distributed in the hope that it will be useful,
1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 + * GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License
1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 + */
1.21 +
1.22 +package org.sonews.config;
1.23 +
1.24 +import java.io.FileInputStream;
1.25 +import java.io.FileNotFoundException;
1.26 +import java.io.FileOutputStream;
1.27 +import java.io.IOException;
1.28 +import java.util.Properties;
1.29 +
1.30 +/**
1.31 + * Manages the bootstrap configuration. It MUST contain all config values
1.32 + * that are needed to establish a database connection.
1.33 + * For further configuration values use the Config class instead as that class
1.34 + * stores its values within the database.
1.35 + * @author Christian Lins
1.36 + * @since sonews/0.5.0
1.37 + */
1.38 +class FileConfig extends AbstractConfig
1.39 +{
1.40 +
1.41 + private static final Properties defaultConfig = new Properties();
1.42 +
1.43 + private static FileConfig instance = null;
1.44 +
1.45 + static
1.46 + {
1.47 + // Set some default values
1.48 + defaultConfig.setProperty(Config.STORAGE_DATABASE, "jdbc:mysql://localhost/sonews");
1.49 + defaultConfig.setProperty(Config.STORAGE_DBMSDRIVER, "com.mysql.jdbc.Driver");
1.50 + defaultConfig.setProperty(Config.STORAGE_USER, "sonews_user");
1.51 + defaultConfig.setProperty(Config.STORAGE_PASSWORD, "mysecret");
1.52 + defaultConfig.setProperty(Config.DEBUG, "false");
1.53 + }
1.54 +
1.55 + /**
1.56 + * Note: this method is not thread-safe
1.57 + * @return A Config instance
1.58 + */
1.59 + public static synchronized FileConfig getInstance()
1.60 + {
1.61 + if(instance == null)
1.62 + {
1.63 + instance = new FileConfig();
1.64 + }
1.65 + return instance;
1.66 + }
1.67 +
1.68 + // Every config instance is initialized with the default values.
1.69 + private final Properties settings = (Properties)defaultConfig.clone();
1.70 +
1.71 + /**
1.72 + * Config is a singelton class with only one instance at time.
1.73 + * So the constructor is private to prevent the creation of more
1.74 + * then one Config instance.
1.75 + * @see Config.getInstance() to retrieve an instance of Config
1.76 + */
1.77 + private FileConfig()
1.78 + {
1.79 + try
1.80 + {
1.81 + // Load settings from file
1.82 + load();
1.83 + }
1.84 + catch(IOException ex)
1.85 + {
1.86 + ex.printStackTrace();
1.87 + }
1.88 + }
1.89 +
1.90 + /**
1.91 + * Loads the configuration from the config file. By default this is done
1.92 + * by the (private) constructor but it can be useful to reload the config
1.93 + * by invoking this method.
1.94 + * @throws IOException
1.95 + */
1.96 + public void load()
1.97 + throws IOException
1.98 + {
1.99 + FileInputStream in = null;
1.100 +
1.101 + try
1.102 + {
1.103 + in = new FileInputStream(
1.104 + Config.inst().get(Config.LEVEL_CLI, Config.CONFIGFILE, "sonews.conf"));
1.105 + settings.load(in);
1.106 + }
1.107 + catch (FileNotFoundException e)
1.108 + {
1.109 + // MUST NOT use Log otherwise endless loop
1.110 + System.err.println(e.getMessage());
1.111 + save();
1.112 + }
1.113 + finally
1.114 + {
1.115 + if(in != null)
1.116 + in.close();
1.117 + }
1.118 + }
1.119 +
1.120 + /**
1.121 + * Saves this Config to the config file. By default this is done
1.122 + * at program end.
1.123 + * @throws FileNotFoundException
1.124 + * @throws IOException
1.125 + */
1.126 + public void save() throws FileNotFoundException, IOException
1.127 + {
1.128 + FileOutputStream out = null;
1.129 + try
1.130 + {
1.131 + out = new FileOutputStream(
1.132 + Config.inst().get(Config.LEVEL_CLI, Config.CONFIGFILE, "sonews.conf"));
1.133 + settings.store(out, "SONEWS Config File");
1.134 + out.flush();
1.135 + }
1.136 + catch(IOException ex)
1.137 + {
1.138 + throw ex;
1.139 + }
1.140 + finally
1.141 + {
1.142 + if(out != null)
1.143 + out.close();
1.144 + }
1.145 + }
1.146 +
1.147 + /**
1.148 + * Returns the value that is stored within this config
1.149 + * identified by the given key. If the key cannot be found
1.150 + * the default value is returned.
1.151 + * @param key Key to identify the value.
1.152 + * @param def The default value that is returned if the key
1.153 + * is not found in this Config.
1.154 + * @return
1.155 + */
1.156 + @Override
1.157 + public String get(String key, String def)
1.158 + {
1.159 + return settings.getProperty(key, def);
1.160 + }
1.161 +
1.162 + /**
1.163 + * Sets the value for a given key.
1.164 + * @param key
1.165 + * @param value
1.166 + */
1.167 + public void set(final String key, final String value)
1.168 + {
1.169 + settings.setProperty(key, value);
1.170 + }
1.171 +
1.172 +}