Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   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.util.Date;
 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<StringStringsetting = 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 apithrows IOException {
 		if(api == null) {
 			throw new IllegalArgumentException("Valid data source parameter is required.");
 		}
 
 		this. = api;
 	}

Retrieve a configuration setting. First checks if the configuration setting is cached in memory, otherwise the configuration setting is loaded from the database.

Parameters:
name Name of the configuration setting, ie smtp.hostname
Returns:
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 also:
getAll()
get(java.lang.String,java.lang.String)
 
 	public String get(String namethrows IOException {
 		if( < (new Date()).getTime()) {
 			try {
 			} catch(Exception e) {
 				..println("WARNING: configuration settings out of date. Error loading settings from the data store.");
 				// Don't show this error again for another 60 seconds.
 				 = new Date().getTime() + 60*1000;
 			}
 		}
 		String value = .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.

Parameters:
name Name of the configuration setting, ie smtp.hostname
defaultValue The value to return if this setting has no value.
Returns:
String representation of the configuration setting.
Throws:
java.io.IOException
See also:
getAll()
get(java.lang.String)
	public String get(String nameString defaultValuethrows IOException {
		if( < (new Date()).getTime()) {
			try {
catch(Exception e) {
				..println("WARNING: configuration settings out of date. Error loading settings from the data store.");
				// Don't show this error again for another 60 seconds.
				 = new Date().getTime() + 60*1000;
			}
		}
		String value = .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.

	public Map<StringStringgetAll() throws IOException {
		Map<StringStringall = new Hashtable<>();
		if( < (new Date()).getTime()) {
			try {
catch(Exception e) {
				..println("WARNING: configuration settings out of date. Error loading settings from the data store.");
				// Don't show this error again for another 60 seconds.
				 = new Date().getTime() + 60*1000;
			}
		}
		while(i.hasNext()) {
			String key = i.next();
			all.put(key.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.

	public void refresh() throws IOException {
		 = 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.

Parameters:
name Name of the configuration setting, ie smtp.hostname
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 nameString valuethrows IOException {
		if( < (new Date()).getTime()) {
		}
		if(.get(name) != null && .get(name).equals(value)) {
			return;
		}
		.upsertSetting(nullnamevalue);
		.put(namevalue);
	}

Remove a configuration setting direclty from cache and from database.

Parameters:
name Name of the configuration setting, ie smtp.hostname
Throws:
java.io.IOException
See also:
deleteAll()
	public void remove(String namethrows IOException {
		.deleteSetting(nullname);
	}

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<StringStringnewSettings = new Hashtable<>();
		for(KeyValue r : .getSettings(null)) {
			if(r.getValue() == null) {
				newSettings.put(r.getKey(), "");
else {
				newSettings.put(r.getKey(), r.getValue());
			}
		}
		// Reset the data expire time
		try {
			 = (new Date()).getTime() + Long.parseLong(newSettings.get("setting.cache.timeout"))*1000;
catch(Exception e) {
			// Set a default time to expire in 60 seconds
			 = (new Date()).getTime() + 60*1000;
		}
		 = newSettings;
	}

Remove all configuration settings from cache and from disk. This is usually only useful for unit testing.

	public void deleteAll() throws IOException {
		 = new Hashtable<>();
	}
New to GrepCode? Check out our FAQ X