base.jee.api.Settings Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of base Show documentation
Show all versions of base Show documentation
A collection of basic java utility classes that provide basic features for a standalone/simple JEE application. Backed by a Cassandra, MySQL, or SQLite database, it provides, web page templates, user and group management, and a searchable online audit log of all user activity.
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
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;
/**
* Read and write configuration settings from the data store. Configuration
* settings are cached to improve performance. By default, settings are
* cached for 60 seconds, but this may be adjusted by setting a value, in
* seconds for the setting.cache.timeout
setting, ie:
*
*
* settings.set("setting.cache.timeout", 10); // Cache for 10 seconds
*
*/
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 Name of the configuration setting, ie smtp.hostname
* @return String representation of the configuration setting.
* @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
* @see Settings#getAll()
* @see Settings#get(String, String)
*/
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 Name of the configuration setting, ie smtp.hostname
* @param defaultValue The value to return if this setting has no value.
* @return String representation of the configuration setting.
* @throws java.io.IOException
* @see Settings#getAll()
* @see Settings#get(String)
*/
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. Because
* configuration settings are cached in memory, usually this will
* return immediately with a cached version of the settings. If the
* cache has expired, settings will be loaded from disk.
*
* @return All current settings.
* @throws java.io.IOException
* @see Settings#get(String, String)
* @see Settings#get(String)
*/
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;
}
/**
* Mark any cached setting values as expired. Next lookup of a configuration
* setting will trigger a read from the data store.
*
* @throws java.io.IOException
*/
public void refresh() throws IOException {
expires = 0;
}
/**
* Update a configuration setting directly in cache and in the database.
* 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 Name of the configuration setting, ie smtp.hostname
* @param value String representation of the configuration setting 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);
}
/**
* Remove a configuration setting direclty from cache and from database.
*
* @param name Name of the configuration setting, ie smtp.hostname
* @throws IOException
* @see Settings#deleteAll()
*/
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;
}
/**
* Remove all configuration settings from cache and from disk. This is
* usually only useful for unit testing.
*
* @throws IOException
* @see Settings#remove(String)
*/
public void deleteAll() throws IOException {
api.deleteAllSettings(null);
setting = new Hashtable<>();
}
}