src/org/sonews/config/FileConfig.java
changeset 103 a788bf0e1080
parent 35 ed84c8bdd87b
     1.1 --- a/src/org/sonews/config/FileConfig.java	Sun Aug 29 17:28:58 2010 +0200
     1.2 +++ b/src/org/sonews/config/FileConfig.java	Thu Oct 20 10:50:58 2011 +0200
     1.3 @@ -35,136 +35,120 @@
     1.4  class FileConfig extends AbstractConfig
     1.5  {
     1.6  
     1.7 -  private static final Properties defaultConfig = new Properties();
     1.8 -  
     1.9 -  private static FileConfig instance = null;
    1.10 -  
    1.11 -  static
    1.12 -  {
    1.13 -    // Set some default values
    1.14 -    defaultConfig.setProperty(Config.STORAGE_DATABASE, "jdbc:mysql://localhost/sonews");
    1.15 -    defaultConfig.setProperty(Config.STORAGE_DBMSDRIVER, "com.mysql.jdbc.Driver");
    1.16 -    defaultConfig.setProperty(Config.STORAGE_USER, "sonews_user");
    1.17 -    defaultConfig.setProperty(Config.STORAGE_PASSWORD, "mysecret");
    1.18 -    defaultConfig.setProperty(Config.DEBUG, "false");
    1.19 -  }
    1.20 -  
    1.21 -  /**
    1.22 -   * Note: this method is not thread-safe
    1.23 -   * @return A Config instance
    1.24 -   */
    1.25 -  public static synchronized FileConfig getInstance()
    1.26 -  {
    1.27 -    if(instance == null)
    1.28 -    {
    1.29 -      instance = new FileConfig();
    1.30 -    }
    1.31 -    return instance;
    1.32 -  }
    1.33 +	private static final Properties defaultConfig = new Properties();
    1.34 +	private static FileConfig instance = null;
    1.35  
    1.36 -  // Every config instance is initialized with the default values.
    1.37 -  private final Properties settings = (Properties)defaultConfig.clone();
    1.38 +	static {
    1.39 +		// Set some default values
    1.40 +		defaultConfig.setProperty(Config.STORAGE_DATABASE, "jdbc:mysql://localhost/sonews");
    1.41 +		defaultConfig.setProperty(Config.STORAGE_DBMSDRIVER, "com.mysql.jdbc.Driver");
    1.42 +		defaultConfig.setProperty(Config.STORAGE_USER, "sonews_user");
    1.43 +		defaultConfig.setProperty(Config.STORAGE_PASSWORD, "mysecret");
    1.44 +		defaultConfig.setProperty(Config.DEBUG, "false");
    1.45 +	}
    1.46  
    1.47 -  /**
    1.48 -   * Config is a singelton class with only one instance at time.
    1.49 -   * So the constructor is private to prevent the creation of more
    1.50 -   * then one Config instance.
    1.51 -   * @see Config.getInstance() to retrieve an instance of Config
    1.52 -   */
    1.53 -  private FileConfig()
    1.54 -  {
    1.55 -    try
    1.56 -    {
    1.57 -      // Load settings from file
    1.58 -      load();
    1.59 -    }
    1.60 -    catch(IOException ex)
    1.61 -    {
    1.62 -      ex.printStackTrace();
    1.63 -    }
    1.64 -  }
    1.65 +	/**
    1.66 +	 * Note: this method is not thread-safe
    1.67 +	 * @return A Config instance
    1.68 +	 */
    1.69 +	public static synchronized FileConfig getInstance()
    1.70 +	{
    1.71 +		if (instance == null) {
    1.72 +			instance = new FileConfig();
    1.73 +		}
    1.74 +		return instance;
    1.75 +	}
    1.76 +	// Every config instance is initialized with the default values.
    1.77 +	private final Properties settings = (Properties) defaultConfig.clone();
    1.78  
    1.79 -  /**
    1.80 -   * Loads the configuration from the config file. By default this is done
    1.81 -   * by the (private) constructor but it can be useful to reload the config
    1.82 -   * by invoking this method.
    1.83 -   * @throws IOException
    1.84 -   */
    1.85 -  public void load() 
    1.86 -    throws IOException
    1.87 -  {
    1.88 -    FileInputStream in = null;
    1.89 -    
    1.90 -    try
    1.91 -    {
    1.92 -      in = new FileInputStream(
    1.93 -        Config.inst().get(Config.LEVEL_CLI, Config.CONFIGFILE, "sonews.conf"));
    1.94 -      settings.load(in);
    1.95 -    }
    1.96 -    catch (FileNotFoundException e)
    1.97 -    {
    1.98 -      // MUST NOT use Log otherwise endless loop
    1.99 -      System.err.println(e.getMessage());
   1.100 -      save();
   1.101 -    }
   1.102 -    finally
   1.103 -    {
   1.104 -      if(in != null)
   1.105 -        in.close();
   1.106 -    }
   1.107 -  }
   1.108 +	/**
   1.109 +	 * Config is a singelton class with only one instance at time.
   1.110 +	 * So the constructor is private to prevent the creation of more
   1.111 +	 * then one Config instance.
   1.112 +	 * @see Config.getInstance() to retrieve an instance of Config
   1.113 +	 */
   1.114 +	private FileConfig()
   1.115 +	{
   1.116 +		try {
   1.117 +			// Load settings from file
   1.118 +			load();
   1.119 +		} catch (IOException ex) {
   1.120 +			ex.printStackTrace();
   1.121 +		}
   1.122 +	}
   1.123  
   1.124 -  /**
   1.125 -   * Saves this Config to the config file. By default this is done
   1.126 -   * at program end.
   1.127 -   * @throws FileNotFoundException
   1.128 -   * @throws IOException
   1.129 -   */
   1.130 -  public void save() throws FileNotFoundException, IOException
   1.131 -  {
   1.132 -    FileOutputStream out = null;
   1.133 -    try
   1.134 -    {
   1.135 -      out = new FileOutputStream(
   1.136 -        Config.inst().get(Config.LEVEL_CLI, Config.CONFIGFILE, "sonews.conf"));
   1.137 -      settings.store(out, "SONEWS Config File");
   1.138 -      out.flush();
   1.139 -    }
   1.140 -    catch(IOException ex)
   1.141 -    {
   1.142 -      throw ex;
   1.143 -    }
   1.144 -    finally
   1.145 -    {
   1.146 -      if(out != null)
   1.147 -        out.close();
   1.148 -    }
   1.149 -  }
   1.150 -  
   1.151 -  /**
   1.152 -   * Returns the value that is stored within this config
   1.153 -   * identified by the given key. If the key cannot be found
   1.154 -   * the default value is returned.
   1.155 -   * @param key Key to identify the value.
   1.156 -   * @param def The default value that is returned if the key
   1.157 -   * is not found in this Config.
   1.158 -   * @return
   1.159 -   */
   1.160 -  @Override
   1.161 -  public String get(String key, String def)
   1.162 -  {
   1.163 -    return settings.getProperty(key, def);
   1.164 -  }
   1.165 +	/**
   1.166 +	 * Loads the configuration from the config file. By default this is done
   1.167 +	 * by the (private) constructor but it can be useful to reload the config
   1.168 +	 * by invoking this method.
   1.169 +	 * @throws IOException
   1.170 +	 */
   1.171 +	public void load()
   1.172 +		throws IOException
   1.173 +	{
   1.174 +		FileInputStream in = null;
   1.175  
   1.176 -  /**
   1.177 -   * Sets the value for a given key.
   1.178 -   * @param key
   1.179 -   * @param value
   1.180 -   */
   1.181 -  @Override
   1.182 -  public void set(final String key, final String value)
   1.183 -  {
   1.184 -    settings.setProperty(key, value);
   1.185 -  }
   1.186 +		try {
   1.187 +			in = new FileInputStream(
   1.188 +				Config.inst().get(Config.LEVEL_CLI, Config.CONFIGFILE, "sonews.conf"));
   1.189 +			settings.load(in);
   1.190 +		} catch (FileNotFoundException e) {
   1.191 +			// MUST NOT use Log otherwise endless loop
   1.192 +			System.err.println(e.getMessage());
   1.193 +			save();
   1.194 +		} finally {
   1.195 +			if (in != null) {
   1.196 +				in.close();
   1.197 +			}
   1.198 +		}
   1.199 +	}
   1.200  
   1.201 +	/**
   1.202 +	 * Saves this Config to the config file. By default this is done
   1.203 +	 * at program end.
   1.204 +	 * @throws FileNotFoundException
   1.205 +	 * @throws IOException
   1.206 +	 */
   1.207 +	public void save() throws FileNotFoundException, IOException
   1.208 +	{
   1.209 +		FileOutputStream out = null;
   1.210 +		try {
   1.211 +			out = new FileOutputStream(
   1.212 +				Config.inst().get(Config.LEVEL_CLI, Config.CONFIGFILE, "sonews.conf"));
   1.213 +			settings.store(out, "SONEWS Config File");
   1.214 +			out.flush();
   1.215 +		} catch (IOException ex) {
   1.216 +			throw ex;
   1.217 +		} finally {
   1.218 +			if (out != null) {
   1.219 +				out.close();
   1.220 +			}
   1.221 +		}
   1.222 +	}
   1.223 +
   1.224 +	/**
   1.225 +	 * Returns the value that is stored within this config
   1.226 +	 * identified by the given key. If the key cannot be found
   1.227 +	 * the default value is returned.
   1.228 +	 * @param key Key to identify the value.
   1.229 +	 * @param def The default value that is returned if the key
   1.230 +	 * is not found in this Config.
   1.231 +	 * @return
   1.232 +	 */
   1.233 +	@Override
   1.234 +	public String get(String key, String def)
   1.235 +	{
   1.236 +		return settings.getProperty(key, def);
   1.237 +	}
   1.238 +
   1.239 +	/**
   1.240 +	 * Sets the value for a given key.
   1.241 +	 * @param key
   1.242 +	 * @param value
   1.243 +	 */
   1.244 +	@Override
   1.245 +	public void set(final String key, final String value)
   1.246 +	{
   1.247 +		settings.setProperty(key, value);
   1.248 +	}
   1.249  }