base.jee.api.Settings Maven / Gradle / Ivy
/**
* Creative commons Attribution-NonCommercial license.
*
* http://creativecommons.org/licenses/by-nc/2.5/au/deed.en_GB
*
* NO WARRANTY IS GIVEN OR IMPLIED, USE AT YOUR OWN RISK.
*/
package base.jee.api;
import base.KeyValue;
import java.io.IOException;
import java.util.Date;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
/**
* Standard mechanism for getting and setting configuration settings.
*/
public class Settings {
/** Cached version of the configuration settings */
private Map setting = new Hashtable<>();
private API api;
/** Indicates at what time the cached setting data will expire */
private long expires = (new Date()).getTime()-60*1000;
public Settings(API api) throws IOException {
if(api == null) {
throw new IllegalArgumentException("Valid data source parameter is required.");
}
this.api = api;
loadSettings();
}
/**
* Retrieve a configuration setting. First checks if the configuration setting
* is cached in memory, otherwise the configuration setting is loaded from
* the database.
*
* @param name
* @return
* @throws java.io.IOException Throws an IOException when there is a problem with the database
* or if the name parameter does not correspond to a valid database entry
*/
public String get(String name) throws IOException {
if(expires < (new Date()).getTime()) {
loadSettings();
}
String value = setting.get(name);
if(value == null) {
throw new IOException("Required configuration parameter not specified: " + name);
}
return value;
}
/**
* Retrieve a configuration setting. First checks if the configuration setting
* is cached in memory, otherwise the configuration setting is loaded from
* the database.
*
* @param name
* @return
* @throws java.io.IOException
*/
public String get(String name, String defaultValue) throws IOException {
if(expires < (new Date()).getTime()) {
loadSettings();
}
String value = setting.get(name);
if(value != null) {
return value;
}
return defaultValue;
}
/**
* Returns a map of all the current setting key value pairs. The settings returned by
* this method are cached and hence at times may be inconsistent with the database.
*
* @return All current settings.
* @throws java.io.IOException
*/
public Map getAll() throws IOException {
Map all = new Hashtable<>();
if(expires < (new Date()).getTime()) {
loadSettings();
}
Iterator i = setting.keySet().iterator();
while(i.hasNext()) {
String key = (String)i.next();
all.put(key, setting.get(key));
}
return all;
}
/**
* Force clear the cache of setting information to ensure the next read of a setting
* returns the most up to date settings.
*
* @throws java.io.IOException
*/
public void refresh() throws IOException {
expires = 0;
}
/**
* Update a configuration setting in memory and in the database itself. If there
* is a problem writing to the database, the value for this setting will not be
* changed in memory or in the database.
*
* @param name
* @param value
* @throws java.io.IOException Thrown when there is a problem writing to the database
*/
public void set(String name, String value) throws IOException {
if(expires < (new Date()).getTime()) {
loadSettings();
}
if(setting.get(name) != null && setting.get(name).equals(value)) {
return;
}
api.upsertSetting(null, name, value);
setting.put(name, value);
}
public void remove(String name) throws IOException {
api.deleteSetting(null, name);
setting.remove(name);
}
/**
* Reload the configuration settings from the database. If any problems occur
* while the settings are being loaded, the previous set of cached settings
* are retained.
*
* @throws java.io.IOException Thrown when there is a problem reading from the database.
*/
private void loadSettings() throws IOException {
Map newSettings = new Hashtable<>();
for(KeyValue r : api.getSettings(null)) {
if(r.getValue() == null) {
newSettings.put(r.getKey(), "");
} else {
newSettings.put(r.getKey(), r.getValue());
}
}
// Reset the data expire time
try {
expires = (new Date()).getTime() + Long.parseLong(newSettings.get("setting.cache.timeout"))*1000;
} catch(Exception e) {
// Set a default time to expire in 60 seconds
expires = (new Date()).getTime() + 60*1000;
}
setting = newSettings;
}
public void deleteAll() throws IOException {
api.deleteAllSettings(null);
setting = new Hashtable<>();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy